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
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 is a simple collection of key-value pairs that can be parsed by the java. util. Properties class. Properties files are widely used for many purposes in all kinds of Java applications, often to store configuration or localization data.
The advantage of using properties file is we can configure things which are prone to change over a period of time without the need of changing anything in code. Properties file provide flexibility in terms of configuration. Sample properties file is shown below, which has information in key-value pair.
A Java properties file is used to store configuration data or settings for a Java project. .properties is a file extension for files that store the configurable parameters of an application. Recompilation is not required if modifications are made to a 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"];
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.
libconfuse (C library) is useful, too; it's been around forever & is flexible.
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:
No C++ wrapper library, that I'm aware of, though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With