标题是不言自明的:有没有人知道C的(好)属性文件阅读器库,如果没有,C ++?
[编辑:具体来说,我想要一个处理Java中使用的.properties文件格式的库: http://en.wikipedia.org/wiki/.properties]
标题是不言自明的:有没有人知道C的(好)属性文件阅读器库,如果没有,C ++?
[编辑:具体来说,我想要一个处理Java中使用的.properties文件格式的库: http://en.wikipedia.org/wiki/.properties]
STLSoft的 1.10 alpha 包含一个 platformstl::properties_file
类。它可用于从文件中读取:
using platformstl::properties_file;
properties_file properties("stuff.properties");
properties_file::value_type value = properties["name"];
或者从记忆中:
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"];
看起来最新的1.10版本有一堆全面的单元测试,并且他们已经升级了类来处理所有的规则和示例 Java文档。
唯一明显的问题是 value_type
是一个实例 stlsoft::basic_string_view
(描述于 这是Dobb博士的文章),有点类似于 std::string
,但实际上并没有自己的记忆。据推测,他们这样做是为了避免不必要的分配,大概是出于性能原因,这是STLSoft设计所珍视的。但这意味着你不能只写
std::string value0 = properties["name0"];
但是,你可以这样做:
std::string value0 = properties["name0"].c_str();
和这个:
std::cout << properties["name0"];
我不确定我同意这个设计决定,因为从文件或内存中读取属性的可能性有多大可能需要绝对的最后一个周期。我认为他们应该改变它使用 std::string
默认情况下,如果明确要求,则使用“字符串视图”。
除此之外, properties_file
上课看起来像是诀窍。
libconfuse(C库)也很有用;它永远存在并且很灵活。
它远远超出了java.util.Properties。但是,它不一定能处理java属性文件格式的边角情况(这似乎是您的要求)。
看看例子:
但是,我知道没有C ++包装器库。
我想通过'属性文件'你的意思是配置文件。
在这种情况下谷歌给出(前4个点击 C config file library
):
Poco还有一个阅读PropertyFiles的实现 http://pocoproject.org/docs/Poco.Util.PropertyFileConfiguration.html
从这里复制一个简单的例子: http://pocoproject.org/slides/180-Configuration.pdf
属性文件内容:
# a comment
! another comment
key1 = value1
key2: 123
key3.longValue = this is a very \
long value
path = c:\\test.dat
代码示例
#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");