xdgjson  0.2.2
Public Member Functions | List of all members
xdgjson Class Reference

Public Member Functions

 xdgjson (const string &filename, const string &subdir="")
 Checks if subdir is present, creates it if necessary. More...
 
const bool read ()
 Read the file. More...
 
const bool write ()
 Write the file. More...
 
Json::Value & get_json ()
 Returns a reference to the config as Json::Value. More...
 
const fs::path get_filepath () const
 Returns the complete filepath.
 

Detailed Description

Examples:
example.cpp.

Constructor & Destructor Documentation

◆ xdgjson()

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

Checks if subdir is present, creates it if necessary.

Example:

xdgjson config("test.json", "subdirectory");
Parameters
filenameThe name of the file, including extension
subdirThe subdir (optional)
11 : _json()
12 {
13  xdgHandle xdg;
14  xdgInitHandle(&xdg);
15  _filepath = xdgConfigHome(&xdg);
16  xdgWipeHandle(&xdg);
17 
18  if (!subdir.empty())
19  {
20  _filepath /= subdir;
21  }
22  if (!fs::exists(_filepath))
23  {
24  fs::create_directories(_filepath);
25  }
26  _filepath /= filename;
27 }

Member Function Documentation

◆ get_json()

Json::Value & xdgjson::get_json ( )

Returns a reference to the config as Json::Value.

Example:

Json::Value &json = config.get_json();
Examples:
example.cpp.
65 {
66  return _json;
67 }

◆ read()

const bool xdgjson::read ( )

Read the file.

Returns
true on success
Examples:
example.cpp.
30 {
31  std::ifstream file(_filepath);
32  if (file.is_open())
33  {
34  std::stringstream config;
35  config << file.rdbuf();
36  file.close();
37  config >> _json;
38 
39  return true;
40  }
41  else
42  {
43  return false;
44  }
45 }

◆ write()

const bool xdgjson::write ( )

Write the file.

Returns
true on success
Examples:
example.cpp.
48 {
49  std::ofstream file(_filepath);
50  if (file.is_open())
51  {
52  const string config = _json.toStyledString();
53  file.write(config.c_str(), config.length());
54  file.close();
55 
56  return true;
57  }
58  else
59  {
60  return false;
61  }
62 }

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