Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain.old/src/lib/util/substring.h @ 10637

Last change on this file since 10637 was 9406, checked in by bensch, 18 years ago

orxonox/trunk: merged the proxy back

merged with commandsvn merge -r9346:HEAD https://svn.orxonox.net/orxonox/branches/proxy .

no conflicts

File size: 4.9 KB
Line 
1/*!
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
23#include <vector>
24#include <string>
25
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 */
32class SubString
33{
34public:
35  typedef enum {
36    SL_NORMAL,
37    SL_ESCAPE,
38    SL_SAFEMODE,
39    SL_SAFEESCAPE,
40    SL_COMMENT,
41  } SPLIT_LINE_STATE;
42
43
44public:
45  SubString();
46  SubString(const std::string& string, char delimiter = ',');
47  SubString(const std::string& string,
48            const std::string& delimiters, const std::string& delimiterNeighbours = "", bool emptyEntries=false,
49            char escapeChar ='\\', char safemode_char = '"', char comment_char = '\0');
50  /** @brief create a Substring as a copy of another one. @param subString the SubString to copy. */
51  SubString(unsigned int argc, const char** argv);
52  SubString(const SubString& subString) { *this = subString; };
53  SubString(const SubString& subString, unsigned int subSetBegin);
54  SubString(const SubString& subString, unsigned int subSetBegin, unsigned int subSetEnd);
55  ~SubString();
56
57  // operate on the SubString
58  SubString& operator=(const SubString& subString);
59  bool operator==(const SubString& subString) const;
60  bool compare(const SubString& subString) const;
61  bool compare(const SubString& subString, unsigned int length) const;
62  SubString operator+(const SubString& subString) const;
63  SubString& operator+=(const SubString& subString);
64  /** @param subString the String to append @returns appended String. @brief added for convenience */
65  SubString& append(const SubString subString) { return (*this += subString); };
66
67  /////////////////////////////////////////
68  // Split and Join the any String. ///////
69  unsigned int split(const std::string& string = "", char delimiter = ',');
70  unsigned int split(const std::string& string,
71                     const std::string& delimiters, const std::string& delimiterNeighbours = "", bool emptyEntries = false,
72                     char escapeChar ='\\', char safemode_char = '"', char comment_char = '\0');
73  std::string join(const std::string& delimiter = " ") const;
74  ////////////////////////////////////////
75
76  // retrieve a SubSet from the String
77  SubString subSet(unsigned int subSetBegin) const;
78  SubString subSet(unsigned int subSetBegin, unsigned int subSetEnd) const;
79
80  // retrieve Information from within
81  /** @returns true if the SubString is empty */
82  inline bool empty() const { return this->strings.empty(); };
83  /** @returns the count of Strings stored in this substring */
84  inline unsigned int size() const { return this->strings.size(); };
85  /** @param i the i'th String @returns the i'th string from the subset of Strings */
86  inline const std::string& operator[](unsigned int i) const { return this->strings[i]; };
87  /** @param i the i'th String @returns the i'th string from the subset of Strings */
88  inline const std::string& getString(unsigned int i) const { return (*this)[i]; };
89  /** @returns the front of the StringList. */
90  inline const std::string& front() const { return this->strings.front(); };
91  /** @returns the back of the StringList. */
92  inline const std::string& back() const { return this->strings.back(); };
93  /** @brief removes the back of the strings list. */
94  inline void pop_back() { this->strings.pop_back(); };
95
96  // the almighty algorithm.
97  static SPLIT_LINE_STATE splitLine(std::vector<std::string>& ret,
98                                    const std::string& line,
99                                    const std::string& delimiters = SubString::WhiteSpaces,
100                                    const std::string& delimiterNeighbours = "",
101                                    bool emptyEntries = false,
102                                    char escape_char = '\\',
103                                    char safemode_char = '"',
104                                    char comment_char = '\0',
105                                    SPLIT_LINE_STATE start_state = SL_NORMAL);
106  // debugging.
107  void debug() const;
108
109public:
110  static const std::string WhiteSpaces;
111  static const std::string WhiteSpacesWithComma;
112
113private:
114  std::vector<std::string>  strings;                      //!< strings produced from a single string splitted in multiple strings
115};
116
117#endif /* __SUBSTRING_H__ */
Note: See TracBrowser for help on using the repository browser.