SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
StringTokenizer.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // A java-style StringTokenizer for c++ (stl)
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 <vector>
35 #include <iostream> // !!! debug only
36 #include "UtilExceptions.h"
37 #include "StringTokenizer.h"
38 
39 #ifdef CHECK_MEMORY_LEAKS
40 #include <foreign/nvwa/debug_new.h>
41 #endif // CHECK_MEMORY_LEAKS
42 
43 
44 // ===========================================================================
45 // variable definitions
46 // ===========================================================================
47 const int StringTokenizer::NEWLINE = -256;
48 const int StringTokenizer::WHITECHARS = -257;
49 const int StringTokenizer::SPACE = 32;
50 
51 
52 // ===========================================================================
53 // method definitions
54 // ===========================================================================
56  : myTosplit(tosplit), myPos(0) {
57  prepareWhitechar(tosplit);
58 }
59 
60 
61 StringTokenizer::StringTokenizer(std::string tosplit, std::string token, bool splitAtAllChars)
62  : myTosplit(tosplit), myPos(0) {
63  prepare(tosplit, token, splitAtAllChars);
64 }
65 
66 
67 StringTokenizer::StringTokenizer(std::string tosplit, int special)
68  : myTosplit(tosplit), myPos(0) {
69  switch (special) {
70  case NEWLINE:
71  prepare(tosplit, "\r\n", true);
72  break;
73  case WHITECHARS:
74  prepareWhitechar(tosplit);
75  break;
76  default:
77  char* buf = new char[2];
78  buf[0] = (char) special;
79  buf[1] = 0;
80  prepare(tosplit, buf, false);
81  delete[] buf;
82  break;
83  }
84 }
85 
86 
88 
90  myPos = 0;
91 }
92 
94  return myPos != myStarts.size();
95 }
96 
97 std::string StringTokenizer::next() {
98  if (myPos >= myStarts.size()) {
99  throw OutOfBoundsException();
100  }
101  if (myLengths[myPos] == 0) {
102  myPos++;
103  return "";
104  }
105  size_t start = myStarts[myPos];
106  size_t length = myLengths[myPos++];
107  return myTosplit.substr(start, length);
108 }
109 
110 std::string StringTokenizer::front() {
111  if (myStarts.size() == 0) {
112  throw OutOfBoundsException();
113  }
114  if (myLengths[0] == 0) {
115  return "";
116  }
117  return myTosplit.substr(myStarts[0], myLengths[0]);
118 }
119 
120 std::string StringTokenizer::get(size_t pos) const {
121  if (pos >= myStarts.size()) {
122  throw OutOfBoundsException();
123  }
124  if (myLengths[pos] == 0) {
125  return "";
126  }
127  size_t start = myStarts[pos];
128  size_t length = myLengths[pos];
129  return myTosplit.substr(start, length);
130 }
131 
132 
133 size_t StringTokenizer::size() const {
134  return myStarts.size();
135 }
136 
137 void StringTokenizer::prepare(const std::string& tosplit, const std::string& token, bool splitAtAllChars) {
138  size_t beg = 0;
139  size_t len = token.length();
140  if (splitAtAllChars) {
141  len = 1;
142  }
143  while (beg < tosplit.length()) {
144  size_t end;
145  if (splitAtAllChars) {
146  end = tosplit.find_first_of(token, beg);
147  } else {
148  end = tosplit.find(token, beg);
149  }
150  if (end == std::string::npos) {
151  end = tosplit.length();
152  }
153  myStarts.push_back(beg);
154  myLengths.push_back(end - beg);
155  beg = end + len;
156  if (beg == tosplit.length()) {
157  myStarts.push_back(beg - 1);
158  myLengths.push_back(0);
159  }
160  }
161 }
162 
163 void StringTokenizer::prepareWhitechar(const std::string& tosplit) {
164  size_t len = tosplit.length();
165  size_t beg = 0;
166  while (beg < len && tosplit[beg] <= SPACE) {
167  beg++;
168  }
169  while (beg != std::string::npos && beg < len) {
170  size_t end = beg;
171  while (end < len && tosplit[end] > SPACE) {
172  end++;
173  }
174  myStarts.push_back(beg);
175  myLengths.push_back(end - beg);
176  beg = end;
177  while (beg < len && tosplit[beg] <= SPACE) {
178  beg++;
179  }
180  }
181 }
182 
183 std::vector<std::string>
185  std::vector<std::string> ret;
186  ret.reserve(size());
187  while (hasNext()) {
188  ret.push_back(next());
189  }
190  reinit();
191  return ret;
192 }
193 
194 
195 
196 /****************************************************************************/
197