Replace the calls to sscanf(for number parsing) and number parsers with strto(u)l

The wcsto(u)l functions are available for your wide character needs.


git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@5471 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
pierre 2010-05-23 22:31:40 +00:00
parent 19e3e6effe
commit 88cf89e7fe
1 changed files with 15 additions and 68 deletions

View File

@ -23,47 +23,12 @@
// faster than sscanf // faster than sscanf
bool AsciiToHex(const char* _szValue, u32& result) bool AsciiToHex(const char* _szValue, u32& result)
{ {
u32 value = 0; char *endptr = NULL;
size_t finish = strlen(_szValue); u32 value = strtoul(_szValue,&endptr,16);
if (!endptr || *endptr != '\0')
if (finish > 8) return false;
finish = 8; // Max 32-bit values are supported.
for (size_t count = 0; count < finish; count++)
{
value <<= 4;
switch (_szValue[count])
{
case '0': break;
case '1': value += 1; break;
case '2': value += 2; break;
case '3': value += 3; break;
case '4': value += 4; break;
case '5': value += 5; break;
case '6': value += 6; break;
case '7': value += 7; break;
case '8': value += 8; break;
case '9': value += 9; break;
case 'A':
case 'a': value += 10; break;
case 'B':
case 'b': value += 11; break;
case 'C':
case 'c': value += 12; break;
case 'D':
case 'd': value += 13; break;
case 'E':
case 'e': value += 14; break;
case 'F':
case 'f': value += 15; break;
default:
return false;
break;
}
}
result = value; result = value;
return (true); return true;
} }
// Convert AB to it's ascii table entry numbers 0x4142 // Convert AB to it's ascii table entry numbers 0x4142
@ -271,30 +236,10 @@ std::string StripNewline(const std::string& s)
bool TryParseInt(const char* str, int* outVal) bool TryParseInt(const char* str, int* outVal)
{ {
const char* s = str; char *endptr = NULL;
int value = 0; int value = strtol(str,&endptr,10);
bool negativ = false; if (!endptr || *endptr != '\0')
return false;
if (*s == '-')
{
negativ = true;
s++;
}
while (*s)
{
char c = *s++;
if ((c < '0') || (c > '9'))
{
return false;
}
value = value * 10 + (c - '0');
}
if (negativ)
value = -value;
*outVal = value; *outVal = value;
return true; return true;
} }
@ -441,10 +386,12 @@ void SplitString(const std::string& str, const std::string& delim, std::vector<s
bool TryParseUInt(const std::string& str, u32* output) bool TryParseUInt(const std::string& str, u32* output)
{ {
if (!strcmp(str.substr(0, 2).c_str(), "0x") || !strcmp(str.substr(0, 2).c_str(), "0X")) char *endptr = NULL;
return sscanf(str.c_str() + 2, "%x", output) > 0; u32 value = strtoul(str.c_str(),&endptr,0);
else if (!endptr || *endptr != '\0')
return sscanf(str.c_str(), "%d", output) > 0; return false;
*output = value;
return true;
} }
int ChooseStringFrom(const char* str, const char* * items) int ChooseStringFrom(const char* str, const char* * items)