C/C++ Unix configuration file library [closed]

霸气de小男生 提交于 2019-12-05 05:27:33

问题


Where can I find a C or C++ library for reading and manipulating Unix configuration files (format: name=value\n)?


回答1:


For plain C, libconfuse is quite good




回答2:


I will advice you to use boost::property_tree library for C++. It has quiet detailed manual. Further I'll advice you to use "info" config file.

Example of config file:

; this is just comment line

firstParamSection 
{
   stringParam "string"
   intParam 10
}

Example of code to retrieve this parameters from config file:

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/info_parser.hpp>
#include <string>

int main (int argc, char *argv[]) {
  std::string testString;
  int testInt;

  boost::property_tree::ptree pTree;
  try {
    read_info("test/config/file/name", pTree);
  }
  catch (boost::property_tree::info_parser_error e) {
    std::cout << "error" << std::endl;
  }

  try {
    testString = pTree.get<std::string>("firstParamSection.stringParam");
    testInt = pTree.get<int>("firstParamSection.intParam");
  }

  catch(boost::property_tree::ptree_bad_path e) {
    std::cout << "error" << std::endl;
  }



回答3:


I have written a config parser for "info" style config files myself a few weeks ago. It's fully XDG compliant, sections can be nested and it's pretty easy to use:

// read config file "barc" in directory $XDG_CONFIG_HOME/foo, e.g. /home/bwk/.config/foo/barc
config_read("foo", "barc");

// can read a specific file as well:
config_read_file("/etc/tralalarc");

// or from an open FILE *fp
config_read_fp(fp);

// or n characters directly from memory
config_read_mem(0xDEADBEEF, n);


// retrieve value associated with "key" in section "here", sub-section "my"
char *val = config_get("here.my.key");

You can also set/lock config variables including comments and write the config back to disk. It's pretty self-explaining, but it lacks documentation. See config.* here.

I'd be happy to add documentation and/or interface as needed.




回答4:


Take a look at Augeas, its pretty universal.



来源:https://stackoverflow.com/questions/5700466/c-c-unix-configuration-file-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!