Orxonox  0.0.5 Codename: Arcturus
SubString.h
Go to the documentation of this file.
1 /*
2  * ORXONOX - the hottest 3D action shooter ever to exist
3  * > www.orxonox.net <
4  *
5  *
6  * License notice:
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  * Author:
23  * Christian Meyer
24  * Co-authors:
25  * Benjamin Grauer
26  * Fabian 'x3n' Landau
27  *
28 
29 // splitLine
30 // STL string tokenizer
31 //
32 // Created by Clemens Wacha.
33 // Version 1.0
34 // Copyright (c) 2005 Clemens Wacha. All rights reserved.
35 
36  * Extended by Fabian 'x3n' Landau by the SL_PARENTHESES mode.
37  */
38 
83 #ifndef __SubString_H__
84 #define __SubString_H__
85 
86 #include "UtilPrereqs.h"
87 
88 #include <vector>
89 #include <string>
90 
91 namespace orxonox
92 {
102  {
104  enum class SPLIT_LINE_STATE
105  {
106  NORMAL,
107  ESCAPE,
108  SAFEMODE,
109  SAFEESCAPE,
110  COMMENT,
111  PARENTHESES,
112  PARENTHESESESCAPE,
113  };
114 
115  public:
116  SubString();
118  SubString(const SubString& other) = default;
120  SubString(SubString&& other) = default;
121  SubString(const std::string& line,
122  const std::string& delimiters = SubString::WhiteSpaces,
123  const std::string& delimiterNeighbours = "",
124  bool bAllowEmptyEntries=false,
125  char escapeChar ='\\',
126  bool bRemoveEscapeChar = true,
127  char safemodeChar = '"',
128  bool bRemoveSafemodeChar = true,
129  char openparenthesisChar = '{',
130  char closeparenthesisChar = '}',
131  bool bRemoveParenthesisChars = true,
132  char commentChar = '\0');
133  SubString(size_t argc, const char** argv);
134  SubString(const SubString& other, size_t begin, size_t length = std::string::npos);
135  ~SubString();
136 
137  // operate on the SubString
138  SubString& operator=(const SubString& other);
140  SubString& operator=(SubString&& other) = default;
141  bool operator==(const SubString& other) const;
142  bool compare(const SubString& other, size_t length = std::string::npos) const;
143  SubString operator+(const SubString& other) const;
144  SubString& operator+=(const SubString& other);
146  inline SubString& append(const SubString& other) { return (*this += other); }
147 
149  // Split and Join the any String. ///////
150  size_t split(const std::string& line,
151  const std::string& delimiters = SubString::WhiteSpaces,
152  const std::string& delimiterNeighbours = "",
153  bool bAllowEmptyEntries = false,
154  char escapeChar ='\\',
155  bool bRemoveEscapeChar = true,
156  char safemodeChar = '"',
157  bool bRemoveSafemodeChar = true,
158  char openparenthesisChar = '{',
159  char closeparenthesisChar = '}',
160  bool bRemoveParenthesisChars = true,
161  char commentChar = '\0');
162 
163  std::string join(const std::string& delimiter = " ") const;
165 
166  // retrieve a SubSet from the String
167  SubString subSet(size_t begin, size_t length = std::string::npos) const;
168 
169  // retrieve Information from within
171  inline bool empty() const { return this->tokens_.empty(); }
173  inline size_t size() const { return this->tokens_.size(); }
175  inline const std::string& operator[](size_t index) const { return this->tokens_[index]; }
177  inline const std::string& getString(size_t index) const { return (*this)[index]; }
179  inline const std::vector<std::string>& getAllStrings() const { return this->tokens_; }
181  inline bool isInSafemode(size_t index) const { return this->bTokenInSafemode_[index]; }
183  inline const std::string& front() const { return this->tokens_.front(); }
185  inline const std::string& back() const { return this->tokens_.back(); }
187  inline void pop_back() { this->tokens_.pop_back(); this->bTokenInSafemode_.pop_back(); }
188 
189  void debug() const;
190 
191  public:
192  static const std::string WhiteSpaces;
194  static const SubString NullSubString;
195 
196  private:
197  // the almighty algorithm.
198  static SPLIT_LINE_STATE splitLine(std::vector<std::string>& tokens,
199  std::vector<bool>& bTokenInSafemode,
200  const std::string& line,
201  const std::string& delimiters = SubString::WhiteSpaces,
202  const std::string& delimiterNeighbours = "",
203  bool bAllowEmptyEntries = false,
204  char escapeChar = '\\',
205  bool bRemoveEscapeChar = true,
206  char safemodeChar = '"',
207  bool bRemoveSafemodeChar = true,
208  char openparenthesisChar = '{',
209  char closeparenthesisChar = '}',
210  bool bRemoveParenthesisChars = true,
211  char commentChar = '\0',
212  SPLIT_LINE_STATE start_state = SPLIT_LINE_STATE::NORMAL);
213 
214  std::vector<std::string> tokens_;
215  std::vector<bool> bTokenInSafemode_;
216  };
217 }
218 
219 #endif /* __SubString_H__ */
#define _UtilExport
Definition: UtilPrereqs.h:60
bool empty() const
Returns true if the SubString is empty.
Definition: SubString.h:171
void pop_back()
Removes the back of the list of tokens.
Definition: SubString.h:187
bool isInSafemode(size_t index) const
Returns true if the token is in safemode.
Definition: SubString.h:181
const std::string & front() const
Returns the front of the list of tokens.
Definition: SubString.h:183
void debug(const std::string &text)
Prints debug output with verbose level.
Definition: ConsoleCommandCompilation.cc:161
SPLIT_LINE_STATE
An enumerator for the internal state of the parser.
Definition: SubString.h:104
::std::string string
Definition: gtest-port.h:756
TiXmlString operator+(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.cpp:86
size_t size() const
Returns the number of tokens stored in this SubString.
Definition: SubString.h:173
SubString & append(const SubString &other)
Appends the tokens of another SubString to this.
Definition: SubString.h:146
static const SubString NullSubString
An empty SubString.
Definition: SubString.h:194
const std::string & getString(size_t index) const
Returns the i&#39;th token from the subset of strings.
Definition: SubString.h:177
const std::vector< std::string > & getAllStrings() const
Returns all tokens as std::vector.
Definition: SubString.h:179
const std::string & operator[](size_t index) const
Returns the i&#39;th token from the subset of strings.
Definition: SubString.h:175
static const std::string WhiteSpacesWithComma
All whitespaces and the comma (usually used as delimiters)
Definition: SubString.h:193
A class that splits a string into multiple tokens using different options.
Definition: SubString.h:101
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
std::vector< bool > bTokenInSafemode_
Saves for each token if it was in safe mode (between quotation marks or parenthesis) ...
Definition: SubString.h:215
Shared library macros, enums, constants and forward declarations for the util library ...
static const std::string WhiteSpaces
All whitespaces (usually used as delimiters or delimiterNeighbours.
Definition: SubString.h:192
const std::string & back() const
Returns the back of the list of tokens.
Definition: SubString.h:185
constexpr bool operator==(bool x, tribool y)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: tribool.h:117
std::vector< std::string > tokens_
The tokens after splitting the input line.
Definition: SubString.h:214