[1494] | 1 | /* * ORXONOX - the hottest 3D action shooter ever to exist * > www.orxonox.net < * * * License notice: * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Author: * Christian Meyer * Co-authors: * Benjamin Grauer * Fabian 'x3n' Landau *// splitLine// STL string tokenizer//// Created by Clemens Wacha.// Version 1.0// Copyright (c) 2005 Clemens Wacha. All rights reserved. * Extended by Fabian 'x3n' Landau with the SL_PARENTHESES mode. */ /*! |
---|
[836] | 2 | * @file substring.h |
---|
| 3 | * @brief a small class to get the parts of a string separated by commas |
---|
| 4 | * |
---|
| 5 | * This class is also identified as a Tokenizer. It splits up one long |
---|
| 6 | * String into multiple small ones by a designated Delimiter. |
---|
| 7 | * |
---|
| 8 | * Substring is Advanced, and it is possible, to split a string by ',' |
---|
| 9 | * but also removing leading and trailing spaces around the comma. |
---|
| 10 | * |
---|
| 11 | * @example |
---|
| 12 | * Split the String std::string st = "1345, The new empire , is , orxonox" |
---|
| 13 | * is splitted with: |
---|
| 14 | * SubString(st, ',', " \n\t") |
---|
| 15 | * into |
---|
| 16 | * "1345", "The new empire", "is", "orxonox" |
---|
| 17 | * As you can see, the useless spaces around ',' were removed. |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | #ifndef __SUBSTRING_H__ |
---|
| 21 | #define __SUBSTRING_H__ |
---|
| 22 | |
---|
[1494] | 23 | #include "UtilPrereqs.h" |
---|
[836] | 24 | #include <vector> |
---|
| 25 | #include <string> |
---|
| 26 | |
---|
| 27 | //! A class that can load one string and split it in multipe ones |
---|
| 28 | /** |
---|
| 29 | * SubString is a very Powerfull way to create a SubSet from a String |
---|
| 30 | * It can be used, to Split strings append them and join them again. |
---|
| 31 | */ |
---|
| 32 | class _UtilExport SubString |
---|
| 33 | { |
---|
| 34 | public: |
---|
| 35 | //! An enumerator for the State the Parser is in |
---|
| 36 | typedef enum { |
---|
| 37 | SL_NORMAL, //!< Normal state |
---|
| 38 | SL_ESCAPE, //!< After an escape character |
---|
| 39 | SL_SAFEMODE, //!< In safe mode (between "" mostly). |
---|
| 40 | SL_SAFEESCAPE, //!< In safe mode with the internal escape character, that escapes even the savemode character. |
---|
[1494] | 41 | SL_COMMENT, //!< In Comment mode. SL_PARENTHESES, //!< Between parentheses (usually '(' and ')') SL_PARENTHESESESCAPE, //!< Between parentheses with the internal escape character, that escapes even the closing paranthesis character. |
---|
[836] | 42 | } SPLIT_LINE_STATE; |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | public: |
---|
| 46 | SubString(); |
---|
| 47 | SubString(const std::string& string, char delimiter = ','); |
---|
| 48 | SubString(const std::string& string, |
---|
| 49 | const std::string& delimiters, const std::string& delimiterNeighbours = "", bool emptyEntries=false, |
---|
[1494] | 50 | char escapeChar ='\\', bool removeEscapeChar = true, char safemode_char = '"', bool removeSafemodeChar = true, char openparenthesis_char = '(', char closeparenthesis_char = ')', bool removeParenthesisChars = true, char comment_char = '\0'); |
---|
[836] | 51 | SubString(unsigned int argc, const char** argv); |
---|
| 52 | /** @brief create a Substring as a copy of another one. @param subString the SubString to copy. */ |
---|
| 53 | SubString(const SubString& subString) { *this = subString; }; |
---|
| 54 | SubString(const SubString& subString, unsigned int subSetBegin); |
---|
| 55 | SubString(const SubString& subString, unsigned int subSetBegin, unsigned int subSetEnd); |
---|
| 56 | ~SubString(); |
---|
| 57 | |
---|
| 58 | // operate on the SubString |
---|
| 59 | SubString& operator=(const SubString& subString); |
---|
| 60 | bool operator==(const SubString& subString) const; |
---|
| 61 | bool compare(const SubString& subString) const; |
---|
| 62 | bool compare(const SubString& subString, unsigned int length) const; |
---|
| 63 | SubString operator+(const SubString& subString) const; |
---|
| 64 | SubString& operator+=(const SubString& subString); |
---|
| 65 | /** @param subString the String to append @returns appended String. @brief added for convenience */ |
---|
| 66 | SubString& append(const SubString subString) { return (*this += subString); }; |
---|
| 67 | |
---|
| 68 | ///////////////////////////////////////// |
---|
| 69 | // Split and Join the any String. /////// |
---|
| 70 | unsigned int split(const std::string& string = "", char delimiter = ','); |
---|
| 71 | unsigned int split(const std::string& string, |
---|
| 72 | const std::string& delimiters, const std::string& delimiterNeighbours = "", bool emptyEntries = false, |
---|
[1494] | 73 | char escapeChar ='\\', bool removeExcapeChar = true, char safemode_char = '"', bool removeSafemodeChar = true, char openparenthesis_char = '(', char closeparenthesis_char = ')', bool removeParenthesisChars = true, char comment_char = '\0'); |
---|
[836] | 74 | std::string join(const std::string& delimiter = " ") const; |
---|
| 75 | //////////////////////////////////////// |
---|
| 76 | |
---|
| 77 | // retrieve a SubSet from the String |
---|
| 78 | SubString subSet(unsigned int subSetBegin) const; |
---|
| 79 | SubString subSet(unsigned int subSetBegin, unsigned int subSetEnd) const; |
---|
| 80 | |
---|
| 81 | // retrieve Information from within |
---|
[1052] | 82 | /** @brief Returns true if the SubString is empty */ |
---|
[836] | 83 | inline bool empty() const { return this->strings.empty(); }; |
---|
[1052] | 84 | /** @brief Returns the count of Strings stored in this substring */ |
---|
[836] | 85 | inline unsigned int size() const { return this->strings.size(); }; |
---|
[1052] | 86 | /** @brief Returns the i'th string from the subset of Strings @param i the i'th String */ |
---|
[836] | 87 | inline const std::string& operator[](unsigned int i) const { return this->strings[i]; }; |
---|
[1052] | 88 | /** @brief Returns the i'th string from the subset of Strings @param i the i'th String */ |
---|
[1494] | 89 | inline const std::string& getString(unsigned int i) const { return (*this)[i]; }; /** @brief Returns true if the token is in safemode. @param i the i'th token */ inline bool isInSafemode(unsigned int i) const { return this->bInSafemode[i]; } |
---|
[1052] | 90 | /** @brief Returns the front of the StringList. */ |
---|
[836] | 91 | inline const std::string& front() const { return this->strings.front(); }; |
---|
[1052] | 92 | /** @brief Returns the back of the StringList. */ |
---|
[836] | 93 | inline const std::string& back() const { return this->strings.back(); }; |
---|
| 94 | /** @brief removes the back of the strings list. */ |
---|
[1052] | 95 | inline void pop_back() { this->strings.pop_back(); this->bInSafemode.pop_back(); }; |
---|
[836] | 96 | |
---|
| 97 | // the almighty algorithm. |
---|
[1494] | 98 | static SPLIT_LINE_STATE splitLine(std::vector<std::string>& ret, std::vector<bool>& bInSafemode, |
---|
[836] | 99 | const std::string& line, |
---|
| 100 | const std::string& delimiters = SubString::WhiteSpaces, |
---|
| 101 | const std::string& delimiterNeighbours = "", |
---|
| 102 | bool emptyEntries = false, |
---|
[1494] | 103 | char escape_char = '\\', bool removeExcapeChar = true, |
---|
[836] | 104 | char safemode_char = '"', |
---|
[1494] | 105 | bool removeSafemodeChar = true, char openparenthesis_char = '(', char closeparenthesis_char = ')', bool removeParenthesisChars = true, char comment_char = '\0', |
---|
[836] | 106 | SPLIT_LINE_STATE start_state = SL_NORMAL); |
---|
| 107 | // debugging. |
---|
| 108 | void debug() const; |
---|
| 109 | |
---|
| 110 | public: |
---|
| 111 | static const std::string WhiteSpaces; |
---|
| 112 | static const std::string WhiteSpacesWithComma; |
---|
| 113 | static const SubString NullSubString; |
---|
| 114 | |
---|
| 115 | private: |
---|
[1494] | 116 | std::vector<std::string> strings; //!< strings produced from a single string splitted in multiple strings std::vector<bool> bInSafemode; |
---|
[836] | 117 | }; |
---|
| 118 | |
---|
| 119 | #endif /* __SUBSTRING_H__ */ |
---|