C/C++ Unix配置文件库

发布于 2024-11-02 04:25:27 字数 1539 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

誰認得朕 2024-11-09 04:25:27

我建议您使用 C++ 的 boost::property_tree 库。它有安静的详细手册。此外,我建议您使用“info”配置文件。

配置文件示例:

; this is just comment line

firstParamSection 
{
   stringParam "string"
   intParam 10
}

从配置文件检索此参数的代码示例:

#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;
  }

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;
  }
风蛊 2024-11-09 04:25:27

对于纯 C 语言, libconfuse 相当不错

For plain C, libconfuse is quite good

涫野音 2024-11-09 04:25:27

几周前,我自己为“info”样式配置文件编写了一个配置解析器。它完全符合 XDG,部分可以嵌套,并且非常易于使用:

// 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");

您还可以设置/锁定配置变量(包括注释)并将配置写回磁盘。它非常不言自明,但缺乏文档。请参阅 config.* 此处

我很乐意根据需要添加文档和/或界面。

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.

千紇 2024-11-09 04:25:27

看看 Augeas,它非常通用。

Take a look at Augeas, its pretty universal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文