From 815d1149f9fb6be2c1bc05fb04f574eb928e050e Mon Sep 17 00:00:00 2001 From: Tim Redfern Date: Fri, 3 Jan 2014 19:57:48 +0000 Subject: updated refactored files --- NT/src/utils.cpp | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 NT/src/utils.cpp (limited to 'NT/src/utils.cpp') diff --git a/NT/src/utils.cpp b/NT/src/utils.cpp new file mode 100644 index 0000000..a3f0f8a --- /dev/null +++ b/NT/src/utils.cpp @@ -0,0 +1,140 @@ +#include "utils.h" + +using namespace std; + +//double equality +bool fequal(const double u,const double v){ + if (abs(u-v)-FLOAT_THRESHOLD) return true; + else return false; +}; +bool fgreater_or_equal(const double u,const double v){ + //v is more or equal to u + if (v-u>-FLOAT_THRESHOLD) return true; + else return false; +}; +bool fless(const double u,const double v){ + //v is less than u + if (u-v>FLOAT_THRESHOLD) return true; + else return false; +}; +bool fgreater(const double u,const double v){ + //v is greater than u + if (v-u>FLOAT_THRESHOLD) return true; + else return false; +}; + +//----------------------------------------with thanks to openframeworks +template <> +string toHex(const string& value) { + ostringstream out; + // how many bytes are in the string + int numBytes = value.size(); + for(int i = 0; i < numBytes; i++) { + // print each byte as a 2-character wide hex value + out << setfill('0') << setw(2) << hex << (unsigned int) ((unsigned char)value[i]); + } + return out.str(); +} + +//---------------------------------------- +string toHex(const char* value) { + // this function is necessary if you want to print a string + // using a syntax like toHex("test") + return toHex((string) value); +} + +//---------------------------------------- +int toInt(const string& intString) { + int x = 0; + istringstream cur(intString); + cur >> x; + return x; +} + +//---------------------------------------- +int hexToInt(const string& intHexString) { + int x = 0; + istringstream cur(intHexString); + cur >> hex >> x; + return x; +} + +//---------------------------------------- +char hexToChar(const string& charHexString) { + int x = 0; + istringstream cur(charHexString); + cur >> hex >> x; + return (char) x; +} + +//---------------------------------------- +double hexToFloat(const string& doubleHexString) { + union intFloatUnion { + int x; + double f; + } myUnion; + myUnion.x = 0; + istringstream cur(doubleHexString); + cur >> hex >> myUnion.x; + return myUnion.f; +} + +//---------------------------------------- +string hexToString(const string& stringHexString) { + stringstream out; + stringstream stream(stringHexString); + // a hex string has two characters per byte + int numBytes = stringHexString.size() / 2; + for(int i = 0; i < numBytes; i++) { + string curByte; + // grab two characters from the hex string + stream >> setw(2) >> curByte; + // prepare to parse the two characters + stringstream curByteStream(curByte); + int cur = 0; + // parse the two characters as a hex-encoded int + curByteStream >> hex >> cur; + // add the int as a char to our output stream + out << (char) cur; + } + return out.str(); +} + +//---------------------------------------- +double toFloat(const string& doubleString) { + double x = 0; + istringstream cur(doubleString); + cur >> x; + return x; +} + +//---------------------------------------- +bool toBool(const string& boolString) { + static const string trueString = "true"; + static const string falseString = "false"; + string lower = Poco::toLower(boolString); + if(lower == trueString) { + return true; + } + if(lower == falseString) { + return false; + } + bool x = false; + istringstream cur(lower); + cur >> x; + return x; +} + +//---------------------------------------- +char toChar(const string& charString) { + char x = '\0'; + istringstream cur(charString); + cur >> x; + return x; +} + -- cgit v1.2.3