c ++ read properties / conf configuration file

Introduction 1. libConfuse

libconfuse is a C library for configuration file parser, under license authorization ISC, which support section (list) and value (string, integer, float, Boolean, or other portions), and other functions (e.g. single / double-quoted strings, environment variable expansion functions include statements nested). It is the ability of the configuration file can be added using a simple API to make the program very easy to read the configuration file.

A detailed description please visit: http: //www.nongnu.org/confuse/, the code is hosted in github: https: //github.com/martinh/libconfuse

We can use the library to implement c ++ libconfuse read parameters from the configuration file.

 

2. Install

Details visible: https: //github.com/martinh/libconfuse

installation steps:

1 wget https://github.com/martinh/libconfuse/releases/download/v3.2/confuse-3.2.tar.gz
2 ./configure
3 make
4 sudo make install

 

3. Use

Details can be seen: http: //www.nongnu.org/confuse/tutorial-html/index.html

CMakeLists.txt

cmake_minimum_required(VERSION 3.12)
project(properties)

set(CMAKE_CXX_STANDARD 14)

add_executable(properties main.cpp)

target_link_libraries(properties confuse)

main.cpp

#include <stdio.h>
#include <confuse.h>

int main(void) {
    cfg_opt_t opts[] =
            {
                    CFG_STR("target", "World", CFGF_NONE),  //设置默认值
                    CFG_END()
            };
    cfg_t *cfg;

    cfg = cfg_init(opts, CFGF_NONE);
    if (cfg_parse(cfg, "../stuff.properties") == CFG_PARSE_ERROR)
        return 1;

    printf("Hello, %s!\n", cfg_getstr(cfg, "target"));

    cfg_free(cfg);
    return 0;
}

stuff.properties

target=zjp

 

Guess you like

Origin www.cnblogs.com/JP6907/p/11422668.html