summaryrefslogtreecommitdiffstats
path: root/src/uscxml/util/Convenience.cpp
diff options
context:
space:
mode:
authorStefan Radomski <github@mintwerk.de>2016-05-12 13:12:33 (GMT)
committerStefan Radomski <github@mintwerk.de>2016-05-12 13:12:33 (GMT)
commitb62e7979600feee23dc7cdb61042a8fc7673122b (patch)
treef7351372f37979dd2d048e0b68a16a4cd3b2aadb /src/uscxml/util/Convenience.cpp
parent1b11b310be61e51b3ac5ebb83f7c8a33aef3d6e8 (diff)
downloaduscxml-b62e7979600feee23dc7cdb61042a8fc7673122b.zip
uscxml-b62e7979600feee23dc7cdb61042a8fc7673122b.tar.gz
uscxml-b62e7979600feee23dc7cdb61042a8fc7673122b.tar.bz2
Major Refactoring v2.0
Diffstat (limited to 'src/uscxml/util/Convenience.cpp')
-rw-r--r--src/uscxml/util/Convenience.cpp177
1 files changed, 177 insertions, 0 deletions
diff --git a/src/uscxml/util/Convenience.cpp b/src/uscxml/util/Convenience.cpp
new file mode 100644
index 0000000..7ceb875
--- /dev/null
+++ b/src/uscxml/util/Convenience.cpp
@@ -0,0 +1,177 @@
+/**
+ * @file
+ * @author 2012-2013 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de)
+ * @copyright Simplified BSD
+ *
+ * @cond
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the FreeBSD license as published by the FreeBSD
+ * project.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * You should have received a copy of the FreeBSD license along with this
+ * program. If not, see <http://www.opensource.org/licenses/bsd-license>.
+ * @endcond
+ */
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include "Convenience.h"
+
+namespace uscxml {
+
+bool isnan(double x) {
+ return x != x;
+}
+
+bool isNumeric(const char* pszInput, int nNumberBase) {
+ std::string base = ".-0123456789ABCDEF";
+ std::string input = pszInput;
+ return (input.find_first_not_of(base.substr(0, nNumberBase + 2)) == std::string::npos);
+}
+
+bool isInteger(const char* pszInput, int nNumberBase) {
+ std::string base = "-0123456789ABCDEF";
+ std::string input = pszInput;
+ return (input.find_first_not_of(base.substr(0, nNumberBase + 1)) == std::string::npos);
+}
+
+bool iequals(const std::string& a, const std::string& b) {
+ // this impementation beats boost::iequals 2700ms vs 2100ms for test-performance.scxml - we don't care for non-ascii yet
+ unsigned int size = a.size();
+ if (b.size() != size)
+ return false;
+ for (unsigned int i = 0; i < size; ++i)
+ if (tolower(a[i]) != tolower(b[i]))
+ return false;
+ return true;
+}
+
+bool equals(const std::string& a, const std::string& b) {
+ unsigned int size = a.size();
+ if (b.size() != size)
+ return false;
+ for (unsigned int i = 0; i < size; ++i)
+ if (a[i] != b[i])
+ return false;
+ return true;
+}
+
+bool stringIsTrue(const std::string& value) {
+ return (iequals(value, "on") ||
+ iequals(value, "true") ||
+ iequals(value, "1") ||
+ iequals(value, "yes"));
+}
+
+bool envVarIsTrue(const char* name) {
+ const char* value = getenv(name);
+ if (value == NULL)
+ return false;
+ return stringIsTrue(value);
+}
+
+bool envVarIEquals(const char* name, const char* value) {
+ const char* envVarValue = getenv(name);
+ if (envVarValue == NULL)
+ return false;
+ return iequals(envVarValue, value);
+}
+
+std::string escape(const std::string& a) {
+ std::stringstream b;
+ // see http://en.cppreference.com/w/cpp/language/escape
+
+ std::string::const_iterator it = a.begin();
+ while (it != a.end()) {
+ char c = *it++;
+ switch (c) {
+ case '\\':
+ b << '\\' << '\\';
+ break;
+ case '\0':
+ b << '\\' << '0';
+ break;
+ case '"':
+ b << '\\' << '"';
+ break;
+ case '\a':
+ b << '\\' << 'a';
+ break;
+ case '\b':
+ b << '\\' << 'b';
+ break;
+ case '\f':
+ b << '\\' << 'f';
+ break;
+ case '\n':
+ b << '\\' << 'n';
+ break;
+ case '\r':
+ b << '\\' << 'r';
+ break;
+ case '\t':
+ b << '\\' << 't';
+ break;
+ case '\v':
+ b << '\\' << 'v';
+ break;
+ default:
+ b << c;
+ }
+ }
+
+ return b.str();
+}
+
+std::string unescape(const std::string& a) {
+ std::stringstream b;
+ // see http://en.cppreference.com/w/cpp/language/escape
+
+ std::string::const_iterator it = a.begin();
+ while (it != a.end()) {
+ char c = *it++;
+ if (c == '\\' && it != a.end()) {
+ switch (*it++) {
+ case '\\':
+ c = '\\';
+ break;
+ case '0':
+ c = '\0';
+ break;
+ case '"':
+ c = '"';
+ break;
+ case 'a':
+ c = '\a';
+ break;
+ case 'b':
+ c = '\b';
+ break;
+ case 'f':
+ c = '\f';
+ break;
+ case 'n':
+ c = '\n';
+ break;
+ case 'r':
+ c = '\r';
+ break;
+ case 't':
+ c = '\t';
+ break;
+ case 'v':
+ c = '\v';
+ break;
+ }
+ }
+ b << c;
+ }
+
+ return b.str();
+}
+
+}