xdgcfg  0.3.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...
 
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.
 
void set_verbose (bool verbose)
 Sets verbosity.
 
bool get_verbose () const
 Returns verbosity.
 

Detailed Description

Examples:
example.cpp.

Constructor & Destructor Documentation

◆ xdgcfg()

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

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)
44  : _cfg()
45  , _verbose(false)
46  {
47  xdgHandle xdg;
48  xdgInitHandle(&xdg);
49  _filepath = xdgConfigHome(&xdg);
50  xdgWipeHandle(&xdg);
51 
52  if (!subdir.empty())
53  {
54  _filepath /= subdir;
55  }
56  if (!fs::exists(_filepath))
57  {
58  fs::create_directories(_filepath);
59  }
60  _filepath /= filename;
61  }

Member Function Documentation

◆ get_cfg()

libconfig::Config& xdgcfg::get_cfg ( )
inline

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

Example:

libconfig::Config &cfg = config.get_cfg();
Examples:
example.cpp.
129  {
130  return _cfg;
131  }

◆ read()

uint_fast8_t xdgcfg::read ( )
inline

Read the file.

Returns
0 on success, 1 on I/O error, 2 on parse error.
Examples:
example.cpp.
69  {
70  try
71  {
72  _cfg.readFile(_filepath.c_str());
73  }
74  catch (const libconfig::FileIOException &e)
75  {
76  if (_verbose)
77  {
78  cerr << "I/O error while reading " << _filepath
79  << " - " << e.what() << endl;
80  }
81  return 1;
82  }
83  catch (const libconfig::ParseException &e)
84  {
85  if (_verbose)
86  {
87  cerr << "Parse error at " << e.getFile() << ":" << e.getLine()
88  << " - " << e.getError() << endl;
89  }
90  return 2;
91  }
92 
93  return 0;
94  }

◆ write()

bool xdgcfg::write ( )
inline

Write the file.

Returns
true on success
Examples:
example.cpp.
102  {
103  try
104  {
105  _cfg.writeFile(_filepath.c_str());
106  }
107  catch (const libconfig::FileIOException &e)
108  {
109  if (_verbose)
110  {
111  cerr << "I/O error while writing " << _filepath
112  << " - " << e.what() << endl;
113  }
114  return false;
115  }
116 
117  return true;
118  }

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