Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/util/SubString.h @ 10162

Last change on this file since 10162 was 9550, checked in by landauf, 12 years ago

merged testing branch back to trunk. unbelievable it took me 13 months to finish this chore…

  • Property svn:eol-style set to native
File size: 9.6 KB
RevLine 
[1505]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
[1791]36 *   Extended by Fabian 'x3n' Landau by the SL_PARENTHESES mode.
[1505]37 */
38
[7401]39 /**
40    @file
41    @ingroup String
42    @brief A helper class to split a string into several tokens.
[836]43
[7401]44    @anchor SubStringExample
45
46    The class SubString can be used to split an std::string into multiple tokens, using
47    a delimiter. SubString allows different options, for example to remove whitespaces
48    around the delimiters or different safe-mode chars, like quotation marks and braces.
49
50    You can access the tokens of the SubString using the [] operator like an array.
51    SubString also supports to join the tokens (or a subset of the tokens) again using
52    @ref orxonox::SubString::join() "join()". It's even possible to get a subset of the
53    SubString as another SubString using @ref orxonox::SubString::subSet() "subSet()".
54
55    Example:
56    @code
57    std::string text = "This is a test, \"Hello \\\" World\" and vector {1, 2, 3}";
58    SubString tokens(text, SubString::WhiteSpaces, "", false, '\\', true, '"', true, '{', '}', true, '\0');
59
[9550]60    for (size_t i = 0; i < tokens.size(); ++i)
[8858]61        orxout() << i << ": " << tokens[i] << endl;
[7401]62    @endcode
63
64    The output of this code is:
65     - 0: This
66     - 1: is
67     - 2: a
68     - 3: test,
69     - 4: Hello " World
70     - 5: and
71     - 6: vector
72     - 7: 1, 2, 3
73
74    The string was split using the delimiter " ". A string between quotation mark is not
75    split, the same holds for strings between '{' and '}'. Note how the quotation marks and
76    the braces were removed from the tokens, because the corresponding argument is 'true'.
77
78    Also note that the comma after "test" in token 3 is still there - it is neither part of the
79    delimiters SubString::WhiteSpaces nor part of the delimiterNeighbours parameter, so it
80    remains a part of the token.
81*/
82
[1784]83#ifndef __SubString_H__
84#define __SubString_H__
[836]85
[1505]86#include "UtilPrereqs.h"
[1062]87
[836]88#include <vector>
89#include <string>
90
[2171]91namespace orxonox
[836]92{
[2171]93    /**
[7401]94        @brief A class that splits a string into multiple tokens using different options.
95
96        The string is split into multiple tokens using a delimiter. Different options like
97        escape character, quotation marks, and more can be used to satisfy your needs.
98
99        See @ref SubStringExample "this description" for an example.
100    */
[2171]101    class _UtilExport SubString
102    {
[7401]103        /// An enumerator for the internal state of the parser
104        enum SPLIT_LINE_STATE
105        {
[2171]106            SL_NORMAL,            //!< Normal state
107            SL_ESCAPE,            //!< After an escape character
[7401]108            SL_SAFEMODE,          //!< In safe mode (usually between quotation marks).
[2171]109            SL_SAFEESCAPE,        //!< In safe mode with the internal escape character, that escapes even the savemode character.
110            SL_COMMENT,           //!< In Comment mode.
[7284]111            SL_PARENTHESES,       //!< Between parentheses (usually '{' and '}')
[8706]112            SL_PARENTHESESESCAPE, //!< Between parentheses with the internal escape character, that escapes even the closing parenthesis character.
[7401]113        };
[836]114
[2171]115    public:
116        SubString();
[7401]117        SubString(const std::string& line,
118                  const std::string& delimiters = SubString::WhiteSpaces,
119                  const std::string& delimiterNeighbours = "",
120                  bool bAllowEmptyEntries=false,
121                  char escapeChar ='\\',
122                  bool bRemoveEscapeChar = true,
123                  char safemodeChar = '"',
124                  bool bRemoveSafemodeChar = true,
125                  char openparenthesisChar = '{',
126                  char closeparenthesisChar = '}',
127                  bool bRemoveParenthesisChars = true,
128                  char commentChar = '\0');
[9550]129        SubString(size_t argc, const char** argv);
130        SubString(const SubString& other, size_t begin, size_t length = std::string::npos);
[2171]131        ~SubString();
[836]132
[2171]133        // operate on the SubString
[7401]134        SubString& operator=(const SubString& other);
135        bool operator==(const SubString& other) const;
[9550]136        bool compare(const SubString& other, size_t length = std::string::npos) const;
[7401]137        SubString operator+(const SubString& other) const;
138        SubString& operator+=(const SubString& other);
139        /// Appends the tokens of another SubString to this. @return This SubString.
140        inline SubString& append(const SubString& other) { return (*this += other); }
[836]141
[2171]142        /////////////////////////////////////////
143        // Split and Join the any String. ///////
[9550]144        size_t split(const std::string& line,
145                     const std::string& delimiters = SubString::WhiteSpaces,
146                     const std::string& delimiterNeighbours = "",
147                     bool bAllowEmptyEntries = false,
148                     char escapeChar ='\\',
149                     bool bRemoveEscapeChar = true,
150                     char safemodeChar = '"',
151                     bool bRemoveSafemodeChar = true,
152                     char openparenthesisChar = '{',
153                     char closeparenthesisChar = '}',
154                     bool bRemoveParenthesisChars = true,
155                     char commentChar = '\0');
[7401]156
[2171]157        std::string join(const std::string& delimiter = " ") const;
158        ////////////////////////////////////////
[836]159
[2171]160        // retrieve a SubSet from the String
[9550]161        SubString subSet(size_t begin, size_t length = std::string::npos) const;
[836]162
[2171]163        // retrieve Information from within
[7401]164        /// Returns true if the SubString is empty
165        inline bool empty() const { return this->tokens_.empty(); }
166        /// Returns the number of tokens stored in this SubString
[9550]167        inline size_t size() const { return this->tokens_.size(); }
[8706]168        /// Returns the i'th token from the subset of strings @param index The index of the requested token
[9550]169        inline const std::string& operator[](size_t index) const { return this->tokens_[index]; }
[7401]170        /// Returns the i'th token from the subset of strings @param index The index of the requested token
[9550]171        inline const std::string& getString(size_t index) const { return (*this)[index]; }
[7401]172        /// Returns all tokens as std::vector
173        inline const std::vector<std::string>& getAllStrings() const { return this->tokens_; }
174        /// Returns true if the token is in safemode. @param index The index of the token
[9550]175        inline bool isInSafemode(size_t index) const { return this->bTokenInSafemode_[index]; }
[7401]176        /// Returns the front of the list of tokens.
177        inline const std::string& front() const { return this->tokens_.front(); }
178        /// Returns the back of the list of tokens.
179        inline const std::string& back() const { return this->tokens_.back(); }
180        /// Removes the back of the list of tokens.
181        inline void pop_back() { this->tokens_.pop_back(); this->bTokenInSafemode_.pop_back(); }
[836]182
[7401]183        void debug() const;
184
185    public:
186        static const std::string WhiteSpaces;           ///< All whitespaces (usually used as delimiters or delimiterNeighbours
187        static const std::string WhiteSpacesWithComma;  ///< All whitespaces and the comma (usually used as delimiters)
188        static const SubString   NullSubString;         ///< An empty SubString
189
190    private:
[2171]191        // the almighty algorithm.
[7401]192        static SPLIT_LINE_STATE splitLine(std::vector<std::string>& tokens,
193                                          std::vector<bool>& bTokenInSafemode,
[2171]194                                          const std::string& line,
195                                          const std::string& delimiters = SubString::WhiteSpaces,
196                                          const std::string& delimiterNeighbours = "",
[7401]197                                          bool bAllowEmptyEntries = false,
198                                          char escapeChar = '\\',
199                                          bool bRemoveEscapeChar = true,
200                                          char safemodeChar = '"',
201                                          bool bRemoveSafemodeChar = true,
202                                          char openparenthesisChar = '{',
203                                          char closeparenthesisChar = '}',
204                                          bool bRemoveParenthesisChars = true,
205                                          char commentChar = '\0',
[2171]206                                          SPLIT_LINE_STATE start_state = SL_NORMAL);
[836]207
[8706]208        std::vector<std::string>  tokens_;              ///< The tokens after splitting the input line
[7401]209        std::vector<bool>         bTokenInSafemode_;    ///< Saves for each token if it was in safe mode (between quotation marks or parenthesis)
[2171]210    };
211}
[836]212
[1784]213#endif /* __SubString_H__ */
Note: See TracBrowser for help on using the repository browser.