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



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020