Properties file library for C (or C++)(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
推荐答案
STLSoft 的 1.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_type 是 stlsoft::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++)的属性文件库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C(或 C++)的属性文件库
基础教程推荐
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 初始化列表*参数*评估顺序 2021-01-01
- CString 到 char* 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
