C(或 C++)的属性文件库 [英] Properties file library for C (or C++)

查看:22
本文介绍了C(或 C++)的属性文件库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题一目了然:有谁知道 C 或 C++ 的(好的)属性文件阅读器库吗?

The title is pretty self-explanatory: does anyone know of a (good) properties file reader library for C or, if not, C++?

具体来说,我想要一个处理 Java 中使用的 .properties 文件格式的库:http://en.wikipedia.org/wiki/.properties

To be specific, I want a library which handles the .properties file format used in Java: http://en.wikipedia.org/wiki/.properties

推荐答案

STLSoft1.10 alpha 包含一个 platformstl::properties_file 类.它可用于从文件中读取:

STLSoft's 1.10 alpha contains a platformstl::properties_file class. It can be used to read from a file:

using platformstl::properties_file;

properties_file  properties("stuff.properties");

properties_file::value_type  value = properties["name"];

或从记忆中:

properties_file  properties(
    "name0=value1
 name1 value1 
 name\ 2 : value\ 2  ",
    properties_file::contents);

properties_file::value_type  value0 = properties["name0"];

properties_file::value_type  value1 = properties["name1"];

properties_file::value_type  value2 = properties["name 2"];

看起来最新的 1.10 版本有一堆全面的单元测试,并且他们已经升级了类来处理 Java 文档.

Looks like the latest 1.10 release has a bunch of comprehensive unit-tests, and that they've upgraded the class to handle all the rules and examples given in the Java documentation.

唯一明显的问题是 value_typestlsoft::basic_string_view(在 这篇 Dobb 博士的文章中描述a>),它有点类似于 std::string,但实际上并不拥有它的内存.据推测,他们这样做是为了避免不必要的分配,大概是出于性能原因,这是 STLSoft 设计所珍视的.但这意味着你不能只写

The only apparent rub is that the value_type is an instance of stlsoft::basic_string_view (described in this Dr Dobb's article), which is somewhat similar to std::string, but doesn't actually own its memory. Presumably they do this to avoid unneccessary allocations, presumably for performance reasons, which is something the STLSoft design holds dear. But it means that you can't just write

std::string  value0 = properties["name0"];

但是,您可以这样做:

std::string  value0 = properties["name0"].c_str();

还有这个:

std::cout << properties["name0"];

我不确定我是否同意这个设计决定,因为从文件或内存中读取属性的可能性有多大,需要绝对的最后一个周期.我认为他们应该将其更改为默认使用 std::string,然后在明确需要时使用字符串视图".

I'm not sure I agree with this design decision, since how likely is it that reading properties - from file or from memory - is going to need the absolute last cycle. I think they should change it to use std::string by default, and then use the "string view" if explicitly required.

除此之外,properties_file 类看起来可以解决问题.

Other than that, the properties_file class looks like it does the trick.

这篇关于C(或 C++)的属性文件库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆