Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core2/src/util/SubString.h @ 1001

Last change on this file since 1001 was 994, checked in by landauf, 17 years ago
  • added some symbols to the CommandExecutor: a) expression | expression: the pipe leads the output from the right expression into the left one b) expression > file: writes the output of the expression into a file c) expression < file: reads a file and uses it's content as input for the expression
  • added new console commands: a) echo text: returns the input b) read file: reads a file and returns the content c) write file text: writes text into a file d) append file text: appends text to a file
  • added stripEnclosingQuotes function to String.h, that removes enclosing quotes (if there are some). whitespaces outside the quotes are stripped, whitespaces inside the quotes stay. removes the quotes only if there is nothing else than whitespaces outside of them. what it changes: "expression" → expression what it let unchanged:
    • ex"press"ion
    • a"expression"b
    • a"expression"
    • "expression"b
    • express"ion
  • extended SubString: added some bools to determine the behaviour when dividing a string like the following up into pieces: mytext "this is a quoted area" blub (0, 1, 2)

this usually results in:
mytext / this is a quoted area / blub / 0, 1, 2

but now you can change it to:
mytext / "this is a quoted area" / blub / (0, 1, 2)

this is important if the string wents through several substring splitups and the quotes and brackets should stay.

File size: 7.7 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 *      Fabian 'x3n' Landau
26 *
27
28//  splitLine
29//  STL string tokenizer
30//
31//  Created by Clemens Wacha.
32//  Version 1.0
33//  Copyright (c) 2005 Clemens Wacha. All rights reserved.
34
35 * Extended by Fabian 'x3n' Landau with the SL_PARENTHESES mode.
36 */
37
38 /*!
39 * @file substring.h
40 * @brief a small class to get the parts of a string separated by commas
41 *
42 * This class is also identified as a Tokenizer. It splits up one long
43 * String into multiple small ones by a designated Delimiter.
44 *
45 * Substring is Advanced, and it is possible, to split a string by ','
46 * but also removing leading and trailing spaces around the comma.
47 *
48 * @example
49 * Split the String std::string st = "1345, The new empire   , is , orxonox"
50 * is splitted with:
51 * SubString(st, ',', " \n\t")
52 * into
53 * "1345", "The new empire", "is", "orxonox"
54 * As you can see, the useless spaces around ',' were removed.
55 */
56
57#ifndef __SUBSTRING_H__
58#define __SUBSTRING_H__
59
60#include <vector>
61#include <string>
62
63#include "UtilPrereqs.h"
64
65//! A class that can load one string and split it in multipe ones
66/**
67 * SubString is a very Powerfull way to create a SubSet from a String
68 * It can be used, to Split strings append them and join them again.
69 */
70class _UtilExport SubString
71{
72public:
73  //! An enumerator for the State the Parser is in
74  typedef enum {
75    SL_NORMAL,            //!< Normal state
76    SL_ESCAPE,            //!< After an escape character
77    SL_SAFEMODE,          //!< In safe mode (between "" mostly).
78    SL_SAFEESCAPE,        //!< In safe mode with the internal escape character, that escapes even the savemode character.
79    SL_COMMENT,           //!< In Comment mode.
80    SL_PARENTHESES,       //!< Between parentheses (usually '(' and ')')
81    SL_PARENTHESESESCAPE, //!< Between parentheses with the internal escape character, that escapes even the closing paranthesis character.
82  } SPLIT_LINE_STATE;
83
84
85public:
86  SubString();
87  SubString(const std::string& string, char delimiter = ',');
88  SubString(const std::string& string,
89            const std::string& delimiters, const std::string& delimiterNeighbours = "", bool emptyEntries=false,
90            char escapeChar ='\\', bool removeExcapeChar = true, char safemode_char = '"', bool removeSafemodeChar = true,
91            char openparenthesis_char = '(', char closeparenthesis_char = ')',  bool removeParenthesisChars = true, char comment_char = '\0');
92  SubString(unsigned int argc, const char** argv);
93  /** @brief create a Substring as a copy of another one. @param subString the SubString to copy. */
94  SubString(const SubString& subString) { *this = subString; };
95  SubString(const SubString& subString, unsigned int subSetBegin);
96  SubString(const SubString& subString, unsigned int subSetBegin, unsigned int subSetEnd);
97  ~SubString();
98
99  // operate on the SubString
100  SubString& operator=(const SubString& subString);
101  bool operator==(const SubString& subString) const;
102  bool compare(const SubString& subString) const;
103  bool compare(const SubString& subString, unsigned int length) const;
104  SubString operator+(const SubString& subString) const;
105  SubString& operator+=(const SubString& subString);
106  /** @param subString the String to append @returns appended String. @brief added for convenience */
107  SubString& append(const SubString subString) { return (*this += subString); };
108
109  /////////////////////////////////////////
110  // Split and Join the any String. ///////
111  unsigned int split(const std::string& string = "", char delimiter = ',');
112  unsigned int split(const std::string& string,
113                     const std::string& delimiters, const std::string& delimiterNeighbours = "", bool emptyEntries = false,
114                     char escapeChar ='\\', bool removeExcapeChar = true, char safemode_char = '"', bool removeSafemodeChar = true,
115                     char openparenthesis_char = '(', char closeparenthesis_char = ')',  bool removeParenthesisChars = true, char comment_char = '\0');
116  std::string join(const std::string& delimiter = " ") const;
117  ////////////////////////////////////////
118
119  // retrieve a SubSet from the String
120  SubString subSet(unsigned int subSetBegin) const;
121  SubString subSet(unsigned int subSetBegin, unsigned int subSetEnd) const;
122
123  // retrieve Information from within
124  /** @brief Returns true if the SubString is empty */
125  inline bool empty() const { return this->strings.empty(); };
126  /** @brief Returns the count of Strings stored in this substring */
127  inline unsigned int size() const { return this->strings.size(); };
128  /** @brief Returns the i'th string from the subset of Strings @param i the i'th String */
129  inline const std::string& operator[](unsigned int i) const { return this->strings[i]; };
130  /** @brief Returns the i'th string from the subset of Strings @param i the i'th String */
131  inline const std::string& getString(unsigned int i) const { return (*this)[i]; };
132  /** @brief Returns true if the token is in safemode. @param i the i'th token */
133  inline bool isInSafemode(unsigned int i) const { return this->bInSafemode[i]; }
134  /** @brief Returns the front of the StringList. */
135  inline const std::string& front() const { return this->strings.front(); };
136  /** @brief Returns the back of the StringList. */
137  inline const std::string& back() const { return this->strings.back(); };
138  /** @brief removes the back of the strings list. */
139  inline void pop_back() { this->strings.pop_back(); this->bInSafemode.pop_back(); };
140
141  // the almighty algorithm.
142  static SPLIT_LINE_STATE splitLine(std::vector<std::string>& ret,
143                                    std::vector<bool>& bInSafemode,
144                                    const std::string& line,
145                                    const std::string& delimiters = SubString::WhiteSpaces,
146                                    const std::string& delimiterNeighbours = "",
147                                    bool emptyEntries = false,
148                                    char escape_char = '\\',
149                                    bool removeExcapeChar = true,
150                                    char safemode_char = '"',
151                                    bool removeSafemodeChar = true,
152                                    char openparenthesis_char = '(',
153                                    char closeparenthesis_char = ')',
154                                    bool removeParenthesisChars = true,
155                                    char comment_char = '\0',
156                                    SPLIT_LINE_STATE start_state = SL_NORMAL);
157  // debugging.
158  void debug() const;
159
160public:
161  static const std::string WhiteSpaces;
162  static const std::string WhiteSpacesWithComma;
163  static const SubString   NullSubString;
164
165private:
166  std::vector<std::string>  strings;                      //!< strings produced from a single string splitted in multiple strings
167  std::vector<bool>         bInSafemode;
168};
169
170#endif /* __SUBSTRING_H__ */
Note: See TracBrowser for help on using the repository browser.