SUMO - Simulation of Urban MObility

StringTokenizer.cpp

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