SUMO - Simulation of Urban MObility
StringTokenizer.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00009 // A java-style StringTokenizer for c++ (stl)
00010 /****************************************************************************/
00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00013 /****************************************************************************/
00014 //
00015 //   This file is part of SUMO.
00016 //   SUMO is free software: you can redistribute it and/or modify
00017 //   it under the terms of the GNU General Public License as published by
00018 //   the Free Software Foundation, either version 3 of the License, or
00019 //   (at your option) any later version.
00020 //
00021 /****************************************************************************/
00022 
00023 
00024 // ===========================================================================
00025 // included modules
00026 // ===========================================================================
00027 #ifdef _MSC_VER
00028 #include <windows_config.h>
00029 #else
00030 #include <config.h>
00031 #endif
00032 
00033 #include <string>
00034 #include <vector>
00035 #include <iostream> // !!! debug only
00036 #include "UtilExceptions.h"
00037 #include "StringTokenizer.h"
00038 
00039 #ifdef CHECK_MEMORY_LEAKS
00040 #include <foreign/nvwa/debug_new.h>
00041 #endif // CHECK_MEMORY_LEAKS
00042 
00043 
00044 // ===========================================================================
00045 // variable definitions
00046 // ===========================================================================
00047 const int StringTokenizer::NEWLINE = -256;
00048 const int StringTokenizer::WHITECHARS = -257;
00049 const int StringTokenizer::SPACE = 32;
00050 
00051 
00052 // ===========================================================================
00053 // method definitions
00054 // ===========================================================================
00055 StringTokenizer::StringTokenizer(std::string tosplit)
00056     : myTosplit(tosplit), myPos(0) {
00057     prepareWhitechar(tosplit);
00058 }
00059 
00060 
00061 StringTokenizer::StringTokenizer(std::string tosplit, std::string token, bool splitAtAllChars)
00062     : myTosplit(tosplit), myPos(0) {
00063     prepare(tosplit, token, splitAtAllChars);
00064 }
00065 
00066 
00067 StringTokenizer::StringTokenizer(std::string tosplit, int special)
00068     : myTosplit(tosplit), myPos(0) {
00069     switch (special) {
00070         case NEWLINE:
00071             prepare(tosplit, "\r\n", true);
00072             break;
00073         case WHITECHARS:
00074             prepareWhitechar(tosplit);
00075             break;
00076         default:
00077             char* buf = new char[2];
00078             buf[0] = (char) special;
00079             buf[1] = 0;
00080             prepare(tosplit, buf, false);
00081             delete[] buf;
00082             break;
00083     }
00084 }
00085 
00086 
00087 StringTokenizer::~StringTokenizer() {}
00088 
00089 void StringTokenizer::reinit() {
00090     myPos = 0;
00091 }
00092 
00093 bool StringTokenizer::hasNext() {
00094     return myPos != myStarts.size();
00095 }
00096 
00097 std::string StringTokenizer::next() {
00098     if (myPos >= myStarts.size()) {
00099         throw OutOfBoundsException();
00100     }
00101     if (myLengths[myPos] == 0) {
00102         myPos++;
00103         return "";
00104     }
00105     size_t start = myStarts[myPos];
00106     size_t length = myLengths[myPos++];
00107     return myTosplit.substr(start, length);
00108 }
00109 
00110 std::string StringTokenizer::front() {
00111     if (myStarts.size() == 0) {
00112         throw OutOfBoundsException();
00113     }
00114     if (myLengths[0] == 0) {
00115         return "";
00116     }
00117     return myTosplit.substr(myStarts[0], myLengths[0]);
00118 }
00119 
00120 std::string StringTokenizer::get(size_t pos) const {
00121     if (pos >= myStarts.size()) {
00122         throw OutOfBoundsException();
00123     }
00124     if (myLengths[pos] == 0) {
00125         return "";
00126     }
00127     size_t start = myStarts[pos];
00128     size_t length = myLengths[pos];
00129     return myTosplit.substr(start, length);
00130 }
00131 
00132 
00133 size_t StringTokenizer::size() const {
00134     return myStarts.size();
00135 }
00136 
00137 void StringTokenizer::prepare(const std::string& tosplit, const std::string& token, bool splitAtAllChars) {
00138     size_t beg = 0;
00139     size_t len = token.length();
00140     if (splitAtAllChars) {
00141         len = 1;
00142     }
00143     while (beg < tosplit.length()) {
00144         size_t end;
00145         if (splitAtAllChars) {
00146             end = tosplit.find_first_of(token, beg);
00147         } else {
00148             end = tosplit.find(token, beg);
00149         }
00150         if (end == std::string::npos) {
00151             end = tosplit.length();
00152         }
00153         myStarts.push_back(beg);
00154         myLengths.push_back(end - beg);
00155         beg = end + len;
00156         if (beg == tosplit.length()) {
00157             myStarts.push_back(beg - 1);
00158             myLengths.push_back(0);
00159         }
00160     }
00161 }
00162 
00163 void StringTokenizer::prepareWhitechar(const std::string& tosplit) {
00164     size_t len = tosplit.length();
00165     size_t beg = 0;
00166     while (beg < len && tosplit[beg] <= SPACE) {
00167         beg++;
00168     }
00169     while (beg != std::string::npos && beg < len) {
00170         size_t end = beg;
00171         while (end < len && tosplit[end] > SPACE) {
00172             end++;
00173         }
00174         myStarts.push_back(beg);
00175         myLengths.push_back(end - beg);
00176         beg = end;
00177         while (beg < len && tosplit[beg] <= SPACE) {
00178             beg++;
00179         }
00180     }
00181 }
00182 
00183 std::vector<std::string>
00184 StringTokenizer::getVector() {
00185     std::vector<std::string> ret;
00186     ret.reserve(size());
00187     while (hasNext()) {
00188         ret.push_back(next());
00189     }
00190     reinit();
00191     return ret;
00192 }
00193 
00194 
00195 
00196 /****************************************************************************/
00197 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines