diff options
Diffstat (limited to 'NT/src/utils.h')
| -rw-r--r-- | NT/src/utils.h | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/NT/src/utils.h b/NT/src/utils.h new file mode 100644 index 0000000..61e2c42 --- /dev/null +++ b/NT/src/utils.h @@ -0,0 +1,97 @@ +#ifndef ROTOR_utils_H +#define ROTOR_utils_H + +#include <cmath> +#include <string> +#include <sstream> +#include <vector> +#include <iomanip> + +#include "Poco/String.h" + +#define FLOAT_THRESHOLD .001f + +//double equality +bool fequal(const double u,const double v); +bool fless_or_equal(const double u,const double v); +bool fgreater_or_equal(const double u,const double v); +bool fless(const double u,const double v); +bool fgreater(const double u,const double v); + +//----------------------------------------with thanks to openframeworks + +template <class T> +std::string toString(const T& value){ + std::ostringstream out; + out << value; + return out.str(); +} + +/// like sprintf "%4f" format, in this example precision=4 +template <class T> +std::string toString(const T& value, int precision){ + std::ostringstream out; + out << std::fixed << std::setprecision(precision) << value; + return out.str(); +} + +/// like sprintf "% 4d" or "% 4f" format, in this example width=4, fill=' ' +template <class T> +std::string toString(const T& value, int width, char fill ){ + std::ostringstream out; + out << std::fixed << std::setfill(fill) << std::setw(width) << value; + return out.str(); +} + +/// like sprintf "%04.2d" or "%04.2f" format, in this example precision=2, width=4, fill='0' +template <class T> +std::string toString(const T& value, int precision, int width, char fill ){ + std::ostringstream out; + out << std::fixed << std::setfill(fill) << std::setw(width) << std::setprecision(precision) << value; + return out.str(); +} + +template<class T> +std::string toString(const std::vector<T>& values) { + std::stringstream out; + int n = values.size(); + out << "{"; + if(n > 0) { + for(int i = 0; i < n - 1; i++) { + out << values[i] << ", "; + } + out << values[n - 1]; + } + out << "}"; + return out.str(); +} + +template <class T> +std::string toHex(const T& value) { + std::ostringstream out; + // pretend that the value is a bunch of bytes + unsigned char* valuePtr = (unsigned char*) &value; + // the number of bytes is determined by the datatype + int numBytes = sizeof(T); + // the bytes are stored backwards (least significant first) + for(int i = numBytes - 1; i >= 0; i--) { + // print each byte out as a 2-character wide hex value + out << std::setfill('0') << std::setw(2) << std::hex << (int) valuePtr[i]; + } + return out.str(); +} +template <> +std::string toHex(const std::string& value); +std::string toHex(const char* value); + +int hexToInt(const std::string& intHexstring); +char hexToChar(const std::string& charHexString); +double hexToFloat(const std::string& doubleHexString); +std::string hexToString(const std::string& stringHexString); + +int toInt(const std::string& intString); +char toChar(const std::string& charString); +double toFloat(const std::string& doubleString); +bool toBool(const std::string& boolString); + +#endif |
