Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutorial/src/util/substring.cc @ 884

Last change on this file since 884 was 859, checked in by landauf, 17 years ago

more or less a copy of the trunk

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