C(或 C++)的属性文件库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/874052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Properties file library for C (or C++)
提问by JamieH
The title is pretty self-explanatory: does anyone know of a (good) properties file reader library for C or, if not, C++?
标题不言自明:有人知道 C 或 C++ 的(好的)属性文件阅读器库吗?
Edit: To be specific, I want a library which handles the .properties file format used in Java: http://en.wikipedia.org/wiki/.properties
编辑:具体来说,我想要一个处理 Java 中使用的 .properties 文件格式的库:http: //en.wikipedia.org/wiki/.properties
采纳答案by dcw
STLSoft's 1.10 alphacontains a platformstl::properties_file
class. It can be used to read from a file:
STLSoft的1.10 alpha包含一个platformstl::properties_file
类。它可用于从文件中读取:
using platformstl::properties_file;
properties_file properties("stuff.properties");
properties_file::value_type value = properties["name"];
or from memory:
或从记忆中:
properties_file properties(
"name0=value1\n name1 value1 \n 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"];
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.
看起来最新的 1.10 版本有一堆全面的单元测试,并且他们已经升级了类来处理Java 文档中给出的所有规则和示例。
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
唯一明显的问题value_type
是 是stlsoft::basic_string_view
(在Dobb 博士的文章中描述)的一个实例,它有点类似于std::string
,但实际上并不拥有它的记忆。据推测,他们这样做是为了避免不必要的分配,大概是出于性能原因,这是 STLSoft 设计所珍视的。但这意味着你不能只写
std::string value0 = properties["name0"];
You can, however, do this:
但是,您可以这样做:
std::string value0 = properties["name0"].c_str();
and this:
和这个:
std::cout << properties["name0"];
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.
我不确定我是否同意这个设计决定,因为读取属性 - 从文件或从内存 - 需要绝对的最后一个周期的可能性有多大。我认为他们应该将其更改为std::string
默认使用,然后在明确需要时使用“字符串视图”。
Other than that, the properties_file
class looks like it does the trick.
除此之外,这个properties_file
类看起来很有效。
回答by michael
libconfuse (C library) is useful, too; it's been around forever & is flexible.
libconfuse(C 库)也很有用;它一直存在并且很灵活。
- ( www.nongnu.org/confuse/ ) http://www.nongnu.org/confuse/tutorial-html/index.html
- ( www.nongnu.org/confuse/ ) http://www.nongnu.org/confuse/tutorial-html/index.html
It goes way, way beyond java.util.Properties. Though, it won't necessarily handle the corner cases of the java properties file format (which seems to be your requirement).
它远远超出了 java.util.Properties。虽然,它不一定会处理 java 属性文件格式的极端情况(这似乎是您的要求)。
See the examples:
请参阅示例:
- simple: www.nongnu.org/confuse/simple.conf
- crazy: www.nongnu.org/confuse/test.conf
- 简单:www.nongnu.org/confuse/simple.conf
- 疯狂:www.nongnu.org/confuse/test.conf
No C++ wrapper library, that I'm aware of, though.
不过,我知道没有 C++ 包装器库。
回答by Mhh Lecker
Poco also has an Implementation for Reading PropertyFiles http://pocoproject.org/docs/Poco.Util.PropertyFileConfiguration.html
Poco 还有一个用于读取 PropertyFiles 的实现 http://pocoproject.org/docs/Poco.Util.PropertyFileConfiguration.html
A Simple example copied from here: http://pocoproject.org/slides/180-Configuration.pdf
从这里复制的一个简单示例:http: //pocoproject.org/slides/180-Configuration.pdf
Property file content:
属性文件内容:
# a comment
! another comment
key1 = value1
key2: 123
key3.longValue = this is a very \
long value
path = c:\test.dat
Code example
代码示例
#include <Poco/Util/PropertyFileConfiguration.h>
using Poco::AutoPtr;
using Poco::Util::PropertyFileConfiguration;
AutoPtr<PropertyFileConfiguration> pConf;
pConf = new PropertyFileConfiguration("test.properties");
std::string key1 = pConf->getString("key1");
int value = pConf->getInt("key2");
std::string longVal = pConf->getString("key3.longValue");
回答by qrdl
I guess by 'properties file' you mean config file.
我猜“属性文件”是指配置文件。
In this case Google gives (first 4 hits for C config file library
):
在这种情况下,谷歌给出(前 4 次点击C config file library
):