Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core/src/util/substring.h @ 809

Last change on this file since 809 was 792, checked in by landauf, 17 years ago

upload of the work i did before the exams (not yet finished nor working)

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