Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/util/substring.cc @ 9643

Last change on this file since 9643 was 9406, checked in by bensch, 18 years ago

orxonox/trunk: merged the proxy back

merged with commandsvn merge -r9346:HEAD https://svn.orxonox.net/orxonox/branches/proxy .

no conflicts

File size: 13.0 KB
RevLine 
[4597]1/*
[3941]2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Christian Meyer
[4597]13   co-programmer: Benjamin Grauer
14
15   2005-06-10: some naming conventions
[7221]16
17//
18//  splitLine
19//  STL string tokenizer
20//
21//  Created by Clemens Wacha.
22//  Version 1.0
23//  Copyright (c) 2005 Clemens Wacha. All rights reserved.
24//
25
[4220]26*/
27
28#include "substring.h"
29
[7319]30/**
[7323]31 * @brief default constructor
32 */
33SubString::SubString()
[7398]34{}
[7323]35
36
37/**
[7319]38 * @brief create a SubString from
39 * @param string the String to Spilit
[7474]40 * @param delimiter the Character at which to split string (delimiter)
[7319]41 */
[7474]42SubString::SubString(const std::string& string, char delimiter)
[4220]43{
[7474]44  this->split(string, delimiter);
[7221]45}
[4597]46
[7319]47
48/**
49 * @brief Splits a String into multiple splitters.
50 * @param string the String to split
[7474]51 * @param delimiters multiple set of characters at what to split. (delimiters)
52 * @param delimiterNeighbours neighbours of the delimiters, that will be erased only when near a delimiter.
[7319]53 * @param escapeChar The Escape Character that overrides splitters commends and so on...
54 * @param safemode_char within these characters splitting won't happen
55 * @param comment_char the Comment character.
56 */
[7474]57SubString::SubString(const std::string& string,
58                     const std::string& delimiters, const std::string& delimiterNeighbours, bool emptyEntries,
59                     char escapeChar, char safemode_char, char comment_char)
[7221]60{
[7474]61  SubString::splitLine(this->strings, string, delimiters, delimiterNeighbours, emptyEntries, escapeChar, safemode_char, comment_char);
[7221]62}
[4597]63
[7221]64/**
[7319]65 * @brief creates a SubSet of a SubString.
66 * @param subString the SubString to take a set from.
67 * @param subSetBegin the beginning to the end
[7221]68 */
[7319]69SubString::SubString(const SubString& subString, unsigned int subSetBegin)
70{
71  for (unsigned int i = subSetBegin; i < subString.size(); i++)
72    this->strings.push_back(subString[i]);
73}
74
75
76/**
77 * @brief creates a SubSet of a SubString.
78 * @param subString the SubString to take a Set from
79 * @param subSetBegin the beginning to the end
80 * @param subSetEnd the end of the SubSet (max subString.size() will be checked internaly)
81 */
82SubString::SubString(const SubString& subString, unsigned int subSetBegin, unsigned int subSetEnd)
83{
84  for (unsigned int i = subSetBegin; i < subString.size() || i < subSetEnd; i++)
85    this->strings.push_back(subString[i]);
86}
87
[7477]88/**
89 * @brief creates a Substring from a count and values set.
90 * @param argc: the Arguments Count.
91 * @param argv: Argument Values.
92 */
93SubString::SubString(unsigned int argc, const char** argv)
94{
95  for(unsigned int i = 0; i < argc; ++i)
96    this->strings.push_back(std::string(argv[i]));
97}
[7319]98
99/**
100 * @brief removes the object from memory
101 */
102SubString::~SubString()
103{ }
104
[7325]105/** @brief An empty String */
[9406]106// const std::string SubString::emptyString = "";
[7325]107/** @brief Helper that gets you a String consisting of all White Spaces */
108const std::string SubString::WhiteSpaces = " \n\t";
109/** @brief Helper that gets you a String consisting of all WhiteSpaces and the Comma */
110const std::string SubString::WhiteSpacesWithComma = " \n\t,";
[4597]111
[7319]112/**
113 * @brief stores the Value of subString in this SubString
114 * @param subString will be copied into this String.
115 * @returns this SubString.
116 */
117SubString& SubString::operator=(const SubString& subString)
118{
119  this->strings = subString.strings;
120  return *this;
121}
[4597]122
123
[7319]124/**
125 * @brief comparator.
126 * @param subString the SubString to compare against this one.
127 * @returns true if the Stored Strings match
128 */
[7398]129bool SubString::operator==(const SubString& subString) const
[7319]130{
131  return (this->strings == subString.strings);
132}
133
[7398]134/**
135 * @brief comparator.
136 * @param subString the SubString to compare against this one.
137 * @returns true if the Stored Strings match
138 */
139bool SubString::compare(const SubString& subString) const
140{
141  return (*this == subString);
142}
[7319]143
144/**
[7398]145 * @brief comparator.
146 * @param subString the SubString to compare against this one.
147 * @returns true if the Stored Strings match
148 */
149bool SubString::compare(const SubString& subString, unsigned int length) const
150{
151  if (length > this->size() || length > subString.size())
152    return false;
153
154  for (unsigned int i = 0; i < length; i++)
155    if (this->strings[i] != subString.strings[i])
156      return false;
157  return true;
158}
159
160
161/**
[7319]162 * @brief append operator
163 * @param subString the String to append.
164 * @returns a SubString where this and subString are appended.
165 */
166SubString SubString::operator+(const SubString& subString) const
167{
[7323]168  return SubString(*this) += subString;
[7319]169}
170
171
172/**
173 * @brief append operator.
174 * @param subString append subString to this SubString.
175 * @returns this substring appended with subString
176 */
177SubString& SubString::operator+=(const SubString& subString)
178{
179  for (unsigned int i = 0; i < subString.size(); i++)
180    this->strings.push_back(subString[i]);
[7323]181  return *this;
[7319]182}
183
184
185/**
186 * @brief Split the String at
187 * @param string where to split
188 * @param splitter delimiter.
189 */
[7221]190unsigned int SubString::split(const std::string& string, char splitter)
191{
192  this->strings.clear();
193  char split[2];
194  split[0] = splitter;
195  split[1] = '\0';
[7320]196  SubString::splitLine(this->strings, string, split);
[7221]197  return strings.size();
[4220]198}
199
[5183]200
[3941]201/**
[7319]202 * @brief Splits a String into multiple splitters.
203 * @param string the String to split
[7474]204 * @param delimiters multiple set of characters at what to split. (delimiters)
205 * @param delimiterNeighbours: Neighbours to the Delimiters that will be erased too.
206 * @param emptyEntries: If empty entries are added to the List of SubStrings
[7319]207 * @param escapeChar The Escape Character that overrides splitters commends and so on...
208 * @param safemode_char within these characters splitting won't happen
209 * @param comment_char the Comment character.
210 */
[7474]211unsigned int SubString::split(const std::string& string,
212                              const std::string& delimiters, const std::string& delimiterNeighbours, bool emptyEntries,
213                              char escapeChar,char safemode_char, char comment_char)
[7221]214{
215  this->strings.clear();
[7474]216  SubString::splitLine(this->strings, string, delimiters, delimiterNeighbours, emptyEntries, escapeChar, safemode_char, comment_char);
[7323]217  return this->strings.size();
[7221]218}
[5183]219
220
[7221]221/**
[7319]222 * @brief joins together all Strings of this Substring.
223 * @param delimiter the String between the subStrings.
224 * @returns the joined String.
225 */
226std::string SubString::join(const std::string& delimiter) const
227{
228  if (!this->strings.empty())
229  {
230    std::string retVal = this->strings[0];
[7321]231    for (unsigned int i = 1; i < this->strings.size(); i++)
[7319]232      retVal += delimiter + this->strings[i];
233    return retVal;
234  }
235  else
[9406]236  {
237    static std::string empty;
238    return empty;
239  }
[7319]240}
241
242
243/**
244 * @brief creates a SubSet of a SubString.
245 * @param subSetBegin the beginning to the end
246 * @returns the SubSet
247 *
248 * This function is added for your convenience, and does the same as
249 * SubString::SubString(const SubString& subString, unsigned int subSetBegin)
250 */
[9406]251SubString SubString::subSet(unsigned int subSetBegin) const
[7319]252{
253  return SubString(*this, subSetBegin);
254}
255
256
257/**
258 * @brief creates a SubSet of a SubString.
259 * @param subSetBegin the beginning to
260 * @param subSetEnd the end of the SubSet to select (if bigger than subString.size() it will be downset.)
261 * @returns the SubSet
262 *
263 * This function is added for your convenience, and does the same as
264 * SubString::SubString(const SubString& subString, unsigned int subSetBegin)
265 */
[9406]266SubString SubString::subSet(unsigned int subSetBegin, unsigned int subSetEnd) const
[7319]267{
268  return SubString(*this, subSetBegin, subSetEnd);
269}
270
271
272/**
[7221]273 * @brief splits line into tokens and stores them in ret.
274 * @param ret the Array, where the Splitted strings will be stored in
275 * @param offsets an Array of Offsets, here the distance from the inputstring
276 * to the beginning of the current token is stored
277 * @param line the inputLine to split
278 * @param delimiters a String of Delimiters (here the input will be splitted)
[7474]279 * @param delimiterNeighbour Naighbours to the Delimitter, that will be removed if they are to the left or the right of a Delimiter.
280 * @param emptyEntries: if empty Strings are added to the List of Strings.
[7221]281 * @param escape_char: Escape carater (escapes splitters)
282 * @param safemode_char: the beginning of the safemode is marked with this
283 * @param comment_char: the beginning of a comment is marked with this: (until the end of a Line)
284 * @param start_state: the Initial state on how to parse the String.
285 * @returns SPLIT_LINE_STATE the parser was in when returning
286 *
[7319]287 * This is the Actual Splitting Algorithm from Clemens Wacha
[7221]288 * Supports delimiters, escape characters,
289 * ignores special  characters between safemode_char and between comment_char and linend '\n'.
290 */
[7474]291SubString::SPLIT_LINE_STATE
292SubString::splitLine(std::vector<std::string>& ret,
293                     const std::string& line,
294                     const std::string& delimiters,
295                     const std::string& delimiterNeighbours,
296                     bool emptyEntries,
297                     char escape_char,
298                     char safemode_char,
299                     char comment_char,
300                     SPLIT_LINE_STATE start_state)
[7221]301{
302  SPLIT_LINE_STATE state = start_state;
[5183]303  unsigned int i = 0;
[7474]304  unsigned int fallBackNeighbours = 0;
305
[7221]306  std::string token;
[5183]307
[7221]308  if(start_state != SL_NORMAL && ret.size() > 0)
[5656]309  {
[7221]310    token = ret[ret.size()-1];
311    ret.pop_back();
[5656]312  }
313
[7221]314  while(i < line.size())
[5656]315  {
[7221]316    switch(state)
[5656]317    {
[7474]318      case SL_NORMAL:
[7319]319        if(line[i] == escape_char)
[7221]320        {
[7319]321          state = SL_ESCAPE;
[7221]322        }
[7319]323        else if(line[i] == safemode_char)
[7221]324        {
[7319]325          state = SL_SAFEMODE;
[7221]326        }
[7319]327        else if(line[i] == comment_char)
328        {
[7474]329          if (fallBackNeighbours > 0)
330            token = token.substr(0, token.size() - fallBackNeighbours);
[7319]331          /// FINISH
[7474]332          if(emptyEntries || token.size() > 0)
[7319]333          {
334            ret.push_back(token);
335            token.clear();
336          }
337          token += line[i];       // EAT
338          state = SL_COMMENT;
339        }
340        else if(delimiters.find(line[i]) != std::string::npos)
341        {
342          // line[i] is a delimiter
[7474]343          if (fallBackNeighbours > 0)
344            token = token.substr(0, token.size() - fallBackNeighbours);
[7319]345          /// FINISH
[7474]346          if(emptyEntries || token.size() > 0)
[7319]347          {
348            ret.push_back(token);
349            token.clear();
350          }
[7474]351          state = SL_NORMAL;
[7319]352        }
353        else
354        {
[7474]355          if (delimiterNeighbours.find(line[i]) != std::string::npos)
356          {
357            if (token.size() > 0)
358              ++fallBackNeighbours;
359            else
360            {
361              i++;
362              continue;
363            }
364          }
365          else
366            fallBackNeighbours = 0;
[7319]367          token += line[i];       // EAT
368        }
369        break;
[7474]370      case SL_ESCAPE:
[7319]371        if(line[i] == 'n') token += '\n';
372        else if(line[i] == 't') token += '\t';
373        else if(line[i] == 'v') token += '\v';
374        else if(line[i] == 'b') token += '\b';
375        else if(line[i] == 'r') token += '\r';
376        else if(line[i] == 'f') token += '\f';
377        else if(line[i] == 'a') token += '\a';
378        else if(line[i] == '?') token += '\?';
379        else token += line[i];  // EAT
[7221]380        state = SL_NORMAL;
[7319]381        break;
[7474]382      case SL_SAFEMODE:
[7319]383        if(line[i] == safemode_char)
[7221]384        {
[7319]385          state = SL_NORMAL;
[7221]386        }
[7319]387        else if(line[i] == escape_char)
388        {
389          state = SL_SAFEESCAPE;
390        }
391        else
392        {
393          token += line[i];       // EAT
394        }
395        break;
[7474]396
397      case SL_SAFEESCAPE:
[7319]398        if(line[i] == 'n') token += '\n';
399        else if(line[i] == 't') token += '\t';
400        else if(line[i] == 'v') token += '\v';
401        else if(line[i] == 'b') token += '\b';
402        else if(line[i] == 'r') token += '\r';
403        else if(line[i] == 'f') token += '\f';
404        else if(line[i] == 'a') token += '\a';
405        else if(line[i] == '?') token += '\?';
406        else token += line[i];  // EAT
407        state = SL_SAFEMODE;
408        break;
[7474]409
410      case SL_COMMENT:
[7319]411        if(line[i] == '\n')
412        {
413          /// FINISH
414          if(token.size() > 0)
415          {
416            ret.push_back(token);
417            token.clear();
418          }
419          state = SL_NORMAL;
420        }
421        else
422        {
423          token += line[i];       // EAT
424        }
425        break;
[7474]426
427      default:
[7319]428        // nothing
429        break;
[5656]430    }
[7221]431    i++;
[5656]432  }
433
[7221]434  /// FINISH
[7474]435  if (fallBackNeighbours > 0)
436    token = token.substr(0, token.size() - fallBackNeighbours);
437  if(emptyEntries || token.size() > 0)
[5656]438  {
[7221]439    ret.push_back(token);
440    token.clear();
[5656]441  }
[7221]442  return(state);
[5656]443}
444
[3941]445
446/**
[7319]447 * @brief Some nice debug information about this SubString
[5200]448 */
[4833]449void SubString::debug() const
450{
[7221]451  printf("Substring-information::count=%d ::", this->strings.size());
452  for (unsigned int i = 0; i < this->strings.size(); i++)
453    printf("s%d='%s'::", i, this->strings[i].c_str());
454  printf("\n");
[4833]455}
Note: See TracBrowser for help on using the repository browser.