mirror of
https://github.com/1dot13/source.git
synced 2026-07-22 13:40:22 +02:00
- Needed GameDir files are already committet to SVN GameDir git-svn-id: https://ja2svn.mooo.com/source/ja2/trunk/GameSource/ja2_v1.13/Build@2759 3b4a5df2-a311-0410-b5c6-a8a6f20db521
203 lines
5.0 KiB
C++
203 lines
5.0 KiB
C++
#include "sgp.h"
|
|
#include "overhead types.h"
|
|
#include "overhead.h"
|
|
#include "text.h"
|
|
#include "Debug Control.h"
|
|
#include "expat.h"
|
|
#include "XML.h"
|
|
#include "PostalService.h"
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
using namespace std;
|
|
|
|
CPostalService gPostalService;
|
|
|
|
typedef struct
|
|
{
|
|
UINT8 ubMapY;
|
|
UINT8 ubMapX;
|
|
UINT8 ubMapZ;
|
|
UINT32 uiIndex;
|
|
INT16 sGridNo;
|
|
CHAR16 szName[MAX_DEST_NAME_LENGTH+1];
|
|
} DestinationReadInStruct;
|
|
|
|
struct
|
|
{
|
|
PARSE_STAGE curElement;
|
|
|
|
CHAR8 szCharData[MAX_CHAR_DATA_LENGTH+1];
|
|
|
|
DestinationReadInStruct tempDest;
|
|
UINT32 maxArraySize;
|
|
UINT32 curIndex;
|
|
UINT32 currentDepth;
|
|
UINT32 maxReadDepth;
|
|
}
|
|
typedef destinationParseData;
|
|
|
|
static void XMLCALL
|
|
destinationStartElementHandle(void *userData, const XML_Char *name, const XML_Char **atts)
|
|
{
|
|
destinationParseData * pData = (destinationParseData *)userData;
|
|
|
|
if(pData->currentDepth <= pData->maxReadDepth)
|
|
{
|
|
if(strcmp(name, "DESTINATIONLIST") == 0 && pData->curElement == ELEMENT_NONE)
|
|
{
|
|
pData->curElement = ELEMENT_LIST;
|
|
|
|
pData->maxReadDepth++;
|
|
}
|
|
else if(strcmp(name, "DESTINATION") == 0 && pData->curElement == ELEMENT_LIST)
|
|
{
|
|
pData->curElement = ELEMENT;
|
|
memset(&pData->tempDest, 0, sizeof(DestinationReadInStruct));
|
|
pData->maxReadDepth++;
|
|
pData->curIndex++;
|
|
}
|
|
else if(pData->curElement == ELEMENT &&
|
|
(strcmp(name, "name") == 0 ||
|
|
strcmp(name, "ubMapX") == 0 ||
|
|
strcmp(name, "ubMapY") == 0 ||
|
|
strcmp(name, "ubMapZ") == 0 ||
|
|
strcmp(name, "sGridNo") == 0 ||
|
|
strcmp(name, "uiIndex") == 0))
|
|
{
|
|
pData->curElement = ELEMENT_PROPERTY;
|
|
|
|
pData->maxReadDepth++;
|
|
}
|
|
|
|
pData->szCharData[0] = '\0';
|
|
}
|
|
|
|
pData->currentDepth++;
|
|
|
|
}
|
|
|
|
static void XMLCALL
|
|
destinationCharacterDataHandle(void *userData, const XML_Char *str, int len)
|
|
{
|
|
destinationParseData * pData = (destinationParseData *)userData;
|
|
|
|
if( (pData->currentDepth <= pData->maxReadDepth) &&
|
|
(strlen(pData->szCharData) < MAX_CHAR_DATA_LENGTH)
|
|
){
|
|
strncat(pData->szCharData,str,__min((unsigned int)len,MAX_CHAR_DATA_LENGTH-strlen(pData->szCharData)));
|
|
}
|
|
}
|
|
|
|
static void XMLCALL
|
|
destinationEndElementHandle(void *userData, const XML_Char *name)
|
|
{
|
|
destinationParseData * pData = (destinationParseData *)userData;
|
|
|
|
if(pData->currentDepth <= pData->maxReadDepth)
|
|
{
|
|
if(strcmp(name, "DESTINATIONLIST") == 0)
|
|
{
|
|
pData->curElement = ELEMENT_NONE;
|
|
}
|
|
else if(strcmp(name, "DESTINATION") == 0)
|
|
{
|
|
pData->curElement = ELEMENT_LIST;
|
|
|
|
gPostalService.AddDestination(pData->tempDest.uiIndex, pData->tempDest.ubMapX, pData->tempDest.ubMapY, pData->tempDest.ubMapZ, pData->tempDest.sGridNo, pData->tempDest.szName);
|
|
}
|
|
else if(strcmp(name, "name") == 0)
|
|
{
|
|
pData->curElement = ELEMENT;
|
|
MultiByteToWideChar(CP_UTF8, 0, pData->szCharData, -1, pData->tempDest.szName, MAX_DEST_NAME_LENGTH);
|
|
|
|
}
|
|
else if(strcmp(name, "ubMapX") == 0)
|
|
{
|
|
pData->curElement = ELEMENT;
|
|
pData->tempDest.ubMapX = (UINT8)strtoul(pData->szCharData, NULL, 10);
|
|
}
|
|
else if(strcmp(name, "ubMapY") == 0)
|
|
{
|
|
pData->curElement = ELEMENT;
|
|
pData->tempDest.ubMapY = (UINT8)strtoul(pData->szCharData, NULL, 10);
|
|
|
|
}
|
|
else if(strcmp(name, "ubMapZ") == 0)
|
|
{
|
|
pData->curElement = ELEMENT;
|
|
pData->tempDest.ubMapZ = (UINT8)strtoul(pData->szCharData, NULL, 10);
|
|
}
|
|
else if(strcmp(name, "sGridNo") == 0)
|
|
{
|
|
pData->curElement = ELEMENT;
|
|
pData->tempDest.sGridNo = (UINT16) atoi(pData->szCharData);
|
|
}
|
|
else if(strcmp(name, "uiIndex") == 0)
|
|
{
|
|
pData->curElement = ELEMENT;
|
|
pData->tempDest.uiIndex = (UINT8)strtoul(pData->szCharData, NULL, 10);
|
|
}
|
|
|
|
|
|
pData->maxReadDepth--;
|
|
}
|
|
|
|
pData->currentDepth--;
|
|
}
|
|
|
|
BOOLEAN ReadInShippingDestinations(STR fileName)
|
|
{
|
|
HWFILE hFile;
|
|
UINT32 uiBytesRead;
|
|
UINT32 uiFSize;
|
|
CHAR8 * lpcBuffer;
|
|
XML_Parser parser = XML_ParserCreate(NULL);
|
|
|
|
destinationParseData pData;
|
|
|
|
DebugMsg(TOPIC_JA2, DBG_LEVEL_3, "Loading ShippingDestinations.xml" );
|
|
|
|
hFile = FileOpen( fileName, FILE_ACCESS_READ, FALSE );
|
|
if ( !hFile )
|
|
return( FALSE );
|
|
|
|
uiFSize = FileGetSize(hFile);
|
|
lpcBuffer = (CHAR8 *) MemAlloc(uiFSize+1);
|
|
|
|
if ( !FileRead( hFile, lpcBuffer, uiFSize, &uiBytesRead ) )
|
|
{
|
|
MemFree(lpcBuffer);
|
|
return( FALSE );
|
|
}
|
|
|
|
lpcBuffer[uiFSize] = 0;
|
|
|
|
FileClose( hFile );
|
|
|
|
XML_SetElementHandler(parser, destinationStartElementHandle, destinationEndElementHandle);
|
|
XML_SetCharacterDataHandler(parser, destinationCharacterDataHandle);
|
|
|
|
memset(&pData,0,sizeof(pData));
|
|
pData.maxArraySize = sizeof(UINT16);
|
|
pData.curIndex = -1;
|
|
|
|
XML_SetUserData(parser, &pData);
|
|
|
|
if(!XML_Parse(parser, lpcBuffer, uiFSize, TRUE))
|
|
{
|
|
CHAR8 errorBuf[511];
|
|
|
|
sprintf(errorBuf, "XML Parser Error in ShippingDestinations.xml: %s at line %d", XML_ErrorString(XML_GetErrorCode(parser)), XML_GetCurrentLineNumber(parser));
|
|
LiveMessage(errorBuf);
|
|
|
|
MemFree(lpcBuffer);
|
|
return FALSE;
|
|
}
|
|
|
|
MemFree(lpcBuffer);
|
|
|
|
XML_ParserFree(parser);
|
|
|
|
return( TRUE );
|
|
} |