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