Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7574 was 7477, checked in by bensch, 19 years ago

orxonox/trunk: new SubString variant

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