xdgcfg  0.2.2
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 fs::path 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)
13 : _cfg()
14 , _verbose(false)
15 {
16  xdgHandle xdg;
17  xdgInitHandle(&xdg);
18  _filepath = xdgConfigHome(&xdg);
19  xdgWipeHandle(&xdg);
20 
21  if (!subdir.empty())
22  {
23  _filepath /= subdir;
24  }
25  if (!fs::exists(_filepath))
26  {
27  fs::create_directories(_filepath);
28  }
29  _filepath /= filename;
30 }

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.
80 {
81  return _cfg;
82 }

◆ 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.
33 {
34  try
35  {
36  _cfg.readFile(_filepath.c_str());
37  }
38  catch (const libconfig::FileIOException &e)
39  {
40  if (_verbose)
41  {
42  cerr << "I/O error while reading " << _filepath
43  << " - " << e.what() << endl;
44  }
45  return 1;
46  }
47  catch (const libconfig::ParseException &e)
48  {
49  if (_verbose)
50  {
51  cerr << "Parse error at " << e.getFile() << ":" << e.getLine()
52  << " - " << e.getError() << endl;
53  }
54  return 2;
55  }
56 
57  return 0;
58 }

◆ write()

const bool xdgcfg::write ( )

Write the file.

Returns
true on success
Examples:
example.cpp.
61 {
62  try
63  {
64  _cfg.writeFile(_filepath.c_str());
65  }
66  catch (const libconfig::FileIOException &e)
67  {
68  if (_verbose)
69  {
70  cerr << "I/O error while writing " << _filepath
71  << " - " << e.what() << endl;
72  }
73  return false;
74  }
75 
76  return true;
77 }

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