Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/lib/util/substring.h @ 7793

Last change on this file since 7793 was 7340, checked in by bensch, 19 years ago

orxonox/trunk: fixed a ShellCommand-bug

File size: 3.6 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
12typedef 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/**
22 * SubString is a very Powerfull way to create a SubSet from a String
23 * It can be used, to Split strings append them and join them again.
24 */
25class SubString
26{
27public:
28  SubString();
29  SubString(const std::string& string, char splitter = ',');
30  SubString(const std::string& string, const std::string& splitters, char escapeChar ='\\', char safemode_char = '"', char comment_char = '\0');
31  /** @brief create a Substring as a copy of another one. @param subString the SubString to copy. */
32  SubString(const SubString& subString) { *this = subString; };
33  SubString(const SubString& subString, unsigned int subSetBegin);
34  SubString(const SubString& subString, unsigned int subSetBegin, unsigned int subSetEnd);
35  ~SubString();
36
37  // operate on the SubString
38  SubString& operator=(const SubString& subString);
39  bool operator==(const SubString& subString);
40  SubString operator+(const SubString& subString) const;
41  SubString& operator+=(const SubString& subString);
42  /** @param subString the String to append @returns appended String. @brief added for convenience */
43  SubString& append(const SubString subString) { return (*this += subString); };
44
45  /////////////////////////////////////////
46  // Split and Join the any String. ///////
47  unsigned int split(const std::string& string = "", char splitter = ',');
48  unsigned int split(const std::string& string, const std::string& splitters, char escapeChar ='\\', char safemode_char = '"', char comment_char = '\0');
49  std::string join(const std::string& delimiter = " ") const;
50  ////////////////////////////////////////
51
52  // retrieve a SubSet from the String
53  SubString getSubSet(unsigned int subSetBegin) const;
54  SubString getSubSet(unsigned int subSetBegin, unsigned int subSetEnd) const;
55
56  // retrieve Information from within
57  /** @returns true if the SubString is empty */
58  inline bool empty() const { return this->strings.empty(); };
59  /** @returns the count of Strings stored in this substring */
60  inline unsigned int size() const { return this->strings.size(); };
61  /** @param i the i'th String @returns the i'th string from the subset of Strings */
62  const std::string& operator[](unsigned int i) const { return (i < this->strings.size()) ? this->strings[i] : emptyString;return this->getString(i); };
63  /** @param i the i'th String @returns the i'th string from the subset of Strings */
64  const std::string& getString(unsigned int i) const { return (*this)[i]; };
65
66  // the almighty algorithm.
67  static SPLIT_LINE_STATE splitLine(std::vector<std::string>& ret,
68                                    const std::string& line,
69                                    const std::string& delimiters = SubString::WhiteSpaces,
70                                    char escape_char = '\\',
71                                    char safemode_char = '"',
72                                    char comment_char = '\0',
73                                    SPLIT_LINE_STATE start_state = SL_NORMAL);
74  // debugging.
75  void debug() const;
76
77public:
78  static const std::string WhiteSpaces;
79  static const std::string WhiteSpacesWithComma;
80
81private:
82  std::vector<std::string>  strings;                      //!< strings produced from a single string splitted in multiple strings
83
84  static const std::string emptyString;
85};
86
87#endif /* _SUBSTRING_H */
Note: See TracBrowser for help on using the repository browser.