37#include <xercesc/util/TransService.hpp>
38#include <xercesc/util/TranscodingException.hpp>
56 const std::string::size_type endpos = str.find_last_not_of(
" \t\n\r");
57 if (std::string::npos != endpos) {
58 const int startpos = (int)str.find_first_not_of(
" \t\n\r");
59 return str.substr(startpos, endpos - startpos + 1);
67 const std::string::size_type endpos = str.find_last_not_of(
"0");
68 if (endpos != std::string::npos && str.back() ==
'0') {
69 std::string res = str.substr(0,
MAX2((
int)str.size() - max, (
int)endpos + 1));
78 std::transform(s.begin(), s.end(), s.begin(), [](
char c) {
79 return (char)::tolower(c);
89 for (
int i = 0; i < (int)str.length(); i++) {
90 const unsigned char c = str[i];
94 result += (char)(0xc2 + (c > 0xbf));
95 result += (char)((c & 0x3f) + 0x80);
104 str =
replace(str,
"\xE4",
"ae");
105 str =
replace(str,
"\xC4",
"Ae");
106 str =
replace(str,
"\xF6",
"oe");
107 str =
replace(str,
"\xD6",
"Oe");
108 str =
replace(str,
"\xFC",
"ue");
109 str =
replace(str,
"\xDC",
"Ue");
110 str =
replace(str,
"\xDF",
"ss");
111 str =
replace(str,
"\xC9",
"E");
112 str =
replace(str,
"\xE9",
"e");
113 str =
replace(str,
"\xC8",
"E");
114 str =
replace(str,
"\xE8",
"e");
121 std::string::size_type idx = str.find(what);
122 const int what_len = (int)what.length();
124 const int by_len = (int)by.length();
125 while (idx != std::string::npos) {
126 str = str.replace(idx, what_len, by);
127 idx = str.find(what, idx + by_len);
137 if (timeRef !=
nullptr) {
138 const std::string::size_type localTimeIndex = str.find(
"${LOCALTIME}");
139 const std::string::size_type utcIndex = str.find(
"${UTC}");
140 const bool isUTC = utcIndex != std::string::npos;
141 if (localTimeIndex != std::string::npos || isUTC) {
142 const time_t rawtime = std::chrono::system_clock::to_time_t(*timeRef);
144 struct tm* timeinfo = isUTC ? gmtime(&rawtime) : localtime(&rawtime);
145 strftime(buffer, 80,
"%Y-%m-%d-%H-%M-%S.", timeinfo);
146 auto seconds = std::chrono::time_point_cast<std::chrono::seconds>(*timeRef);
147 auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(*timeRef - seconds);
148 const std::string micro = buffer +
toString(microseconds.count());
150 s.replace(utcIndex, 6, micro);
152 s.replace(localTimeIndex, 12, micro);
156 const std::string::size_type pidIndex = str.find(
"${PID}");
157 if (pidIndex != std::string::npos) {
159 s.replace(pidIndex, 6,
toString(::GetCurrentProcessId()));
161 s.replace(pidIndex, 6,
toString(::getpid()));
164 if (std::getenv(
"SUMO_LOGO") ==
nullptr) {
165 s =
replace(s,
"${SUMO_LOGO}",
"${SUMO_HOME}/data/logo/sumo-128x138.png");
167 const std::string::size_type tildeIndex = str.find(
"~");
168 if (tildeIndex == 0) {
169 s.replace(0, 1,
"${HOME}");
171 s =
replace(s,
",~",
",${HOME}");
173 if (std::getenv(
"HOME") ==
nullptr) {
174 s =
replace(s,
"${HOME}",
"${USERPROFILE}");
182 std::regex envVarExpr(R
"(\$\{(.+?)\})");
186 std::string strIter = s;
189 while (std::regex_search(strIter, match, envVarExpr)) {
190 std::string varName = match[1];
193 std::string varValue;
194 if (std::getenv(varName.c_str()) !=
nullptr) {
195 varValue = std::getenv(varName.c_str());
199 s = std::regex_replace(s, std::regex(
"\\$\\{" + varName +
"\\}"), varValue);
202 strIter = match.suffix();
210 return str.compare(0, prefix.length(), prefix) == 0;
216 if (str.length() >= suffix.length()) {
217 return str.compare(str.length() - suffix.length(), suffix.length(), suffix) == 0;
226 return std::string(
MAX2(0, length - (
int)str.size()), padding) + str;
232 std::string result =
replace(orig,
"&",
"&");
233 result =
replace(result,
">",
">");
234 result =
replace(result,
"<",
"<");
235 result =
replace(result,
"\"",
""");
236 if (maskDoubleHyphen) {
237 result =
replace(result,
"--",
"--");
239 for (
char invalid =
'\1'; invalid <
' '; invalid++) {
240 result =
replace(result, std::string(1, invalid).c_str(),
"");
242 return replace(result,
"'",
"'");
248 std::ostringstream out;
250 for (
int i = 0; i < (int)toEncode.length(); ++i) {
251 const char t = toEncode.at(i);
253 if ((encodeWhich !=
"" && encodeWhich.find(t) == std::string::npos) ||
254 (encodeWhich ==
"" &&
255 ((t >= 45 && t <= 57) ||
256 (t >= 65 && t <= 90) ||
258 (t >= 97 && t <= 122) ||
261 out << toEncode.at(i);
273 std::ostringstream out;
275 for (
int i = 0; i < (int)toDecode.length(); ++i) {
276 if (toDecode.at(i) ==
'%') {
277 std::string str(toDecode.substr(i + 1, 2));
281 out << toDecode.at(i);
294 s <<
"%" << std::setw(2) << std::setfill(
'0') << std::hex << i;
304 std::istringstream in(str);
310 return static_cast<unsigned char>(c);
316 long long int result =
toLong(sData);
317 if (result > std::numeric_limits<int>::max() || result < std::numeric_limits<int>::min()) {
326 if (sData.length() == 0) {
335 const char*
const data = sData.c_str();
336 if (data == 0 || data[0] == 0) {
342 long long int ret = _strtoi64(data, &end, 10);
344 long long int ret = strtoll(data, &end, 10);
346 if (errno == ERANGE) {
350 if ((
int)(end - data) != (
int)strlen(data)) {
359 if (sData.length() == 0) {
365 if (sData[0] ==
'#') {
366 result = std::stoi(sData.substr(1), &idx, 16);
369 result = std::stoi(sData, &idx, 16);
374 if (idx != sData.length()) {
383 if (sData.size() == 0) {
388 const double result = std::stod(sData, &idx);
389 if (idx != sData.size()) {
403 if (sData.length() == 0) {
412 if (sData.length() == 0) {
416 if (s ==
"1" || s ==
"yes" || s ==
"true" || s ==
"on" || s ==
"x" || s ==
"t") {
419 if (s ==
"0" || s ==
"no" || s ==
"false" || s ==
"off" || s ==
"-" || s ==
"f") {
434#if _XERCES_VERSION < 30100
435 char* t = XERCES_CPP_NAMESPACE::XMLString::transcode(data);
436 std::string result(t);
437 XERCES_CPP_NAMESPACE::XMLString::release(&t);
441 XERCES_CPP_NAMESPACE::TranscodeToStr utf8(data,
"UTF-8");
442 return reinterpret_cast<const char*
>(utf8.str());
443 }
catch (XERCES_CPP_NAMESPACE::TranscodingException&) {
452#if _XERCES_VERSION > 30100
455 myLCPTranscoder = XERCES_CPP_NAMESPACE::XMLPlatformUtils::fgTransService->makeNewLCPTranscoder(XERCES_CPP_NAMESPACE::XMLPlatformUtils::fgMemoryManager);
460 }
catch (XERCES_CPP_NAMESPACE::TranscodingException&) {}
468#if _XERCES_VERSION > 30100
471 myLCPTranscoder = XERCES_CPP_NAMESPACE::XMLPlatformUtils::fgTransService->makeNewLCPTranscoder(XERCES_CPP_NAMESPACE::XMLPlatformUtils::fgMemoryManager);
474 XERCES_CPP_NAMESPACE::TranscodeFromStr utf8(
reinterpret_cast<const XMLByte*
>(utf8String.c_str()), utf8String.size(),
"UTF-8");
477 }
catch (XERCES_CPP_NAMESPACE::TranscodingException&) {}
485 std::string result = s;
486 result.erase(0, s.find_first_not_of(t));
492 std::string result = s;
493 result.erase(s.find_last_not_of(t) + 1);
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
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="")
static std::string charToHex(unsigned char c)
static std::string urlDecode(const std::string &encoded)
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 replace(std::string str, const std::string &what, const std::string &by)
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 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 unsigned char hexToChar(const std::string &str)
static bool startsWith(const std::string &str, const std::string prefix)
Checks whether a given string starts with the prefix.
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)
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 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 toBool(const std::string &sData)
converts a string into the bool value described by it by calling the char-type converter