Main MRPT website > C++ reference
MRPT logo
CConfigFile.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 CConfigFile_H
29 #define CConfigFile_H
30 
31 #include <mrpt/utils/utils_defs.h>
34 
35 /*---------------------------------------------------------------
36  Class
37  ---------------------------------------------------------------*/
38 namespace mrpt
39 {
40 namespace utils
41 {
42  /** This class allows loading and storing values and vectors of different types from ".ini" files easily.
43  * The contents of the file will be modified by "write" operations in memory, and will be saved back
44  * to the file at the destructor, and only if at least one write operation has been applied.
45  * \ingroup mrpt_base_grp
46  */
48  {
49  private:
50  /** The name of the file
51  */
52  std::string m_file;
53 
54  /** The interface to the file:
55  */
57 
58  /** If modified since load.
59  */
60  bool m_modified;
61 
62  protected:
63  /** A virtual method to write a generic string.
64  */
65  void writeString(const std::string &section,const std::string &name, const std::string &str);
66 
67  /** A virtual method to read a generic string.
68  * \exception std::exception If the key name is not found and "failIfNotFound" is true. Otherwise the "defaultValue" is returned.
69  */
70  std::string readString(
71  const std::string &section,
72  const std::string &name,
73  const std::string &defaultStr,
74  bool failIfNotFound = false) const;
75 
76  public:
77  /** Constructor that opens a configuration file. */
78  CConfigFile( const std::string &fileName );
79 
80  /** Constructor, does not open any file. You should call "setFileName" before reading or writting or otherwise nothing will be read and write operations will be eventually lost.
81  * However, it's perfectly right to use this object without an associated file, in which case it will behave as an "in-memory" file.
82  */
83  CConfigFile();
84 
85  /** Associate this object with the given file, so future read/write operations will be applied to that file (it's synchronized at destruction).
86  */
87  void setFileName(const std::string &fil_path);
88 
89  /** Dumps the changes to the physical configuration file now, not waiting until destruction. */
90  void writeNow();
91 
92  /** Returns the file currently open by this object.
93  */
94  std::string getAssociatedFile() const { return m_file; }
95 
96  /** Destructor
97  */
98  virtual ~CConfigFile();
99 
100  /** Returns a list with all the section names.
101  */
102  virtual void getAllSections( vector_string &sections ) const;
103 
104  /** Returs a list with all the keys into a section.
105  */
106  virtual void getAllKeys( const std::string section, vector_string &keys ) const;
107 
108  }; // End of class def.
109 
110  } // End of namespace
111 } // end of namespace
112 #endif



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