Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/util/substring.h @ 7474

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

orxonox/trunk: SubString::split algorithm revisited: now it Splits Strings where Delimitters are, but it ereases the Neighbours of those delimiters, if you want it
also there is now a way to have empty Entries.

File size: 4.1 KB
Line 
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 */
18class SubString
19{
20public:
21  typedef enum {
22    SL_NORMAL,
23    SL_ESCAPE,
24    SL_SAFEMODE,
25    SL_SAFEESCAPE,
26    SL_COMMENT,
27  } SPLIT_LINE_STATE;
28
29
30public:
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(const SubString& subString) { *this = subString; };
38  SubString(const SubString& subString, unsigned int subSetBegin);
39  SubString(const SubString& subString, unsigned int subSetBegin, unsigned int subSetEnd);
40  ~SubString();
41
42  // operate on the SubString
43  SubString& operator=(const SubString& subString);
44  bool operator==(const SubString& subString) const;
45  bool compare(const SubString& subString) const;
46  bool compare(const SubString& subString, unsigned int length) const;
47  SubString operator+(const SubString& subString) const;
48  SubString& operator+=(const SubString& subString);
49  /** @param subString the String to append @returns appended String. @brief added for convenience */
50  SubString& append(const SubString subString) { return (*this += subString); };
51
52  /////////////////////////////////////////
53  // Split and Join the any String. ///////
54  unsigned int split(const std::string& string = "", char delimiter = ',');
55  unsigned int split(const std::string& string,
56                     const std::string& delimiters, const std::string& delimiterNeighbours = "", bool emptyEntries = false,
57                     char escapeChar ='\\', char safemode_char = '"', char comment_char = '\0');
58  std::string join(const std::string& delimiter = " ") const;
59  ////////////////////////////////////////
60
61  // retrieve a SubSet from the String
62  SubString getSubSet(unsigned int subSetBegin) const;
63  SubString getSubSet(unsigned int subSetBegin, unsigned int subSetEnd) const;
64
65  // retrieve Information from within
66  /** @returns true if the SubString is empty */
67  inline bool empty() const { return this->strings.empty(); };
68  /** @returns the count of Strings stored in this substring */
69  inline unsigned int size() const { return this->strings.size(); };
70  /** @param i the i'th String @returns the i'th string from the subset of Strings */
71const std::string& operator[](unsigned int i) const { return (i < this->strings.size()) ? this->strings[i] : emptyString;return this->getString(i); };
72  /** @param i the i'th String @returns the i'th string from the subset of Strings */
73  const std::string& getString(unsigned int i) const { return (*this)[i]; };
74
75  // the almighty algorithm.
76  static SPLIT_LINE_STATE splitLine(std::vector<std::string>& ret,
77                                    const std::string& line,
78                                    const std::string& delimiters = SubString::WhiteSpaces,
79                                    const std::string& delimiterNeighbours = "",
80                                    bool emptyEntries = false,
81                                    char escape_char = '\\',
82                                    char safemode_char = '"',
83                                    char comment_char = '\0',
84                                    SPLIT_LINE_STATE start_state = SL_NORMAL);
85  // debugging.
86  void debug() const;
87
88public:
89  static const std::string WhiteSpaces;
90  static const std::string WhiteSpacesWithComma;
91
92private:
93  std::vector<std::string>  strings;                      //!< strings produced from a single string splitted in multiple strings
94
95  static const std::string emptyString;
96};
97
98#endif /* _SUBSTRING_H */
Note: See TracBrowser for help on using the repository browser.