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
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. */
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);
[4220]40  ~SubString();
[4597]41
[7319]42  // operate on the SubString
43  SubString& operator=(const SubString& subString);
[7398]44  bool operator==(const SubString& subString) const;
45  bool compare(const SubString& subString) const;
46  bool compare(const SubString& subString, unsigned int length) const;
[7319]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. ///////
[7474]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');
[7320]58  std::string join(const std::string& delimiter = " ") const;
[7319]59  ////////////////////////////////////////
[6648]60
[7319]61  // retrieve a SubSet from the String
62  SubString getSubSet(unsigned int subSetBegin) const;
63  SubString getSubSet(unsigned int subSetBegin, unsigned int subSetEnd) const;
[4597]64
[7319]65  // retrieve Information from within
[7340]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 */
[7319]69  inline unsigned int size() const { return this->strings.size(); };
[7340]70  /** @param i the i'th String @returns the i'th string from the subset of Strings */
[7474]71const std::string& operator[](unsigned int i) const { return (i < this->strings.size()) ? this->strings[i] : emptyString;return this->getString(i); };
[7340]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]; };
[7319]74
75  // the almighty algorithm.
[7320]76  static SPLIT_LINE_STATE splitLine(std::vector<std::string>& ret,
77                                    const std::string& line,
[7325]78                                    const std::string& delimiters = SubString::WhiteSpaces,
[7474]79                                    const std::string& delimiterNeighbours = "",
80                                    bool emptyEntries = false,
[7320]81                                    char escape_char = '\\',
82                                    char safemode_char = '"',
83                                    char comment_char = '\0',
[7221]84                                    SPLIT_LINE_STATE start_state = SL_NORMAL);
[7319]85  // debugging.
[4833]86  void debug() const;
87
[7325]88public:
89  static const std::string WhiteSpaces;
90  static const std::string WhiteSpacesWithComma;
91
[7319]92private:
93  std::vector<std::string>  strings;                      //!< strings produced from a single string splitted in multiple strings
[7221]94
[7319]95  static const std::string emptyString;
[3941]96};
97
98#endif /* _SUBSTRING_H */
Note: See TracBrowser for help on using the repository browser.