Main MRPT website > C++ reference for MRPT 1.9.9
CStringList.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #ifndef CStringList_H
10 #define CStringList_H
11 
13 #include <deque>
14 #include <iterator>
15 
16 namespace mrpt
17 {
18 namespace utils
19 {
20 /** A class for storing a list of text lines.
21  * This class is optimized for managing strings on a "per-line" basis,
22  * although methods are also provided to obtain/set the
23  * whole string list as a single, multi-line string.
24  * There are methods for saving and loading to/from text files.
25  * You can access to lines directly by "CStringList::get" or through the
26  * operator "CStringList::operator ()". The later can be
27  * used both, to read and to write elements.
28  * Also methods are provided for accesing the text by key if they are formated
29  * as "key=value" lines.
30  * \ingroup mrpt_base_grp
31  */
33 {
35 
36  protected:
37  /** The internal list of strings
38  */
39  std::deque<std::string> m_strings;
40 
41  public:
42  /** Default constructor (empty string list)
43  */
44  CStringList();
45 
46  /** Constructor from a text
47  */
48  CStringList(const std::string& text);
49 
50  /** Explicit constructor from deque<string> */
51  explicit CStringList(const std::deque<std::string>& lines)
52  : m_strings(lines)
53  {
54  }
55 
56  /** Explicit constructor from vector<string> */
57  explicit CStringList(const std::vector<std::string>& lines)
58  {
59  std::copy(lines.begin(), lines.end(), std::back_inserter(m_strings));
60  }
61 
62  /** Appends a new string at the end of the string list.
63  * \sa insert,set
64  */
65  void add(const std::string& str);
66 
67  /** An alternative way of adding strings to the list */
69  {
70  add(s);
71  return *this;
72  }
73 
74  /** Inserts a new item at a given position (0=insert at the beggining,1=put
75  * into the second position,...)
76  * \sa add,set
77  */
78  void insert(size_t index, const std::string& str);
79 
80  /** Overwrites an existing position with a new value (0=first elements)
81  * \sa insert
82  */
83  void set(size_t index, const std::string& str);
84 
85  /** Clear the whole list
86  */
87  void clear();
88 
89  /** Returns the number of text lines in the list
90  */
91  size_t size() const;
92 
93  /** Delete the element at a given position (0=first element)
94  */
95  void remove(size_t index);
96 
97  /** Looks for a given string in the list, and returns its index, or returns
98  * "false" otherwise.
99  * \return true if string has been found.
100  */
101  bool find(
102  const std::string& compareText, size_t foundIndex,
103  bool caseSensitive = true) const;
104 
105  /** Returns one string from the line list
106  */
107  void get(size_t index, std::string& outText) const;
108 
109  /** Returns one string from the line list
110  */
111  std::string operator()(size_t index) const;
112 
113  /** Returns a reference to one string from the line list
114  */
115  std::string& operator()(size_t index);
116 
117  /** Returns the whole string list as a single string with '\r\n' characters
118  * for newlines.
119  */
120  void getText(std::string& outText) const;
121 
122  /** Returns the whole string list as a single string with '\r\n' characters
123  * for newlines.
124  */
125  inline std::string getText() const
126  {
127  std::string s;
128  getText(s);
129  return s;
130  }
131 
132  /** Fills the string list by parsing a single string with '\r', '\n', or
133  * '\r\n' characters indicatng newlines.
134  */
135  void setText(const std::string& inText);
136 
137  /** Load the string list from a file.
138  */
139  void loadFromFile(const std::string& fileName);
140 
141  /** Save the string list to a file.
142  */
143  void saveToFile(const std::string& fileName) const;
144 
145  /** Returns the value of the given key ("key=value").
146  * \exception std::exception If the key is not found in the string list.
147  */
148  std::string get_string(const std::string& keyName);
149 
150  /** Returns the value of the given key ("key=value").
151  * \exception std::exception If the key is not found in the string list.
152  */
153  float get_float(const std::string& keyName);
154 
155  /** Returns the value of the given key ("key=value").
156  * \exception std::exception If the key is not found in the string list.
157  */
158  int get_int(const std::string& keyName);
159 
160  /** Returns the value of the given key ("key=value").
161  * \exception std::exception If the key is not found in the string list.
162  */
163  double get_double(const std::string& keyName);
164 
165  /** Returns the value of the given key ("key=value").
166  * \exception std::exception If the key is not found in the string list.
167  */
168  bool get_bool(const std::string& keyName);
169 
170  /** Sets the value of a given key ("key=value"), overwritten previous value
171  * if it existed.
172  */
173  void set(const std::string& keyName, const std::string& value);
174 
175  /** Sets the value of a given key ("key=value"), overwritten previous value
176  * if it existed.
177  */
178  void set(const std::string& keyName, const int& value);
179 
180  /** Sets the value of a given key ("key=value"), overwritten previous value
181  * if it existed.
182  */
183  void set(const std::string& keyName, const float& value);
184 
185  /** Sets the value of a given key ("key=value"), overwritten previous value
186  * if it existed.
187  */
188  void set(const std::string& keyName, const double& value);
189 
190  /** Sets the value of a given key ("key=value"), overwritten previous value
191  * if it existed.
192  */
193  void set(const std::string& keyName, const bool& value);
194 };
195 
196 } // End of namespace
197 } // End of namespace
198 
199 #endif
void insert(size_t index, const std::string &str)
Inserts a new item at a given position (0=insert at the beggining,1=put into the second position...
Definition: CStringList.cpp:47
CStringList()
Default constructor (empty string list)
Definition: CStringList.cpp:29
CStringList(const std::vector< std::string > &lines)
Explicit constructor from vector<string>
Definition: CStringList.h:57
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:44
bool find(const std::string &compareText, size_t foundIndex, bool caseSensitive=true) const
Looks for a given string in the list, and returns its index, or returns "false" otherwise.
Definition: CStringList.cpp:90
bool get_bool(const std::string &keyName)
Returns the value of the given key ("key=value").
float get_float(const std::string &keyName)
Returns the value of the given key ("key=value").
std::string getText() const
Returns the whole string list as a single string with &#39; &#39; characters for newlines.
Definition: CStringList.h:125
GLdouble s
Definition: glext.h:3676
std::string get_string(const std::string &keyName)
Returns the value of the given key ("key=value").
std::string operator()(size_t index) const
Returns one string from the line list.
A class for storing a list of text lines.
Definition: CStringList.h:32
GLuint index
Definition: glext.h:4054
void clear()
Clear the whole list.
Definition: CStringList.cpp:73
void setText(const std::string &inText)
Fills the string list by parsing a single string with &#39;&#39;, &#39; &#39;, or &#39; &#39; characters indicatng newlines...
CStringList & operator<<(const std::string &s)
An alternative way of adding strings to the list.
Definition: CStringList.h:68
GLsizei const GLchar ** string
Definition: glext.h:4101
size_t size() const
Returns the number of text lines in the list.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
double get_double(const std::string &keyName)
Returns the value of the given key ("key=value").
int get_int(const std::string &keyName)
Returns the value of the given key ("key=value").
GLsizei const GLfloat * value
Definition: glext.h:4117
void saveToFile(const std::string &fileName) const
Save the string list to a file.
std::deque< std::string > m_strings
The internal list of strings.
Definition: CStringList.h:39
CStringList(const std::deque< std::string > &lines)
Explicit constructor from deque<string>
Definition: CStringList.h:51
void loadFromFile(const std::string &fileName)
Load the string list from a file.
void add(const std::string &str)
Appends a new string at the end of the string list.
Definition: CStringList.cpp:43



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019