SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
StringUtils.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Some static methods for string processing
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
12 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <string>
34 #include <iostream>
35 #include <cstdio>
38 #include <utils/common/ToString.h>
39 #include "StringUtils.h"
40 
41 #ifdef CHECK_MEMORY_LEAKS
42 #include <foreign/nvwa/debug_new.h>
43 #endif // CHECK_MEMORY_LEAKS
44 
45 
46 // ===========================================================================
47 // static member definitions
48 // ===========================================================================
49 std::string StringUtils::emptyString;
50 
51 
52 // ===========================================================================
53 // method definitions
54 // ===========================================================================
55 std::string
56 StringUtils::prune(std::string str) {
57  const size_t endpos = str.find_last_not_of(" \t\n\r");
58  if (std::string::npos != endpos) {
59  const size_t startpos = str.find_first_not_of(" \t\n\r");
60  return str.substr(startpos, endpos - startpos + 1);
61  }
62  return "";
63 }
64 
65 
66 std::string
67 StringUtils::to_lower_case(std::string str) {
68  for (size_t i = 0; i < str.length(); i++) {
69  if (str[i] >= 'A' && str[i] <= 'Z') {
70  str[i] = str[i] + 'a' - 'A';
71  }
72  }
73  return str;
74 }
75 
76 
77 std::string
78 StringUtils::convertUmlaute(std::string str) {
79  str = replace(str, "ä", "ae");
80  str = replace(str, "Ä", "Ae");
81  str = replace(str, "ö", "oe");
82  str = replace(str, "Ö", "Oe");
83  str = replace(str, "ü", "ue");
84  str = replace(str, "Ü", "Ue");
85  str = replace(str, "ß", "ss");
86  str = replace(str, "É", "E");
87  str = replace(str, "é", "e");
88  str = replace(str, "È", "E");
89  str = replace(str, "è", "e");
90  return str;
91 }
92 
93 
94 
95 std::string
96 StringUtils::replace(std::string str, const char* what,
97  const char* by) {
98  const std::string what_tmp(what);
99  const std::string by_tmp(by);
100  size_t idx = str.find(what);
101  const size_t what_len = what_tmp.length();
102  if (what_len > 0) {
103  const size_t by_len = by_tmp.length();
104  while (idx != std::string::npos) {
105  str = str.replace(idx, what_len, by);
106  idx = str.find(what, idx + by_len);
107  }
108  }
109  return str;
110 }
111 
112 
113 std::string
115  std::ostringstream oss;
116  if (time < 0) {
117  oss << "-";
118  time = -time;
119  }
120  char buffer[10];
121  sprintf(buffer, "%02i:", (time / 3600));
122  oss << buffer;
123  time = time % 3600;
124  sprintf(buffer, "%02i:", (time / 60));
125  oss << buffer;
126  time = time % 60;
127  sprintf(buffer, "%02i", time);
128  oss << buffer;
129  return oss.str();
130 }
131 
132 
133 std::string
134 StringUtils::escapeXML(const std::string& orig) {
135  std::string result = replace(orig, "&", "&amp;");
136  result = replace(result, ">", "&gt;");
137  result = replace(result, "<", "&lt;");
138  result = replace(result, "\"", "&quot;");
139  for (char invalid = '\1'; invalid < ' '; invalid++) {
140  result = replace(result, std::string(1, invalid).c_str(), "");
141  }
142  return replace(result, "'", "&apos;");
143 }
144 
145 
146 
147 /****************************************************************************/
148