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-sim.org/
12 // Copyright (C) 2001-2014 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(const 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::latin1_to_utf8(std::string str) {
79  // inspired by http://stackoverflow.com/questions/4059775/convert-iso-8859-1-strings-to-utf-8-in-c-c
80  std::string result;
81  for (size_t i = 0; i < str.length(); i++) {
82  const unsigned char c = str[i];
83  if (c < 128) {
84  result += c;
85  } else {
86  result += (char)(0xc2 + (c > 0xbf));
87  result += (char)((c & 0x3f) + 0x80);
88  }
89  }
90  return result;
91 }
92 
93 
94 std::string
95 StringUtils::convertUmlaute(std::string str) {
96  str = replace(str, "\xE4", "ae");
97  str = replace(str, "\xC4", "Ae");
98  str = replace(str, "\xF6", "oe");
99  str = replace(str, "\xD6", "Oe");
100  str = replace(str, "\xFC", "ue");
101  str = replace(str, "\xDC", "Ue");
102  str = replace(str, "\xDF", "ss");
103  str = replace(str, "\xC9", "E");
104  str = replace(str, "\xE9", "e");
105  str = replace(str, "\xC8", "E");
106  str = replace(str, "\xE8", "e");
107  return str;
108 }
109 
110 
111 
112 std::string
113 StringUtils::replace(std::string str, const char* what,
114  const char* by) {
115  const std::string what_tmp(what);
116  const std::string by_tmp(by);
117  size_t idx = str.find(what);
118  const size_t what_len = what_tmp.length();
119  if (what_len > 0) {
120  const size_t by_len = by_tmp.length();
121  while (idx != std::string::npos) {
122  str = str.replace(idx, what_len, by);
123  idx = str.find(what, idx + by_len);
124  }
125  }
126  return str;
127 }
128 
129 
130 std::string
132  std::ostringstream oss;
133  if (time < 0) {
134  oss << "-";
135  time = -time;
136  }
137  char buffer[10];
138  sprintf(buffer, "%02i:", (time / 3600));
139  oss << buffer;
140  time = time % 3600;
141  sprintf(buffer, "%02i:", (time / 60));
142  oss << buffer;
143  time = time % 60;
144  sprintf(buffer, "%02i", time);
145  oss << buffer;
146  return oss.str();
147 }
148 
149 
150 std::string
151 StringUtils::escapeXML(const std::string& orig) {
152  std::string result = replace(orig, "&", "&amp;");
153  result = replace(result, ">", "&gt;");
154  result = replace(result, "<", "&lt;");
155  result = replace(result, "\"", "&quot;");
156  for (char invalid = '\1'; invalid < ' '; invalid++) {
157  result = replace(result, std::string(1, invalid).c_str(), "");
158  }
159  return replace(result, "'", "&apos;");
160 }
161 
162 
163 
164 /****************************************************************************/
165