summaryrefslogtreecommitdiffstats
path: root/src/uscxml/util/Convenience.cpp
blob: 4cfbb5ea8903d51e4b437ca593eb5bdad17b17d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
 *  @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 {

NumAttr::NumAttr(const std::string& str) {
	size_t valueStart = str.find_first_of("0123456789.");
	if (valueStart != std::string::npos) {
		size_t valueEnd = str.find_last_of("0123456789.");
		if (valueEnd != std::string::npos) {
			value = str.substr(valueStart, (valueEnd - valueStart) + 1);
			size_t unitStart = str.find_first_not_of(" \t", valueEnd + 1);
			if (unitStart != std::string::npos) {
				size_t unitEnd = str.find_last_of(" \t");
				if (unitEnd != std::string::npos && unitEnd > unitStart) {
					unit = str.substr(unitStart, unitEnd - unitStart);
				} else {
					unit = str.substr(unitStart, str.length() - unitStart);
				}
			}
		}
	}
}


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
#if 0
	// not platform independent?
	return strcasecmp(a.c_str(), b.c_str()) == 0;
#else
	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;
#endif
}

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();
}

}