Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/blink/src/lib/util/substring.cc @ 10649

Last change on this file since 10649 was 9869, checked in by bensch, 18 years ago

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

File size: 13.1 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/**
31 * @brief default constructor
32 */
33SubString::SubString()
34{}
35
36
37/**
38 * @brief create a SubString from
39 * @param string the String to Spilit
40 * @param delimiter the Character at which to split string (delimiter)
41 */
42SubString::SubString(const std::string& string, char delimiter)
43{
44  this->split(string, delimiter);
45}
46
47
48/**
49 * @brief Splits a String into multiple splitters.
50 * @param string the String to split
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.
53 * @param emptyEntries If empty entries should be allewed or removed.
54 * @param escapeChar The Escape Character that overrides splitters commends and so on...
55 * @param safemode_char within these characters splitting won't happen
56 * @param comment_char the Comment character.
57 */
58SubString::SubString(const std::string& string,
59                     const std::string& delimiters, const std::string& delimiterNeighbours, bool emptyEntries,
60                     char escapeChar, char safemode_char, char comment_char)
61{
62  SubString::splitLine(this->strings, string, delimiters, delimiterNeighbours, emptyEntries, escapeChar, safemode_char, comment_char);
63}
64
65/**
66 * @brief creates a SubSet of a SubString.
67 * @param subString the SubString to take a set from.
68 * @param subSetBegin the beginning to the end
69 */
70SubString::SubString(const SubString& subString, unsigned int subSetBegin)
71{
72  for (unsigned int i = subSetBegin; i < subString.size(); i++)
73    this->strings.push_back(subString[i]);
74}
75
76
77/**
78 * @brief creates a SubSet of a SubString.
79 * @param subString the SubString to take a Set from
80 * @param subSetBegin the beginning to the end
81 * @param subSetEnd the end of the SubSet (max subString.size() will be checked internaly)
82 */
83SubString::SubString(const SubString& subString, unsigned int subSetBegin, unsigned int subSetEnd)
84{
85  for (unsigned int i = subSetBegin; i < subString.size() || i < subSetEnd; i++)
86    this->strings.push_back(subString[i]);
87}
88
89/**
90 * @brief creates a Substring from a count and values set.
91 * @param argc: the Arguments Count.
92 * @param argv: Argument Values.
93 */
94SubString::SubString(unsigned int argc, const char** argv)
95{
96  for(unsigned int i = 0; i < argc; ++i)
97    this->strings.push_back(std::string(argv[i]));
98}
99
100/**
101 * @brief removes the object from memory
102 */
103SubString::~SubString()
104{ }
105
106/** @brief An empty String */
107// const std::string SubString::emptyString = "";
108/** @brief Helper that gets you a String consisting of all White Spaces */
109const std::string SubString::WhiteSpaces = " \n\t";
110/** @brief Helper that gets you a String consisting of all WhiteSpaces and the Comma */
111const std::string SubString::WhiteSpacesWithComma = " \n\t,";
112/** An Empty SubString */
113const SubString SubString::NullSubString = SubString();
114
115/**
116 * @brief stores the Value of subString in this SubString
117 * @param subString will be copied into this String.
118 * @returns this SubString.
119 */
120SubString& SubString::operator=(const SubString& subString)
121{
122  this->strings = subString.strings;
123  return *this;
124}
125
126
127/**
128 * @brief comparator.
129 * @param subString the SubString to compare against this one.
130 * @returns true if the Stored Strings match
131 */
132bool SubString::operator==(const SubString& subString) const
133{
134  return (this->strings == subString.strings);
135}
136
137/**
138 * @brief comparator.
139 * @param subString the SubString to compare against this one.
140 * @returns true if the Stored Strings match
141 */
142bool SubString::compare(const SubString& subString) const
143{
144  return (*this == subString);
145}
146
147/**
148 * @brief comparator.
149 * @param subString the SubString to compare against this one.
150 * @param length how many entries to compare. (from 0 to length)
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  {
241    static std::string empty;
242    return empty;
243  }
244}
245
246
247/**
248 * @brief creates a SubSet of a SubString.
249 * @param subSetBegin the beginning to the end
250 * @returns the SubSet
251 *
252 * This function is added for your convenience, and does the same as
253 * SubString::SubString(const SubString& subString, unsigned int subSetBegin)
254 */
255SubString SubString::subSet(unsigned int subSetBegin) const
256{
257  return SubString(*this, subSetBegin);
258}
259
260
261/**
262 * @brief creates a SubSet of a SubString.
263 * @param subSetBegin the beginning to
264 * @param subSetEnd the end of the SubSet to select (if bigger than subString.size() it will be downset.)
265 * @returns the SubSet
266 *
267 * This function is added for your convenience, and does the same as
268 * SubString::SubString(const SubString& subString, unsigned int subSetBegin)
269 */
270SubString SubString::subSet(unsigned int subSetBegin, unsigned int subSetEnd) const
271{
272  return SubString(*this, subSetBegin, subSetEnd);
273}
274
275
276/**
277 * @brief splits line into tokens and stores them in ret.
278 * @param ret the Array, where the Splitted strings will be stored in
279 * to the beginning of the current token is stored
280 * @param line the inputLine to split
281 * @param delimiters a String of Delimiters (here the input will be splitted)
282 * @param delimiterNeighbours Naighbours to the Delimitter, that will be removed if they are to the left or the right of a Delimiter.
283 * @param emptyEntries: if empty Strings are added to the List of Strings.
284 * @param escape_char: Escape carater (escapes splitters)
285 * @param safemode_char: the beginning of the safemode is marked with this
286 * @param comment_char: the beginning of a comment is marked with this: (until the end of a Line)
287 * @param start_state: the Initial state on how to parse the String.
288 * @returns SPLIT_LINE_STATE the parser was in when returning
289 *
290 * This is the Actual Splitting Algorithm from Clemens Wacha
291 * Supports delimiters, escape characters,
292 * ignores special  characters between safemode_char and between comment_char and linend '\n'.
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.