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 
40 // ===========================================================================
41 // variable definitions
42 // ===========================================================================
43 const int StringTokenizer::NEWLINE = -256;
44 const int StringTokenizer::WHITECHARS = -257;
45 const int StringTokenizer::SPACE = 32;
46 const int StringTokenizer::TAB = 9;
47 
48 
49 // ===========================================================================
50 // method definitions
51 // ===========================================================================
53  : myTosplit(tosplit), myPos(0) {
54  prepareWhitechar(tosplit);
55 }
56 
57 
58 StringTokenizer::StringTokenizer(std::string tosplit, std::string token, bool splitAtAllChars)
59  : myTosplit(tosplit), myPos(0) {
60  prepare(tosplit, token, splitAtAllChars);
61 }
62 
63 
64 StringTokenizer::StringTokenizer(std::string tosplit, int special)
65  : myTosplit(tosplit), myPos(0) {
66  switch (special) {
67  case NEWLINE:
68  prepare(tosplit, "\r\n", true);
69  break;
70  case TAB:
71  prepare(tosplit, "\t", 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 != (int)myStarts.size();
95 }
96 
97 std::string StringTokenizer::next() {
98  if (myPos >= (int)myStarts.size()) {
99  throw OutOfBoundsException();
100  }
101  if (myLengths[myPos] == 0) {
102  myPos++;
103  return "";
104  }
105  int start = myStarts[myPos];
106  int 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(int pos) const {
121  if (pos >= (int)myStarts.size()) {
122  throw OutOfBoundsException();
123  }
124  if (myLengths[pos] == 0) {
125  return "";
126  }
127  int start = myStarts[pos];
128  int length = myLengths[pos];
129  return myTosplit.substr(start, length);
130 }
131 
132 
134  return (int)myStarts.size();
135 }
136 
137 void StringTokenizer::prepare(const std::string& tosplit, const std::string& token, bool splitAtAllChars) {
138  int beg = 0;
139  int len = (int)token.length();
140  if (splitAtAllChars) {
141  len = 1;
142  }
143  while (beg < (int)tosplit.length()) {
144  std::string::size_type 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((int)end - beg);
155  beg = (int)end + len;
156  if (beg == (int)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  std::string::size_type len = tosplit.length();
165  std::string::size_type beg = 0;
166  while (beg < len && tosplit[beg] <= SPACE) {
167  beg++;
168  }
169  while (beg != std::string::npos && beg < len) {
170  std::string::size_type end = beg;
171  while (end < len && tosplit[end] > SPACE) {
172  end++;
173  }
174  myStarts.push_back((int)beg);
175  myLengths.push_back((int)end - (int)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 
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)