--- a/misc.cpp Mon Mar 18 04:03:05 2013 +0200 +++ b/misc.cpp Mon Mar 18 12:15:23 2013 +0200 @@ -1,24 +1,24 @@ #include "common.h" #include <math.h> -double GetWordFloat (str& s, const ushort n) { +double getWordFloat (str& s, const ushort n) { return atof ((s / " ")[n]); } -long GetWordInt (str& s, const ushort n) { +long getWordInt (str& s, const ushort n) { return atol ((s / " ")[n]); } -vertex ParseVertex (str& s, const ushort n) { +vertex parseVertex (str& s, const ushort n) { vertex v; - v.x = GetWordFloat (s, n); - v.y = GetWordFloat (s, n + 1); - v.z = GetWordFloat (s, n + 2); + v.x = getWordFloat (s, n); + v.y = getWordFloat (s, n + 1); + v.z = getWordFloat (s, n + 2); return v; } -void StripWhitespace (str& s) { +void stripWhitespace (str& s) { str other; for (size_t i = 0; i < ~s; i++) @@ -54,4 +54,39 @@ zRep -= 1; return zRep; +} + +// ============================================================================= +// isNumber (str&) +// +// Returns whether a given string represents a floating point number +// TODO: Does LDraw support scientific notation? +// ============================================================================= +bool isNumber (str& zToken) { + char* cpPointer = &zToken[0]; + bool bGotDot = false; + + // Allow leading hyphen for negatives + if (*cpPointer == '-') + cpPointer++; + + while (*cpPointer != '\0') { + if (*cpPointer == '.' && !bGotDot) { + // Decimal point + bGotDot = true; + cpPointer++; + continue; + } + + if (*cpPointer >= '0' && *cpPointer <= '9') { + cpPointer++; + continue; // Digit + } + + // If the above cases didn't catch this character, it was + // illegal and this is therefore not a number. + return false; + } + + return true; } \ No newline at end of file