Properties file library for C (or C++)

馋奶兔 提交于 2019-12-28 08:39:50

问题


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

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


回答1:


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"];

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.

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"];

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.

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




回答2:


libconfuse (C library) is useful, too; it's been around forever & is flexible.

  • ( 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).

See the examples:

  • simple: www.nongnu.org/confuse/simple.conf
  • crazy: www.nongnu.org/confuse/test.conf

No C++ wrapper library, that I'm aware of, though.




回答3:


Poco also has an Implementation for Reading PropertyFiles http://pocoproject.org/docs/Poco.Util.PropertyFileConfiguration.html

A Simple example copied from here: 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");



回答4:


I guess by 'properties file' you mean config file.

In this case Google gives (first 4 hits for C config file library):

  • http://www.hyperrealm.com/libconfig/
  • http://rudeserver.com/config/
  • http://freshmeat.net/projects/cfl/
  • http://liblcfg.carnivore.it/


来源:https://stackoverflow.com/questions/874052/properties-file-library-for-c-or-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!