Main MRPT website > C++ reference for MRPT 1.9.9
CStringList.cpp
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 
10 #include "base-precomp.h" // Precompiled headers
11 
12 #include <mrpt/utils/CStringList.h>
15 #include <mrpt/system/filesystem.h>
17 #include <mrpt/system/os.h>
18 
19 using namespace mrpt::utils;
20 using namespace mrpt::system;
21 using namespace std;
22 
23 // This must be added to any CSerializable class implementation file.
25 
26 /*---------------------------------------------------------------
27  Constructor
28  ---------------------------------------------------------------*/
30 /*---------------------------------------------------------------
31  Constructor
32  ---------------------------------------------------------------*/
33 CStringList::CStringList(const string& text)
34 {
36  setText(text);
37  MRPT_END
38 }
39 
40 /*---------------------------------------------------------------
41  add
42  ---------------------------------------------------------------*/
43 void CStringList::add(const string& str) { m_strings.push_back(str); }
44 /*---------------------------------------------------------------
45  insert
46  ---------------------------------------------------------------*/
47 void CStringList::insert(size_t index, const string& str)
48 {
50  if (index >= m_strings.size()) THROW_EXCEPTION("index out of bounds!");
51 
52  m_strings.insert(m_strings.begin() + index, str);
53 
54  MRPT_END
55 }
56 
57 /*---------------------------------------------------------------
58  set
59  ---------------------------------------------------------------*/
60 void CStringList::set(size_t index, const string& str)
61 {
63  if (index >= m_strings.size()) THROW_EXCEPTION("index out of bounds!");
64 
65  m_strings[index] = str;
66 
67  MRPT_END
68 }
69 
70 /*---------------------------------------------------------------
71  clear
72  ---------------------------------------------------------------*/
73 void CStringList::clear() { m_strings.clear(); }
74 /*---------------------------------------------------------------
75  remove
76  ---------------------------------------------------------------*/
78 {
80  if (index >= m_strings.size()) THROW_EXCEPTION("index out of bounds!");
81 
82  m_strings.erase(m_strings.begin() + index);
83 
84  MRPT_END
85 }
86 
87 /*---------------------------------------------------------------
88  find
89  ---------------------------------------------------------------*/
91  const string& compareText, size_t foundIndex, bool caseSensitive) const
92 {
94 
95  foundIndex = 0;
96  if (caseSensitive)
97  {
98  for (deque<string>::const_iterator it = m_strings.begin();
99  it != m_strings.end(); ++it, foundIndex++)
100  if (!os::_strcmp(compareText.c_str(), it->c_str())) return true;
101  }
102  else
103  {
104  for (deque<string>::const_iterator it = m_strings.begin();
105  it != m_strings.end(); ++it, foundIndex++)
106  if (!os::_strcmpi(compareText.c_str(), it->c_str())) return true;
107  }
108 
109  return false;
110  MRPT_END
111 }
112 
113 /*---------------------------------------------------------------
114  getText
115  ---------------------------------------------------------------*/
116 void CStringList::getText(string& outText) const
117 {
118  MRPT_START
120  size_t curPos = 0, totalLen = 0;
121 
122  // 1) Compute overall length, including 2 chars per new-line:
123  // ----------------------------------------------------------------
124  for (it = m_strings.begin(); it != m_strings.end(); it++)
125  totalLen += it->size() + 2;
126 
127  outText.resize(totalLen);
128 
129  // 2) Copy the text out:
130  // ----------------------------------------------------------------
131  for (it = m_strings.begin(); it != m_strings.end(); it++)
132  {
133  os::memcpy(&outText[curPos], totalLen, it->c_str(), it->size());
134  curPos += it->size();
135  outText[curPos++] = '\r';
136  outText[curPos++] = '\n';
137  }
138 
139  MRPT_END
140 }
141 
142 /*---------------------------------------------------------------
143  setText
144  ---------------------------------------------------------------*/
145 void CStringList::setText(const string& inText)
146 {
147  MRPT_START
148  mrpt::system::tokenize(inText, "\r\n", m_strings);
149  MRPT_END
150 }
151 
152 /*---------------------------------------------------------------
153  loadFromFile
154  ---------------------------------------------------------------*/
155 void CStringList::loadFromFile(const string& fileName)
156 {
157  MRPT_START
158 
160 
161  CFileInputStream fil(fileName.c_str());
162 
163  // Load the whole file into a string:
164  size_t nBytes = fil.getTotalBytesCount();
165  string wholeStr;
166  wholeStr.resize(nBytes);
167 
168  // "Rewind" :-)
169  fil.Seek(0);
170  if (nBytes != fil.ReadBuffer(&wholeStr[0], nBytes))
171  THROW_EXCEPTION("Error reading text from file!");
172 
173  // Parse:
174  setText(wholeStr);
175 
176  MRPT_END
177 }
178 
179 /*---------------------------------------------------------------
180  saveToFile
181  ---------------------------------------------------------------*/
182 void CStringList::saveToFile(const string& fileName) const
183 {
184  MRPT_START
185 
186  CFileOutputStream fil(fileName.c_str());
188 
189  for (it = m_strings.begin(); it != m_strings.end(); ++it)
190  {
191  fil.WriteBuffer(it->c_str(), it->size());
192  fil.WriteBuffer("\r\n", 2);
193  }
194 
195  MRPT_END
196 }
197 
198 /*---------------------------------------------------------------
199  writeToStream
200  ---------------------------------------------------------------*/
202  mrpt::utils::CStream& out, int* out_Version) const
203 {
204  if (out_Version)
205  *out_Version = 0;
206  else
207  {
208  uint32_t i, N = (uint32_t)m_strings.size();
209 
210  out << N;
211 
212  for (i = 0; i < N; i++) out << m_strings[i];
213  }
214 }
215 
216 /*---------------------------------------------------------------
217  readFromStream
218  ---------------------------------------------------------------*/
220 {
221  switch (version)
222  {
223  case 0:
224  {
225  uint32_t i, N;
226 
227  in >> N;
228 
229  m_strings.resize(N);
230 
231  for (i = 0; i < N; i++) in >> m_strings[i];
232  }
233  break;
234  default:
236  };
237 }
238 
239 /*---------------------------------------------------------------
240  size
241  ---------------------------------------------------------------*/
242 size_t CStringList::size() const { return m_strings.size(); }
243 /*---------------------------------------------------------------
244  get
245  ---------------------------------------------------------------*/
246 void CStringList::get(size_t index, string& outText) const
247 {
248  MRPT_START
249  if (index >= m_strings.size()) THROW_EXCEPTION("index out of bounds!");
250 
251  outText = m_strings[index];
252 
253  MRPT_END
254 }
255 
256 /*---------------------------------------------------------------
257  operator ()
258  ---------------------------------------------------------------*/
259 string CStringList::operator()(size_t index) const
260 {
261  MRPT_START
262  if (index >= m_strings.size()) THROW_EXCEPTION("index out of bounds!");
263 
264  return m_strings[index];
265  MRPT_END
266 }
267 
268 /*---------------------------------------------------------------
269  operator ()
270  ---------------------------------------------------------------*/
272 {
273  MRPT_START
274  if (index >= m_strings.size()) THROW_EXCEPTION("index out of bounds!");
275 
276  return m_strings[index];
277  MRPT_END
278 }
279 
280 /*---------------------------------------------------------------
281  get_string
282  ---------------------------------------------------------------*/
283 string CStringList::get_string(const string& keyName)
284 {
285  MRPT_START
286  string strToLookFor(keyName + string("="));
287  size_t idx = string::npos;
288 
289  for (deque<string>::iterator it = m_strings.begin(); it != m_strings.end();
290  ++it)
291  {
292  idx = it->find(strToLookFor);
293  if (idx == 0) return it->substr(strToLookFor.size());
294  }
295 
296  THROW_EXCEPTION(format("Key '%s' not found!", keyName.c_str()));
297 
298  MRPT_END
299 }
300 
301 /*---------------------------------------------------------------
302  get_float
303  ---------------------------------------------------------------*/
304 float CStringList::get_float(const string& keyName)
305 {
306  MRPT_START
307  string s(get_string(keyName));
308  return (float)atof(s.c_str());
309  MRPT_END
310 }
311 
312 /*---------------------------------------------------------------
313  get_int
314  ---------------------------------------------------------------*/
315 int CStringList::get_int(const string& keyName)
316 {
317  MRPT_START
318  string s(get_string(keyName));
319  return atoi(s.c_str());
320  MRPT_END
321 }
322 
323 /*---------------------------------------------------------------
324  get_double
325  ---------------------------------------------------------------*/
326 double CStringList::get_double(const string& keyName)
327 {
328  MRPT_START
329  string s(get_string(keyName));
330  return atof(s.c_str());
331  MRPT_END
332 }
333 
334 /*---------------------------------------------------------------
335  get_bool
336  ---------------------------------------------------------------*/
337 bool CStringList::get_bool(const string& keyName)
338 {
339  MRPT_START
340  string s(get_string(keyName));
341  return atoi(s.c_str()) != 0;
342  MRPT_END
343 }
344 
345 /*---------------------------------------------------------------
346  set
347  ---------------------------------------------------------------*/
348 void CStringList::set(const string& keyName, const string& value)
349 {
350  MRPT_START
351  string strToLookFor(keyName + string("="));
352  size_t idx = string::npos;
353 
354  for (deque<string>::iterator it = m_strings.begin(); it != m_strings.end();
355  ++it)
356  {
357  idx = it->find(strToLookFor);
358  if (idx == 0)
359  {
360  // Replace existing string:
361  (*it) = strToLookFor + value;
362  return;
363  }
364  }
365 
366  // It is a new key: Append it!
367  m_strings.push_back(strToLookFor + value);
368 
369  MRPT_END
370 }
371 /*---------------------------------------------------------------
372  set
373  ---------------------------------------------------------------*/
374 void CStringList::set(const string& keyName, const int& value)
375 {
376  MRPT_START
377  set(keyName, format("%i", value));
378  MRPT_END
379 }
380 /*---------------------------------------------------------------
381  set
382  ---------------------------------------------------------------*/
383 void CStringList::set(const string& keyName, const float& value)
384 {
385  MRPT_START
386  set(keyName, format("%.10e", value));
387  MRPT_END
388 }
389 
390 /*---------------------------------------------------------------
391  set
392  ---------------------------------------------------------------*/
393 void CStringList::set(const string& keyName, const double& value)
394 {
395  MRPT_START
396  set(keyName, format("%.16e", value));
397  MRPT_END
398 }
399 
400 /*---------------------------------------------------------------
401  set
402  ---------------------------------------------------------------*/
403 void CStringList::set(const string& keyName, const bool& value)
404 {
405  MRPT_START
406  set(keyName, string(value ? "1" : "0"));
407  MRPT_END
408 }
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
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
CStringList()
Default constructor (empty string list)
Definition: CStringList.cpp:29
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:30
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:44
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
void set(size_t index, const std::string &str)
Overwrites an existing position with a new value (0=first elements)
Definition: CStringList.cpp:60
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
#define THROW_EXCEPTION(msg)
Scalar * iterator
Definition: eigen_plugins.h:26
bool fileExists(const std::string &fileName)
Test if a given file (or directory) exists.
Definition: filesystem.cpp:127
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").
STL namespace.
std::string getText() const
Returns the whole string list as a single string with &#39; &#39; characters for newlines.
Definition: CStringList.h:125
const Scalar * const_iterator
Definition: eigen_plugins.h:27
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.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
A class for storing a list of text lines.
Definition: CStringList.h:32
void remove(size_t index)
Delete the element at a given position (0=first element)
Definition: CStringList.cpp:77
#define MRPT_END
This CStream derived class allow using a file as a write-only, binary stream.
GLuint index
Definition: glext.h:4054
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
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...
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:19
GLsizei const GLchar ** string
Definition: glext.h:4101
size_t size() const
Returns the number of text lines in the list.
void tokenize(const std::string &inString, const std::string &inDelimiters, std::deque< std::string > &outTokens, bool skipBlankTokens=true) noexcept
Tokenizes a string according to a set of delimiting characters.
#define MRPT_START
GLuint in
Definition: glext.h:7274
#define ASSERT_(f)
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").
void readFromStream(mrpt::utils::CStream &in, int version) override
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
GLsizei const GLfloat * value
Definition: glext.h:4117
This CStream derived class allow using a file as a read-only, binary stream.
void get(size_t index, std::string &outText) const
Returns one string from the line list.
void saveToFile(const std::string &fileName) const
Save the string list to a file.
unsigned __int32 uint32_t
Definition: rptypes.h:47
uint64_t getTotalBytesCount() override
Method for getting the total number of bytes in the buffer.
int _strcmp(const char *str1, const char *str2) noexcept
An OS-independent version of strcmp.
Definition: os.cpp:311
void loadFromFile(const std::string &fileName)
Load the string list from a file.
void memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) noexcept
An OS and compiler independent version of "memcpy".
Definition: os.cpp:355
void add(const std::string &str)
Appends a new string at the end of the string list.
Definition: CStringList.cpp:43
int _strcmpi(const char *str1, const char *str2) noexcept
An OS-independent version of strcmpi.
Definition: os.cpp:319



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