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 | typedef enum { |
---|
13 | SL_NORMAL, |
---|
14 | SL_ESCAPE, |
---|
15 | SL_SAFEMODE, |
---|
16 | SL_SAFEESCAPE, |
---|
17 | SL_COMMENT, |
---|
18 | } SPLIT_LINE_STATE; |
---|
19 | |
---|
20 | //! A class that can load one string and split it in multipe ones |
---|
21 | class SubString |
---|
22 | { |
---|
23 | public: |
---|
24 | SubString(const std::string& string = "", char splitter = ','); |
---|
25 | SubString(const std::string& string, bool whiteSpaces); |
---|
26 | SubString(const std::string& string, const std::string& splitters, char escapeChar ='\\', char safemode_char = '"', char comment_char = '\0'); |
---|
27 | ~SubString(); |
---|
28 | |
---|
29 | unsigned int split(const std::string& string = "", char splitter = ','); |
---|
30 | unsigned int split(const std::string& string, bool whiteSpaces); |
---|
31 | unsigned int split(const std::string& string, const std::string& splitters, char escapeChar ='\\', char safemode_char = '"', char comment_char = '\0'); |
---|
32 | |
---|
33 | inline unsigned int getCount() { return this->strings.size(); }; |
---|
34 | const std::string& getString(unsigned int i) { return (i < this->strings.size()) ? this->strings[i] : emptyString; }; // safety-precaution |
---|
35 | const std::string& operator[](unsigned int i) { return this->getString(i); }; |
---|
36 | unsigned int getOffset(unsigned int i); |
---|
37 | |
---|
38 | static SPLIT_LINE_STATE splitLine(std::vector<std::string>& ret,std::vector<unsigned int>& offsets, |
---|
39 | const std::string& line, const std::string& delimiters = " \t\r\n", |
---|
40 | char escape_char = '\\', char safemode_char = '"', char comment_char = '\0', |
---|
41 | SPLIT_LINE_STATE start_state = SL_NORMAL); |
---|
42 | |
---|
43 | void debug() const; |
---|
44 | |
---|
45 | private: |
---|
46 | std::vector<std::string> strings; //!< strings produced from a single string splitted in multiple strings |
---|
47 | std::vector<unsigned int> offsets; //!< offsets of the beginning of the input-string to the beginning of each substring. |
---|
48 | |
---|
49 | static const std::string emptyString; |
---|
50 | }; |
---|
51 | |
---|
52 | #endif /* _SUBSTRING_H */ |
---|