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