Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7434 was 7398, checked in by bensch, 19 years ago

orxonox/trunk: SubString more functionality

File size: 3.7 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
[7319]12typedef enum {
13  SL_NORMAL,
14  SL_ESCAPE,
15  SL_SAFEMODE,
16  SL_SAFEESCAPE,
17  SL_COMMENT,
18} SPLIT_LINE_STATE;
[7221]19
[4482]20//! A class that can load one string and split it in multipe ones
[7319]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 */
[3941]25class SubString
26{
[7319]27public:
[7323]28  SubString();
29  SubString(const std::string& string, char splitter = ',');
[7221]30  SubString(const std::string& string, const std::string& splitters, char escapeChar ='\\', char safemode_char = '"', char comment_char = '\0');
[7319]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);
[4220]35  ~SubString();
[4597]36
[7319]37  // operate on the SubString
38  SubString& operator=(const SubString& subString);
[7398]39  bool operator==(const SubString& subString) const;
40  bool compare(const SubString& subString) const;
41  bool compare(const SubString& subString, unsigned int length) const;
[7319]42  SubString operator+(const SubString& subString) const;
43  SubString& operator+=(const SubString& subString);
44  /** @param subString the String to append @returns appended String. @brief added for convenience */
45  SubString& append(const SubString subString) { return (*this += subString); };
46
47  /////////////////////////////////////////
48  // Split and Join the any String. ///////
[7221]49  unsigned int split(const std::string& string = "", char splitter = ',');
50  unsigned int split(const std::string& string, const std::string& splitters, char escapeChar ='\\', char safemode_char = '"', char comment_char = '\0');
[7320]51  std::string join(const std::string& delimiter = " ") const;
[7319]52  ////////////////////////////////////////
[6648]53
[7319]54  // retrieve a SubSet from the String
55  SubString getSubSet(unsigned int subSetBegin) const;
56  SubString getSubSet(unsigned int subSetBegin, unsigned int subSetEnd) const;
[4597]57
[7319]58  // retrieve Information from within
[7340]59  /** @returns true if the SubString is empty */
60  inline bool empty() const { return this->strings.empty(); };
61  /** @returns the count of Strings stored in this substring */
[7319]62  inline unsigned int size() const { return this->strings.size(); };
[7340]63  /** @param i the i'th String @returns the i'th string from the subset of Strings */
64  const std::string& operator[](unsigned int i) const { return (i < this->strings.size()) ? this->strings[i] : emptyString;return this->getString(i); };
65  /** @param i the i'th String @returns the i'th string from the subset of Strings */
66  const std::string& getString(unsigned int i) const { return (*this)[i]; };
[7319]67
68  // the almighty algorithm.
[7320]69  static SPLIT_LINE_STATE splitLine(std::vector<std::string>& ret,
70                                    const std::string& line,
[7325]71                                    const std::string& delimiters = SubString::WhiteSpaces,
[7320]72                                    char escape_char = '\\',
73                                    char safemode_char = '"',
74                                    char comment_char = '\0',
[7221]75                                    SPLIT_LINE_STATE start_state = SL_NORMAL);
[7319]76  // debugging.
[4833]77  void debug() const;
78
[7325]79public:
80  static const std::string WhiteSpaces;
81  static const std::string WhiteSpacesWithComma;
82
[7319]83private:
84  std::vector<std::string>  strings;                      //!< strings produced from a single string splitted in multiple strings
[7221]85
[7319]86  static const std::string emptyString;
[3941]87};
88
89#endif /* _SUBSTRING_H */
Note: See TracBrowser for help on using the repository browser.