From 171268a565003cb760b4efff35e8d71a7b9df6b7 Mon Sep 17 00:00:00 2001 From: runningnak3d Date: Fri, 18 Mar 2011 09:36:59 -0600 Subject: Core/Storage: Add some sanity checks and better define some SQL and DBC storage format field types. * Also LOTS of whitespace cleanup * Some Trinity code style applied * Some if / else if -> switch / case * Original ideal for patch by Vinolentus --- src/server/shared/DataStores/DBCFileLoader.cpp | 170 +++++++++++++++---------- src/server/shared/Database/SQLStorage.cpp | 48 +++++-- src/server/shared/Database/SQLStorageImpl.h | 109 +++++++++------- 3 files changed, 202 insertions(+), 125 deletions(-) (limited to 'src') diff --git a/src/server/shared/DataStores/DBCFileLoader.cpp b/src/server/shared/DataStores/DBCFileLoader.cpp index 524012cc28e..d1deb3b4c5c 100755 --- a/src/server/shared/DataStores/DBCFileLoader.cpp +++ b/src/server/shared/DataStores/DBCFileLoader.cpp @@ -41,7 +41,7 @@ bool DBCFileLoader::Load(const char *filename, const char *fmt) if (!f) return false; - if (fread(&header,4,1,f) != 1) // Number of records + if (fread(&header, 4, 1, f) != 1) // Number of records { fclose(f); return false; @@ -56,7 +56,7 @@ bool DBCFileLoader::Load(const char *filename, const char *fmt) return false; } - if (fread(&recordCount,4,1,f) != 1) // Number of records + if (fread(&recordCount, 4, 1, f) != 1) // Number of records { fclose(f); return false; @@ -64,7 +64,7 @@ bool DBCFileLoader::Load(const char *filename, const char *fmt) EndianConvert(recordCount); - if (fread(&fieldCount,4,1,f) != 1) // Number of fields + if (fread(&fieldCount, 4, 1, f) != 1) // Number of fields { fclose(f); return false; @@ -72,7 +72,7 @@ bool DBCFileLoader::Load(const char *filename, const char *fmt) EndianConvert(fieldCount); - if (fread(&recordSize,4,1,f) != 1) // Size of a record + if (fread(&recordSize, 4, 1, f) != 1) // Size of a record { fclose(f); return false; @@ -80,7 +80,7 @@ bool DBCFileLoader::Load(const char *filename, const char *fmt) EndianConvert(recordSize); - if (fread(&stringSize,4,1,f) != 1) // String size + if (fread(&stringSize, 4, 1, f) != 1) // String size { fclose(f); return false; @@ -90,19 +90,19 @@ bool DBCFileLoader::Load(const char *filename, const char *fmt) fieldsOffset = new uint32[fieldCount]; fieldsOffset[0] = 0; - for (uint32 i = 1; i < fieldCount; i++) + for (uint32 i = 1; i < fieldCount; ++i) { fieldsOffset[i] = fieldsOffset[i - 1]; if (fmt[i - 1] == 'b' || fmt[i - 1] == 'X') // byte fields - fieldsOffset[i] += 1; + fieldsOffset[i] += sizeof(uint8); else // 4 byte fields (int32/float/strings) - fieldsOffset[i] += 4; + fieldsOffset[i] += sizeof(uint32); } - data = new unsigned char[recordSize*recordCount+stringSize]; + data = new unsigned char[recordSize * recordCount + stringSize]; stringTable = data + recordSize*recordCount; - if (fread(data,recordSize*recordCount+stringSize,1,f) != 1) + if (fread(data, recordSize * recordCount + stringSize, 1, f) != 1) { fclose(f); return false; @@ -125,33 +125,41 @@ DBCFileLoader::~DBCFileLoader() DBCFileLoader::Record DBCFileLoader::getRecord(size_t id) { assert(data); - return Record(*this, data + id*recordSize); + return Record(*this, data + id * recordSize); } -uint32 DBCFileLoader::GetFormatRecordSize(const char * format,int32* index_pos) +uint32 DBCFileLoader::GetFormatRecordSize(const char * format, int32* index_pos) { uint32 recordsize = 0; int32 i = -1; - for (uint32 x=0; format[x]; ++x) + for (uint32 x = 0; format[x]; ++x) { switch(format[x]) { case FT_FLOAT: + recordsize += sizeof(float); + break; case FT_INT: - recordsize+=4; + recordsize += sizeof(uint32); break; case FT_STRING: - recordsize+=sizeof(char*); + recordsize += sizeof(char*); break; case FT_SORT: - i=x; + i = x; break; case FT_IND: - i=x; - recordsize+=4; + i = x; + recordsize += sizeof(uint32); break; case FT_BYTE: - recordsize += 1; + recordsize += sizeof(uint8); + break; + case FT_LOGIC: + assert(false && "Attempted to load DBC files that do not have field types that match what is in the core. Check DBCfmt.h or your DBC files."); + break; + default: + assert(false && "Unknown field format character in DBCfmt.h"); break; } } @@ -181,16 +189,16 @@ char* DBCFileLoader::AutoProduceData(const char* format, uint32& records, char** //get struct size and index pos int32 i; - uint32 recordsize=GetFormatRecordSize(format,&i); + uint32 recordsize = GetFormatRecordSize(format, &i); - if (i>=0) + if (i >= 0) { - uint32 maxi=0; + uint32 maxi = 0; //find max index - for (uint32 y=0; ymaxi)maxi=ind; + uint32 ind = getRecord(y).getUInt (i); + if (indi > maxi)maxi = ind; } // If higher index avalible from sql - use it instead of dbcs @@ -198,47 +206,57 @@ char* DBCFileLoader::AutoProduceData(const char* format, uint32& records, char** maxi = sqlHighestIndex; ++maxi; - records=maxi; - indexTable=new ptr[maxi]; - memset(indexTable,0,maxi*sizeof(ptr)); + records = maxi; + indexTable = new ptr[maxi]; + memset(indexTable, 0, imaxi * sizeof(ptr)); } else { records = recordCount + sqlRecordCount; - indexTable = new ptr[recordCount+ sqlRecordCount]; + indexTable = new ptr[recordCount + sqlRecordCount]; } - char* dataTable= new char[(recordCount + sqlRecordCount)*recordsize]; + char* dataTable = new char[(recordCount + sqlRecordCount) * recordsize]; - uint32 offset=0; + uint32 offset = 0; - for (uint32 y =0; y=0) - indexTable[getRecord(y).getUInt(i)]=&dataTable[offset]; + if (i >= 0) + indexTable[getRecord(y).getUInt(i)] = &dataTable[offset]; else - indexTable[y]=&dataTable[offset]; + indexTable[y] = &dataTable[offset]; - for (uint32 x=0; x::convert(uint32 /*field_pos*/, S src, D &dst) template void SQLStorageLoaderBase::convert_str_to_str(uint32 /*field_pos*/, char *src, char *&dst) { - if(!src) + if (!src) { dst = new char[1]; *dst = 0; @@ -66,24 +66,32 @@ void SQLStorageLoaderBase::storeValue(V value, SQLStorage &store, char *p, in { case FT_LOGIC: subclass->convert(x, value, *((bool*)(&p[offset])) ); - offset+=sizeof(bool); + offset += sizeof(bool); break; case FT_BYTE: subclass->convert(x, value, *((char*)(&p[offset])) ); - offset+=sizeof(char); + offset += sizeof(char); break; case FT_INT: subclass->convert(x, value, *((uint32*)(&p[offset])) ); - offset+=sizeof(uint32); + offset += sizeof(uint32); break; case FT_FLOAT: subclass->convert(x, value, *((float*)(&p[offset])) ); - offset+=sizeof(float); + offset += sizeof(float); break; case FT_STRING: subclass->convert_to_str(x, value, *((char**)(&p[offset])) ); - offset+=sizeof(char*); + offset += sizeof(char*); break; + case FT_NA: + case FT_NA_BYTE: + break; + case FT_SORT: + assert(false && "SQL storage has a field type that does not match what is in the core. Check SQLStorage.cpp or your database."); + break; + default: + assert(false && "Unknown field format character in SQLStorage.cpp"); } } @@ -95,24 +103,32 @@ void SQLStorageLoaderBase::storeValue(char * value, SQLStorage &store, char * { case FT_LOGIC: subclass->convert_from_str(x, value, *((bool*)(&p[offset])) ); - offset+=sizeof(bool); + offset += sizeof(bool); break; case FT_BYTE: subclass->convert_from_str(x, value, *((char*)(&p[offset])) ); - offset+=sizeof(char); + offset += sizeof(char); break; case FT_INT: subclass->convert_from_str(x, value, *((uint32*)(&p[offset])) ); - offset+=sizeof(uint32); + offset += sizeof(uint32); break; case FT_FLOAT: subclass->convert_from_str(x, value, *((float*)(&p[offset])) ); - offset+=sizeof(float); + offset += sizeof(float); break; case FT_STRING: subclass->convert_str_to_str(x, value, *((char**)(&p[offset])) ); - offset+=sizeof(char*); + offset += sizeof(char*); + break; + case FT_NA: + case FT_NA_BYTE: break; + case FT_SORT: + assert(false && "SQL storage has a field type that does not match what is in the core. Check SQLStorage.cpp or your database."); + break; + default: + assert(false && "Unknown field format character in SQLStorage.cpp"); } } @@ -121,17 +137,17 @@ void SQLStorageLoaderBase::Load(SQLStorage &store) { uint32 maxi; Field *fields; - QueryResult result = WorldDatabase.PQuery("SELECT MAX(%s) FROM %s", store.entry_field, store.table); + QueryResult result = WorldDatabase.PQuery("SELECT MAX(%s) FROM %s", store.entry_field, store.table); if(!result) { sLog->outError("Error loading %s table (not exist?)\n", store.table); exit(1); // Stop server at loading non exited table or not accessable table } - maxi = (*result)[0].GetUInt32()+1; + maxi = (*result)[0].GetUInt32() + 1; result = WorldDatabase.PQuery("SELECT COUNT(*) FROM %s", store.table); - if(result) + if (result) { fields = result->Fetch(); store.RecordCount = fields[0].GetUInt32(); @@ -141,7 +157,7 @@ void SQLStorageLoaderBase::Load(SQLStorage &store) result = WorldDatabase.PQuery("SELECT * FROM %s", store.table); - if(!result) + if (!result) { sLog->outError("%s table is empty!\n", store.table); store.RecordCount = 0; @@ -151,54 +167,59 @@ void SQLStorageLoaderBase::Load(SQLStorage &store) uint32 recordsize = 0; uint32 offset = 0; - if(store.iNumFields != result->GetFieldCount()) + if (store.iNumFields != result->GetFieldCount()) { store.RecordCount = 0; sLog->outError("Error in %s table, probably sql file format was updated (there should be %d fields in sql).\n", store.table, store.iNumFields); exit(1); // Stop server at loading broken or non-compatible table. } - //get struct size - uint32 sc=0; - uint32 bo=0; - uint32 bb=0; - for (uint32 x=0; x< store.iNumFields; x++) - if(store.dst_format[x]==FT_STRING) - ++sc; - else if (store.dst_format[x]==FT_LOGIC) - ++bo; - else if (store.dst_format[x]==FT_BYTE) - ++bb; - recordsize=(store.iNumFields-sc-bo-bb)*4+sc*sizeof(char*)+bo*sizeof(bool)+bb*sizeof(char); - - char** newIndex=new char*[maxi]; - memset(newIndex,0,maxi*sizeof(char*)); - - char * _data= new char[store.RecordCount *recordsize]; - uint32 count=0; + char** newIndex = new char*[maxi]; + memset(newIndex, 0, maxi * sizeof(char*)); + + char * _data = new char[store.RecordCount *recordsize]; + uint32 count = 0; do { fields = result->Fetch(); - char *p=(char*)&_data[recordsize*count]; - newIndex[fields[0].GetUInt32()]=p; + char *p = (char*)&_data[recordsize*count]; + newIndex[fields[0].GetUInt32()] = p; - offset=0; - for (uint32 x = 0; x < store.iNumFields; x++) + offset = 0; + for (uint32 x = 0; x < store.iNumFields; ++x) switch(store.src_format[x]) { case FT_LOGIC: - storeValue((bool)(fields[x].GetUInt32() > 0), store, p, x, offset); break; + storeValue((bool)(fields[x].GetUInt32() > 0), store, p, x, offset); + break; case FT_BYTE: - storeValue((char)fields[x].GetUInt8(), store, p, x, offset); break; + storeValue((char)fields[x].GetUInt8(), store, p, x, offset); + break; case FT_INT: - storeValue((uint32)fields[x].GetUInt32(), store, p, x, offset); break; + storeValue((uint32)fields[x].GetUInt32(), store, p, x, offset); + break; case FT_FLOAT: - storeValue((float)fields[x].GetFloat(), store, p, x, offset); break; + storeValue((float)fields[x].GetFloat(), store, p, x, offset); + break; case FT_STRING: - storeValue((char*)fields[x].GetCString(), store, p, x, offset); break; + storeValue((char*)fields[x].GetCString(), store, p, x, offset); + break; + case FT_NA: + case FT_NA_BYTE: + break; + case FT_IND: + case FT_SORT: + assert(false && "SQL storage has a field type that does not match what is in the core. Check SQLStorage.cpp or your database."); + break; + default: + assert(false && "Unknown field format character in SQLStorage.cpp"); + } ++count; - }while( result->NextRow() ); + } + while( result->NextRow() ); + + delete result; store.pIndex = newIndex; store.MaxEntry = maxi; -- cgit v1.2.3