Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/util/SubString.h @ 2051

Last change on this file since 2051 was 1791, checked in by landauf, 16 years ago

added comments to all my classes in util

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