Main MRPT website > C++ reference
MRPT logo
CStringList.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef CStringList_H
29 #define CStringList_H
30 
31 #include <mrpt/utils/utils_defs.h>
33 
34 namespace mrpt
35 {
36  namespace utils
37  {
38  // This must be added to any CSerializable derived class:
40 
41  /** A class for storing a list of text lines.
42  * This class is optimized for managing strings on a "per-line" basis, although methods are also provided to obtain/set the
43  * whole string list as a single, multi-line string.
44  * There are methods for saving and loading to/from text files.
45  * You can access to lines directly by "CStringList::get" or through the operator "CStringList::operator ()". The later can be
46  * used both, to read and to write elements.
47  * Also methods are provided for accesing the text by key if they are formated as "key=value" lines.
48  * \ingroup mrpt_base_grp
49  */
50  class BASE_IMPEXP CStringList : public mrpt::utils::CSerializable
51  {
52  // This must be added to any CSerializable derived class:
54 
55  protected:
56  /** The internal list of strings
57  */
58  std::deque<std::string> m_strings;
59 
60  public:
61  /** Default constructor (empty string list)
62  */
63  CStringList();
64 
65  /** Constructor from a text
66  */
67  CStringList(const std::string& text);
68 
69  /** Explicit constructor from deque<string> */
70  explicit CStringList(const std::deque<std::string>& lines) : m_strings(lines) { }
71 
72  /** Explicit constructor from vector<string> */
73  explicit CStringList(const std::vector<std::string>& lines)
74  {
75  std::copy(lines.begin(),lines.end(),std::back_inserter(m_strings));
76  }
77 
78  /** Appends a new string at the end of the string list.
79  * \sa insert,set
80  */
81  void add( const std::string &str );
82 
83  /** An alternative way of adding strings to the list */
84  CStringList & operator << (const std::string &s) { add(s); return *this; }
85 
86  /** Inserts a new item at a given position (0=insert at the beggining,1=put into the second position,...)
87  * \sa add,set
88  */
89  void insert( size_t index, const std::string &str );
90 
91  /** Overwrites an existing position with a new value (0=first elements)
92  * \sa insert
93  */
94  void set( size_t index, const std::string &str );
95 
96  /** Clear the whole list
97  */
98  void clear();
99 
100  /** Returns the number of text lines in the list
101  */
102  size_t size() const;
103 
104  /** Delete the element at a given position (0=first element)
105  */
106  void remove(size_t index);
107 
108  /** Looks for a given string in the list, and returns its index, or returns "false" otherwise.
109  * \return true if string has been found.
110  */
111  bool find(
112  const std::string &compareText,
113  size_t foundIndex,
114  bool caseSensitive = true) const;
115 
116  /** Returns one string from the line list
117  */
118  void get(size_t index, std::string &outText) const;
119 
120  /** Returns one string from the line list
121  */
122  std::string operator ()(size_t index) const;
123 
124  /** Returns a reference to one string from the line list
125  */
126  std::string& operator ()(size_t index);
127 
128  /** Returns the whole string list as a single string with '\r\n' characters for newlines.
129  */
130  void getText(std::string &outText) const;
131 
132  /** Returns the whole string list as a single string with '\r\n' characters for newlines.
133  */
134  inline std::string getText() const
135  {
136  std::string s;
137  getText(s);
138  return s;
139  }
140 
141  /** Fills the string list by parsing a single string with '\r', '\n', or '\r\n' characters indicatng newlines.
142  */
143  void setText(const std::string &inText);
144 
145  /** Load the string list from a file.
146  */
147  void loadFromFile(const std::string &fileName);
148 
149  /** Save the string list to a file.
150  */
151  void saveToFile(const std::string &fileName) const;
152 
153  /** Returns the value of the given key ("key=value").
154  * \exception std::exception If the key is not found in the string list.
155  */
156  std::string get_string( const std::string &keyName );
157 
158  /** Returns the value of the given key ("key=value").
159  * \exception std::exception If the key is not found in the string list.
160  */
161  float get_float( const std::string &keyName );
162 
163  /** Returns the value of the given key ("key=value").
164  * \exception std::exception If the key is not found in the string list.
165  */
166  int get_int( const std::string &keyName );
167 
168  /** Returns the value of the given key ("key=value").
169  * \exception std::exception If the key is not found in the string list.
170  */
171  double get_double( const std::string &keyName );
172 
173  /** Returns the value of the given key ("key=value").
174  * \exception std::exception If the key is not found in the string list.
175  */
176  bool get_bool( const std::string &keyName );
177 
178  /** Sets the value of a given key ("key=value"), overwritten previous value if it existed.
179  */
180  void set( const std::string &keyName, const std::string &value );
181 
182  /** Sets the value of a given key ("key=value"), overwritten previous value if it existed.
183  */
184  void set( const std::string &keyName, const int &value );
185 
186  /** Sets the value of a given key ("key=value"), overwritten previous value if it existed.
187  */
188  void set( const std::string &keyName, const float &value );
189 
190  /** Sets the value of a given key ("key=value"), overwritten previous value if it existed.
191  */
192  void set( const std::string &keyName, const double &value );
193 
194  /** Sets the value of a given key ("key=value"), overwritten previous value if it existed.
195  */
196  void set( const std::string &keyName, const bool &value );
197 
198  };
199 
200  } // End of namespace
201 } // End of namespace
202 
203 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013