Main MRPT website > C++ reference for MRPT 1.5.7
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  // This must be added to any CSerializable derived class:
22 
23  /** A class for storing a list of text lines.
24  * This class is optimized for managing strings on a "per-line" basis, although methods are also provided to obtain/set the
25  * whole string list as a single, multi-line string.
26  * There are methods for saving and loading to/from text files.
27  * You can access to lines directly by "CStringList::get" or through the operator "CStringList::operator ()". The later can be
28  * used both, to read and to write elements.
29  * Also methods are provided for accesing the text by key if they are formated as "key=value" lines.
30  * \ingroup mrpt_base_grp
31  */
32  class BASE_IMPEXP CStringList : public mrpt::utils::CSerializable
33  {
34  // This must be added to any CSerializable derived class:
36 
37  protected:
38  /** The internal list of strings
39  */
40  std::deque<std::string> m_strings;
41 
42  public:
43  /** Default constructor (empty string list)
44  */
45  CStringList();
46 
47  /** Constructor from a text
48  */
49  CStringList(const std::string& text);
50 
51  /** Explicit constructor from deque<string> */
52  explicit CStringList(const std::deque<std::string>& lines) : m_strings(lines) { }
53 
54  /** Explicit constructor from vector<string> */
55  explicit CStringList(const std::vector<std::string>& lines)
56  {
57  std::copy(lines.begin(),lines.end(),std::back_inserter(m_strings));
58  }
59 
60  /** Appends a new string at the end of the string list.
61  * \sa insert,set
62  */
63  void add( const std::string &str );
64 
65  /** An alternative way of adding strings to the list */
66  CStringList & operator << (const std::string &s) { add(s); return *this; }
67 
68  /** Inserts a new item at a given position (0=insert at the beggining,1=put into the second position,...)
69  * \sa add,set
70  */
71  void insert( size_t index, const std::string &str );
72 
73  /** Overwrites an existing position with a new value (0=first elements)
74  * \sa insert
75  */
76  void set( size_t index, const std::string &str );
77 
78  /** Clear the whole list
79  */
80  void clear();
81 
82  /** Returns the number of text lines in the list
83  */
84  size_t size() const;
85 
86  /** Delete the element at a given position (0=first element)
87  */
88  void remove(size_t index);
89 
90  /** Looks for a given string in the list, and returns its index, or returns "false" otherwise.
91  * \return true if string has been found.
92  */
93  bool find(
94  const std::string &compareText,
95  size_t foundIndex,
96  bool caseSensitive = true) const;
97 
98  /** Returns one string from the line list
99  */
100  void get(size_t index, std::string &outText) const;
101 
102  /** Returns one string from the line list
103  */
104  std::string operator ()(size_t index) const;
105 
106  /** Returns a reference to one string from the line list
107  */
108  std::string& operator ()(size_t index);
109 
110  /** Returns the whole string list as a single string with '\r\n' characters for newlines.
111  */
112  void getText(std::string &outText) const;
113 
114  /** Returns the whole string list as a single string with '\r\n' characters for newlines.
115  */
116  inline std::string getText() const
117  {
118  std::string s;
119  getText(s);
120  return s;
121  }
122 
123  /** Fills the string list by parsing a single string with '\r', '\n', or '\r\n' characters indicatng newlines.
124  */
125  void setText(const std::string &inText);
126 
127  /** Load the string list from a file.
128  */
129  void loadFromFile(const std::string &fileName);
130 
131  /** Save the string list to a file.
132  */
133  void saveToFile(const std::string &fileName) const;
134 
135  /** Returns the value of the given key ("key=value").
136  * \exception std::exception If the key is not found in the string list.
137  */
138  std::string get_string( const std::string &keyName );
139 
140  /** Returns the value of the given key ("key=value").
141  * \exception std::exception If the key is not found in the string list.
142  */
143  float get_float( const std::string &keyName );
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  int get_int( 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  double get_double( 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  bool get_bool( const std::string &keyName );
159 
160  /** Sets the value of a given key ("key=value"), overwritten previous value if it existed.
161  */
162  void set( const std::string &keyName, const std::string &value );
163 
164  /** Sets the value of a given key ("key=value"), overwritten previous value if it existed.
165  */
166  void set( const std::string &keyName, const int &value );
167 
168  /** Sets the value of a given key ("key=value"), overwritten previous value if it existed.
169  */
170  void set( const std::string &keyName, const float &value );
171 
172  /** Sets the value of a given key ("key=value"), overwritten previous value if it existed.
173  */
174  void set( const std::string &keyName, const double &value );
175 
176  /** Sets the value of a given key ("key=value"), overwritten previous value if it existed.
177  */
178  void set( const std::string &keyName, const bool &value );
179 
180  };
182 
183  } // End of namespace
184 } // End of namespace
185 
186 #endif
CStringList(const std::vector< std::string > &lines)
Explicit constructor from vector<string>
Definition: CStringList.h:55
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:39
std::string getText() const
Returns the whole string list as a single string with &#39; &#39; characters for newlines.
Definition: CStringList.h:116
const_iterator find(const KEY &key) const
Definition: ts_hash_map.h:139
GLdouble s
Definition: glext.h:3602
void clear()
Clear the contents of this container.
Definition: ts_hash_map.h:113
A class for storing a list of text lines.
Definition: CStringList.h:32
GLuint index
Definition: glext.h:3891
#define DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE(class_name, base_name)
This declaration must be inserted in all CSerializable classes definition, before the class declarati...
CStream BASE_IMPEXP & operator<<(mrpt::utils::CStream &s, const char *a)
Definition: CStream.cpp:130
GLsizei const GLchar ** string
Definition: glext.h:3919
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...
#define DEFINE_SERIALIZABLE_POST_CUSTOM_BASE(class_name, base_name)
GLsizei const GLfloat * value
Definition: glext.h:3929
GLsizeiptr size
Definition: glext.h:3779
std::deque< std::string > m_strings
The internal list of strings.
Definition: CStringList.h:40
CStringList(const std::deque< std::string > &lines)
Explicit constructor from deque<string>
Definition: CStringList.h:52



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 8277875f6 Mon Jun 11 02:47:32 2018 +0200 at lun oct 28 01:50:49 CET 2019