Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/std/src/lib/util/substring.cc @ 7211

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

orxonox/trunk: new SubString class

File size: 5.4 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
19/**
20 *  breaks a string into parts that were initially seperated by comma
21 * @param string the string to break into substrings
22*/
23
24#include "substring.h"
25
26#include <string.h>
27#include <cassert>
28
29SubString::SubString(const std::string& string, char splitter)
30{
31  char split[2];
32  split[0] = splitter;
33  split[1] = '\0';
34  SubString::splitLine(this->strings, this->offsets,
35                       string, split);
36}
37
38
39/**
40 * Splits a String into a Substring removing all whiteSpaces
41 * @param string the String to Split
42 * @param whiteSpaces MUST BE __TRUE__
43 *
44 */
45SubString::SubString(const std::string& string, bool whiteSpaces)
46{
47  SubString::splitLine(this->strings, this->offsets,
48                      string);
49}
50
51SubString::SubString(const std::string& string, const std::string& splitters, char escapeChar,char safemode_char, char comment_char)
52{
53  SubString::splitLine(this->strings, this->offsets,
54                       string, splitters, escapeChar, safemode_char);
55}
56
57
58/**
59 * splits line into tokens and stores them in ret. Supports delimiters, escape characters,
60 * ignores special  characters between safemode_char and between comment_char and linend '\n'.
61 *
62 * @returns SPLIT_LINE_STATE the parser was in when returning
63 */
64SPLIT_LINE_STATE SubString::splitLine(std::vector<std::string>& ret, std::vector<unsigned int>& offsets,
65                                      const std::string& line, const std::string& delimiters,
66                                      char escape_char, char safemode_char, char comment_char,
67                                      SPLIT_LINE_STATE start_state)
68{
69  SPLIT_LINE_STATE state = start_state;
70  unsigned int i = 0;
71  std::string token;
72
73  if(start_state != SL_NORMAL && ret.size() > 0)
74  {
75    token = ret[ret.size()-1];
76    ret.pop_back();
77  }
78
79  while(i < line.size())
80  {
81    switch(state)
82    {
83    case SL_NORMAL:
84      if(line[i] == escape_char)
85      {
86        state = SL_ESCAPE;
87      }
88      else if(line[i] == safemode_char)
89      {
90        state = SL_SAFEMODE;
91      }
92      else if(line[i] == comment_char)
93      {
94        /// FINISH
95        if(token.size() > 0)
96        {
97          ret.push_back(token);
98          offsets.push_back(i);
99          token.clear();
100        }
101        token += line[i];       // EAT
102        state = SL_COMMENT;
103      }
104      else if(delimiters.find(line[i]) != std::string::npos)
105      {
106        // line[i] is a delimiter
107        /// FINISH
108        if(token.size() > 0)
109        {
110          ret.push_back(token);
111          offsets.push_back(i);
112          token.clear();
113        }
114      }
115      else
116      {
117        token += line[i];       // EAT
118      }
119      break;
120    case SL_ESCAPE:
121      if(line[i] == 'n') token += '\n';
122      else if(line[i] == 't') token += '\t';
123      else if(line[i] == 'v') token += '\v';
124      else if(line[i] == 'b') token += '\b';
125      else if(line[i] == 'r') token += '\r';
126      else if(line[i] == 'f') token += '\f';
127      else if(line[i] == 'a') token += '\a';
128      else if(line[i] == '?') token += '\?';
129      else token += line[i];  // EAT
130      state = SL_NORMAL;
131      break;
132    case SL_SAFEMODE:
133      if(line[i] == safemode_char)
134      {
135        state = SL_NORMAL;
136      }
137      else if(line[i] == escape_char)
138      {
139        state = SL_SAFEESCAPE;
140      }
141      else
142      {
143        token += line[i];       // EAT
144      }
145      break;
146    case SL_SAFEESCAPE:
147      if(line[i] == 'n') token += '\n';
148      else if(line[i] == 't') token += '\t';
149      else if(line[i] == 'v') token += '\v';
150      else if(line[i] == 'b') token += '\b';
151      else if(line[i] == 'r') token += '\r';
152      else if(line[i] == 'f') token += '\f';
153      else if(line[i] == 'a') token += '\a';
154      else if(line[i] == '?') token += '\?';
155      else token += line[i];  // EAT
156      state = SL_SAFEMODE;
157      break;
158    case SL_COMMENT:
159      if(line[i] == '\n')
160      {
161        /// FINISH
162        if(token.size() > 0)
163        {
164          ret.push_back(token);
165          offsets.push_back(i);
166          token.clear();
167        }
168        state = SL_NORMAL;
169      }
170      else
171      {
172        token += line[i];       // EAT
173      }
174      break;
175    default:
176      // nothing
177      break;
178    }
179    i++;
180  }
181
182  /// FINISH
183  if(token.size() > 0)
184  {
185    ret.push_back(token);
186    offsets.push_back(i);
187    token.clear();
188  }
189  return(state);
190}
191
192/**
193 *  removes the object from memory
194*/
195SubString::~SubString()
196{ }
197
198/**
199 * get a particular substring's offset
200 * @param i the ID of the substring to get the offset from
201 * @returns the offset or NULL if an invalid ID was given
202 */
203unsigned int SubString::getOffset(unsigned int i)
204{
205  if( i < this->offsets.size() && i >= 0)
206    return this->offsets[i];
207  else
208    return 0;
209}
210
211/**
212 * Some nice debug information about this SubString
213 */
214void SubString::debug() const
215{
216  printf("Substring-information::count=%d ::", this->strings.size());
217  for (unsigned int i = 0; i < this->strings.size(); i++)
218    printf("s%d='%s'::", i, this->strings[i].c_str());
219  printf("\n");
220}
Note: See TracBrowser for help on using the repository browser.