mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-16 15:41:16 +01:00
Refactor ce_dictionary_make so that it returns NULL rather than an
broken dict when the file's missing or corrupt.
This commit is contained in:
parent
7a306d1b7e
commit
b14562e883
2 changed files with 116 additions and 97 deletions
|
@ -51,40 +51,54 @@ ce_dictionary_make( CEAppGlobals* globals, XP_UCHAR* dictName )
|
||||||
CEDictionaryCtxt* ctxt = (CEDictionaryCtxt*)NULL;
|
CEDictionaryCtxt* ctxt = (CEDictionaryCtxt*)NULL;
|
||||||
HANDLE mappedFile = NULL;
|
HANDLE mappedFile = NULL;
|
||||||
|
|
||||||
ctxt = (CEDictionaryCtxt*)XP_MALLOC(globals->mpool, sizeof(*ctxt));
|
|
||||||
XP_MEMSET( ctxt, 0, sizeof(*ctxt) );
|
|
||||||
|
|
||||||
dict_super_init( (DictionaryCtxt*)ctxt );
|
|
||||||
MPASSIGN( ctxt->super.mpool, globals->mpool );
|
|
||||||
|
|
||||||
if ( !!dictName ) {
|
|
||||||
wchar_t nameBuf[MAX_PATH+1];
|
wchar_t nameBuf[MAX_PATH+1];
|
||||||
HANDLE hFile;
|
HANDLE hFile;
|
||||||
XP_U8* ptr;
|
XP_U8* ptr;
|
||||||
|
HANDLE mappedFile;
|
||||||
|
|
||||||
ctxt->super.destructor = ce_dict_destroy;
|
XP_ASSERT( !!dictName );
|
||||||
ctxt->super.func_dict_getShortName = ce_dict_getShortName;
|
|
||||||
|
|
||||||
XP_DEBUGF( "looking for dict %s", dictName );
|
XP_DEBUGF( "looking for dict %s", dictName );
|
||||||
|
|
||||||
MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, dictName, -1,
|
MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, dictName, -1,
|
||||||
nameBuf, sizeof(nameBuf)/sizeof(nameBuf[0]) );
|
nameBuf, sizeof(nameBuf)/sizeof(nameBuf[0]) );
|
||||||
|
|
||||||
ptr = openMappedFile( nameBuf, &ctxt->mappedFile, &hFile );
|
ptr = openMappedFile( nameBuf, &mappedFile, &hFile );
|
||||||
|
|
||||||
if ( !!ptr ) {
|
while( !!ptr ) { /* lets us break.... */
|
||||||
XP_U32 offset;
|
XP_U32 offset;
|
||||||
XP_U16 numFaces;
|
XP_U16 numFaces;
|
||||||
XP_U16 i;
|
XP_U16 i;
|
||||||
XP_U16 flags;
|
XP_U16 flags;
|
||||||
XP_U32 dictLength;
|
XP_U32 dictLength;
|
||||||
|
void* mappedBase = (void*)ptr;
|
||||||
ctxt->mappedBase = (void*)ptr;
|
XP_U8 nodeSize;
|
||||||
XP_DEBUGF( "ptr starting at 0x%lx", ptr );
|
|
||||||
|
|
||||||
flags = n_ptr_tohs( &ptr );
|
flags = n_ptr_tohs( &ptr );
|
||||||
XP_DEBUGF( "ce_dictionary_make: flags=0x%x", flags );
|
XP_DEBUGF( "ce_dictionary_make: flags=0x%x", flags );
|
||||||
|
|
||||||
|
#ifdef NODE_CAN_4
|
||||||
|
if ( flags == 0x0002 ) {
|
||||||
|
nodeSize = 3;
|
||||||
|
} else if ( flags == 0x0003 ) {
|
||||||
|
nodeSize = 4;
|
||||||
|
} else {
|
||||||
|
break; /* we want to return NULL */
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
if( flags != 0x0001 ) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
ctxt = (CEDictionaryCtxt*)ce_dictionary_make_empty( globals );
|
||||||
|
|
||||||
|
ctxt->super.nodeSize = nodeSize;
|
||||||
|
|
||||||
|
ctxt->super.destructor = ce_dict_destroy;
|
||||||
|
ctxt->super.func_dict_getShortName = ce_dict_getShortName;
|
||||||
|
|
||||||
|
ctxt->mappedBase = mappedBase;
|
||||||
|
XP_DEBUGF( "ptr starting at 0x%lx", ptr );
|
||||||
|
|
||||||
numFaces = (XP_U16)(*ptr++);
|
numFaces = (XP_U16)(*ptr++);
|
||||||
ctxt->super.nFaces = (XP_U8)numFaces;
|
ctxt->super.nFaces = (XP_U8)numFaces;
|
||||||
XP_DEBUGF( "read %d faces from dict", (short)numFaces );
|
XP_DEBUGF( "read %d faces from dict", (short)numFaces );
|
||||||
|
@ -93,21 +107,12 @@ ce_dictionary_make( CEAppGlobals* globals, XP_UCHAR* dictName )
|
||||||
numFaces * sizeof(ctxt->super.faces16[0]) );
|
numFaces * sizeof(ctxt->super.faces16[0]) );
|
||||||
|
|
||||||
#ifdef NODE_CAN_4
|
#ifdef NODE_CAN_4
|
||||||
if ( flags == 0x0002 ) {
|
ctxt->super.is_4_byte = (ctxt->super.nodeSize == 4);
|
||||||
ctxt->super.nodeSize = 3;
|
|
||||||
} else if ( flags == 0x0003 ) {
|
|
||||||
ctxt->super.nodeSize = 4;
|
|
||||||
} else {
|
|
||||||
XP_ASSERT( 0 );
|
|
||||||
}
|
|
||||||
|
|
||||||
ctxt->super.is_4_byte = ctxt->super.nodeSize == 4;
|
|
||||||
|
|
||||||
for ( i = 0; i < numFaces; ++i ) {
|
for ( i = 0; i < numFaces; ++i ) {
|
||||||
ctxt->super.faces16[i] = n_ptr_tohs(&ptr);
|
ctxt->super.faces16[i] = n_ptr_tohs(&ptr);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
XP_ASSERT( flags == 0x0001 );
|
|
||||||
for ( i = 0; i < numFaces; ++i ) {
|
for ( i = 0; i < numFaces; ++i ) {
|
||||||
ctxt->super.faces16[i] = (XP_CHAR16)*ptr++;
|
ctxt->super.faces16[i] = (XP_CHAR16)*ptr++;
|
||||||
}
|
}
|
||||||
|
@ -157,14 +162,27 @@ ce_dictionary_make( CEAppGlobals* globals, XP_UCHAR* dictName )
|
||||||
ctxt->super.topEdge = (array_edge*)NULL;
|
ctxt->super.topEdge = (array_edge*)NULL;
|
||||||
ctxt->super.base = (array_edge*)NULL;
|
ctxt->super.base = (array_edge*)NULL;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
setBlankTile( (DictionaryCtxt*)ctxt );
|
setBlankTile( (DictionaryCtxt*)ctxt );
|
||||||
|
|
||||||
ctxt->super.name = copyString(MPPARM(globals->mpool) dictName);
|
ctxt->super.name = copyString(MPPARM(globals->mpool) dictName);
|
||||||
|
break; /* exit phony while loop */
|
||||||
}
|
}
|
||||||
return (DictionaryCtxt*)ctxt;
|
return (DictionaryCtxt*)ctxt;
|
||||||
} /* ce_dictionary_make */
|
} /* ce_dictionary_make */
|
||||||
|
|
||||||
|
DictionaryCtxt*
|
||||||
|
ce_dictionary_make_empty( CEAppGlobals* globals )
|
||||||
|
{
|
||||||
|
CEDictionaryCtxt* ctxt = (CEDictionaryCtxt*)XP_MALLOC(globals->mpool,
|
||||||
|
sizeof(*ctxt));
|
||||||
|
XP_MEMSET( ctxt, 0, sizeof(*ctxt) );
|
||||||
|
|
||||||
|
dict_super_init( (DictionaryCtxt*)ctxt );
|
||||||
|
MPASSIGN( ctxt->super.mpool, globals->mpool );
|
||||||
|
return (DictionaryCtxt*)ctxt;
|
||||||
|
} /* ce_dictionary_make_empty */
|
||||||
|
|
||||||
static void
|
static void
|
||||||
ceLoadSpecialData( CEDictionaryCtxt* ctxt, XP_U8** ptrp )
|
ceLoadSpecialData( CEDictionaryCtxt* ctxt, XP_U8** ptrp )
|
||||||
{
|
{
|
||||||
|
|
|
@ -29,6 +29,7 @@ typedef struct CEBitmapInfo {
|
||||||
} CEBitmapInfo;
|
} CEBitmapInfo;
|
||||||
|
|
||||||
DictionaryCtxt* ce_dictionary_make(CEAppGlobals* globals, XP_UCHAR* name);
|
DictionaryCtxt* ce_dictionary_make(CEAppGlobals* globals, XP_UCHAR* name);
|
||||||
|
DictionaryCtxt* ce_dictionary_make_empty( globals );
|
||||||
|
|
||||||
XP_Bool ce_pickDictFile( CEAppGlobals* globals, XP_UCHAR* buf, XP_U16 len );
|
XP_Bool ce_pickDictFile( CEAppGlobals* globals, XP_UCHAR* buf, XP_U16 len );
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue