SUMO - Simulation of Urban MObility
StringTokenizer.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // A java-style StringTokenizer for c++ (stl)
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <string>
33 #include <vector>
34 #include <iostream> // !!! debug only
35 #include "UtilExceptions.h"
36 #include "StringTokenizer.h"
37 
38 
39 // ===========================================================================
40 // variable definitions
41 // ===========================================================================
42 const int StringTokenizer::NEWLINE = -256;
43 const int StringTokenizer::WHITECHARS = -257;
44 const int StringTokenizer::SPACE = 32;
45 const int StringTokenizer::TAB = 9;
46 
47 
48 // ===========================================================================
49 // method definitions
50 // ===========================================================================
52  : myTosplit(tosplit), myPos(0) {
53  prepareWhitechar(tosplit);
54 }
55 
56 
57 StringTokenizer::StringTokenizer(std::string tosplit, std::string token, bool splitAtAllChars)
58  : myTosplit(tosplit), myPos(0) {
59  prepare(tosplit, token, splitAtAllChars);
60 }
61 
62 
63 StringTokenizer::StringTokenizer(std::string tosplit, int special)
64  : myTosplit(tosplit), myPos(0) {
65  switch (special) {
66  case NEWLINE:
67  prepare(tosplit, "\r\n", true);
68  break;
69  case TAB:
70  prepare(tosplit, "\t", true);
71  break;
72  case WHITECHARS:
73  prepareWhitechar(tosplit);
74  break;
75  default:
76  char* buf = new char[2];
77  buf[0] = (char) special;
78  buf[1] = 0;
79  prepare(tosplit, buf, false);
80  delete[] buf;
81  break;
82  }
83 }
84 
85 
87 
89  myPos = 0;
90 }
91 
93  return myPos != (int)myStarts.size();
94 }
95 
96 std::string StringTokenizer::next() {
97  if (myPos >= (int)myStarts.size()) {
98  throw OutOfBoundsException();
99  }
100  if (myLengths[myPos] == 0) {
101  myPos++;
102  return "";
103  }
104  int start = myStarts[myPos];
105  int length = myLengths[myPos++];
106  return myTosplit.substr(start, length);
107 }
108 
109 std::string StringTokenizer::front() {
110  if (myStarts.size() == 0) {
111  throw OutOfBoundsException();
112  }
113  if (myLengths[0] == 0) {
114  return "";
115  }
116  return myTosplit.substr(myStarts[0], myLengths[0]);
117 }
118 
119 std::string StringTokenizer::get(int pos) const {
120  if (pos >= (int)myStarts.size()) {
121  throw OutOfBoundsException();
122  }
123  if (myLengths[pos] == 0) {
124  return "";
125  }
126  int start = myStarts[pos];
127  int length = myLengths[pos];
128  return myTosplit.substr(start, length);
129 }
130 
131 
133  return (int)myStarts.size();
134 }
135 
136 void StringTokenizer::prepare(const std::string& tosplit, const std::string& token, bool splitAtAllChars) {
137  int beg = 0;
138  int len = (int)token.length();
139  if (splitAtAllChars) {
140  len = 1;
141  }
142  while (beg < (int)tosplit.length()) {
143  std::string::size_type end;
144  if (splitAtAllChars) {
145  end = tosplit.find_first_of(token, beg);
146  } else {
147  end = tosplit.find(token, beg);
148  }
149  if (end == std::string::npos) {
150  end = tosplit.length();
151  }
152  myStarts.push_back(beg);
153  myLengths.push_back((int)end - beg);
154  beg = (int)end + len;
155  if (beg == (int)tosplit.length()) {
156  myStarts.push_back(beg - 1);
157  myLengths.push_back(0);
158  }
159  }
160 }
161 
162 void StringTokenizer::prepareWhitechar(const std::string& tosplit) {
163  std::string::size_type len = tosplit.length();
164  std::string::size_type beg = 0;
165  while (beg < len && tosplit[beg] <= SPACE) {
166  beg++;
167  }
168  while (beg != std::string::npos && beg < len) {
169  std::string::size_type end = beg;
170  while (end < len && tosplit[end] > SPACE) {
171  end++;
172  }
173  myStarts.push_back((int)beg);
174  myLengths.push_back((int)end - (int)beg);
175  beg = end;
176  while (beg < len && tosplit[beg] <= SPACE) {
177  beg++;
178  }
179  }
180 }
181 
182 std::vector<std::string>
184  std::vector<std::string> ret;
185  ret.reserve(size());
186  while (hasNext()) {
187  ret.push_back(next());
188  }
189  reinit();
190  return ret;
191 }
192 
193 
194 
195 /****************************************************************************/
196 
std::string next()
static const int WHITECHARS
std::string myTosplit
std::string get(int pos) const
static const int NEWLINE
void prepare(const std::string &tosplit, const std::string &token, bool splitAtAllChars)
SizeVector myLengths
static const int SPACE
SizeVector myStarts
std::string front()
static const int TAB
std::vector< std::string > getVector()
void prepareWhitechar(const std::string &tosplit)