Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7897 was 7477, checked in by bensch, 19 years ago

orxonox/trunk: new SubString variant

File size: 4.1 KB
RevLine 
[4597]1/*!
[7325]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
12
[4482]13//! A class that can load one string and split it in multipe ones
[7319]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 */
[3941]18class SubString
19{
[7319]20public:
[7474]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:
[7323]31  SubString();
[7474]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');
[7319]36  /** @brief create a Substring as a copy of another one. @param subString the SubString to copy. */
[7477]37  SubString(unsigned int argc, const char** argv);
[7319]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);
[4220]41  ~SubString();
[4597]42
[7319]43  // operate on the SubString
44  SubString& operator=(const SubString& subString);
[7398]45  bool operator==(const SubString& subString) const;
46  bool compare(const SubString& subString) const;
47  bool compare(const SubString& subString, unsigned int length) const;
[7319]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. ///////
[7474]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');
[7320]59  std::string join(const std::string& delimiter = " ") const;
[7319]60  ////////////////////////////////////////
[6648]61
[7319]62  // retrieve a SubSet from the String
63  SubString getSubSet(unsigned int subSetBegin) const;
64  SubString getSubSet(unsigned int subSetBegin, unsigned int subSetEnd) const;
[4597]65
[7319]66  // retrieve Information from within
[7340]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 */
[7319]70  inline unsigned int size() const { return this->strings.size(); };
[7340]71  /** @param i the i'th String @returns the i'th string from the subset of Strings */
[7474]72const std::string& operator[](unsigned int i) const { return (i < this->strings.size()) ? this->strings[i] : emptyString;return this->getString(i); };
[7340]73  /** @param i the i'th String @returns the i'th string from the subset of Strings */
74  const std::string& getString(unsigned int i) const { return (*this)[i]; };
[7319]75
76  // the almighty algorithm.
[7320]77  static SPLIT_LINE_STATE splitLine(std::vector<std::string>& ret,
78                                    const std::string& line,
[7325]79                                    const std::string& delimiters = SubString::WhiteSpaces,
[7474]80                                    const std::string& delimiterNeighbours = "",
81                                    bool emptyEntries = false,
[7320]82                                    char escape_char = '\\',
83                                    char safemode_char = '"',
84                                    char comment_char = '\0',
[7221]85                                    SPLIT_LINE_STATE start_state = SL_NORMAL);
[7319]86  // debugging.
[4833]87  void debug() const;
88
[7325]89public:
90  static const std::string WhiteSpaces;
91  static const std::string WhiteSpacesWithComma;
92
[7319]93private:
94  std::vector<std::string>  strings;                      //!< strings produced from a single string splitted in multiple strings
[7221]95
[7319]96  static const std::string emptyString;
[3941]97};
98
99#endif /* _SUBSTRING_H */
Note: See TracBrowser for help on using the repository browser.