summaryrefslogtreecommitdiff
path: root/NT/src/utils.cpp
diff options
context:
space:
mode:
authorTim Redfern <tim@eclectronics.org>2014-01-03 19:57:48 +0000
committerTim Redfern <tim@eclectronics.org>2014-01-03 19:57:48 +0000
commit815d1149f9fb6be2c1bc05fb04f574eb928e050e (patch)
tree647ac891d00ab5561926a6566d37c70346df634a /NT/src/utils.cpp
parent820b73f2abdf51bd59cb5b74739fb6f264b7c54e (diff)
updated refactored files
Diffstat (limited to 'NT/src/utils.cpp')
-rw-r--r--NT/src/utils.cpp140
1 files changed, 140 insertions, 0 deletions
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 fless_or_equal(const double u,const double v){
+ //v is less or equal to u
+ if (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;
+}
+