我正在解析一个 txt 文件,我在其中放置了模板(GUID)以帮助识别数据块......我在解析文件中的 STRING 变量时遇到了问题。在解析时,我使用了一个 DataPtr 对象,该对象按数据类型的大小进行迭代......问题是我无法正确计算字符串的大小,以便(在读取字符串后)继续下一个数据类型。这是一个要解析的示例文件:
xof 0303txt 0032
template Effect {
<C7D92586-6C57-42d3-8F03-0BDF87EC1848>
STRING filename;
}
template Light {
<ADA248C7-97E3-4a42-A4C1-74EE8536FF37>
DWORD type;
array float position[3];
array float direction[3];
array float ambient[4];
array float diffuse[4];
array float specular[4];
float power;
}
template Object {
<B6494248-7D84-42d3-A4AA-0747E22E08D3>
DWORD type;
array float position[3];
array float orientation[4];
STRING name;
STRING filename;
}
Effect {
".//Data/Scene/TrainingRoom/Effects/effect.fx";
}
Light {
1;
-3.0; 1.0; -3.0;
3.0; -1.0; 3.0;
1.0; 1.0; 1.0; 1.0;
1.0; 0.0; 0.0; 0.0;
1.0; 1.0; 1.0; 1.0;
0.1;
}
Object {
1;
0.0; 0.0; 0.0;
0.0; 0.0; 0.0; 1.0;
".//Data/Scene/TrainingRoom/TrainingRoom.x";
}
这是当前工作的代码示例(不带字符串标签):
if(CompareDataObjectType(pDataObj, TID_D3DRMLightCustom))
{
D3DXVECTOR3 *vecPtr = NULL;
D3DXVECTOR4 *colorPtr = NULL;
DWORD *DataPtr = (DWORD*)GetObjectData(pDataObj); // Data Pointer points at 1st data location
m_light.type = *DataPtr++;
vecPtr = (D3DXVECTOR3*)DataPtr;
m_light.position = *vecPtr;
DataPtr+=3;
vecPtr = (D3DXVECTOR3*)DataPtr;
m_light.direction = *vecPtr;
DataPtr+=3;
colorPtr = (D3DXVECTOR4*)DataPtr;
m_light.ambient = *colorPtr;
DataPtr+=4;
colorPtr = (D3DXVECTOR4*)DataPtr;
m_light.diffuse = *colorPtr;
DataPtr+=4;
colorPtr = (D3DXVECTOR4*)DataPtr;
m_light.specular = *colorPtr;
DataPtr+=4;
float *fPtr = (float*)DataPtr;
m_light.power = *fPtr;
return true;
}
这一切都好......以下返回不正确的结果:
if(CompareDataObjectType(pDataObj, TID_D3DRMObject))
{
D3DXVECTOR3 *vecPtr = NULL;
D3DXQUATERNION *quatPtr = NULL;
DWORD *DataPtr = (DWORD*)GetObjectData(pDataObj);
m_object.type = *DataPtr++;
vecPtr = (D3DXVECTOR3*)DataPtr;
m_object.position = *vecPtr;
DataPtr+=3;
quatPtr = (D3DXQUATERNION*)DataPtr;
m_object.orientation = *quatPtr;
DataPtr+=4;
m_object.name = (LPCSTR)DataPtr;
size_t length = strlen(m_object.name);
DataPtr+=length;
m_object.filename = (LPCSTR)DataPtr; // filename only contains a portion of the correct string...
return true;
}