C(或C ++)的属性文件库
标题是不言自明的:有没有人知道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版本有一堆全面的unit testing,并且他们已经升级了类来处理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库)也很有用; 它永远存在并且很灵活。
- (www.nongnu.org/confuse/)http://www.nongnu.org/confuse/tutorial-html/index.html
它远远超出了java.util.Properties。 但是,它不一定能处理java属性文件格式的边角情况(这似乎是您的要求)。
看看例子:
- 简单:www.nongnu.org/confuse/simple.conf
- 疯狂:www.nongnu.org/confuse/test.conf
但是,我知道没有C ++包装器库。
我想通过’属性文件’你的意思是配置文件。
在这种情况下,Google给出了( C config file library
前4次点击):
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 using Poco::AutoPtr; using Poco::Util::PropertyFileConfiguration; AutoPtr pConf; pConf = new PropertyFileConfiguration("test.properties"); std::string key1 = pConf->getString("key1"); int value = pConf->getInt("key2"); std::string longVal = pConf->getString("key3.longValue");