00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://www.mrpt.org/ | 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 * \ingroup mrpt_base_grp 00049 */ 00050 class BASE_IMPEXP CStringList : public mrpt::utils::CSerializable 00051 { 00052 // This must be added to any CSerializable derived class: 00053 DEFINE_SERIALIZABLE( CStringList ) 00054 00055 protected: 00056 /** The internal list of strings 00057 */ 00058 std::deque<std::string> m_strings; 00059 00060 public: 00061 /** Default constructor (empty string list) 00062 */ 00063 CStringList(); 00064 00065 /** Constructor from a text 00066 */ 00067 CStringList(const std::string& text); 00068 00069 /** Explicit constructor from deque<string> */ 00070 explicit CStringList(const std::deque<std::string>& lines) : m_strings(lines) { } 00071 00072 /** Explicit constructor from vector<string> */ 00073 explicit CStringList(const std::vector<std::string>& lines) 00074 { 00075 std::copy(lines.begin(),lines.end(),std::back_inserter(m_strings)); 00076 } 00077 00078 /** Appends a new string at the end of the string list. 00079 * \sa insert,set 00080 */ 00081 void add( const std::string &str ); 00082 00083 /** An alternative way of adding strings to the list */ 00084 CStringList & operator << (const std::string &s) { add(s); return *this; } 00085 00086 /** Inserts a new item at a given position (0=insert at the beggining,1=put into the second position,...) 00087 * \sa add,set 00088 */ 00089 void insert( size_t index, const std::string &str ); 00090 00091 /** Overwrites an existing position with a new value (0=first elements) 00092 * \sa insert 00093 */ 00094 void set( size_t index, const std::string &str ); 00095 00096 /** Clear the whole list 00097 */ 00098 void clear(); 00099 00100 /** Returns the number of text lines in the list 00101 */ 00102 size_t size() const; 00103 00104 /** Delete the element at a given position (0=first element) 00105 */ 00106 void remove(size_t index); 00107 00108 /** Looks for a given string in the list, and returns its index, or returns "false" otherwise. 00109 * \return true if string has been found. 00110 */ 00111 bool find( 00112 const std::string &compareText, 00113 size_t foundIndex, 00114 bool caseSensitive = true) const; 00115 00116 /** Returns one string from the line list 00117 */ 00118 void get(size_t index, std::string &outText) const; 00119 00120 /** Returns one string from the line list 00121 */ 00122 std::string operator ()(size_t index) const; 00123 00124 /** Returns a reference to one string from the line list 00125 */ 00126 std::string& operator ()(size_t index); 00127 00128 /** Returns the whole string list as a single string with '\r\n' characters for newlines. 00129 */ 00130 void getText(std::string &outText) const; 00131 00132 /** Returns the whole string list as a single string with '\r\n' characters for newlines. 00133 */ 00134 inline std::string getText() const 00135 { 00136 std::string s; 00137 getText(s); 00138 return s; 00139 } 00140 00141 /** Fills the string list by parsing a single string with '\r', '\n', or '\r\n' characters indicatng newlines. 00142 */ 00143 void setText(const std::string &inText); 00144 00145 /** Load the string list from a file. 00146 */ 00147 void loadFromFile(const std::string &fileName); 00148 00149 /** Save the string list to a file. 00150 */ 00151 void saveToFile(const std::string &fileName) const; 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 std::string get_string( 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 float get_float( 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 int get_int( const std::string &keyName ); 00167 00168 /** Returns the value of the given key ("key=value"). 00169 * \exception std::exception If the key is not found in the string list. 00170 */ 00171 double get_double( const std::string &keyName ); 00172 00173 /** Returns the value of the given key ("key=value"). 00174 * \exception std::exception If the key is not found in the string list. 00175 */ 00176 bool get_bool( const std::string &keyName ); 00177 00178 /** Sets the value of a given key ("key=value"), overwritten previous value if it existed. 00179 */ 00180 void set( const std::string &keyName, const std::string &value ); 00181 00182 /** Sets the value of a given key ("key=value"), overwritten previous value if it existed. 00183 */ 00184 void set( const std::string &keyName, const int &value ); 00185 00186 /** Sets the value of a given key ("key=value"), overwritten previous value if it existed. 00187 */ 00188 void set( const std::string &keyName, const float &value ); 00189 00190 /** Sets the value of a given key ("key=value"), overwritten previous value if it existed. 00191 */ 00192 void set( const std::string &keyName, const double &value ); 00193 00194 /** Sets the value of a given key ("key=value"), overwritten previous value if it existed. 00195 */ 00196 void set( const std::string &keyName, const bool &value ); 00197 00198 }; 00199 00200 } // End of namespace 00201 } // End of namespace 00202 00203 #endif
| Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011 |