xdgcfg  0.4.1
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...
 
uint_fast8_t read ()
 Read the file. More...
 
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. More...
 
void set_verbose (bool verbose)
 Sets verbosity. More...
 
bool get_verbose () const
 Returns verbosity. More...
 

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

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.
76 {
77  return _cfg;
78 }

◆ get_filepath()

const fs::path xdgcfg::get_filepath ( ) const

Returns the complete filepath.

81 {
82  return _filepath;
83 }

◆ get_verbose()

bool xdgcfg::get_verbose ( ) const

Returns verbosity.

91 {
92  return _verbose;
93 }

◆ read()

uint_fast8_t xdgcfg::read ( )

Read the file.

Returns
0 on success, 1 on I/O error, 2 on parse error.
Examples:
example.cpp.
29 {
30  try
31  {
32  _cfg.readFile(_filepath.c_str());
33  }
34  catch (const libconfig::FileIOException &e)
35  {
36  if (_verbose)
37  {
38  cerr << "I/O error while reading " << _filepath
39  << " - " << e.what() << endl;
40  }
41  return 1;
42  }
43  catch (const libconfig::ParseException &e)
44  {
45  if (_verbose)
46  {
47  cerr << "Parse error at " << e.getFile() << ":" << e.getLine()
48  << " - " << e.getError() << endl;
49  }
50  return 2;
51  }
52 
53  return 0;
54 }

◆ set_verbose()

void xdgcfg::set_verbose ( bool  verbose)

Sets verbosity.

Examples:
example.cpp.
86 {
87  _verbose = verbose;
88 }

◆ write()

bool xdgcfg::write ( )

Write the file.

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

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