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



Page generated by Doxygen 1.9.1 for MRPT 1.4.0 SVN: at Mon Apr 18 03:56:21 UTC 2022