This repository has been archived on 2020-05-16. You can view files and clone it, but cannot push or open issues or pull requests.
xdgcfg/examples/example.cpp

35 lines
820 B
C++
Raw Normal View History

2019-08-03 15:01:15 +02:00
/* This file is part of xdgcfg. */
2018-08-10 02:22:06 +02:00
#include <iostream>
#include <libconfig.h++>
#include "xdgcfg.hpp"
2018-12-29 04:10:17 +01:00
int main()
2018-08-10 02:22:06 +02:00
{
xdgcfg config("test.cfg", // File name.
"xdgcfg"); // Sub directory (optional).
config.set_verbose(true); // Print error messages.
2018-08-10 02:22:06 +02:00
if (config.read() != 0)
{
std::cout << "File not found.\n";
2018-08-10 02:22:06 +02:00
}
// Get a reference to the libconfig::Config object and use it as you would
// normally do.
2018-08-10 02:22:06 +02:00
libconfig::Config &cfg = config.get_cfg();
libconfig::Setting &root = cfg.getRoot();
2018-08-10 02:22:06 +02:00
if (!root.exists("Hello"))
{
root.add("Hello", libconfig::Setting::TypeString) = "World";
}
if (!config.write())
{
std::cerr << "Writing failed.\n";
}
2018-08-10 02:22:06 +02:00
std::cout << "Hello: " << root["Hello"].c_str() << std::endl;
}