[859] | 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 | |
---|
| 62 | //! A class that can load one string and split it in multipe ones |
---|
| 63 | /** |
---|
| 64 | * SubString is a very Powerfull way to create a SubSet from a String |
---|
| 65 | * It can be used, to Split strings append them and join them again. |
---|
| 66 | */ |
---|
| 67 | class SubString |
---|
| 68 | { |
---|
| 69 | public: |
---|
| 70 | //! An enumerator for the State the Parser is in |
---|
| 71 | typedef enum { |
---|
| 72 | SL_NORMAL, //!< Normal state |
---|
| 73 | SL_ESCAPE, //!< After an escape character |
---|
| 74 | SL_SAFEMODE, //!< In safe mode (between "" mostly). |
---|
| 75 | SL_SAFEESCAPE, //!< In safe mode with the internal escape character, that escapes even the savemode character. |
---|
| 76 | SL_COMMENT, //!< In Comment mode. |
---|
| 77 | } SPLIT_LINE_STATE; |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | public: |
---|
| 81 | SubString(); |
---|
| 82 | SubString(const std::string& string, char delimiter = ','); |
---|
| 83 | SubString(const std::string& string, |
---|
| 84 | const std::string& delimiters, const std::string& delimiterNeighbours = "", bool emptyEntries=false, |
---|
| 85 | char escapeChar ='\\', char safemode_char = '"', char comment_char = '\0'); |
---|
| 86 | SubString(unsigned int argc, const char** argv); |
---|
| 87 | /** @brief create a Substring as a copy of another one. @param subString the SubString to copy. */ |
---|
| 88 | SubString(const SubString& subString) { *this = subString; }; |
---|
| 89 | SubString(const SubString& subString, unsigned int subSetBegin); |
---|
| 90 | SubString(const SubString& subString, unsigned int subSetBegin, unsigned int subSetEnd); |
---|
| 91 | ~SubString(); |
---|
| 92 | |
---|
| 93 | // operate on the SubString |
---|
| 94 | SubString& operator=(const SubString& subString); |
---|
| 95 | bool operator==(const SubString& subString) const; |
---|
| 96 | bool compare(const SubString& subString) const; |
---|
| 97 | bool compare(const SubString& subString, unsigned int length) const; |
---|
| 98 | SubString operator+(const SubString& subString) const; |
---|
| 99 | SubString& operator+=(const SubString& subString); |
---|
| 100 | /** @param subString the String to append @returns appended String. @brief added for convenience */ |
---|
| 101 | SubString& append(const SubString subString) { return (*this += subString); }; |
---|
| 102 | |
---|
| 103 | ///////////////////////////////////////// |
---|
| 104 | // Split and Join the any String. /////// |
---|
| 105 | unsigned int split(const std::string& string = "", char delimiter = ','); |
---|
| 106 | unsigned int split(const std::string& string, |
---|
| 107 | const std::string& delimiters, const std::string& delimiterNeighbours = "", bool emptyEntries = false, |
---|
| 108 | char escapeChar ='\\', char safemode_char = '"', char comment_char = '\0'); |
---|
| 109 | std::string join(const std::string& delimiter = " ") const; |
---|
| 110 | //////////////////////////////////////// |
---|
| 111 | |
---|
| 112 | // retrieve a SubSet from the String |
---|
| 113 | SubString subSet(unsigned int subSetBegin) const; |
---|
| 114 | SubString subSet(unsigned int subSetBegin, unsigned int subSetEnd) const; |
---|
| 115 | |
---|
| 116 | // retrieve Information from within |
---|
| 117 | /** @returns true if the SubString is empty */ |
---|
| 118 | inline bool empty() const { return this->strings.empty(); }; |
---|
| 119 | /** @returns the count of Strings stored in this substring */ |
---|
| 120 | inline unsigned int size() const { return this->strings.size(); }; |
---|
| 121 | /** @param i the i'th String @returns the i'th string from the subset of Strings */ |
---|
| 122 | inline const std::string& operator[](unsigned int i) const { return this->strings[i]; }; |
---|
| 123 | /** @param i the i'th String @returns the i'th string from the subset of Strings */ |
---|
| 124 | inline const std::string& getString(unsigned int i) const { return (*this)[i]; }; |
---|
| 125 | /** @returns the front of the StringList. */ |
---|
| 126 | inline const std::string& front() const { return this->strings.front(); }; |
---|
| 127 | /** @returns the back of the StringList. */ |
---|
| 128 | inline const std::string& back() const { return this->strings.back(); }; |
---|
| 129 | /** @brief removes the back of the strings list. */ |
---|
| 130 | inline void pop_back() { this->strings.pop_back(); }; |
---|
| 131 | |
---|
| 132 | // the almighty algorithm. |
---|
| 133 | static SPLIT_LINE_STATE splitLine(std::vector<std::string>& ret, |
---|
| 134 | const std::string& line, |
---|
| 135 | const std::string& delimiters = SubString::WhiteSpaces, |
---|
| 136 | const std::string& delimiterNeighbours = "", |
---|
| 137 | bool emptyEntries = false, |
---|
| 138 | char escape_char = '\\', |
---|
| 139 | char safemode_char = '"', |
---|
| 140 | char comment_char = '\0', |
---|
| 141 | SPLIT_LINE_STATE start_state = SL_NORMAL); |
---|
| 142 | // debugging. |
---|
| 143 | void debug() const; |
---|
| 144 | |
---|
| 145 | public: |
---|
| 146 | static const std::string WhiteSpaces; |
---|
| 147 | static const std::string WhiteSpacesWithComma; |
---|
| 148 | static const SubString NullSubString; |
---|
| 149 | |
---|
| 150 | private: |
---|
| 151 | std::vector<std::string> strings; //!< strings produced from a single string splitted in multiple strings |
---|
| 152 | }; |
---|
| 153 | |
---|
| 154 | #endif /* __SUBSTRING_H__ */ |
---|