C++轻量级配置库

run*_*ead 13 c++ configuration-files

我正在寻找一个具有非限制性许可证的跨平台C++轻量级配置库.我需要比标准属性文件更复杂的部分,但我不想使用XML(写得太多:-)).

我想用这种方式编写配置:

render = 
{
    window = 
    {
        width = 800,
        height = 600
    }
}
Run Code Online (Sandbox Code Playgroud)

Kar*_*oor 15

有boost的property_tree.该许可证允许商业用途.

你的例子:

ptree pt;
pt.put("render.window.width", 800);
pt.put("render.window.height", 600);
Run Code Online (Sandbox Code Playgroud)

这可以例如导出到JSON

write_json("my_config.json", pt);
Run Code Online (Sandbox Code Playgroud)

然后看起来像

{
  "render":
  {
    "window":
    {
      "width": 800;
      "height": 600;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

与导出为XML,INI和INFO的方式相同.

  • @hubrobin:你不需要整个Boost库来完成这项工作,IIRC.Boost有一个名为[bcp]的工具(http://www.boost.org/doc/libs/release/tools/bcp/doc/html/index.html),专门用于提取单个库. (5认同)
  • 以我的经验.. Boost远不是轻量级的库,但我会看一下,因为它看起来很有前途 (2认同)

Mr *_*unz 5

您也可以尝试JsonCpp并在中写入配置文件Json,该语法与您喜欢的语法非常相似:

// Configuration options
{
    // Default encoding for text
    "encoding" : "UTF-8",

    // Plug-ins loaded at start-up
    "plug-ins" : [
        "python",
        "c++",
        "ruby"
        ],

    // Tab indent size
    "indent" : { "length" : 3, "use_space": true }
}
Run Code Online (Sandbox Code Playgroud)

在下,MIT License因此非常宽松。

  • 自从写了这个答案以来,[现代C ++的JSON](https://github.com/nlohmann/json)已经成为C ++非常流行的JSON库,也值得一看。 (2认同)