Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/input/src/util/SubString.h @ 963

Last change on this file since 963 was 871, checked in by landauf, 17 years ago

merged core branch to trunk

File size: 6.9 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 ='\\', char safemode_char = '"', char openparenthesis_char = '(', char closeparenthesis_char = ')', char comment_char = '\0');
91  SubString(unsigned int argc, const char** argv);
92  /** @brief create a Substring as a copy of another one. @param subString the SubString to copy. */
93  SubString(const SubString& subString) { *this = subString; };
94  SubString(const SubString& subString, unsigned int subSetBegin);
95  SubString(const SubString& subString, unsigned int subSetBegin, unsigned int subSetEnd);
96  ~SubString();
97
98  // operate on the SubString
99  SubString& operator=(const SubString& subString);
100  bool operator==(const SubString& subString) const;
101  bool compare(const SubString& subString) const;
102  bool compare(const SubString& subString, unsigned int length) const;
103  SubString operator+(const SubString& subString) const;
104  SubString& operator+=(const SubString& subString);
105  /** @param subString the String to append @returns appended String. @brief added for convenience */
106  SubString& append(const SubString subString) { return (*this += subString); };
107
108  /////////////////////////////////////////
109  // Split and Join the any String. ///////
110  unsigned int split(const std::string& string = "", char delimiter = ',');
111  unsigned int split(const std::string& string,
112                     const std::string& delimiters, const std::string& delimiterNeighbours = "", bool emptyEntries = false,
113                     char escapeChar ='\\', char safemode_char = '"', char openparenthesis_char = '(', char closeparenthesis_char = ')', char comment_char = '\0');
114  std::string join(const std::string& delimiter = " ") const;
115  ////////////////////////////////////////
116
117  // retrieve a SubSet from the String
118  SubString subSet(unsigned int subSetBegin) const;
119  SubString subSet(unsigned int subSetBegin, unsigned int subSetEnd) const;
120
121  // retrieve Information from within
122  /** @returns true if the SubString is empty */
123  inline bool empty() const { return this->strings.empty(); };
124  /** @returns the count of Strings stored in this substring */
125  inline unsigned int size() const { return this->strings.size(); };
126  /** @param i the i'th String @returns the i'th string from the subset of Strings */
127  inline const std::string& operator[](unsigned int i) const { return this->strings[i]; };
128  /** @param i the i'th String @returns the i'th string from the subset of Strings */
129  inline const std::string& getString(unsigned int i) const { return (*this)[i]; };
130  /** @returns the front of the StringList. */
131  inline const std::string& front() const { return this->strings.front(); };
132  /** @returns the back of the StringList. */
133  inline const std::string& back() const { return this->strings.back(); };
134  /** @brief removes the back of the strings list. */
135  inline void pop_back() { this->strings.pop_back(); };
136
137  // the almighty algorithm.
138  static SPLIT_LINE_STATE splitLine(std::vector<std::string>& ret,
139                                    const std::string& line,
140                                    const std::string& delimiters = SubString::WhiteSpaces,
141                                    const std::string& delimiterNeighbours = "",
142                                    bool emptyEntries = false,
143                                    char escape_char = '\\',
144                                    char safemode_char = '"',
145                                    char openparenthesis_char = '(',
146                                    char closeparenthesis_char = ')',
147                                    char comment_char = '\0',
148                                    SPLIT_LINE_STATE start_state = SL_NORMAL);
149  // debugging.
150  void debug() const;
151
152public:
153  static const std::string WhiteSpaces;
154  static const std::string WhiteSpacesWithComma;
155  static const SubString   NullSubString;
156
157private:
158  std::vector<std::string>  strings;                      //!< strings produced from a single string splitted in multiple strings
159};
160
161#endif /* __SUBSTRING_H__ */
Note: See TracBrowser for help on using the repository browser.