|
SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00007 // Some static methods for string processing 00008 /****************************************************************************/ 00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00010 // Copyright (C) 2001-2011 DLR (http://www.dlr.de/) and contributors 00011 /****************************************************************************/ 00012 // 00013 // This program is free software; you can redistribute it and/or modify 00014 // it under the terms of the GNU General Public License as published by 00015 // the Free Software Foundation; either version 2 of the License, or 00016 // (at your option) any later version. 00017 // 00018 /****************************************************************************/ 00019 00020 00021 // =========================================================================== 00022 // included modules 00023 // =========================================================================== 00024 #ifdef _MSC_VER 00025 #include <windows_config.h> 00026 #else 00027 #include <config.h> 00028 #endif 00029 00030 #include <string> 00031 #include <iostream> 00032 #include <cstdio> 00033 #include <utils/common/UtilExceptions.h> 00034 #include <utils/common/TplConvert.h> 00035 #include <utils/common/ToString.h> 00036 #include "StringUtils.h" 00037 00038 #ifdef CHECK_MEMORY_LEAKS 00039 #include <foreign/nvwa/debug_new.h> 00040 #endif // CHECK_MEMORY_LEAKS 00041 00042 00043 // =========================================================================== 00044 // static member definitions 00045 // =========================================================================== 00046 std::string StringUtils::emptyString; 00047 00048 00049 // =========================================================================== 00050 // method definitions 00051 // =========================================================================== 00052 std::string 00053 StringUtils::prune(std::string str) { 00054 const size_t endpos = str.find_last_not_of(" \t\n\r"); 00055 if (std::string::npos != endpos) { 00056 const size_t startpos = str.find_first_not_of(" \t\n\r"); 00057 return str.substr(startpos, endpos - startpos + 1); 00058 } 00059 return ""; 00060 } 00061 00062 00063 std::string 00064 StringUtils::to_lower_case(std::string str) { 00065 for (size_t i=0; i<str.length(); i++) { 00066 if (str[i]>='A'&&str[i]<='Z') { 00067 str[i] = str[i] + 'a' - 'A'; 00068 } 00069 } 00070 return str; 00071 } 00072 00073 00074 std::string 00075 StringUtils::convertUmlaute(std::string str) { 00076 str = replace(str, "ä", "ae"); 00077 str = replace(str, "Ä", "Ae"); 00078 str = replace(str, "ö", "oe"); 00079 str = replace(str, "Ö", "Oe"); 00080 str = replace(str, "ü", "ue"); 00081 str = replace(str, "Ü", "Ue"); 00082 str = replace(str, "ß", "ss"); 00083 str = replace(str, "É", "E"); 00084 str = replace(str, "é", "e"); 00085 str = replace(str, "È", "E"); 00086 str = replace(str, "è", "e"); 00087 return str; 00088 } 00089 00090 00091 00092 std::string 00093 StringUtils::replace(std::string str, const char* what, 00094 const char* by) { 00095 const std::string what_tmp(what); 00096 const std::string by_tmp(by); 00097 size_t idx = str.find(what); 00098 const size_t what_len = what_tmp.length(); 00099 if (what_len > 0) { 00100 const size_t by_len = by_tmp.length(); 00101 while (idx != std::string::npos) { 00102 str = str.replace(idx, what_len, by); 00103 idx = str.find(what, idx + by_len); 00104 } 00105 } 00106 return str; 00107 } 00108 00109 00110 std::string 00111 StringUtils::toTimeString(int time) { 00112 std::ostringstream oss; 00113 if (time < 0) { 00114 oss << "-"; 00115 time = -time; 00116 } 00117 char buffer[10]; 00118 sprintf(buffer, "%02i:",(time/3600)); 00119 oss << buffer; 00120 time=time%3600; 00121 sprintf(buffer, "%02i:",(time/60)); 00122 oss << buffer; 00123 time=time%60; 00124 sprintf(buffer, "%02i", time); 00125 oss << buffer; 00126 return oss.str(); 00127 } 00128 00129 00130 std::string 00131 StringUtils::escapeXML(const std::string& orig) throw() { 00132 std::string result = replace(orig, "&", "&"); 00133 result = replace(result, ">", ">"); 00134 result = replace(result, "<", "<"); 00135 result = replace(result, "\"", """); 00136 for (char invalid = '\1'; invalid < ' '; invalid++) { 00137 result = replace(result, std::string(1, invalid).c_str(), ""); 00138 } 00139 return replace(result, "'", "'"); 00140 } 00141 00142 00143 00144 /****************************************************************************/ 00145