43#define KM_PER_MILE 1.609344
57 const std::string::size_type endpos = str.find_last_not_of(
" \t\n\r");
58 if (std::string::npos != endpos) {
59 const int startpos = (int)str.find_first_not_of(
" \t\n\r");
60 return str.substr(startpos, endpos - startpos + 1);
68 const std::string::size_type endpos = str.find_last_not_of(
"0");
69 if (endpos != std::string::npos && str.back() ==
'0') {
70 std::string res = str.substr(0,
MAX2((
int)str.size() - max, (
int)endpos + 1));
79 std::transform(s.begin(), s.end(), s.begin(), [](
char c) {
80 return (char)::tolower(c);
89 std::transform(s.begin(), s.end(), s.begin(), [](
char c) {
90 return (char)::toupper(c);
100 for (
const auto& c : str) {
101 const unsigned char uc = (
unsigned char)c;
105 result += (char)(0xc2 + (uc > 0xbf));
106 result += (char)((uc & 0x3f) + 0x80);
115 str =
replace(str,
"\xE4",
"ae");
116 str =
replace(str,
"\xC4",
"Ae");
117 str =
replace(str,
"\xF6",
"oe");
118 str =
replace(str,
"\xD6",
"Oe");
119 str =
replace(str,
"\xFC",
"ue");
120 str =
replace(str,
"\xDC",
"Ue");
121 str =
replace(str,
"\xDF",
"ss");
122 str =
replace(str,
"\xC9",
"E");
123 str =
replace(str,
"\xE9",
"e");
124 str =
replace(str,
"\xC8",
"E");
125 str =
replace(str,
"\xE8",
"e");
132 std::string::size_type idx = str.find(what);
133 const int what_len = (int)what.length();
135 const int by_len = (int)by.length();
136 while (idx != std::string::npos) {
137 str = str.replace(idx, what_len, by);
138 idx = str.find(what, idx + by_len);
148 if (timeRef !=
nullptr) {
149 const std::string::size_type localTimeIndex = str.find(
"${LOCALTIME}");
150 const std::string::size_type utcIndex = str.find(
"${UTC}");
151 const bool isUTC = utcIndex != std::string::npos;
152 if (localTimeIndex != std::string::npos || isUTC) {
153 const time_t rawtime = std::chrono::system_clock::to_time_t(*timeRef);
155 struct tm* timeinfo = isUTC ? gmtime(&rawtime) : localtime(&rawtime);
156 strftime(buffer, 80,
"%Y-%m-%d-%H-%M-%S.", timeinfo);
157 auto seconds = std::chrono::time_point_cast<std::chrono::seconds>(*timeRef);
158 auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(*timeRef - seconds);
159 const std::string micro = buffer +
toString(microseconds.count());
161 s.replace(utcIndex, 6, micro);
163 s.replace(localTimeIndex, 12, micro);
167 const std::string::size_type pidIndex = str.find(
"${PID}");
168 if (pidIndex != std::string::npos) {
170 s.replace(pidIndex, 6,
toString(::GetCurrentProcessId()));
172 s.replace(pidIndex, 6,
toString(::getpid()));
175 if (std::getenv(
"SUMO_LOGO") ==
nullptr) {
176 s =
replace(s,
"${SUMO_LOGO}",
"${SUMO_HOME}/data/logo/sumo-128x138.png");
178 const std::string::size_type tildeIndex = str.find(
"~");
179 if (tildeIndex == 0) {
180 s.replace(0, 1,
"${HOME}");
182 s =
replace(s,
",~",
",${HOME}");
184 if (std::getenv(
"HOME") ==
nullptr) {
185 s =
replace(s,
"${HOME}",
"${USERPROFILE}");
193 std::regex envVarExpr(R
"(\$\{(.+?)\})");
197 std::string strIter = s;
200 while (std::regex_search(strIter, match, envVarExpr)) {
201 std::string varName = match[1];
204 std::string varValue;
205 if (std::getenv(varName.c_str()) !=
nullptr) {
206 varValue = std::getenv(varName.c_str());
210 s = std::regex_replace(s, std::regex(
"\\$\\{" + varName +
"\\}"), varValue);
213 strIter = match.suffix();
221 std::chrono::system_clock::time_point now;
222 if (timeRef !=
nullptr) {
225 now = std::chrono::system_clock::now();
232 const char*
const sourceDateEpoch = std::getenv(
"SOURCE_DATE_EPOCH");
233 if (sourceDateEpoch !=
nullptr) {
234 now = std::chrono::system_clock::from_time_t(
static_cast<std::time_t
>(std::strtoll(sourceDateEpoch,
nullptr, 10)));
237 const auto now_seconds = std::chrono::time_point_cast<std::chrono::seconds>(now);
238 const std::time_t now_c = std::chrono::system_clock::to_time_t(now);
239 const auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(now - now_seconds).count();
240 std::tm local_tm = *std::localtime(&now_c);
246 std::tm utc_tm = *std::gmtime(&now_c);
247 const double offset = std::difftime(std::mktime(&local_tm), std::mktime(&utc_tm)) / 3600.0;
248 const int hours_offset =
static_cast<int>(offset);
249 const int minutes_offset =
static_cast<int>((offset - hours_offset) * 60);
252 std::ostringstream oss;
254 std::strftime(buf,
sizeof(buf),
"%Y-%m-%dT%H:%M:%S", &local_tm);
256 << std::setw(6) << std::setfill(
'0') << std::abs(microseconds)
257 << (hours_offset >= 0 ?
"+" :
"-")
258 << std::setw(2) << std::setfill(
'0') << std::abs(hours_offset) <<
":"
259 << std::setw(2) << std::setfill(
'0') << std::abs(minutes_offset);
266 return str.compare(0, prefix.length(), prefix) == 0;
272 if (str.length() >= suffix.length()) {
273 return str.compare(str.length() - suffix.length(), suffix.length(), suffix) == 0;
282 return std::string(
MAX2(0, length - (
int)str.size()), padding) + str;
288 std::string result =
replace(orig,
"&",
"&");
289 result =
replace(result,
">",
">");
290 result =
replace(result,
"<",
"<");
291 result =
replace(result,
"\"",
""");
292 if (maskDoubleHyphen) {
293 result =
replace(result,
"--",
"--");
295 for (
char invalid =
'\1'; invalid <
' '; invalid++) {
296 result =
replace(result, std::string(1, invalid).c_str(),
"");
298 return replace(result,
"'",
"'");
304 std::string result =
replace(orig,
"\"",
"\\\"");
311 std::ostringstream out;
313 for (
int i = 0; i < (int)toEncode.length(); ++i) {
314 const char t = toEncode.at(i);
316 if ((encodeWhich !=
"" && encodeWhich.find(t) == std::string::npos) ||
317 (encodeWhich ==
"" &&
318 ((t >= 45 && t <= 57) ||
319 (t >= 65 && t <= 90) ||
321 (t >= 97 && t <= 122) ||
324 out << toEncode.at(i);
336 std::ostringstream out;
338 for (
int i = 0; i < (int)toDecode.length(); ++i) {
339 if (toDecode.at(i) ==
'%') {
340 std::string str(toDecode.substr(i + 1, 2));
344 out << toDecode.at(i);
357 s <<
"%" << std::setw(2) << std::setfill(
'0') << std::hex << i;
367 std::istringstream in(str);
373 return static_cast<unsigned char>(c);
379 long long int result =
toLong(sData);
380 if (result > std::numeric_limits<int>::max() || result < std::numeric_limits<int>::min()) {
391 const long long int result =
toLong(sData);
393 return ((result <= std::numeric_limits<int>::max()) && (result >= std::numeric_limits<int>::min()));
401 if (sData.length() == 0) {
410 const char*
const data = sData.c_str();
411 if (data == 0 || data[0] == 0) {
417 long long int ret = _strtoi64(data, &end, 10);
419 long long int ret = strtoll(data, &end, 10);
421 if (errno == ERANGE) {
425 if ((
int)(end - data) != (
int)strlen(data)) {
434 const char*
const data = sData.c_str();
435 if (data == 0 || data[0] == 0) {
443 _strtoi64(data, &end, 10);
445 strtoll(data, &end, 10);
448 if (errno == ERANGE) {
452 if ((
int)(end - data) != (
int)strlen(data)) {
461 if (sData.length() == 0) {
467 if (sData[0] ==
'#') {
468 result = std::stoi(sData.substr(1), &idx, 16);
471 result = std::stoi(sData, &idx, 16);
476 if (idx != sData.length()) {
485 if (sData.length() == 0) {
489 if (sData[0] ==
'#') {
490 sData = sData.substr(1);
492 const char* sDataPtr = sData.c_str();
497 strtol(sDataPtr, &returnPtr, 16);
499 if (errno == ERANGE) {
503 if (sDataPtr == returnPtr) {
507 if (
static_cast<size_t>(returnPtr - sDataPtr) != sData.size()) {
516 if (sData.size() == 0) {
521 const double result = std::stod(sData, &idx);
522 if (idx != sData.size()) {
536 if (sData.size() == 0) {
539 const char* sDataPtr = sData.c_str();
544 strtod(sDataPtr, &returnPtr);
546 if (errno == ERANGE) {
550 if (sDataPtr == returnPtr) {
554 if (
static_cast<size_t>(returnPtr - sDataPtr) != sData.size()) {
563 if (sData.length() == 0) {
572 if (sData.length() == 0) {
576 if (s ==
"1" || s ==
"yes" || s ==
"true" || s ==
"on" || s ==
"x" || s ==
"t") {
579 if (s ==
"0" || s ==
"no" || s ==
"false" || s ==
"off" || s ==
"-" || s ==
"f") {
588 if (sData.length() == 0) {
593 if (s ==
"1" || s ==
"yes" || s ==
"true" || s ==
"on" || s ==
"x" || s ==
"t") {
597 if (s ==
"0" || s ==
"no" || s ==
"false" || s ==
"off" || s ==
"-" || s ==
"f") {
614 if (sData.size() == 0) {
619 const double result = std::stod(sData, &idx);
620 if (idx != sData.size()) {
621 const std::string unit =
prune(sData.substr(idx));
622 if (unit ==
"m" || unit ==
"metre" || unit ==
"meter" || unit ==
"metres" || unit ==
"meters") {
625 if (unit ==
"km" || unit ==
"kilometre" || unit ==
"kilometer" || unit ==
"kilometres" || unit ==
"kilometers") {
626 return result * 1000.;
628 if (unit ==
"mi" || unit ==
"mile" || unit ==
"miles") {
632 return result * 1852.;
634 if (unit ==
"ft" || unit ==
"foot" || unit ==
"feet") {
635 return result * 12. * 0.0254;
637 if (unit ==
"\"" || unit ==
"in" || unit ==
"inch" || unit ==
"inches") {
638 return result * 0.0254;
640 if (unit[0] ==
'\'') {
641 double inches = 12 * result;
642 if (unit.length() > 1) {
643 inches += std::stod(unit.substr(1), &idx);
644 if (unit.substr(idx) ==
"\"") {
645 return inches * 0.0254;
662 if (sData.size() == 0) {
667 const double result = std::stod(sData, &idx);
668 if (idx != sData.size()) {
669 const std::string unit =
prune(sData.substr(idx));
670 if (unit ==
"km/h" || unit ==
"kph" || unit ==
"kmh" || unit ==
"kmph") {
679 if (unit ==
"knots") {
680 return result * 1.852 / 3.6;
684 return defaultKmph ? result / 3.6 : result;
696 std::string result = s;
697 result.erase(0, s.find_first_not_of(t));
703 std::string result = s;
704 result.erase(s.find_last_not_of(t) + 1);
719 bool firstLine =
true;
720 bool firstWord =
true;
721 for (std::string p : parts) {
722 if ((
int)(line.size() + p.size()) < width || firstWord) {
740 if (line.size() > 0) {
757 while (valueStr.size() > 1) {
758 if (valueStr.back() ==
'0') {
760 }
else if (valueStr.back() ==
'.') {
const std::string invalid_return< std::string >::value
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 to_upper_case(const std::string &str)
Transfers the content to upper case.
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 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 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 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