xdgcfg  0.1.0
Public Member Functions | List of all members
xdgcfg Class Reference

Public Member Functions

 xdgcfg (const string &filename, const string &subdir="")
 Checks if subdir is present, creates it if necessary. More...
 
const uint_fast8_t read ()
 Read the file. More...
 
const bool write ()
 Write the file. More...
 
libconfig::Config & get_cfg ()
 Returns a reference to the config as libconfig::Config. More...
 
const string get_filepath () const
 Returns the complete filepath.
 
const void set_verbose (bool verbose)
 Sets verbosity.
 
const bool get_verbose () const
 Returns verbosity.
 

Detailed Description

Examples:
example.cpp.

Constructor & Destructor Documentation

◆ xdgcfg()

xdgcfg::xdgcfg ( const string &  filename,
const string &  subdir = "" 
)
explicit

Checks if subdir is present, creates it if necessary.

Example:

xdgcfg config("test.cfg", "subdirectory");
Parameters
filenameThe name of the file, including extension
subdirThe subdir (optional)
24 : _cfg()
25 , _verbose(false)
26 {
27  xdgHandle xdg;
28  xdgInitHandle(&xdg);
29  _filepath = xdgConfigHome(&xdg);
30  xdgWipeHandle(&xdg);
31 
32  if (!subdir.empty())
33  {
34  _filepath += '/' + subdir;
35  if (!fs::exists(_filepath))
36  {
37  fs::create_directory(_filepath);
38  }
39  }
40  _filepath += '/' + filename;
41 }

Member Function Documentation

◆ get_cfg()

libconfig::Config & xdgcfg::get_cfg ( )

Returns a reference to the config as libconfig::Config.

Example:

libconfig::Config &cfg = config.get_cfg();
Examples:
example.cpp.
91 {
92  return _cfg;
93 }

◆ read()

const uint_fast8_t xdgcfg::read ( )

Read the file.

Returns
0 on success, 1 on I/O error, 2 on parse error.
Examples:
example.cpp.
44 {
45  try
46  {
47  _cfg.readFile(_filepath.c_str());
48  }
49  catch (const libconfig::FileIOException &e)
50  {
51  if (_verbose)
52  {
53  cerr << "I/O error while reading " << _filepath
54  << " - " << e.what() << endl;
55  }
56  return 1;
57  }
58  catch (const libconfig::ParseException &e)
59  {
60  if (_verbose)
61  {
62  cerr << "Parse error at " << e.getFile() << ":" << e.getLine()
63  << " - " << e.getError() << endl;
64  }
65  return 2;
66  }
67 
68  return 0;
69 }

◆ write()

const bool xdgcfg::write ( )

Write the file.

Returns
true on success
Examples:
example.cpp.
72 {
73  try
74  {
75  _cfg.writeFile(_filepath.c_str());
76  }
77  catch (const libconfig::FileIOException &e)
78  {
79  if (_verbose)
80  {
81  cerr << "I/O error while writing " << _filepath
82  << " - " << e.what() << endl;
83  }
84  return false;
85  }
86 
87  return true;
88 }

The documentation for this class was generated from the following files: