Replaced tabs with four spaces
This commit is contained in:
@@ -20,285 +20,285 @@
|
||||
#define OLDMODELSTYLE
|
||||
|
||||
/* trpage_model.cpp
|
||||
This source file contains the methods trpgModel and trpgModelTable.
|
||||
You should only modify this code if you want to add data to these classes.
|
||||
*/
|
||||
This source file contains the methods trpgModel and trpgModelTable.
|
||||
You should only modify this code if you want to add data to these classes.
|
||||
*/
|
||||
|
||||
#include <trpage_geom.h>
|
||||
#include <trpage_read.h>
|
||||
|
||||
/* Write Model class
|
||||
Represents a model reference.
|
||||
*/
|
||||
Represents a model reference.
|
||||
*/
|
||||
trpgModel::trpgModel()
|
||||
{
|
||||
name = NULL;
|
||||
type = External;
|
||||
useCount = 0;
|
||||
diskRef = -1;
|
||||
handle = -1;
|
||||
writeHandle = false;
|
||||
name = NULL;
|
||||
type = External;
|
||||
useCount = 0;
|
||||
diskRef = -1;
|
||||
handle = -1;
|
||||
writeHandle = false;
|
||||
}
|
||||
|
||||
trpgModel::trpgModel(const trpgModel &in):
|
||||
trpgReadWriteable(in)
|
||||
{
|
||||
if (in.name)
|
||||
{
|
||||
name = new char[strlen(in.name)+1];
|
||||
strcpy(name,in.name);
|
||||
}
|
||||
else
|
||||
name = NULL;
|
||||
if (in.name)
|
||||
{
|
||||
name = new char[strlen(in.name)+1];
|
||||
strcpy(name,in.name);
|
||||
}
|
||||
else
|
||||
name = NULL;
|
||||
|
||||
type=in.type;
|
||||
useCount=in.useCount;
|
||||
diskRef=in.diskRef;
|
||||
handle = in.handle;
|
||||
writeHandle = in.writeHandle;
|
||||
type=in.type;
|
||||
useCount=in.useCount;
|
||||
diskRef=in.diskRef;
|
||||
handle = in.handle;
|
||||
writeHandle = in.writeHandle;
|
||||
}
|
||||
|
||||
|
||||
// Reset function
|
||||
void trpgModel::Reset()
|
||||
{
|
||||
if (name)
|
||||
delete [] name;
|
||||
name = NULL;
|
||||
useCount = 0;
|
||||
diskRef = -1;
|
||||
handle = -1;
|
||||
writeHandle = false;
|
||||
if (name)
|
||||
delete [] name;
|
||||
name = NULL;
|
||||
useCount = 0;
|
||||
diskRef = -1;
|
||||
handle = -1;
|
||||
writeHandle = false;
|
||||
}
|
||||
|
||||
trpgModel::~trpgModel()
|
||||
{
|
||||
Reset();
|
||||
Reset();
|
||||
}
|
||||
|
||||
// Set functions
|
||||
void trpgModel::SetType(int t)
|
||||
{
|
||||
type = t;
|
||||
type = t;
|
||||
}
|
||||
|
||||
void trpgModel::SetName(const char *nm)
|
||||
{
|
||||
if (name)
|
||||
delete [] name;
|
||||
if (name)
|
||||
delete [] name;
|
||||
|
||||
if (nm)
|
||||
{
|
||||
name = new char[(nm ? strlen(nm) : 0)+1];
|
||||
strcpy(name,nm);
|
||||
}
|
||||
if (nm)
|
||||
{
|
||||
name = new char[(nm ? strlen(nm) : 0)+1];
|
||||
strcpy(name,nm);
|
||||
}
|
||||
}
|
||||
void trpgModel::SetReference(trpgDiskRef pos)
|
||||
{
|
||||
diskRef = pos;
|
||||
diskRef = pos;
|
||||
}
|
||||
void trpgModel::SetNumTiles(int num)
|
||||
{
|
||||
useCount = num;
|
||||
useCount = num;
|
||||
}
|
||||
void trpgModel::AddTile()
|
||||
{
|
||||
useCount++;
|
||||
useCount++;
|
||||
}
|
||||
|
||||
// Validity check
|
||||
bool trpgModel::isValid() const
|
||||
{
|
||||
if (type == External && !name)
|
||||
{
|
||||
strcpy(errMess, "Model is external with no name");
|
||||
return false;
|
||||
}
|
||||
if (type == External && !name)
|
||||
{
|
||||
strcpy(errMess, "Model is external with no name");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Copy from one to another
|
||||
trpgModel& trpgModel::operator = (const trpgModel &in)
|
||||
{
|
||||
if (name) {
|
||||
delete [] name;
|
||||
name = NULL;
|
||||
}
|
||||
if (name) {
|
||||
delete [] name;
|
||||
name = NULL;
|
||||
}
|
||||
|
||||
type = in.type;
|
||||
if (in.name)
|
||||
SetName(in.name);
|
||||
diskRef = in.diskRef;
|
||||
useCount = in.useCount;
|
||||
writeHandle = in.writeHandle;
|
||||
handle = in.handle;
|
||||
return *this;
|
||||
type = in.type;
|
||||
if (in.name)
|
||||
SetName(in.name);
|
||||
diskRef = in.diskRef;
|
||||
useCount = in.useCount;
|
||||
writeHandle = in.writeHandle;
|
||||
handle = in.handle;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Compare two models
|
||||
int trpgModel::operator == (const trpgModel &in) const
|
||||
{
|
||||
if (type != in.type)
|
||||
return 0;
|
||||
if (type != in.type)
|
||||
return 0;
|
||||
|
||||
switch (type) {
|
||||
case Local:
|
||||
if (diskRef == in.diskRef)
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
break;
|
||||
case External:
|
||||
if (!name && !in.name)
|
||||
return 1;
|
||||
if (!name || !in.name)
|
||||
return 0;
|
||||
if (strcmp(name,in.name))
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
switch (type) {
|
||||
case Local:
|
||||
if (diskRef == in.diskRef)
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
break;
|
||||
case External:
|
||||
if (!name && !in.name)
|
||||
return 1;
|
||||
if (!name || !in.name)
|
||||
return 0;
|
||||
if (strcmp(name,in.name))
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
|
||||
return 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Write a model reference out
|
||||
bool trpgModel::Write(trpgWriteBuffer &buf)
|
||||
{
|
||||
if (!isValid())
|
||||
return false;
|
||||
if (!isValid())
|
||||
return false;
|
||||
|
||||
// We will use two different tokens to track the
|
||||
// format used in terrapage 2.2, and older versions
|
||||
int tok = TRPGMODELREF;
|
||||
if(writeHandle)
|
||||
tok = TRPGMODELREF2;
|
||||
// We will use two different tokens to track the
|
||||
// format used in terrapage 2.2, and older versions
|
||||
int tok = TRPGMODELREF;
|
||||
if(writeHandle)
|
||||
tok = TRPGMODELREF2;
|
||||
|
||||
// Nick messed up the model entries when checking into txv4; now we're
|
||||
// a bit stuck because U3 dbs don't have models in the U2 viewer.
|
||||
// This will force the old behavior.
|
||||
#ifdef OLDMODELSTYLE
|
||||
buf.Begin(tok);
|
||||
buf.Add(type);
|
||||
//writeHandle is only set for terrapage 2.2, and we use the different token.
|
||||
if(writeHandle) {
|
||||
buf.Add((int)handle);
|
||||
}
|
||||
if (name)
|
||||
buf.Add(name);
|
||||
else
|
||||
buf.Begin(tok);
|
||||
buf.Add(type);
|
||||
//writeHandle is only set for terrapage 2.2, and we use the different token.
|
||||
if(writeHandle) {
|
||||
buf.Add((int)handle);
|
||||
}
|
||||
if (name)
|
||||
buf.Add(name);
|
||||
else
|
||||
buf.Add(diskRef);
|
||||
buf.Add(useCount);
|
||||
buf.Add(useCount);
|
||||
|
||||
#else
|
||||
buf.Begin(tok);
|
||||
if(writeHandle) {
|
||||
buf.Add((int)handle);
|
||||
}
|
||||
buf.Add(type);
|
||||
buf.Add(name);
|
||||
buf.Add(diskRef);
|
||||
buf.Add(useCount);
|
||||
buf.Begin(tok);
|
||||
if(writeHandle) {
|
||||
buf.Add((int)handle);
|
||||
}
|
||||
buf.Add(type);
|
||||
buf.Add(name);
|
||||
buf.Add(diskRef);
|
||||
buf.Add(useCount);
|
||||
#endif
|
||||
buf.End();
|
||||
buf.End();
|
||||
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* *******************
|
||||
Model Read Methods
|
||||
*******************
|
||||
*/
|
||||
/* *******************
|
||||
Model Read Methods
|
||||
*******************
|
||||
*/
|
||||
// Get methods
|
||||
bool trpgModel::GetType(int &t)
|
||||
{
|
||||
if (!isValid()) return false;
|
||||
t = type;
|
||||
return true;
|
||||
if (!isValid()) return false;
|
||||
t = type;
|
||||
return true;
|
||||
}
|
||||
bool trpgModel::GetName(char *str,int strLen) const
|
||||
{
|
||||
if (!isValid()) return false;
|
||||
int len = (name ? strlen(name) : 0);
|
||||
strncpy(str,name,MIN(len,strLen)+1);
|
||||
return true;
|
||||
if (!isValid()) return false;
|
||||
int len = (name ? strlen(name) : 0);
|
||||
strncpy(str,name,MIN(len,strLen)+1);
|
||||
return true;
|
||||
}
|
||||
bool trpgModel::GetNumTiles(int &ret) const
|
||||
{
|
||||
if (!isValid()) return false;
|
||||
if (!isValid()) return false;
|
||||
|
||||
ret = useCount;
|
||||
return true;
|
||||
ret = useCount;
|
||||
return true;
|
||||
}
|
||||
bool trpgModel::GetReference(trpgDiskRef &ref) const
|
||||
{
|
||||
if (!isValid() || type != Local) return false;
|
||||
ref = diskRef;
|
||||
return true;
|
||||
if (!isValid() || type != Local) return false;
|
||||
ref = diskRef;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool trpgModel::Read(trpgReadBuffer &buf, bool hasHandle)
|
||||
{
|
||||
// MD: added complexity here - written multiple ways by
|
||||
// mistake, unraveling the various cases.
|
||||
char tmpName[1024];
|
||||
// MD: added complexity here - written multiple ways by
|
||||
// mistake, unraveling the various cases.
|
||||
char tmpName[1024];
|
||||
|
||||
try {
|
||||
buf.Get(type);
|
||||
// TerraPage 2.2 will store the unique handle after the type
|
||||
// we use a different token, so this is backwards compatible.
|
||||
if(hasHandle) {
|
||||
try {
|
||||
buf.Get(type);
|
||||
// TerraPage 2.2 will store the unique handle after the type
|
||||
// we use a different token, so this is backwards compatible.
|
||||
if(hasHandle) {
|
||||
int32 tempHandle;
|
||||
if(buf.Get(tempHandle))
|
||||
if(buf.Get(tempHandle))
|
||||
{
|
||||
handle = tempHandle;
|
||||
}
|
||||
else
|
||||
{
|
||||
handle = -1;
|
||||
}
|
||||
}
|
||||
else
|
||||
handle = -1;
|
||||
if (type == Local) {
|
||||
// two possibilities:
|
||||
// name, diskRef, useCount
|
||||
// diskRef, useCount
|
||||
// diskRef + useCount = 12 bytes...
|
||||
if (buf.TestLimit(13))
|
||||
{
|
||||
buf.Get(tmpName,1023);
|
||||
SetName(tmpName);
|
||||
}
|
||||
buf.Get(diskRef);
|
||||
buf.Get(useCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
// two possibilities:
|
||||
// name, diskRef, useCount
|
||||
// name, useCount
|
||||
buf.Get(tmpName,1023);
|
||||
SetName(tmpName);
|
||||
// useCount = 4 bytes...
|
||||
if (buf.TestLimit(5))
|
||||
buf.Get(diskRef);
|
||||
buf.Get(useCount);
|
||||
}
|
||||
}
|
||||
catch(...) {
|
||||
return false;
|
||||
}
|
||||
handle = -1;
|
||||
}
|
||||
}
|
||||
else
|
||||
handle = -1;
|
||||
if (type == Local) {
|
||||
// two possibilities:
|
||||
// name, diskRef, useCount
|
||||
// diskRef, useCount
|
||||
// diskRef + useCount = 12 bytes...
|
||||
if (buf.TestLimit(13))
|
||||
{
|
||||
buf.Get(tmpName,1023);
|
||||
SetName(tmpName);
|
||||
}
|
||||
buf.Get(diskRef);
|
||||
buf.Get(useCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
// two possibilities:
|
||||
// name, diskRef, useCount
|
||||
// name, useCount
|
||||
buf.Get(tmpName,1023);
|
||||
SetName(tmpName);
|
||||
// useCount = 4 bytes...
|
||||
if (buf.TestLimit(5))
|
||||
buf.Get(diskRef);
|
||||
buf.Get(useCount);
|
||||
}
|
||||
}
|
||||
catch(...) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// going to make this fail if the buffer isn't empty.
|
||||
if (buf.TestLimit(1)) return false;
|
||||
// going to make this fail if the buffer isn't empty.
|
||||
if (buf.TestLimit(1)) return false;
|
||||
|
||||
return isValid();
|
||||
return isValid();
|
||||
}
|
||||
|
||||
/* Write Model Reference table
|
||||
Groups of models for the entire file.
|
||||
*/
|
||||
Groups of models for the entire file.
|
||||
*/
|
||||
|
||||
// Constructor
|
||||
trpgModelTable::trpgModelTable()
|
||||
@@ -311,73 +311,73 @@ trpgModelTable::~trpgModelTable()
|
||||
// Reset function
|
||||
void trpgModelTable::Reset()
|
||||
{
|
||||
modelsMap.clear();
|
||||
modelsMap.clear();
|
||||
}
|
||||
|
||||
// Set functions
|
||||
void trpgModelTable::SetNumModels(int /*no*/)
|
||||
{
|
||||
// This method isn't needed with a map
|
||||
//models.resize(no);
|
||||
// This method isn't needed with a map
|
||||
//models.resize(no);
|
||||
}
|
||||
void trpgModelTable::SetModel(int id,const trpgModel &mod)
|
||||
{
|
||||
if (id < 0)
|
||||
return;
|
||||
if (id < 0)
|
||||
return;
|
||||
|
||||
modelsMap[id] = mod;
|
||||
//models[id] = mod;
|
||||
modelsMap[id] = mod;
|
||||
//models[id] = mod;
|
||||
}
|
||||
|
||||
int trpgModelTable::AddModel(trpgModel &mod)
|
||||
{
|
||||
int hdl = modelsMap.size();
|
||||
if(mod.GetHandle()==-1) {
|
||||
modelsMap[hdl] = mod;
|
||||
return hdl;
|
||||
}
|
||||
modelsMap[mod.GetHandle()] = mod;
|
||||
return mod.GetHandle();
|
||||
int hdl = modelsMap.size();
|
||||
if(mod.GetHandle()==-1) {
|
||||
modelsMap[hdl] = mod;
|
||||
return hdl;
|
||||
}
|
||||
modelsMap[mod.GetHandle()] = mod;
|
||||
return mod.GetHandle();
|
||||
}
|
||||
|
||||
int trpgModelTable::FindAddModel(trpgModel &mod)
|
||||
{
|
||||
|
||||
ModelMapType::iterator itr = modelsMap.begin();
|
||||
for ( ; itr != modelsMap.end( ); itr++) {
|
||||
if(itr->second == mod) {
|
||||
return itr->first;
|
||||
}
|
||||
}
|
||||
return AddModel(mod);
|
||||
ModelMapType::iterator itr = modelsMap.begin();
|
||||
for ( ; itr != modelsMap.end( ); itr++) {
|
||||
if(itr->second == mod) {
|
||||
return itr->first;
|
||||
}
|
||||
}
|
||||
return AddModel(mod);
|
||||
}
|
||||
|
||||
bool trpgModelTable::FindByName(const char *name, unsigned int &mId)
|
||||
{
|
||||
ModelMapType::const_iterator itr = modelsMap.begin();
|
||||
for ( ; itr != modelsMap.end( ); itr++) {
|
||||
char theName[1023];
|
||||
itr->second.GetName(theName,1023);
|
||||
if(strcmp(name,theName)==0) {
|
||||
mId = itr->first;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
ModelMapType::const_iterator itr = modelsMap.begin();
|
||||
for ( ; itr != modelsMap.end( ); itr++) {
|
||||
char theName[1023];
|
||||
itr->second.GetName(theName,1023);
|
||||
if(strcmp(name,theName)==0) {
|
||||
mId = itr->first;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Validity check
|
||||
bool trpgModelTable::isValid() const
|
||||
{
|
||||
ModelMapType::const_iterator itr = modelsMap.begin();
|
||||
for ( ; itr != modelsMap.end( ); itr++) {
|
||||
if(!itr->second.isValid()) {
|
||||
if(itr->second.getErrMess())
|
||||
strcpy(errMess, itr->second.getErrMess());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
ModelMapType::const_iterator itr = modelsMap.begin();
|
||||
for ( ; itr != modelsMap.end( ); itr++) {
|
||||
if(!itr->second.isValid()) {
|
||||
if(itr->second.getErrMess())
|
||||
strcpy(errMess, itr->second.getErrMess());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -385,85 +385,85 @@ bool trpgModelTable::isValid() const
|
||||
// Write out the model table
|
||||
bool trpgModelTable::Write(trpgWriteBuffer &buf)
|
||||
{
|
||||
if (!isValid())
|
||||
return false;
|
||||
if (!isValid())
|
||||
return false;
|
||||
|
||||
buf.Begin(TRPGMODELTABLE);
|
||||
buf.Add((int32)modelsMap.size());
|
||||
ModelMapType::iterator itr = modelsMap.begin();
|
||||
for ( ; itr != modelsMap.end( ); itr++) {
|
||||
itr->second.Write(buf);
|
||||
}
|
||||
buf.End();
|
||||
buf.Begin(TRPGMODELTABLE);
|
||||
buf.Add((int32)modelsMap.size());
|
||||
ModelMapType::iterator itr = modelsMap.begin();
|
||||
for ( ; itr != modelsMap.end( ); itr++) {
|
||||
itr->second.Write(buf);
|
||||
}
|
||||
buf.End();
|
||||
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* ***************
|
||||
Model Table Read methods
|
||||
***************
|
||||
*/
|
||||
/* ***************
|
||||
Model Table Read methods
|
||||
***************
|
||||
*/
|
||||
|
||||
// Get methods
|
||||
bool trpgModelTable::GetNumModels(int &nm) const
|
||||
{
|
||||
if (!isValid()) return false;
|
||||
nm = modelsMap.size();
|
||||
return true;
|
||||
if (!isValid()) return false;
|
||||
nm = modelsMap.size();
|
||||
return true;
|
||||
}
|
||||
bool trpgModelTable::GetModel(int id,trpgModel &model) const
|
||||
{
|
||||
if (!isValid() || id < 0 ) //|| id >= models.size())
|
||||
return false;
|
||||
//model = models[id];
|
||||
ModelMapType::const_iterator itr = modelsMap.find(id);
|
||||
if(itr == modelsMap.end()) {
|
||||
return false;
|
||||
}
|
||||
model = itr->second;
|
||||
return true;
|
||||
if (!isValid() || id < 0 ) //|| id >= models.size())
|
||||
return false;
|
||||
//model = models[id];
|
||||
ModelMapType::const_iterator itr = modelsMap.find(id);
|
||||
if(itr == modelsMap.end()) {
|
||||
return false;
|
||||
}
|
||||
model = itr->second;
|
||||
return true;
|
||||
}
|
||||
trpgModel *trpgModelTable::GetModelRef(int id)
|
||||
{
|
||||
if (id < 0 ) //|| id >= models.size())
|
||||
return NULL;
|
||||
//return &models[id];
|
||||
ModelMapType::iterator itr = modelsMap.find(id);
|
||||
if(itr == modelsMap.end()) {
|
||||
return false;
|
||||
}
|
||||
return &(itr->second);
|
||||
if (id < 0 ) //|| id >= models.size())
|
||||
return NULL;
|
||||
//return &models[id];
|
||||
ModelMapType::iterator itr = modelsMap.find(id);
|
||||
if(itr == modelsMap.end()) {
|
||||
return false;
|
||||
}
|
||||
return &(itr->second);
|
||||
}
|
||||
|
||||
bool trpgModelTable::Read(trpgReadBuffer &buf)
|
||||
{
|
||||
int32 numModel;
|
||||
trpgToken tok;
|
||||
int32 len;
|
||||
bool status;
|
||||
int32 numModel;
|
||||
trpgToken tok;
|
||||
int32 len;
|
||||
bool status;
|
||||
|
||||
try {
|
||||
buf.Get(numModel);
|
||||
for (int i=0;i<numModel;i++) {
|
||||
trpgModel model;
|
||||
buf.GetToken(tok,len);
|
||||
bool readHandle;
|
||||
if (tok == TRPGMODELREF)
|
||||
readHandle = false;
|
||||
else if (tok == TRPGMODELREF2)
|
||||
readHandle = true;
|
||||
else
|
||||
throw 1;
|
||||
buf.PushLimit(len);
|
||||
status = model.Read(buf,readHandle);
|
||||
buf.PopLimit();
|
||||
if (!status) throw 1;
|
||||
AddModel(model);
|
||||
}
|
||||
}
|
||||
catch (...) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
buf.Get(numModel);
|
||||
for (int i=0;i<numModel;i++) {
|
||||
trpgModel model;
|
||||
buf.GetToken(tok,len);
|
||||
bool readHandle;
|
||||
if (tok == TRPGMODELREF)
|
||||
readHandle = false;
|
||||
else if (tok == TRPGMODELREF2)
|
||||
readHandle = true;
|
||||
else
|
||||
throw 1;
|
||||
buf.PushLimit(len);
|
||||
status = model.Read(buf,readHandle);
|
||||
buf.PopLimit();
|
||||
if (!status) throw 1;
|
||||
AddModel(model);
|
||||
}
|
||||
}
|
||||
catch (...) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return isValid();
|
||||
return isValid();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user