[4597] | 1 | /*! |
---|
[4220] | 2 | \file substring.h |
---|
| 3 | \brief a small class to get the parts of a string separated by commas |
---|
| 4 | */ |
---|
[3941] | 5 | |
---|
| 6 | #ifndef _SUBSTRING_H |
---|
| 7 | #define _SUBSTRING_H |
---|
[4220] | 8 | |
---|
[7221] | 9 | #include <vector> |
---|
| 10 | #include <string> |
---|
| 11 | |
---|
[7319] | 12 | typedef enum { |
---|
| 13 | SL_NORMAL, |
---|
| 14 | SL_ESCAPE, |
---|
| 15 | SL_SAFEMODE, |
---|
| 16 | SL_SAFEESCAPE, |
---|
| 17 | SL_COMMENT, |
---|
| 18 | } SPLIT_LINE_STATE; |
---|
[7221] | 19 | |
---|
[4482] | 20 | //! A class that can load one string and split it in multipe ones |
---|
[7319] | 21 | /** |
---|
| 22 | * SubString is a very Powerfull way to create a SubSet from a String |
---|
| 23 | * It can be used, to Split strings append them and join them again. |
---|
| 24 | */ |
---|
[3941] | 25 | class SubString |
---|
| 26 | { |
---|
[7319] | 27 | public: |
---|
[7323] | 28 | SubString(); |
---|
| 29 | SubString(const std::string& string, char splitter = ','); |
---|
[7221] | 30 | SubString(const std::string& string, bool whiteSpaces); |
---|
| 31 | SubString(const std::string& string, const std::string& splitters, char escapeChar ='\\', char safemode_char = '"', char comment_char = '\0'); |
---|
[7319] | 32 | /** @brief create a Substring as a copy of another one. @param subString the SubString to copy. */ |
---|
| 33 | SubString(const SubString& subString) { *this = subString; }; |
---|
| 34 | SubString(const SubString& subString, unsigned int subSetBegin); |
---|
| 35 | SubString(const SubString& subString, unsigned int subSetBegin, unsigned int subSetEnd); |
---|
[4220] | 36 | ~SubString(); |
---|
[4597] | 37 | |
---|
[7319] | 38 | // operate on the SubString |
---|
| 39 | SubString& operator=(const SubString& subString); |
---|
| 40 | bool operator==(const SubString& subString); |
---|
| 41 | SubString operator+(const SubString& subString) const; |
---|
| 42 | SubString& operator+=(const SubString& subString); |
---|
| 43 | /** @param subString the String to append @returns appended String. @brief added for convenience */ |
---|
| 44 | SubString& append(const SubString subString) { return (*this += subString); }; |
---|
| 45 | |
---|
| 46 | ///////////////////////////////////////// |
---|
| 47 | // Split and Join the any String. /////// |
---|
[7221] | 48 | unsigned int split(const std::string& string = "", char splitter = ','); |
---|
| 49 | unsigned int split(const std::string& string, bool whiteSpaces); |
---|
| 50 | unsigned int split(const std::string& string, const std::string& splitters, char escapeChar ='\\', char safemode_char = '"', char comment_char = '\0'); |
---|
[7320] | 51 | std::string join(const std::string& delimiter = " ") const; |
---|
[7319] | 52 | //////////////////////////////////////// |
---|
[6648] | 53 | |
---|
[7319] | 54 | // retrieve a SubSet from the String |
---|
| 55 | SubString getSubSet(unsigned int subSetBegin) const; |
---|
| 56 | SubString getSubSet(unsigned int subSetBegin, unsigned int subSetEnd) const; |
---|
[4597] | 57 | |
---|
[7319] | 58 | // retrieve Information from within |
---|
| 59 | inline unsigned int size() const { return this->strings.size(); }; |
---|
[7320] | 60 | const std::string& getString(unsigned int i) const { return (i < this->strings.size()) ? this->strings[i] : emptyString; }; |
---|
[7319] | 61 | const std::string& operator[](unsigned int i) const { return this->getString(i); }; |
---|
| 62 | |
---|
| 63 | // the almighty algorithm. |
---|
[7320] | 64 | static SPLIT_LINE_STATE splitLine(std::vector<std::string>& ret, |
---|
| 65 | const std::string& line, |
---|
| 66 | const std::string& delimiters = " \t\r\n", |
---|
| 67 | char escape_char = '\\', |
---|
| 68 | char safemode_char = '"', |
---|
| 69 | char comment_char = '\0', |
---|
[7221] | 70 | SPLIT_LINE_STATE start_state = SL_NORMAL); |
---|
[7319] | 71 | // debugging. |
---|
[4833] | 72 | void debug() const; |
---|
| 73 | |
---|
[7319] | 74 | private: |
---|
| 75 | std::vector<std::string> strings; //!< strings produced from a single string splitted in multiple strings |
---|
[7221] | 76 | |
---|
[7319] | 77 | static const std::string emptyString; |
---|
[3941] | 78 | }; |
---|
| 79 | |
---|
| 80 | #endif /* _SUBSTRING_H */ |
---|