From de37581daf47934e06327e09c93674cda29f9704 Mon Sep 17 00:00:00 2001 From: Wanne Date: Mon, 2 Apr 2012 08:54:47 +0000 Subject: [PATCH] - Utils patch (by tazpn) o Expand length of gMoneyStatsDesc buffer for Italian and Dutch builds o Move EnumToString to text utils from keymap o Add ParseCommandLine utility function for parsing command line strings git-svn-id: https://ja2svn.mooo.com/source/ja2/trunk/GameSource/ja2_v1.13/Build@5138 3b4a5df2-a311-0410-b5c6-a8a6f20db521 --- Utils/KeyMap.cpp | 44 +----- Utils/Text Utils.cpp | 282 +++++++++++++++++++++++++++++++++++++++ Utils/Text.h | 22 ++- Utils/_ChineseText.cpp | 2 +- Utils/_DutchText.cpp | 2 +- Utils/_EnglishText.cpp | 2 +- Utils/_FrenchText.cpp | 2 +- Utils/_GermanText.cpp | 2 +- Utils/_ItalianText.cpp | 2 +- Utils/_PolishText.cpp | 2 +- Utils/_RussianText.cpp | 2 +- Utils/_TaiwaneseText.cpp | 2 +- 12 files changed, 314 insertions(+), 52 deletions(-) diff --git a/Utils/KeyMap.cpp b/Utils/KeyMap.cpp index c8c212a7..4a835c7a 100644 --- a/Utils/KeyMap.cpp +++ b/Utils/KeyMap.cpp @@ -4,19 +4,9 @@ #include "KeyMap.h" #include #endif +#include "text.h" -// Enumeration support -typedef struct EnumLookupType { - int value; - const STR name; -} EnumLookupType; - -static const STR EnumToString(int value, const EnumLookupType *table); -static int StringToEnum(const STR value, const EnumLookupType *table); -static int EnumToIndex(int value, const EnumLookupType *table); -static inline STR Trim(STR&p); - -static EnumLookupType gKeyTable[] = +static Str8EnumLookupType gKeyTable[] = { {VK_LBUTTON, "LBUTTON"}, {VK_RBUTTON, "RBUTTON"}, @@ -237,36 +227,6 @@ static EnumLookupType gKeyTable[] = {0, NULL} }; - - -// Enumeration Support -const STR EnumToString(int value, const EnumLookupType *table) { - for (const EnumLookupType *itr = table; itr->name != NULL; ++itr) { - if (itr->value == value) return itr->name; - } - return NULL; -} - -int StringToEnum(const STR value, const EnumLookupType *table) { - if (NULL == value || 0 == *value) - return 0; - - for (const EnumLookupType *itr = table; itr->name != NULL; ++itr) { - if (0 == _stricmp(value, itr->name)) - return itr->value; - } - STR end = NULL; - return (int)strtol(value, &end, 0); -} - -int EnumToIndex(int value, const EnumLookupType *table) { - int i = 0; - for (const EnumLookupType *itr = table; itr->name != NULL; ++itr, ++i) { - if (itr->value == value) return i; - } - return -1; -} - static inline STR Trim(STR &p) { while(isspace(*p)) *p++ = 0; TCHAR *e = p + strlen(p) - 1; diff --git a/Utils/Text Utils.cpp b/Utils/Text Utils.cpp index fbf86b22..a4e0ae4a 100644 --- a/Utils/Text Utils.cpp +++ b/Utils/Text Utils.cpp @@ -1020,5 +1020,287 @@ FLOAT GetWeightBasedOnMetricOption( UINT32 uiObjectWeight ) return( fWeight ); } +static inline STR8 Trim(STR8 &p) { + while(isspace(*p)) *p++ = 0; + STR8 e = p + strlen(p) - 1; + while (e > p && isspace(*e)) *e-- = 0; + return p; +} + +static inline STR16 Trim(STR16 &p) { + while(iswspace(*p)) *p++ = 0; + STR16 e = p + wcslen(p) - 1; + while (e > p && iswspace(*e)) *e-- = 0; + return p; +} +int StringToEnum(const STR value, const Str8EnumLookupType *table) +{ + if (NULL == value || 0 == *value) + return 0; + + for (const Str8EnumLookupType *itr = table; itr->name != NULL; ++itr) { + if (0 == _stricmp(value, itr->name)) + return itr->value; + } + STR end = NULL; + return (int)strtol(value, &end, 0); +} + +int StringToEnum(const STR8 value, const Str16EnumLookupType *table) +{ + if (NULL == value || 0 == *value) + return 0; + + int result = 0; + int len = strlen(value)+1; + STR16 wval = (STR16)malloc( len*sizeof(CHAR16) ); + mbstowcs(wval, value, len); + for (const Str16EnumLookupType *itr = table; itr->name != NULL; ++itr) { + if (0 == _wcsicmp(wval, itr->name)) { + result = itr->value; + } + } + free(wval); + return (result) ? result : (int)strtol(value, NULL, 0); +} + +int StringToEnum(const STR16 value, const Str8EnumLookupType *table) +{ + if (NULL == value || 0 == *value) + return 0; + + int result = 0; + int len = wcslen(value)+1; + STR8 mval = (STR8)malloc( len*sizeof(CHAR8) ); + wcstombs(mval, value, len); + for (const Str8EnumLookupType *itr = table; itr->name != NULL; ++itr) { + if (0 == _stricmp(mval, itr->name)) { + result = itr->value; + } + } + free(mval); + return (result) ? result : (int)wcstol(value, NULL, 0); +} + +int StringToEnum(const STR16 value, const Str16EnumLookupType *table) { + if (NULL == value || 0 == *value) + return 0; + + for (const Str16EnumLookupType *itr = table; itr->name != NULL; ++itr) { + if (0 == _wcsicmp(value, itr->name)) + return itr->value; + } + return (int)wcstol(value, NULL, 0); +} + + + +// routine for parsing white space separated lines. Handled like command line parameters w.r.t quotes. +void ParseCommandLine ( + const char *start, + char **argv, + char *args, + int *numargs, + int *numchars + ) +{ + const char NULCHAR = '\0'; + const char SPACECHAR = ' '; + const char TABCHAR = '\t'; + const char RETURNCHAR = '\r'; + const char LINEFEEDCHAR = '\n'; + const char DQUOTECHAR = '\"'; + const char SLASHCHAR = '\\'; + const char *p; + int inquote; /* 1 = inside quotes */ + int copychar; /* 1 = copy char to *args */ + unsigned numslash; /* num of backslashes seen */ + + *numchars = 0; + *numargs = 0; /* the program name at least */ + + p = start; + + inquote = 0; + + /* loop on each argument */ + for(;;) + { + if ( *p ) { while (*p == SPACECHAR || *p == TABCHAR || *p == RETURNCHAR || *p == LINEFEEDCHAR) ++p; } + + if (*p == NULCHAR) break; /* end of args */ + + /* scan an argument */ + if (argv) + *argv++ = args; /* store ptr to arg */ + ++*numargs; + + /* loop through scanning one argument */ + for (;;) + { + copychar = 1; + /* Rules: 2N backslashes + " ==> N backslashes and begin/end quote + 2N+1 backslashes + " ==> N backslashes + literal " + N backslashes ==> N backslashes */ + numslash = 0; + while (*p == SLASHCHAR) + { + /* count number of backslashes for use below */ + ++p; + ++numslash; + } + if (*p == DQUOTECHAR) + { + /* if 2N backslashes before, start/end quote, otherwise copy literally */ + if (numslash % 2 == 0) { + if (inquote) { + if (p[1] == DQUOTECHAR) + p++; /* Double quote inside quoted string */ + else /* skip first quote char and copy second */ + copychar = 0; + } else + copychar = 0; /* don't copy quote */ + + inquote = !inquote; + } + numslash /= 2; /* divide numslash by two */ + } + + /* copy slashes */ + while (numslash--) { + if (args) + *args++ = SLASHCHAR; + ++*numchars; + } + + /* if at end of arg, break loop */ + if (*p == NULCHAR || (!inquote && (*p == SPACECHAR || *p == TABCHAR || *p == RETURNCHAR || *p == LINEFEEDCHAR))) + break; + + /* copy character into argument */ + if (copychar) + { + if (args) + *args++ = *p; + ++*numchars; + } + ++p; + } + + /* null-terminate the argument */ + if (args) + *args++ = NULCHAR; /* terminate string */ + ++*numchars; + } + /* We put one last argument in -- a null ptr */ + if (argv) + *argv++ = NULL; + ++*numargs; +} + + +// routine for parsing white space separated lines. Handled like command line parameters w.r.t quotes. +void ParseCommandLine ( + const wchar_t *start, + wchar_t **argv, + wchar_t *args, + int *numargs, + int *numchars + ) +{ + const wchar_t NULCHAR = L'\0'; + const wchar_t SPACECHAR = L' '; + const wchar_t TABCHAR = L'\t'; + const wchar_t RETURNCHAR = L'\r'; + const wchar_t LINEFEEDCHAR = L'\n'; + const wchar_t DQUOTECHAR = L'\"'; + const wchar_t SLASHCHAR = L'\\'; + const wchar_t *p; + int inquote; /* 1 = inside quotes */ + int copychar; /* 1 = copy char to *args */ + unsigned numslash; /* num of backslashes seen */ + + *numchars = 0; + *numargs = 0; /* the program name at least */ + + p = start; + + inquote = 0; + + /* loop on each argument */ + for(;;) + { + if ( *p ) { while (*p == SPACECHAR || *p == TABCHAR || *p == RETURNCHAR || *p == LINEFEEDCHAR) ++p; } + + if (*p == NULCHAR) break; /* end of args */ + + /* scan an argument */ + if (argv) + *argv++ = args; /* store ptr to arg */ + ++*numargs; + + /* loop through scanning one argument */ + for (;;) + { + copychar = 1; + /* Rules: 2N backslashes + " ==> N backslashes and begin/end quote + 2N+1 backslashes + " ==> N backslashes + literal " + N backslashes ==> N backslashes */ + numslash = 0; + while (*p == SLASHCHAR) + { + /* count number of backslashes for use below */ + ++p; + ++numslash; + } + if (*p == DQUOTECHAR) + { + /* if 2N backslashes before, start/end quote, otherwise copy literally */ + if (numslash % 2 == 0) { + if (inquote) { + if (p[1] == DQUOTECHAR) + p++; /* Double quote inside quoted string */ + else /* skip first quote char and copy second */ + copychar = 0; + } else + copychar = 0; /* don't copy quote */ + + inquote = !inquote; + } + numslash /= 2; /* divide numslash by two */ + } + + /* copy slashes */ + while (numslash--) { + if (args) + *args++ = SLASHCHAR; + ++*numchars; + } + + /* if at end of arg, break loop */ + if (*p == NULCHAR || (!inquote && (*p == SPACECHAR || *p == TABCHAR || *p == RETURNCHAR || *p == LINEFEEDCHAR))) + break; + + /* copy character into argument */ + if (copychar) + { + if (args) + *args++ = *p; + ++*numchars; + } + ++p; + } + + /* null-terminate the argument */ + if (args) + *args++ = NULCHAR; /* terminate string */ + ++*numchars; + } + /* We put one last argument in -- a null ptr */ + if (argv) + *argv++ = NULL; + ++*numargs; +} + diff --git a/Utils/Text.h b/Utils/Text.h index f86a8cff..1acb4fe3 100644 --- a/Utils/Text.h +++ b/Utils/Text.h @@ -611,7 +611,7 @@ extern STR16 gConditionDesc[ 9 ]; // Flugente FTW1: Added list of temperature descriptions extern STR16 gTemperatureDesc[ 11 ]; -extern CHAR16 gMoneyStatsDesc[][ 13 ]; +extern CHAR16 gMoneyStatsDesc[][ 14 ]; // HEADROCK: Altered value to 16 //WarmSteel - And I need 17. extern CHAR16 gWeaponStatsDesc[][ 17 ]; // HEADROCK: Added externs for Item Description Box icon and stat tooltips @@ -2142,6 +2142,26 @@ extern STR16 Additional113Text[]; extern STR16 ranks[]; extern STR16 ranks[]; +// Enumeration support +typedef struct Str8EnumLookupType { + int value; + const STR8 name; +} Str8EnumLookupType; + +typedef struct Str16EnumLookupType { + int value; + const STR16 name; +} Str16EnumLookupType; + +const STR8 EnumToString(int value, const Str8EnumLookupType *table); +const STR16 EnumToString(int value, const Str16EnumLookupType *table); +int StringToEnum(const STR8 value, const Str8EnumLookupType *table); +int StringToEnum(const STR8 value, const Str16EnumLookupType *table); +int StringToEnum(const STR16 value, const Str16EnumLookupType *table); + +void ParseCommandLine(const char *start,char **argv,char *args,int *numargs,int *numchars); +void ParseCommandLine(const wchar_t *start,wchar_t **argv,wchar_t *args,int *numargs,int *numchars); + #endif diff --git a/Utils/_ChineseText.cpp b/Utils/_ChineseText.cpp index 8c4a56cd..ee61ef9f 100644 --- a/Utils/_ChineseText.cpp +++ b/Utils/_ChineseText.cpp @@ -2599,7 +2599,7 @@ STR16 gConditionDesc[] = //The headers used for the merc's money. -CHAR16 gMoneyStatsDesc[][ 13 ] = +CHAR16 gMoneyStatsDesc[][ 14 ] = { L"剩余", L"金额: ",//this is the overall balance diff --git a/Utils/_DutchText.cpp b/Utils/_DutchText.cpp index e99327b8..9d150bf6 100644 --- a/Utils/_DutchText.cpp +++ b/Utils/_DutchText.cpp @@ -2596,7 +2596,7 @@ STR16 gConditionDesc[] = //The headers used for the merc's money. -CHAR16 gMoneyStatsDesc[][ 13 ] = +CHAR16 gMoneyStatsDesc[][ 14 ] = { L"Bedrag", L"Restbedrag:", //this is the overall balance diff --git a/Utils/_EnglishText.cpp b/Utils/_EnglishText.cpp index 459d1b73..7190d6a8 100644 --- a/Utils/_EnglishText.cpp +++ b/Utils/_EnglishText.cpp @@ -2599,7 +2599,7 @@ STR16 gConditionDesc[] = //The headers used for the merc's money. -CHAR16 gMoneyStatsDesc[][ 13 ] = +CHAR16 gMoneyStatsDesc[][ 14 ] = { L"Amount", L"Remaining:", //this is the overall balance diff --git a/Utils/_FrenchText.cpp b/Utils/_FrenchText.cpp index 6605ddb8..7da67d14 100644 --- a/Utils/_FrenchText.cpp +++ b/Utils/_FrenchText.cpp @@ -2598,7 +2598,7 @@ STR16 gConditionDesc[] = //The headers used for the merc's money. -CHAR16 gMoneyStatsDesc[][ 13 ] = +CHAR16 gMoneyStatsDesc[][ 14 ] = { L"Montant", L"Restant :", //this is the overall balance diff --git a/Utils/_GermanText.cpp b/Utils/_GermanText.cpp index fbc4a31e..bb51bff6 100644 --- a/Utils/_GermanText.cpp +++ b/Utils/_GermanText.cpp @@ -2605,7 +2605,7 @@ STR16 gConditionDesc[] = }; //The headers used for the merc's money. -CHAR16 gMoneyStatsDesc[][ 13 ] = +CHAR16 gMoneyStatsDesc[][ 14 ] = { L"Betrag", L"verbleibend:", //this is the overall balance diff --git a/Utils/_ItalianText.cpp b/Utils/_ItalianText.cpp index 472c801f..ce27cf5d 100644 --- a/Utils/_ItalianText.cpp +++ b/Utils/_ItalianText.cpp @@ -2591,7 +2591,7 @@ STR16 gConditionDesc[] = //The headers used for the merc's money. -CHAR16 gMoneyStatsDesc[][ 13 ] = +CHAR16 gMoneyStatsDesc[][ 14 ] = { L"Ammontare", L"Rimanenti:", //this is the overall balance diff --git a/Utils/_PolishText.cpp b/Utils/_PolishText.cpp index f1023d45..b0e77da0 100644 --- a/Utils/_PolishText.cpp +++ b/Utils/_PolishText.cpp @@ -2606,7 +2606,7 @@ STR16 gConditionDesc[] = //The headers used for the merc's money. -CHAR16 gMoneyStatsDesc[][ 13 ] = +CHAR16 gMoneyStatsDesc[][ 14 ] = { L"Kwota", L"Pozostało:", //this is the overall balance diff --git a/Utils/_RussianText.cpp b/Utils/_RussianText.cpp index 0f94aa74..de4e7f71 100644 --- a/Utils/_RussianText.cpp +++ b/Utils/_RussianText.cpp @@ -2599,7 +2599,7 @@ STR16 gConditionDesc[] = //The headers used for the merc's money. -CHAR16 gMoneyStatsDesc[][ 13 ] = +CHAR16 gMoneyStatsDesc[][ 14 ] = { L"Кол-во", L"Осталось:", //this is the overall balance diff --git a/Utils/_TaiwaneseText.cpp b/Utils/_TaiwaneseText.cpp index 79a6f548..9b5613f5 100644 --- a/Utils/_TaiwaneseText.cpp +++ b/Utils/_TaiwaneseText.cpp @@ -2600,7 +2600,7 @@ STR16 gConditionDesc[] = //The headers used for the merc's money. -CHAR16 gMoneyStatsDesc[][ 13 ] = +CHAR16 gMoneyStatsDesc[][ 14 ] = { L"Amount", L"Remaining:", //this is the overall balance