summaryrefslogtreecommitdiff
path: root/src/cat/TPTScriptInterface.cpp
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2013-03-26 09:24:15 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2013-03-26 09:24:15 (GMT)
commit0791f5b0f8c64252c3288d4612655370770dbc2c (patch)
treea3c43eea033b9a9b30708646377705ef2956bd84 /src/cat/TPTScriptInterface.cpp
parent010f7790ebc72baf1fa3c99dfaeb8b69cdd3a0cb (diff)
downloadpowder-0791f5b0f8c64252c3288d4612655370770dbc2c.zip
powder-0791f5b0f8c64252c3288d4612655370770dbc2c.tar.gz
TPTScriptInterface: Ability to read hexadecimal constants
Diffstat (limited to 'src/cat/TPTScriptInterface.cpp')
-rw-r--r--src/cat/TPTScriptInterface.cpp58
1 files changed, 57 insertions, 1 deletions
diff --git a/src/cat/TPTScriptInterface.cpp b/src/cat/TPTScriptInterface.cpp
index 84f55ef..a6a386f 100644
--- a/src/cat/TPTScriptInterface.cpp
+++ b/src/cat/TPTScriptInterface.cpp
@@ -85,10 +85,22 @@ ValueType TPTScriptInterface::testType(std::string word)
{
if(rawWord[i] == ',' && rawWord[i+1] >= '0' && rawWord[i+1] <= '9')
goto parsePoint;
+ else if((rawWord[i] == '#' || rawWord[i] == 'x') &&
+ ((rawWord[i+1] >= '0' && rawWord[i+1] <= '9')
+ || (rawWord[i+1] >= 'a' && rawWord[i+1] <= 'f')
+ || (rawWord[i+1] >= 'A' && rawWord[i+1] <= 'F')))
+ goto parseNumberHex;
else
goto parseString;
}
}
+ parseNumberHex:
+ i++;
+ for(; i < word.length(); i++)
+ if(!((rawWord[i] >= '0' && rawWord[i] <= '9') || (rawWord[i] >= 'a' && rawWord[i] <= 'f') || (rawWord[i] >= 'A' && rawWord[i] <= 'F')))
+ {
+ goto parseString;
+ }
return TypeNumber;
parsePoint:
i++;
@@ -102,6 +114,50 @@ ValueType TPTScriptInterface::testType(std::string word)
return TypeString;
}
+int TPTScriptInterface::parseNumber(char * stringData)
+{
+ char cc;
+ int base = 10;
+ int currentNumber = 0;
+ if(stringData[0] == '#')
+ {
+ stringData++;
+ base = 16;
+ }
+ else if(stringData[0] == '0' && stringData[1] == 'x')
+ {
+ stringData+=2;
+ base = 16;
+ }
+ if(base == 16)
+ {
+ while(cc = *(stringData++))
+ {
+ currentNumber *= base;
+ if(cc >= '0' && cc <= '9')
+ currentNumber += cc - '0';
+ else if(cc >= 'a' && cc <= 'f')
+ currentNumber += (cc - 'A') + 10;
+ else if(cc >= 'A' && cc <= 'F')
+ currentNumber += (cc - 'A') + 10;
+ else
+ break;
+ }
+ }
+ else
+ {
+ while(cc = *(stringData++))
+ {
+ currentNumber *= base;
+ if(cc >= '0' && cc <= '9')
+ currentNumber += cc - '0';
+ else
+ break;
+ }
+ }
+ return currentNumber;
+}
+
AnyType TPTScriptInterface::eval(std::deque<std::string> * words)
{
if(words->size() < 1)
@@ -128,7 +184,7 @@ AnyType TPTScriptInterface::eval(std::deque<std::string> * words)
return tptS_quit(words);
break;
case TypeNumber:
- return NumberType(atoi(rawWord));
+ return NumberType(parseNumber(rawWord));
case TypePoint:
{
int pointX, pointY;