SUMO - Simulation of Urban MObility
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.dlr.de/
12 // Copyright (C) 2001-2017 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 const int StringTokenizer::TAB = 9;
51 
52 
53 // ===========================================================================
54 // method definitions
55 // ===========================================================================
57  : myTosplit(tosplit), myPos(0) {
58  prepareWhitechar(tosplit);
59 }
60 
61 
62 StringTokenizer::StringTokenizer(std::string tosplit, std::string token, bool splitAtAllChars)
63  : myTosplit(tosplit), myPos(0) {
64  prepare(tosplit, token, splitAtAllChars);
65 }
66 
67 
68 StringTokenizer::StringTokenizer(std::string tosplit, int special)
69  : myTosplit(tosplit), myPos(0) {
70  switch (special) {
71  case NEWLINE:
72  prepare(tosplit, "\r\n", true);
73  break;
74  case TAB:
75  prepare(tosplit, "\t", true);
76  break;
77  case WHITECHARS:
78  prepareWhitechar(tosplit);
79  break;
80  default:
81  char* buf = new char[2];
82  buf[0] = (char) special;
83  buf[1] = 0;
84  prepare(tosplit, buf, false);
85  delete[] buf;
86  break;
87  }
88 }
89 
90 
92 
94  myPos = 0;
95 }
96 
98  return myPos != (int)myStarts.size();
99 }
100 
101 std::string StringTokenizer::next() {
102  if (myPos >= (int)myStarts.size()) {
103  throw OutOfBoundsException();
104  }
105  if (myLengths[myPos] == 0) {
106  myPos++;
107  return "";
108  }
109  int start = myStarts[myPos];
110  int length = myLengths[myPos++];
111  return myTosplit.substr(start, length);
112 }
113 
114 std::string StringTokenizer::front() {
115  if (myStarts.size() == 0) {
116  throw OutOfBoundsException();
117  }
118  if (myLengths[0] == 0) {
119  return "";
120  }
121  return myTosplit.substr(myStarts[0], myLengths[0]);
122 }
123 
124 std::string StringTokenizer::get(int pos) const {
125  if (pos >= (int)myStarts.size()) {
126  throw OutOfBoundsException();
127  }
128  if (myLengths[pos] == 0) {
129  return "";
130  }
131  int start = myStarts[pos];
132  int length = myLengths[pos];
133  return myTosplit.substr(start, length);
134 }
135 
136 
138  return (int)myStarts.size();
139 }
140 
141 void StringTokenizer::prepare(const std::string& tosplit, const std::string& token, bool splitAtAllChars) {
142  int beg = 0;
143  int len = (int)token.length();
144  if (splitAtAllChars) {
145  len = 1;
146  }
147  while (beg < (int)tosplit.length()) {
148  std::string::size_type end;
149  if (splitAtAllChars) {
150  end = tosplit.find_first_of(token, beg);
151  } else {
152  end = tosplit.find(token, beg);
153  }
154  if (end == std::string::npos) {
155  end = tosplit.length();
156  }
157  myStarts.push_back(beg);
158  myLengths.push_back((int)end - beg);
159  beg = (int)end + len;
160  if (beg == (int)tosplit.length()) {
161  myStarts.push_back(beg - 1);
162  myLengths.push_back(0);
163  }
164  }
165 }
166 
167 void StringTokenizer::prepareWhitechar(const std::string& tosplit) {
168  std::string::size_type len = tosplit.length();
169  std::string::size_type beg = 0;
170  while (beg < len && tosplit[beg] <= SPACE) {
171  beg++;
172  }
173  while (beg != std::string::npos && beg < len) {
174  std::string::size_type end = beg;
175  while (end < len && tosplit[end] > SPACE) {
176  end++;
177  }
178  myStarts.push_back((int)beg);
179  myLengths.push_back((int)end - (int)beg);
180  beg = end;
181  while (beg < len && tosplit[beg] <= SPACE) {
182  beg++;
183  }
184  }
185 }
186 
187 std::vector<std::string>
189  std::vector<std::string> ret;
190  ret.reserve(size());
191  while (hasNext()) {
192  ret.push_back(next());
193  }
194  reinit();
195  return ret;
196 }
197 
198 
199 
200 /****************************************************************************/
201 
std::string next()
static const int WHITECHARS
std::string myTosplit
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()
std::string get(int pos) const
void prepareWhitechar(const std::string &tosplit)