Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6372 was 5738, checked in by landauf, 15 years ago

merged libraries2 back to trunk

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