cfgfile 0.2.11
Loading...
Searching...
No Matches
cfgfile

cfgfile - is a header-only library for reading/saving configuration files. The main approach of cfgfile that you define root tag of configuration with all neseccary child nested tags. Once you define hierarchy of tags that describes the schema of your configuration you can read and save configuration files from/to any supported stream. cfgfile supports std::istream, std::wistream and QTextStream for reading. Tag in the schema is a class derived from cfgfile::tag_t< Trait > or any ready to use derived classes. To simplify a life you can use generator to generate C++ classed of data and tags with DSL.

Let's say you have data structure representing configuration of your application.

struct data_t {
std::string m_value;
};

And you want to save this structure in the following format.

{data
{value <string>}
}

Then you can define tag for cfgfile to read and save such configuration file.

template< typename Trait >
class tag_data_t
: public cfgfile::tag_no_value_t< Trait >
{
public:
tag_data_t()
: cfgfile::tag_no_value_t< Trait > ( "data", true )
, m_value( *this, "value", true )
{}
tag_data_t( const data_t & cfg )
: cfgfile::tag_no_value_t< Trait > ( "data", true )
, m_value( *this, "value", true )
{
m_value.set_value( cfg.m_value );
}
data_t cfg() const
{
data_t d;
d.m_value = m_value.value();
return d;
}
private:
};
Exception in the library.
Definition exceptions.hpp:51
Tag without a value.
Definition tag_no_value.hpp:57
void set_defined(bool on=true)
Set "defined" property.
Definition tag.hpp:164
Definition const.hpp:38

Once you defined data structure and tag you can read and write configuration.

data_t cfg;
cfg.m_value = "value";
std::ofstream stream( "conf.cfg" );
try {
tag_data_t< cfgfile::string_trait_t > tag( cfg );
cfgfile::write_cfgfile( tag, stream );
stream.close();
}
{
stream.close();
std::cout << x.desc() << std::endl;
}
const Trait::string_t & desc() const noexcept
Definition exceptions.hpp:65

And

data_t cfg;
std::ifstream stream( "conf.cfg" );
try {
tag_data_t< cfgfile::string_trait_t > tag;
cfgfile::read_cfgfile( tag, stream, "conf.cfg" );
stream.close();
cfg = tag.cfg();
}
{
stream.close();
std::cout << x.desc() << std::endl;
}

Q/A

How can I add cfgfile to my project?

  • The simplest way is just copy cfgfile directory with headers to any location in your project. With CMake you can clone entire cfgfile project somewhere in your project and just do add_subdirectory(), if you will do so you have to add include directory path to your project with include_directories( ${cfgfile_INCLUDE_DIRECTORIES} ).

Is it possible to disable STL strings/streams and use Qt only.

  • Yes. You can define CFGFILE_DISABLE_STL and CFGFILE_QT_SUPPORT together. Additionally to CFGFILE_QT_SUPPORT you can define CFGFILE_XML_SUPPORT to enable XML format support.