41#define KM_PER_MILE 1.609344
55 const std::string::size_type endpos = str.find_last_not_of(
" \t\n\r");
56 if (std::string::npos != endpos) {
57 const int startpos = (int)str.find_first_not_of(
" \t\n\r");
58 return str.substr(startpos, endpos - startpos + 1);
66 const std::string::size_type endpos = str.find_last_not_of(
"0");
67 if (endpos != std::string::npos && str.back() ==
'0') {
68 std::string res = str.substr(0,
MAX2((
int)str.size() - max, (
int)endpos + 1));
77 std::transform(s.begin(), s.end(), s.begin(), [](
char c) {
78 return (char)::tolower(c);
87 std::transform(s.begin(), s.end(), s.begin(), [](
char c) {
88 return (char)::toupper(c);
98 for (
const auto& c : str) {
99 const unsigned char uc = (
unsigned char)c;
103 result += (char)(0xc2 + (uc > 0xbf));
104 result += (char)((uc & 0x3f) + 0x80);
113 str =
replace(str,
"\xE4",
"ae");
114 str =
replace(str,
"\xC4",
"Ae");
115 str =
replace(str,
"\xF6",
"oe");
116 str =
replace(str,
"\xD6",
"Oe");
117 str =
replace(str,
"\xFC",
"ue");
118 str =
replace(str,
"\xDC",
"Ue");
119 str =
replace(str,
"\xDF",
"ss");
120 str =
replace(str,
"\xC9",
"E");
121 str =
replace(str,
"\xE9",
"e");
122 str =
replace(str,
"\xC8",
"E");
123 str =
replace(str,
"\xE8",
"e");
130 std::string::size_type idx = str.find(what);
131 const int what_len = (int)what.length();
133 const int by_len = (int)by.length();
134 while (idx != std::string::npos) {
135 str = str.replace(idx, what_len, by);
136 idx = str.find(what, idx + by_len);
146 if (timeRef !=
nullptr) {
147 const std::string::size_type localTimeIndex = str.find(
"${LOCALTIME}");
148 const std::string::size_type utcIndex = str.find(
"${UTC}");
149 const bool isUTC = utcIndex != std::string::npos;
150 if (localTimeIndex != std::string::npos || isUTC) {
151 const time_t rawtime = std::chrono::system_clock::to_time_t(*timeRef);
153 struct tm* timeinfo = isUTC ? gmtime(&rawtime) : localtime(&rawtime);
154 strftime(buffer, 80,
"%Y-%m-%d-%H-%M-%S.", timeinfo);
155 auto seconds = std::chrono::time_point_cast<std::chrono::seconds>(*timeRef);
156 auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(*timeRef - seconds);
157 const std::string micro = buffer +
toString(microseconds.count());
159 s.replace(utcIndex, 6, micro);
161 s.replace(localTimeIndex, 12, micro);
165 const std::string::size_type pidIndex = str.find(
"${PID}");
166 if (pidIndex != std::string::npos) {
168 s.replace(pidIndex, 6,
toString(::GetCurrentProcessId()));
170 s.replace(pidIndex, 6,
toString(::getpid()));
173 if (std::getenv(
"SUMO_LOGO") ==
nullptr) {
174 s =
replace(s,
"${SUMO_LOGO}",
"${SUMO_HOME}/data/logo/sumo-128x138.png");
176 const std::string::size_type tildeIndex = str.find(
"~");
177 if (tildeIndex == 0) {
178 s.replace(0, 1,
"${HOME}");
180 s =
replace(s,
",~",
",${HOME}");
182 if (std::getenv(
"HOME") ==
nullptr) {
183 s =
replace(s,
"${HOME}",
"${USERPROFILE}");
191 std::regex envVarExpr(R
"(\$\{(.+?)\})");
195 std::string strIter = s;
198 while (std::regex_search(strIter, match, envVarExpr)) {
199 std::string varName = match[1];
202 std::string varValue;
203 if (std::getenv(varName.c_str()) !=
nullptr) {
204 varValue = std::getenv(varName.c_str());
208 s = std::regex_replace(s, std::regex(
"\\$\\{" + varName +
"\\}"), varValue);
211 strIter = match.suffix();
219 const std::chrono::system_clock::time_point now = timeRef ==
nullptr ? std::chrono::system_clock::now() : *timeRef;
220 const auto now_seconds = std::chrono::time_point_cast<std::chrono::seconds>(now);
221 const std::time_t now_c = std::chrono::system_clock::to_time_t(now);
222 const auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(now - now_seconds).count();
223 std::tm local_tm = *std::localtime(&now_c);
226 std::time_t utc_time = std::time(
nullptr);
227 std::tm utc_tm = *std::gmtime(&utc_time);
228 const double offset = std::difftime(std::mktime(&local_tm), std::mktime(&utc_tm)) / 3600.0;
229 const int hours_offset =
static_cast<int>(offset);
230 const int minutes_offset =
static_cast<int>((offset - hours_offset) * 60);
233 std::ostringstream oss;
235 std::strftime(buf,
sizeof(buf),
"%Y-%m-%dT%H:%M:%S", &local_tm);
237 << std::setw(6) << std::setfill(
'0') << std::abs(microseconds)
238 << (hours_offset >= 0 ?
"+" :
"-")
239 << std::setw(2) << std::setfill(
'0') << std::abs(hours_offset) <<
":"
240 << std::setw(2) << std::setfill(
'0') << std::abs(minutes_offset);
247 return str.compare(0, prefix.length(), prefix) == 0;
253 if (str.length() >= suffix.length()) {
254 return str.compare(str.length() - suffix.length(), suffix.length(), suffix) == 0;
263 return std::string(
MAX2(0, length - (
int)str.size()), padding) + str;
269 std::string result =
replace(orig,
"&",
"&");
270 result =
replace(result,
">",
">");
271 result =
replace(result,
"<",
"<");
272 result =
replace(result,
"\"",
""");
273 if (maskDoubleHyphen) {
274 result =
replace(result,
"--",
"--");
276 for (
char invalid =
'\1'; invalid <
' '; invalid++) {
277 result =
replace(result, std::string(1, invalid).c_str(),
"");
279 return replace(result,
"'",
"'");
285 return replace(orig,
"\"",
"\\\"");
291 const std::string chars{separator, quote};
292 if (orig.find_first_of(chars) == std::string::npos) {
295 const std::string quoteStr{quote};
296 return quoteStr +
replace(orig, quoteStr, {
'\\', quote}) + quoteStr;
302 std::ostringstream out;
304 for (
int i = 0; i < (int)toEncode.length(); ++i) {
305 const char t = toEncode.at(i);
307 if ((encodeWhich !=
"" && encodeWhich.find(t) == std::string::npos) ||
308 (encodeWhich ==
"" &&
309 ((t >= 45 && t <= 57) ||
310 (t >= 65 && t <= 90) ||
312 (t >= 97 && t <= 122) ||
315 out << toEncode.at(i);
327 std::ostringstream out;
329 for (
int i = 0; i < (int)toDecode.length(); ++i) {
330 if (toDecode.at(i) ==
'%') {
331 std::string str(toDecode.substr(i + 1, 2));
335 out << toDecode.at(i);
348 s <<
"%" << std::setw(2) << std::setfill(
'0') << std::hex << i;
358 std::istringstream in(str);
364 return static_cast<unsigned char>(c);
370 long long int result =
toLong(sData);
371 if (result > std::numeric_limits<int>::max() || result < std::numeric_limits<int>::min()) {
382 const long long int result =
toLong(sData);
384 return ((result <= std::numeric_limits<int>::max()) && (result >= std::numeric_limits<int>::min()));
392 if (sData.length() == 0) {
401 const char*
const data = sData.c_str();
402 if (data == 0 || data[0] == 0) {
408 long long int ret = _strtoi64(data, &end, 10);
410 long long int ret = strtoll(data, &end, 10);
412 if (errno == ERANGE) {
416 if ((
int)(end - data) != (
int)strlen(data)) {
425 const char*
const data = sData.c_str();
426 if (data == 0 || data[0] == 0) {
434 _strtoi64(data, &end, 10);
436 strtoll(data, &end, 10);
439 if (errno == ERANGE) {
443 if ((
int)(end - data) != (
int)strlen(data)) {
452 if (sData.length() == 0) {
458 if (sData[0] ==
'#') {
459 result = std::stoi(sData.substr(1), &idx, 16);
462 result = std::stoi(sData, &idx, 16);
467 if (idx != sData.length()) {
476 if (sData.length() == 0) {
480 if (sData[0] ==
'#') {
481 sData = sData.substr(1);
483 const char* sDataPtr = sData.c_str();
488 strtol(sDataPtr, &returnPtr, 16);
490 if (errno == ERANGE) {
494 if (sDataPtr == returnPtr) {
498 if (
static_cast<size_t>(returnPtr - sDataPtr) != sData.size()) {
507 if (sData.size() == 0) {
512 const double result = std::stod(sData, &idx);
513 if (idx != sData.size()) {
527 if (sData.size() == 0) {
530 const char* sDataPtr = sData.c_str();
535 strtod(sDataPtr, &returnPtr);
537 if (errno == ERANGE) {
541 if (sDataPtr == returnPtr) {
545 if (
static_cast<size_t>(returnPtr - sDataPtr) != sData.size()) {
554 if (sData.length() == 0) {
563 if (sData.find(
":") == std::string::npos) {
565 }
else if (
isTime(sData)) {
573 if (sData.length() == 0) {
577 if (s ==
"1" || s ==
"yes" || s ==
"true" || s ==
"on" || s ==
"x" || s ==
"t") {
580 if (s ==
"0" || s ==
"no" || s ==
"false" || s ==
"off" || s ==
"-" || s ==
"f") {
589 if (sData.length() == 0) {
594 if (s ==
"1" || s ==
"yes" || s ==
"true" || s ==
"on" || s ==
"x" || s ==
"t") {
598 if (s ==
"0" || s ==
"no" || s ==
"false" || s ==
"off" || s ==
"-" || s ==
"f") {
615 if (sData.size() == 0) {
620 const double result = std::stod(sData, &idx);
621 if (idx != sData.size()) {
622 const std::string unit =
prune(sData.substr(idx));
623 if (unit ==
"m" || unit ==
"metre" || unit ==
"meter" || unit ==
"metres" || unit ==
"meters") {
626 if (unit ==
"km" || unit ==
"kilometre" || unit ==
"kilometer" || unit ==
"kilometres" || unit ==
"kilometers") {
627 return result * 1000.;
629 if (unit ==
"mi" || unit ==
"mile" || unit ==
"miles") {
633 return result * 1852.;
635 if (unit ==
"ft" || unit ==
"foot" || unit ==
"feet") {
636 return result * 12. * 0.0254;
638 if (unit ==
"\"" || unit ==
"in" || unit ==
"inch" || unit ==
"inches") {
639 return result * 0.0254;
641 if (unit[0] ==
'\'') {
642 double inches = 12 * result;
643 if (unit.length() > 1) {
644 inches += std::stod(unit.substr(1), &idx);
645 if (unit.substr(idx) ==
"\"") {
646 return inches * 0.0254;
663 if (sData.size() == 0) {
668 const double result = std::stod(sData, &idx);
669 if (idx != sData.size()) {
671 if (unit ==
"km/h" || unit ==
"kph" || unit ==
"kmh" || unit ==
"kmph") {
680 if (unit ==
"knots" || unit ==
"knot" || unit ==
"kts" || unit ==
"kt") {
681 return result * 1.852 / 3.6;
685 return defaultKmph ? result / 3.6 : result;
697 std::string result = s;
698 result.erase(0, s.find_first_not_of(t));
704 std::string result = s;
705 result.erase(s.find_last_not_of(t) + 1);
720 bool firstLine =
true;
721 bool firstWord =
true;
722 for (std::string p : parts) {
723 if ((
int)(line.size() + p.size()) < width || firstWord) {
741 if (line.size() > 0) {
756 auto valueStr =
toString(value, precision);
758 while (valueStr.size() > 1) {
759 if (valueStr.back() ==
'0') {
761 }
else if (valueStr.back() ==
'.') {
774 return (value.size() > 0) && value.find_first_of(
" \t\n\r|\\'\";,<>&") == std::string::npos && value[0] !=
':';
780 return (value.size() > 0) && value.find_first_of(
" \t\n\r|\\'\";,<>&") == std::string::npos;
786 return (value.size() > 0) && value.find_first_of(
" \t\n\r|\\'\";,<>&") == std::string::npos;
792 return (value.size() > 0) && value.find_first_of(
" \t\n\r|\\'\";,<>&") == std::string::npos;
799 return (value.size() > 0) && value.find_first_of(
"\t\n\r|\\'\";,<>&") == std::string::npos;
805 return value.find_first_of(
"\t\n\r&|\\'\"<>") == std::string::npos;
811 return (value.find_first_of(
"\t\n\r@$%^&|{}*'\";<>") == std::string::npos);
818 if (typeIDs.empty()) {
822 for (
auto i : typeIDs) {
840 if (typeIDs.empty()) {
844 for (
const auto& typeID : typeIDs) {
870 std::string result(value);
871 if (result[0] ==
':') {
874 for (
const char c :
" \t\n\r|\\'\";,<>&") {
875 std::replace(result.begin(), result.end(), c,
'_');
887 const UINT fromCodepage = toLocal ? CP_UTF8 : CP_ACP;
888 const UINT toCodepage = toLocal ? CP_ACP : CP_UTF8;
889 const int wideSize = MultiByteToWideChar(fromCodepage, 0, str.c_str(), (
int)str.size(),
nullptr, 0);
893 std::wstring wide(wideSize,
L'\0');
894 MultiByteToWideChar(fromCodepage, 0, str.c_str(), (
int)str.size(), &wide[0], wideSize);
895 const int resultSize = WideCharToMultiByte(toCodepage, 0, wide.c_str(), wideSize,
nullptr, 0,
nullptr,
nullptr);
896 if (resultSize <= 0) {
899 std::string result(resultSize,
'\0');
900 WideCharToMultiByte(toCodepage, 0, wide.c_str(), wideSize, &result[0], resultSize,
nullptr,
nullptr);
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
bool isTime(const std::string &r)
check if the given string is a valid time
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 transcodeLocal(const std::string &str, bool toLocal)
convert a string between UTF-8 and the local codepage (identity on non-Windows systems)
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 makeValidID(const std::string &value)
transform the given string into a valid id for a network element (or any other id)
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 bool isValidParameterKey(const std::string &value)
whether the given string is a valid key for a parameter
static std::string trim_right(const std::string s, const std::string &t=" \t\n")
remove trailing whitespace from string
static bool isValidNetID(const std::string &value)
whether the given string is a valid id for a network element
static std::string trim_left(const std::string s, const std::string &t=" \t\n")
remove leading whitespace from string
static bool isValidListOfTypeID(const std::string &value)
whether the given string is a valid list of ids for an edge or vehicle type (empty aren't allowed)
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 bool isValidVehicleID(const std::string &value)
whether the given string is a valid id for a vehicle or flow
static std::string escapeXML(const std::string &orig, const bool maskDoubleHyphen=false)
Replaces the standard escapes by their XML entities.
static bool isValidDetectorID(const std::string &value)
whether the given string is a valid id for an detector
static bool isHex(std::string sData)
check if the given string can be converted to hex
static bool isValidAdditionalID(const std::string &value)
whether the given string is a valid id for an additional object
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 bool isValidFilename(const std::string &value)
whether the given string is a valid attribute for a filename (for example, a name)
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 double toDoubleAllowTime(const std::string &sData)
converts a string into a double value. Values in human-readable-time format (see string2time) are per...
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 std::string escapeCSV(const std::string &orig, const char separator, const char quote='"')
Adds quotes to strings containing the separator or the quote char and escapes the quote char with a b...
static bool isValidListOfNetIDs(const std::string &value)
whether the given string is a valid list of id for a network (empty aren't allowed)
static bool isValidAttribute(const std::string &value)
whether the given string is a valid attribute for a certain key (for example, a name)
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 isValidTypeID(const std::string &value)
whether the given string is a valid id for an edge or vehicle type
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
#define UNUSED_PARAMETER(x)