2017-08-01 14:30:58 +02:00
|
|
|
/*
|
|
|
|
bParse
|
|
|
|
Copyright (c) 2006-2009 Charlie C & Erwin Coumans http://gamekit.googlecode.com
|
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied warranty.
|
|
|
|
In no event will the authors be held liable for any damages arising from the use of this software.
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it freely,
|
|
|
|
subject to the following restrictions:
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*/
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "b3DNA.h"
|
|
|
|
#include "b3Chunk.h"
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
//this define will force traversal of structures, to check backward (and forward) compatibility
|
|
|
|
//#define TEST_BACKWARD_FORWARD_COMPATIBILITY
|
|
|
|
|
|
|
|
using namespace bParse;
|
|
|
|
|
|
|
|
// ----------------------------------------------------- //
|
|
|
|
bDNA::bDNA()
|
2019-01-03 14:26:51 +01:00
|
|
|
: mPtrLen(0)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
// --
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------- //
|
|
|
|
bDNA::~bDNA()
|
|
|
|
{
|
|
|
|
// --
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------- //
|
|
|
|
bool bDNA::lessThan(bDNA *file)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
return (m_Names.size() < file->m_Names.size());
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------- //
|
|
|
|
char *bDNA::getName(int ind)
|
|
|
|
{
|
|
|
|
assert(ind <= (int)m_Names.size());
|
|
|
|
return m_Names[ind].m_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------- //
|
|
|
|
char *bDNA::getType(int ind)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
assert(ind <= (int)mTypes.size());
|
2017-08-01 14:30:58 +02:00
|
|
|
return mTypes[ind];
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------- //
|
|
|
|
short *bDNA::getStruct(int ind)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
assert(ind <= (int)mStructs.size());
|
2017-08-01 14:30:58 +02:00
|
|
|
return mStructs[ind];
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------- //
|
|
|
|
short bDNA::getLength(int ind)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
assert(ind <= (int)mTlens.size());
|
2017-08-01 14:30:58 +02:00
|
|
|
return mTlens[ind];
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------- //
|
|
|
|
int bDNA::getReverseType(short type)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
int *intPtr = mStructReverse.find(type);
|
2017-08-01 14:30:58 +02:00
|
|
|
if (intPtr)
|
|
|
|
return *intPtr;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------- //
|
|
|
|
int bDNA::getReverseType(const char *type)
|
|
|
|
{
|
|
|
|
b3HashString key(type);
|
2019-01-03 14:26:51 +01:00
|
|
|
int *valuePtr = mTypeLookup.find(key);
|
2017-08-01 14:30:58 +02:00
|
|
|
if (valuePtr)
|
|
|
|
return *valuePtr;
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------- //
|
|
|
|
int bDNA::getNumStructs()
|
|
|
|
{
|
|
|
|
return (int)mStructs.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------- //
|
|
|
|
bool bDNA::flagNotEqual(int dna_nr)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
assert(dna_nr <= (int)mCMPFlags.size());
|
2017-08-01 14:30:58 +02:00
|
|
|
return mCMPFlags[dna_nr] == FDF_STRUCT_NEQU;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------- //
|
|
|
|
bool bDNA::flagEqual(int dna_nr)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
assert(dna_nr <= (int)mCMPFlags.size());
|
2017-08-01 14:30:58 +02:00
|
|
|
int flag = mCMPFlags[dna_nr];
|
2019-01-03 14:26:51 +01:00
|
|
|
return flag == FDF_STRUCT_EQU;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------- //
|
|
|
|
bool bDNA::flagNone(int dna_nr)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
assert(dna_nr <= (int)mCMPFlags.size());
|
2017-08-01 14:30:58 +02:00
|
|
|
return mCMPFlags[dna_nr] == FDF_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------- //
|
|
|
|
int bDNA::getPointerSize()
|
|
|
|
{
|
|
|
|
return mPtrLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------- //
|
|
|
|
void bDNA::initRecurseCmpFlags(int iter)
|
|
|
|
{
|
|
|
|
// iter is FDF_STRUCT_NEQU
|
|
|
|
|
|
|
|
short *oldStrc = mStructs[iter];
|
|
|
|
short type = oldStrc[0];
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int i = 0; i < (int)mStructs.size(); i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
if (i != iter && mCMPFlags[i] == FDF_STRUCT_EQU)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
short *curStruct = mStructs[i];
|
|
|
|
int eleLen = curStruct[1];
|
2019-01-03 14:26:51 +01:00
|
|
|
curStruct += 2;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int j = 0; j < eleLen; j++, curStruct += 2)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
if (curStruct[0] == type)
|
|
|
|
{
|
|
|
|
//char *name = m_Names[curStruct[1]].m_name;
|
|
|
|
//if (name[0] != '*')
|
|
|
|
if (m_Names[curStruct[1]].m_isPointer)
|
|
|
|
{
|
|
|
|
mCMPFlags[i] = FDF_STRUCT_NEQU;
|
|
|
|
initRecurseCmpFlags(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------- //
|
|
|
|
void bDNA::initCmpFlags(bDNA *memDNA)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
// compare the file to memory
|
2017-08-01 14:30:58 +02:00
|
|
|
// this ptr should be the file data
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
assert(!(m_Names.size() == 0)); // && "SDNA empty!");
|
2017-08-01 14:30:58 +02:00
|
|
|
mCMPFlags.resize(mStructs.size(), FDF_NONE);
|
|
|
|
|
|
|
|
int i;
|
2019-01-03 14:26:51 +01:00
|
|
|
for (i = 0; i < (int)mStructs.size(); i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
short *oldStruct = mStructs[i];
|
|
|
|
|
|
|
|
int oldLookup = getReverseType(oldStruct[0]);
|
|
|
|
if (oldLookup == -1)
|
|
|
|
{
|
|
|
|
mCMPFlags[i] = FDF_NONE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
//char* typeName = mTypes[oldStruct[0]];
|
|
|
|
|
|
|
|
//#define SLOW_FORWARD_COMPATIBLE 1
|
|
|
|
#ifdef SLOW_FORWARD_COMPATIBLE
|
2019-01-03 14:26:51 +01:00
|
|
|
char *typeName = mTypes[oldLookup];
|
2017-08-01 14:30:58 +02:00
|
|
|
int newLookup = memDNA->getReverseType(typeName);
|
|
|
|
if (newLookup == -1)
|
|
|
|
{
|
|
|
|
mCMPFlags[i] = FDF_NONE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
short *curStruct = memDNA->mStructs[newLookup];
|
|
|
|
#else
|
|
|
|
// memory for file
|
|
|
|
|
|
|
|
if (oldLookup < memDNA->mStructs.size())
|
|
|
|
{
|
|
|
|
short *curStruct = memDNA->mStructs[oldLookup];
|
2019-01-03 14:26:51 +01:00
|
|
|
#endif
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
// rebuild...
|
|
|
|
mCMPFlags[i] = FDF_STRUCT_NEQU;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
#ifndef TEST_BACKWARD_FORWARD_COMPATIBILITY
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
if (curStruct[1] == oldStruct[1])
|
|
|
|
{
|
|
|
|
// type len same ...
|
|
|
|
if (mTlens[oldStruct[0]] == memDNA->mTlens[curStruct[0]])
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
bool isSame = true;
|
|
|
|
int elementLength = oldStruct[1];
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
curStruct += 2;
|
|
|
|
oldStruct += 2;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int j = 0; j < elementLength; j++, curStruct += 2, oldStruct += 2)
|
|
|
|
{
|
|
|
|
// type the same
|
|
|
|
//const char* typeFileDNA = mTypes[oldStruct[0]];
|
|
|
|
//const char* typeMemDNA = mTypes[curStruct[0]];
|
|
|
|
if (strcmp(mTypes[oldStruct[0]], memDNA->mTypes[curStruct[0]]) != 0)
|
|
|
|
{
|
|
|
|
isSame = false;
|
|
|
|
break;
|
|
|
|
}
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
// name the same
|
|
|
|
if (strcmp(m_Names[oldStruct[1]].m_name, memDNA->m_Names[curStruct[1]].m_name) != 0)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
isSame = false;
|
|
|
|
break;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
// flag valid ==
|
|
|
|
if (isSame)
|
|
|
|
mCMPFlags[i] = FDF_STRUCT_EQU;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
#endif
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
// recurse in
|
|
|
|
for (i = 0; i < (int)mStructs.size(); i++)
|
|
|
|
{
|
|
|
|
if (mCMPFlags[i] == FDF_STRUCT_NEQU)
|
|
|
|
initRecurseCmpFlags(i);
|
|
|
|
}
|
|
|
|
}
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
static int name_is_array(char *name, int *dim1, int *dim2)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
int len = strlen(name);
|
|
|
|
/*fprintf(stderr,"[%s]",name);*/
|
|
|
|
/*if (len >= 1) {
|
|
|
|
if (name[len-1] != ']')
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;*/
|
|
|
|
char *bp;
|
|
|
|
int num;
|
2019-01-03 14:26:51 +01:00
|
|
|
if (dim1)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
*dim1 = 1;
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
if (dim2)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
*dim2 = 1;
|
|
|
|
}
|
|
|
|
bp = strchr(name, '[');
|
2019-01-03 14:26:51 +01:00
|
|
|
if (!bp)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
num = 0;
|
2019-01-03 14:26:51 +01:00
|
|
|
while (++bp < name + len - 1)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
const char c = *bp;
|
2019-01-03 14:26:51 +01:00
|
|
|
if (c == ']')
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
break;
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
if (c <= '9' && c >= '0')
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
num *= 10;
|
|
|
|
num += (c - '0');
|
2019-01-03 14:26:51 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
printf("array parse error.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
if (dim2)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
*dim2 = num;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find second dim, if any. */
|
|
|
|
bp = strchr(bp, '[');
|
2019-01-03 14:26:51 +01:00
|
|
|
if (!bp)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return 1; /* at least we got the first dim. */
|
|
|
|
}
|
|
|
|
num = 0;
|
2019-01-03 14:26:51 +01:00
|
|
|
while (++bp < name + len - 1)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
const char c = *bp;
|
2019-01-03 14:26:51 +01:00
|
|
|
if (c == ']')
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
break;
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
if (c <= '9' && c >= '0')
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
num *= 10;
|
|
|
|
num += (c - '0');
|
2019-01-03 14:26:51 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
printf("array2 parse error.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
if (dim1)
|
|
|
|
{
|
|
|
|
if (dim2)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
*dim1 = *dim2;
|
|
|
|
*dim2 = num;
|
2019-01-03 14:26:51 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
*dim1 = num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------- //
|
|
|
|
void bDNA::init(char *data, int len, bool swap)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
int *intPtr = 0;
|
|
|
|
short *shtPtr = 0;
|
|
|
|
char *cp = 0;
|
|
|
|
int dataLen = 0;
|
2017-08-01 14:30:58 +02:00
|
|
|
//long nr=0;
|
2019-01-03 14:26:51 +01:00
|
|
|
intPtr = (int *)data;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
SDNA (4 bytes) (magic number)
|
|
|
|
NAME (4 bytes)
|
|
|
|
<nr> (4 bytes) amount of names (int)
|
|
|
|
<string>
|
|
|
|
<string>
|
|
|
|
*/
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
if (strncmp(data, "SDNA", 4) == 0)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
// skip ++ NAME
|
2019-01-03 14:26:51 +01:00
|
|
|
intPtr++;
|
|
|
|
intPtr++;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse names
|
2019-01-03 14:26:51 +01:00
|
|
|
if (swap)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
*intPtr = ChunkUtils::swapInt(*intPtr);
|
|
|
|
}
|
|
|
|
dataLen = *intPtr;
|
|
|
|
intPtr++;
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
cp = (char *)intPtr;
|
2017-08-01 14:30:58 +02:00
|
|
|
int i;
|
2019-01-03 14:26:51 +01:00
|
|
|
for (i = 0; i < dataLen; i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
bNameInfo info;
|
|
|
|
info.m_name = cp;
|
|
|
|
info.m_isPointer = (info.m_name[0] == '*') || (info.m_name[1] == '*');
|
2019-01-03 14:26:51 +01:00
|
|
|
name_is_array(info.m_name, &info.m_dim0, &info.m_dim1);
|
2017-08-01 14:30:58 +02:00
|
|
|
m_Names.push_back(info);
|
2019-01-03 14:26:51 +01:00
|
|
|
while (*cp) cp++;
|
2017-08-01 14:30:58 +02:00
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
cp = b3AlignPointer(cp, 4);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
TYPE (4 bytes)
|
|
|
|
<nr> amount of types (int)
|
|
|
|
<string>
|
|
|
|
<string>
|
|
|
|
*/
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
intPtr = (int *)cp;
|
|
|
|
assert(strncmp(cp, "TYPE", 4) == 0);
|
|
|
|
intPtr++;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
if (swap)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
*intPtr = ChunkUtils::swapInt(*intPtr);
|
|
|
|
}
|
|
|
|
dataLen = *intPtr;
|
|
|
|
intPtr++;
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
cp = (char *)intPtr;
|
|
|
|
for (i = 0; i < dataLen; i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
mTypes.push_back(cp);
|
2019-01-03 14:26:51 +01:00
|
|
|
while (*cp) cp++;
|
2017-08-01 14:30:58 +02:00
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
cp = b3AlignPointer(cp, 4);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
TLEN (4 bytes)
|
|
|
|
<len> (short) the lengths of types
|
|
|
|
<len>
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Parse type lens
|
2019-01-03 14:26:51 +01:00
|
|
|
intPtr = (int *)cp;
|
|
|
|
assert(strncmp(cp, "TLEN", 4) == 0);
|
|
|
|
intPtr++;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
dataLen = (int)mTypes.size();
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
shtPtr = (short *)intPtr;
|
|
|
|
for (i = 0; i < dataLen; i++, shtPtr++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
if (swap)
|
|
|
|
shtPtr[0] = ChunkUtils::swapShort(shtPtr[0]);
|
|
|
|
mTlens.push_back(shtPtr[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dataLen & 1) shtPtr++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
STRC (4 bytes)
|
|
|
|
<nr> amount of structs (int)
|
|
|
|
<typenr>
|
|
|
|
<nr_of_elems>
|
|
|
|
<typenr>
|
|
|
|
<namenr>
|
|
|
|
<typenr>
|
|
|
|
<namenr>
|
|
|
|
*/
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
intPtr = (int *)shtPtr;
|
|
|
|
cp = (char *)intPtr;
|
|
|
|
assert(strncmp(cp, "STRC", 4) == 0);
|
|
|
|
intPtr++;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
if (swap)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
*intPtr = ChunkUtils::swapInt(*intPtr);
|
|
|
|
}
|
|
|
|
dataLen = *intPtr;
|
|
|
|
intPtr++;
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
shtPtr = (short *)intPtr;
|
|
|
|
for (i = 0; i < dataLen; i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
mStructs.push_back(shtPtr);
|
2017-08-01 14:30:58 +02:00
|
|
|
if (swap)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
shtPtr[0] = ChunkUtils::swapShort(shtPtr[0]);
|
|
|
|
shtPtr[1] = ChunkUtils::swapShort(shtPtr[1]);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
int len = shtPtr[1];
|
2019-01-03 14:26:51 +01:00
|
|
|
shtPtr += 2;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int a = 0; a < len; a++, shtPtr += 2)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
shtPtr[0] = ChunkUtils::swapShort(shtPtr[0]);
|
|
|
|
shtPtr[1] = ChunkUtils::swapShort(shtPtr[1]);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2019-01-03 14:26:51 +01:00
|
|
|
shtPtr += (2 * shtPtr[1]) + 2;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// build reverse lookups
|
2019-01-03 14:26:51 +01:00
|
|
|
for (i = 0; i < (int)mStructs.size(); i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
short *strc = mStructs.at(i);
|
2019-01-03 14:26:51 +01:00
|
|
|
if (!mPtrLen && strcmp(mTypes[strc[0]], "ListBase") == 0)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
mPtrLen = mTlens[strc[0]] / 2;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
mStructReverse.insert(strc[0], i);
|
2019-01-03 14:26:51 +01:00
|
|
|
mTypeLookup.insert(b3HashString(mTypes[strc[0]]), i);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------- //
|
2019-01-03 14:26:51 +01:00
|
|
|
int bDNA::getArraySize(char *string)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
int ret = 1;
|
|
|
|
int len = strlen(string);
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
char *next = 0;
|
|
|
|
for (int i = 0; i < len; i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
char c = string[i];
|
|
|
|
|
|
|
|
if (c == '[')
|
2019-01-03 14:26:51 +01:00
|
|
|
next = &string[i + 1];
|
|
|
|
else if (c == ']')
|
2017-08-01 14:30:58 +02:00
|
|
|
if (next)
|
|
|
|
ret *= atoi(next);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
// print (string << ' ' << ret);
|
2017-08-01 14:30:58 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bDNA::dumpTypeDefinitions()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
int numTypes = mTypes.size();
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
for (i = 0; i < numTypes; i++)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
for (i = 0; i < (int)mStructs.size(); i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
int totalBytes = 0;
|
2017-08-01 14:30:58 +02:00
|
|
|
short *oldStruct = mStructs[i];
|
|
|
|
|
|
|
|
int oldLookup = getReverseType(oldStruct[0]);
|
|
|
|
if (oldLookup == -1)
|
|
|
|
{
|
|
|
|
mCMPFlags[i] = FDF_NONE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
short *newStruct = mStructs[oldLookup];
|
|
|
|
char *typeName = mTypes[newStruct[0]];
|
|
|
|
printf("%3d: %s ", i, typeName);
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
//char *name = mNames[oldStruct[1]];
|
|
|
|
int len = oldStruct[1];
|
2019-01-03 14:26:51 +01:00
|
|
|
printf(" (%d fields) ", len);
|
|
|
|
oldStruct += 2;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
printf("{");
|
|
|
|
int j;
|
2019-01-03 14:26:51 +01:00
|
|
|
for (j = 0; j < len; ++j, oldStruct += 2)
|
|
|
|
{
|
|
|
|
const char *name = m_Names[oldStruct[1]].m_name;
|
|
|
|
printf("%s %s", mTypes[oldStruct[0]], name);
|
|
|
|
int elemNumBytes = 0;
|
2017-08-01 14:30:58 +02:00
|
|
|
int arrayDimensions = getArraySizeNew(oldStruct[1]);
|
|
|
|
|
|
|
|
if (m_Names[oldStruct[1]].m_isPointer)
|
|
|
|
{
|
|
|
|
elemNumBytes = VOID_IS_8 ? 8 : 4;
|
2019-01-03 14:26:51 +01:00
|
|
|
}
|
|
|
|
else
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
elemNumBytes = getLength(oldStruct[0]);
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
printf(" /* %d bytes */", elemNumBytes * arrayDimensions);
|
|
|
|
|
|
|
|
if (j == len - 1)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
printf(";}");
|
2019-01-03 14:26:51 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
printf("; ");
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
totalBytes += elemNumBytes * arrayDimensions;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
printf("\ntotalBytes=%d\n\n", totalBytes);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* dump out display of types and their sizes */
|
|
|
|
for (i=0; i<bf->types_count; ++i) {
|
|
|
|
/* if (!bf->types[i].is_struct)*/
|
|
|
|
{
|
|
|
|
printf("%3d: sizeof(%s%s)=%d",
|
|
|
|
i,
|
|
|
|
bf->types[i].is_struct ? "struct " : "atomic ",
|
|
|
|
bf->types[i].name, bf->types[i].size);
|
|
|
|
if (bf->types[i].is_struct) {
|
|
|
|
int j;
|
|
|
|
printf(", %d fields: { ", bf->types[i].fieldtypes_count);
|
|
|
|
for (j=0; j<bf->types[i].fieldtypes_count; ++j) {
|
|
|
|
printf("%s %s",
|
|
|
|
bf->types[bf->types[i].fieldtypes[j]].name,
|
|
|
|
bf->names[bf->types[i].fieldnames[j]]);
|
|
|
|
if (j == bf->types[i].fieldtypes_count-1) {
|
|
|
|
printf(";}");
|
|
|
|
} else {
|
|
|
|
printf("; ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//eof
|