37#include <xercesc/util/TransService.hpp>
38#include <xercesc/util/TranscodingException.hpp>
44#define KM_PER_MILE 1.609344
59 const std::string::size_type endpos = str.find_last_not_of(
" \t\n\r");
60 if (std::string::npos != endpos) {
61 const int startpos = (int)str.find_first_not_of(
" \t\n\r");
62 return str.substr(startpos, endpos - startpos + 1);
70 const std::string::size_type endpos = str.find_last_not_of(
"0");
71 if (endpos != std::string::npos && str.back() ==
'0') {
72 std::string res = str.substr(0,
MAX2((
int)str.size() - max, (
int)endpos + 1));
81 std::transform(s.begin(), s.end(), s.begin(), [](
char c) {
82 return (char)::tolower(c);
92 for (
const auto& c : str) {
93 const unsigned char uc = (
unsigned char)c;
97 result += (char)(0xc2 + (uc > 0xbf));
98 result += (char)((uc & 0x3f) + 0x80);
107 str =
replace(str,
"\xE4",
"ae");
108 str =
replace(str,
"\xC4",
"Ae");
109 str =
replace(str,
"\xF6",
"oe");
110 str =
replace(str,
"\xD6",
"Oe");
111 str =
replace(str,
"\xFC",
"ue");
112 str =
replace(str,
"\xDC",
"Ue");
113 str =
replace(str,
"\xDF",
"ss");
114 str =
replace(str,
"\xC9",
"E");
115 str =
replace(str,
"\xE9",
"e");
116 str =
replace(str,
"\xC8",
"E");
117 str =
replace(str,
"\xE8",
"e");
124 std::string::size_type idx = str.find(what);
125 const int what_len = (int)what.length();
127 const int by_len = (int)by.length();
128 while (idx != std::string::npos) {
129 str = str.replace(idx, what_len, by);
130 idx = str.find(what, idx + by_len);
140 if (timeRef !=
nullptr) {
141 const std::string::size_type localTimeIndex = str.find(
"${LOCALTIME}");
142 const std::string::size_type utcIndex = str.find(
"${UTC}");
143 const bool isUTC = utcIndex != std::string::npos;
144 if (localTimeIndex != std::string::npos || isUTC) {
145 const time_t rawtime = std::chrono::system_clock::to_time_t(*timeRef);
147 struct tm* timeinfo = isUTC ? gmtime(&rawtime) : localtime(&rawtime);
148 strftime(buffer, 80,
"%Y-%m-%d-%H-%M-%S.", timeinfo);
149 auto seconds = std::chrono::time_point_cast<std::chrono::seconds>(*timeRef);
150 auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(*timeRef - seconds);
151 const std::string micro = buffer +
toString(microseconds.count());
153 s.replace(utcIndex, 6, micro);
155 s.replace(localTimeIndex, 12, micro);
159 const std::string::size_type pidIndex = str.find(
"${PID}");
160 if (pidIndex != std::string::npos) {
162 s.replace(pidIndex, 6,
toString(::GetCurrentProcessId()));
164 s.replace(pidIndex, 6,
toString(::getpid()));
167 if (std::getenv(
"SUMO_LOGO") ==
nullptr) {
168 s =
replace(s,
"${SUMO_LOGO}",
"${SUMO_HOME}/data/logo/sumo-128x138.png");
170 const std::string::size_type tildeIndex = str.find(
"~");
171 if (tildeIndex == 0) {
172 s.replace(0, 1,
"${HOME}");
174 s =
replace(s,
",~",
",${HOME}");
176 if (std::getenv(
"HOME") ==
nullptr) {
177 s =
replace(s,
"${HOME}",
"${USERPROFILE}");
185 std::regex envVarExpr(R
"(\$\{(.+?)\})");
189 std::string strIter = s;
192 while (std::regex_search(strIter, match, envVarExpr)) {
193 std::string varName = match[1];
196 std::string varValue;
197 if (std::getenv(varName.c_str()) !=
nullptr) {
198 varValue = std::getenv(varName.c_str());
202 s = std::regex_replace(s, std::regex(
"\\$\\{" + varName +
"\\}"), varValue);
205 strIter = match.suffix();
213 const std::chrono::system_clock::time_point now = timeRef ==
nullptr ? std::chrono::system_clock::now() : *timeRef;
214 const auto now_seconds = std::chrono::time_point_cast<std::chrono::seconds>(now);
215 const std::time_t now_c = std::chrono::system_clock::to_time_t(now);
216 const auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(now - now_seconds).count();
217 std::tm local_tm = *std::localtime(&now_c);
220 std::time_t utc_time = std::time(
nullptr);
221 std::tm utc_tm = *std::gmtime(&utc_time);
222 const double offset = std::difftime(std::mktime(&local_tm), std::mktime(&utc_tm)) / 3600.0;
223 const int hours_offset =
static_cast<int>(offset);
224 const int minutes_offset =
static_cast<int>((offset - hours_offset) * 60);
227 std::ostringstream oss;
229 std::strftime(buf,
sizeof(buf),
"%Y-%m-%dT%H:%M:%S", &local_tm);
231 << std::setw(6) << std::setfill(
'0') << std::abs(microseconds)
232 << (hours_offset >= 0 ?
"+" :
"-")
233 << std::setw(2) << std::setfill(
'0') << std::abs(hours_offset) <<
":"
234 << std::setw(2) << std::setfill(
'0') << std::abs(minutes_offset);
241 return str.compare(0, prefix.length(), prefix) == 0;
247 if (str.length() >= suffix.length()) {
248 return str.compare(str.length() - suffix.length(), suffix.length(), suffix) == 0;
257 return std::string(
MAX2(0, length - (
int)str.size()), padding) + str;
263 std::string result =
replace(orig,
"&",
"&");
264 result =
replace(result,
">",
">");
265 result =
replace(result,
"<",
"<");
266 result =
replace(result,
"\"",
""");
267 if (maskDoubleHyphen) {
268 result =
replace(result,
"--",
"--");
270 for (
char invalid =
'\1'; invalid <
' '; invalid++) {
271 result =
replace(result, std::string(1, invalid).c_str(),
"");
273 return replace(result,
"'",
"'");
279 std::string result =
replace(orig,
"\"",
"\\\"");
286 std::ostringstream out;
288 for (
int i = 0; i < (int)toEncode.length(); ++i) {
289 const char t = toEncode.at(i);
291 if ((encodeWhich !=
"" && encodeWhich.find(t) == std::string::npos) ||
292 (encodeWhich ==
"" &&
293 ((t >= 45 && t <= 57) ||
294 (t >= 65 && t <= 90) ||
296 (t >= 97 && t <= 122) ||
299 out << toEncode.at(i);
311 std::ostringstream out;
313 for (
int i = 0; i < (int)toDecode.length(); ++i) {
314 if (toDecode.at(i) ==
'%') {
315 std::string str(toDecode.substr(i + 1, 2));
319 out << toDecode.at(i);
332 s <<
"%" << std::setw(2) << std::setfill(
'0') << std::hex << i;
342 std::istringstream in(str);
348 return static_cast<unsigned char>(c);
354 long long int result =
toLong(sData);
355 if (result > std::numeric_limits<int>::max() || result < std::numeric_limits<int>::min()) {
366 const long long int result =
toLong(sData);
368 return ((result <= std::numeric_limits<int>::max()) && (result >= std::numeric_limits<int>::min()));
376 if (sData.length() == 0) {
385 const char*
const data = sData.c_str();
386 if (data == 0 || data[0] == 0) {
392 long long int ret = _strtoi64(data, &end, 10);
394 long long int ret = strtoll(data, &end, 10);
396 if (errno == ERANGE) {
400 if ((
int)(end - data) != (
int)strlen(data)) {
409 const char*
const data = sData.c_str();
410 if (data == 0 || data[0] == 0) {
418 _strtoi64(data, &end, 10);
420 strtoll(data, &end, 10);
423 if (errno == ERANGE) {
427 if ((
int)(end - data) != (
int)strlen(data)) {
436 if (sData.length() == 0) {
442 if (sData[0] ==
'#') {
443 result = std::stoi(sData.substr(1), &idx, 16);
446 result = std::stoi(sData, &idx, 16);
451 if (idx != sData.length()) {
460 if (sData.length() == 0) {
464 if (sData[0] ==
'#') {
465 sData = sData.substr(1);
467 const char* sDataPtr = sData.c_str();
472 strtol(sDataPtr, &returnPtr, 16);
474 if (errno == ERANGE) {
478 if (sDataPtr == returnPtr) {
482 if (
static_cast<size_t>(returnPtr - sDataPtr) != sData.size()) {
491 if (sData.size() == 0) {
496 const double result = std::stod(sData, &idx);
497 if (idx != sData.size()) {
511 if (sData.size() == 0) {
514 const char* sDataPtr = sData.c_str();
519 strtod(sDataPtr, &returnPtr);
521 if (errno == ERANGE) {
525 if (sDataPtr == returnPtr) {
529 if (
static_cast<size_t>(returnPtr - sDataPtr) != sData.size()) {
538 if (sData.length() == 0) {
547 if (sData.length() == 0) {
551 if (s ==
"1" || s ==
"yes" || s ==
"true" || s ==
"on" || s ==
"x" || s ==
"t") {
554 if (s ==
"0" || s ==
"no" || s ==
"false" || s ==
"off" || s ==
"-" || s ==
"f") {
563 if (sData.length() == 0) {
568 if (s ==
"1" || s ==
"yes" || s ==
"true" || s ==
"on" || s ==
"x" || s ==
"t") {
572 if (s ==
"0" || s ==
"no" || s ==
"false" || s ==
"off" || s ==
"-" || s ==
"f") {
589 if (sData.size() == 0) {
594 const double result = std::stod(sData, &idx);
595 if (idx != sData.size()) {
596 const std::string unit =
prune(sData.substr(idx));
597 if (unit ==
"m" || unit ==
"metre" || unit ==
"meter" || unit ==
"metres" || unit ==
"meters") {
600 if (unit ==
"km" || unit ==
"kilometre" || unit ==
"kilometer" || unit ==
"kilometres" || unit ==
"kilometers") {
601 return result * 1000.;
603 if (unit ==
"mi" || unit ==
"mile" || unit ==
"miles") {
607 return result * 1852.;
609 if (unit ==
"ft" || unit ==
"foot" || unit ==
"feet") {
610 return result * 12. * 0.0254;
612 if (unit ==
"\"" || unit ==
"in" || unit ==
"inch" || unit ==
"inches") {
613 return result * 0.0254;
615 if (unit[0] ==
'\'') {
616 double inches = 12 * result;
617 if (unit.length() > 1) {
618 inches += std::stod(unit.substr(1), &idx);
619 if (unit.substr(idx) ==
"\"") {
620 return inches * 0.0254;
637 if (sData.size() == 0) {
642 const double result = std::stod(sData, &idx);
643 if (idx != sData.size()) {
644 const std::string unit =
prune(sData.substr(idx));
645 if (unit ==
"km/h" || unit ==
"kph" || unit ==
"kmh" || unit ==
"kmph") {
654 if (unit ==
"knots") {
655 return result * 1.852 / 3.6;
659 return defaultKmph ? result / 3.6 : result;
676#if _XERCES_VERSION < 30100
677 char* t = XERCES_CPP_NAMESPACE::XMLString::transcode(data);
678 std::string result(t);
679 XERCES_CPP_NAMESPACE::XMLString::release(&t);
683 XERCES_CPP_NAMESPACE::TranscodeToStr utf8(data,
"UTF-8");
684 return reinterpret_cast<const char*
>(utf8.str());
685 }
catch (XERCES_CPP_NAMESPACE::TranscodingException&) {
694#if _XERCES_VERSION > 30100
697 myLCPTranscoder = XERCES_CPP_NAMESPACE::XMLPlatformUtils::fgTransService->makeNewLCPTranscoder(XERCES_CPP_NAMESPACE::XMLPlatformUtils::fgMemoryManager);
702 }
catch (XERCES_CPP_NAMESPACE::TranscodingException&) {}
710#if _XERCES_VERSION > 30100
713 myLCPTranscoder = XERCES_CPP_NAMESPACE::XMLPlatformUtils::fgTransService->makeNewLCPTranscoder(XERCES_CPP_NAMESPACE::XMLPlatformUtils::fgMemoryManager);
716 XERCES_CPP_NAMESPACE::TranscodeFromStr utf8(
reinterpret_cast<const XMLByte*
>(utf8String.c_str()), utf8String.size(),
"UTF-8");
719 }
catch (XERCES_CPP_NAMESPACE::TranscodingException&) {}
727 std::string result = s;
728 result.erase(0, s.find_first_not_of(t));
734 std::string result = s;
735 result.erase(s.find_last_not_of(t) + 1);
750 bool firstLine =
true;
751 bool firstWord =
true;
752 for (std::string p : parts) {
753 if ((
int)(line.size() + p.size()) < width || firstWord) {
766 result = result + line;
771 if (line.size() > 0) {
777 result = result + line;
792 auto valueStr =
toString(value, precision);
794 while (valueStr.size() > 1) {
795 if (valueStr.back() ==
'0') {
797 }
else if (valueStr.back() ==
'.') {
std::pair< int, double > MMVersion
(M)ajor/(M)inor version for written networks and default version for loading
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
std::vector< std::string > getVector()
return vector of strings
static std::string pruneZeros(const std::string &str, int max)
Removes trailing zeros (at most 'max')
static std::string urlEncode(const std::string &url, const std::string encodeWhich="")
encode url (stem from http://bogomip.net/blog/cpp-url-encoding-and-decoding/)
static bool isDouble(const std::string &sData)
check if the given sData can be conveted to double
static MMVersion toVersion(const std::string &sData)
parse a (network) version string
static bool isBool(const std::string &sData)
check if the given value can be converted to bool
static std::string charToHex(unsigned char c)
char to hexadecimal
static std::string urlDecode(const std::string &encoded)
decode url (stem from http://bogomip.net/blog/cpp-url-encoding-and-decoding/)
static long long int toLong(const std::string &sData)
converts a string into the long value described by it by calling the char-type converter,...
static double toDoubleSecure(const std::string &sData, const double def)
converts a string into the integer value described by it
static std::string trim(const std::string s, const std::string &t=" \t\n")
remove leading and trailing whitespace
static std::string to_lower_case(const std::string &str)
Transfers the content to lower case.
static void resetTranscoder()
must be called when shutting down the xml subsystem
static XERCES_CPP_NAMESPACE::XMLLCPTranscoder * myLCPTranscoder
static std::string trim_right(const std::string s, const std::string &t=" \t\n")
remove trailing whitespace from string
static std::string trim_left(const std::string s, const std::string &t=" \t\n")
remove leading whitespace from string
static std::string escapeShell(const std::string &orig)
Escape special characters with backslash.
static std::string replace(std::string str, const std::string &what, const std::string &by)
Replaces all occurrences of the second string by the third string within the first string.
static int hexToInt(const std::string &sData)
converts a string with a hex value into the integer value described by it by calling the char-type co...
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
static std::string escapeXML(const std::string &orig, const bool maskDoubleHyphen=false)
Replaces the standard escapes by their XML entities.
static bool isHex(std::string sData)
check if the given string can be converted to hex
static std::string latin1_to_utf8(std::string str)
Transfers from Latin 1 (ISO-8859-1) to UTF-8.
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.
static std::string padFront(const std::string &str, int length, char padding)
static std::string convertUmlaute(std::string str)
Converts german "Umlaute" to their latin-version.
static double parseDist(const std::string &sData)
parse a distance, length or width value with a unit
static std::string adjustDecimalValue(double value, int precision)
write with maximum precision if needed but remove trailing zeros
static unsigned char hexToChar(const std::string &str)
hexadecimal to char
static bool startsWith(const std::string &str, const std::string prefix)
Checks whether a given string starts with the prefix.
static std::string wrapText(const std::string s, int width)
remove leading and trailing whitespace
static double parseSpeed(const std::string &sData, const bool defaultKmph=true)
parse a speed value with a unit
static std::string emptyString
An empty string.
static bool endsWith(const std::string &str, const std::string suffix)
Checks whether a given string ends with the suffix.
static std::string substituteEnvironment(const std::string &str, const std::chrono::time_point< std::chrono::system_clock > *const timeRef=nullptr)
Replaces an environment variable with its value (similar to bash); syntax for a variable is ${NAME}.
static bool isLong(const std::string &sData)
Check if the given sData can be converted to long.
static std::string transcode(const XMLCh *const data)
converts a 0-terminated XMLCh* array (usually UTF-16, stemming from Xerces) into std::string in UTF-8
static std::string transcodeToLocal(const std::string &utf8String)
convert a string from UTF-8 to the local codepage
static int toIntSecure(const std::string &sData, int def)
converts a string into the integer value described by it
static std::string isoTimeString(const std::chrono::time_point< std::chrono::system_clock > *const timeRef=nullptr)
Returns an ISO8601 formatted time string with microsecond precision.
static std::string transcodeFromLocal(const std::string &localString)
convert a string from the local codepage to UTF-8
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter,...
static bool isInt(const std::string &sData)
check if the given sData can be converted to int
static bool toBool(const std::string &sData)
converts a string into the bool value described by it by calling the char-type converter