Main MRPT website > C++ reference for MRPT 1.5.7
CConfigFile.h
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 #ifndef CConfigFile_H
10 #define CConfigFile_H
11 
12 #include <mrpt/utils/utils_defs.h>
15 
16 namespace mrpt
17 {
18 namespace utils
19 {
20  /** This class allows loading and storing values and vectors of different types from ".ini" files easily.
21  * The contents of the file will be modified by "write" operations in memory, and will be saved back
22  * to the file at the destructor, and only if at least one write operation has been applied.
23  *
24  * Use base class `CConfigFileBase`'s methods `read_{int,float,double,string,...}()` and `write()` to actually read and write values.
25  *
26  * See: \ref config_file_format
27  *
28  * \ingroup mrpt_base_grp
29  */
31  {
32  private:
33  std::string m_file; //!< The name of the file
34  void_ptr_noncopy m_ini; //!< The interface to the file:
35  bool m_modified; //!< If modified since load.
36 
37  protected:
38  /** A virtual method to write a generic string */
39  void writeString(const std::string &section,const std::string &name, const std::string &str) MRPT_OVERRIDE;
40 
41  /** A virtual method to read a generic string.
42  * \exception std::exception If the key name is not found and "failIfNotFound" is true. Otherwise the "defaultValue" is returned. */
43  std::string readString(
44  const std::string &section,
45  const std::string &name,
46  const std::string &defaultStr,
47  bool failIfNotFound = false) const MRPT_OVERRIDE;
48 
49  public:
50  /** Constructor that opens a configuration file. */
51  CConfigFile( const std::string &fileName );
52 
53  /** 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.
54  * However, it's perfectly right to use this object without an associated file, in which case it will behave as an "in-memory" file.
55  */
56  CConfigFile();
57 
58  /** Associate this object with the given file, so future read/write operations will be applied to that file (it's synchronized at destruction) */
59  void setFileName(const std::string &fil_path);
60 
61  /** Dumps the changes to the physical configuration file now, not waiting until destruction. */
62  void writeNow();
63 
64  void discardSavingChanges(); //!< Discard saving (current) changes to physical file upon destruction
65 
66  /** Returns the file currently open by this object. */
67  std::string getAssociatedFile() const { return m_file; }
68 
69  /** Destructor */
70  virtual ~CConfigFile();
71 
72  /** Returns a list with all the section names. */
73  virtual void getAllSections( vector_string &sections ) const MRPT_OVERRIDE;
74 
75  /** Returs a list with all the keys into a section. */
76  virtual void getAllKeys( const std::string &section, vector_string &keys ) const MRPT_OVERRIDE;
77 
78  }; // End of class def.
79 
80  } // End of namespace
81 } // end of namespace
82 #endif
#define MRPT_OVERRIDE
C++11 "override" for virtuals:
This class allows loading and storing values and vectors of different types from ".ini" files easily.
Definition: CConfigFile.h:30
STL namespace.
This class allows loading and storing values and vectors of different types from a configuration text...
std::vector< std::string > vector_string
A type for passing a vector of strings.
Definition: types_simple.h:30
bool m_modified
If modified since load.
Definition: CConfigFile.h:35
GLsizei const GLchar ** string
Definition: glext.h:3919
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLuint const GLchar * name
Definition: glext.h:3891
std::string m_file
The name of the file.
Definition: CConfigFile.h:33
void_ptr_noncopy m_ini
The interface to the file:
Definition: CConfigFile.h:34



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019