Main MRPT website > C++ reference
MRPT logo

CStringList.h

Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                   http://mrpt.sourceforge.net/                            |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef CStringList_H
00029 #define CStringList_H
00030 
00031 #include <mrpt/utils/utils_defs.h>
00032 #include <mrpt/utils/CSerializable.h>
00033 
00034 namespace mrpt
00035 {
00036         namespace utils
00037         {
00038                 // This must be added to any CSerializable derived class:
00039                 DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE( CStringList, mrpt::utils::CSerializable )
00040 
00041                 /** A class for storing a list of text lines.
00042                   *  This class is optimized for managing strings on a "per-line" basis, although methods are also provided to obtain/set the
00043                   *   whole string list as a single, multi-line string.
00044                   *  There are methods for saving and loading to/from text files.
00045                   *  You can access to lines directly by "CStringList::get" or through the operator "CStringList::operator ()". The later can be
00046                   *   used both, to read and to write elements.
00047                   *  Also methods are provided for accesing the text by key if they are formated as "key=value" lines.
00048                  */
00049                 class BASE_IMPEXP CStringList : public mrpt::utils::CSerializable
00050                 {
00051                         // This must be added to any CSerializable derived class:
00052                         DEFINE_SERIALIZABLE( CStringList )
00053 
00054                 protected:
00055                         /** The internal list of strings
00056                           */
00057                         std::deque<std::string>         m_strings;
00058 
00059                 public:
00060                         /** Default constructor (empty string list)
00061                           */
00062                         CStringList();
00063 
00064                         /** Constructor from a text
00065                           */
00066                         CStringList(const std::string& text);
00067 
00068                         /** Appends a new string at the end of the string list.
00069                           * \sa insert,set
00070                           */
00071                         void  add( const std::string &str );
00072 
00073                         /** An alternative way of adding strings to the list */
00074                         CStringList & operator << (const std::string &s) { add(s); return *this; }
00075 
00076                         /** Inserts a new item at a given position (0=insert at the beggining,1=put into the second position,...)
00077                           * \sa add,set
00078                           */
00079                         void  insert( size_t index, const std::string &str );
00080 
00081                         /** Overwrites an existing position with a new value (0=first elements)
00082                           * \sa insert
00083                           */
00084                         void  set( size_t index, const std::string &str );
00085 
00086                         /** Clear the whole list
00087                           */
00088                         void  clear();
00089 
00090                         /** Returns the number of text lines in the list
00091                           */
00092                         size_t  size() const;
00093 
00094                         /** Delete the element at a given position (0=first element)
00095                           */
00096                         void  remove(size_t index);
00097 
00098                         /** Looks for a given string in the list, and returns its index, or returns "false" otherwise.
00099                           * \return true if string has been found.
00100                           */
00101                         bool  find(
00102                                 const std::string               &compareText,
00103                                 size_t                                  foundIndex,
00104                                 bool                                    caseSensitive = true) const;
00105 
00106                         /** Returns one string from the line list
00107                           */
00108                         void  get(size_t index, std::string &outText) const;
00109 
00110                         /** Returns one string from the line list
00111                           */
00112                         std::string  operator ()(size_t index) const;
00113 
00114                         /** Returns a reference to one string from the line list
00115                           */
00116                         std::string&  operator ()(size_t index);
00117 
00118                         /** Returns the whole string list as a single string with '\r\n' characters for newlines.
00119                           */
00120                         void  getText(std::string &outText) const;
00121 
00122                         /** Returns the whole string list as a single string with '\r\n' characters for newlines.
00123                           */
00124                         inline std::string getText() const
00125                         {
00126                                 std::string s;
00127                                 getText(s);
00128                                 return s;
00129                         }
00130 
00131                         /** Fills the string list by parsing a single string with '\r', '\n', or '\r\n' characters indicatng newlines.
00132                           */
00133                         void  setText(const std::string &inText);
00134 
00135                         /** Load the string list from a file.
00136                           */
00137                         void  loadFromFile(const std::string &fileName);
00138 
00139                         /** Save the string list to a file.
00140                           */
00141                         void  saveToFile(const std::string &fileName) const;
00142 
00143                         /** Returns the value of the given key ("key=value").
00144                           * \exception std::exception If the key is not found in the string list.
00145                           */
00146                         std::string  get_string( const std::string &keyName );
00147 
00148                         /** Returns the value of the given key ("key=value").
00149                           * \exception std::exception If the key is not found in the string list.
00150                           */
00151                         float  get_float( const std::string &keyName );
00152 
00153                         /** Returns the value of the given key ("key=value").
00154                           * \exception std::exception If the key is not found in the string list.
00155                           */
00156                         int  get_int( const std::string &keyName );
00157 
00158                         /** Returns the value of the given key ("key=value").
00159                           * \exception std::exception If the key is not found in the string list.
00160                           */
00161                         double  get_double( const std::string &keyName );
00162 
00163                         /** Returns the value of the given key ("key=value").
00164                           * \exception std::exception If the key is not found in the string list.
00165                           */
00166                         bool  get_bool( const std::string &keyName );
00167 
00168                         /** Sets the value of a given key ("key=value"), overwritten previous value if it existed.
00169                           */
00170                         void  set( const std::string &keyName, const std::string &value );
00171 
00172                         /** Sets the value of a given key ("key=value"), overwritten previous value if it existed.
00173                           */
00174                         void  set( const std::string &keyName, const int &value );
00175 
00176                         /** Sets the value of a given key ("key=value"), overwritten previous value if it existed.
00177                           */
00178                         void  set( const std::string &keyName, const float &value );
00179 
00180                         /** Sets the value of a given key ("key=value"), overwritten previous value if it existed.
00181                           */
00182                         void  set( const std::string &keyName, const double &value );
00183 
00184                         /** Sets the value of a given key ("key=value"), overwritten previous value if it existed.
00185                           */
00186                         void  set( const std::string &keyName, const bool &value );
00187 
00188                 };
00189 
00190         } // End of namespace
00191 } // End of namespace
00192 
00193 #endif



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:40:17 UTC 2011