- 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
This commit is contained in:
Wanne
2012-04-02 08:54:47 +00:00
parent ca873a8222
commit de37581daf
12 changed files with 314 additions and 52 deletions
+2 -42
View File
@@ -4,19 +4,9 @@
#include "KeyMap.h"
#include <windows.h>
#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;
+282
View File
@@ -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;
}
+21 -1
View File
@@ -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
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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