Properties file library for C (or C++) - c++

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

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

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

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/

Related

Translating wrapped function like struct of C/C++.bt (Binary Template) to Java 8

Sorry, I have almost 20 years without touching C/C++.
I Would like translate this code to Java 8, I found this Reference Manual.
I was reading that in this link.
You can read...
Binary Templates are easy to write and look similar to C/C++ structs except they may contain if, for, or while statements as well as functions or complex expressions.
Powerful enough to parse almost any binary file format.
Can be set to run automatically when files are opened.
Templates may be shared and a list of templates for download is available in our Template Repository.
I begins and in the line 2063 I found (sorry, I could only translate, sad 4 lines :(! ).
APFS apfs(0);
I was reading C - function inside struct but looks, so many different!
Reviewing I jump to the line 1994, it looks like a structure with a function call, instead of having attributes!!!.
typedef struct APFS(uint64 apfs_offset) {
DefineApfsContainer(apfs_offset);
} ;
Inmediatly you can see in the line 1998
void DefineApfsContainer(uint64 apfs_offset) {
Apfs_Offset = apfs_offset;
FSeek(Apfs_Offset);
if (!CheckApfsAndSetBlockSize()) {
Printf("\nError, starting point not an APFS container superblock. Set the 'Apfs_Offset' variable to correct value!");
Exit(1);
}
obj csb;//container super block in block zero
SeekBlock(csb.body.xp_desc_base);
CheckpointDesc cp(csb.body.xp_desc_blocks);
//SeekBlock(csb.body.xp_data_base);
//obj checkpoint_data_nx[csb.body.xp_data_blocks] <optimize=false>;
// Checkpoint processing
local uint i = 0;
local uint64 max_xid = 0;
local uint64 max_xid_block_pos = 0;
SeekBlock(csb.body.xp_desc_base);
local uint64 base_pos = FTell();
local uint64 pos = 0;
for (i=0; i< csb.body.xp_desc_blocks; ++i) { // assuming cont. blocks
if (cp.checkpoint_desc_nx[i].hdr.type == obj_type_container_superblock) {
if (cp.checkpoint_desc_nx[i].hdr.xid >= max_xid) {
// validate it
pos = base_pos + (i * Block_Size);
if (fletcher64(pos, Block_Size) == 0) {
max_xid = cp.checkpoint_desc_nx[i].hdr.xid;
max_xid_block_pos = pos;
}
}
}
}
if (max_xid > csb.hdr.xid)
Printf("\nFound newer xid=%Lu # offset 0x%Lu. Using this Container superblock.", max_xid, pos);
FSeek(pos);
obj valid_csb;
BookmarkVolumes(valid_csb);
if (valid_csb.body.efi_jumpstart) {
SeekBlock(valid_csb.body.efi_jumpstart);
obj efi_info;
}
/*if (valid_csb.body.keylocker_block_count) {
SeekBlock(valid_csb.body.keylocker_paddr);
obj keybag; // This is encrypted!
}*/
}
I need to understand what does that (I don't mean the code, but the way of coding), in order to translate.
How should I understand this code (a function as a structure or a structure from a function)?
The nugget of subject.
How should I translate it to Java?
This code doesn’t quite translate to Java because it’s code-generating code, ie. code that some tool uses to generate the actual C-like code (in a nutshell), or code for a bespoke virtual machine that then implements the data extraction and data packing. The idiomatic way to do this in Java without writing a stand-alone tool would be by attributes and similar mechanisms, introspection, and runtime code generation, repurposing Java syntax to express the same idea. You could attempt to do manual translation - that would grow the code a whole lot. The binary template concept is very powerful - leads to very concise code that would otherwise be tedious to implement.
I’d say that a manual translation will be more work than writing the translator, because you’ll be debugging all the manual mistakes till the cows come home. The syntax is limited enough so that a parser and translator written in Java will be about the same number of lines as a “straightforward” Java implementation of the binary template.
You may look at the tool this template is written for and see if the tool offers a way of translating the template. If the tool is open source then you already have a parser :)

Config File using Environment Variables

I have a .cfg file and I'd like to use an environment variable to configure one of the fields.
directory=${HOME}/folder1/
However, when I parse this config, it's reading ${HOME} as a string, which is obviously not what I want.
I wrote my own parser in C++, in case I need to do something special. Right now it is a very basic read and parse.
void Config_Parser::parse_config_by_delimiter(string config, string delimiter) {
ifstream infile(config);
while (infile >> line) {
key = line.substr(0, line.find(delimiter));
value = line.substr(line.find(delimiter)+1);
if (this->config_settings.find(key) != this->config_settings.end()) {
cout << "Cannot use config... same key is set multiple times" << endl;
}
this->config_settings.insert({key, value});
}
}
The code seems to work fine for all other config settings (anything not using an environment variable), so I don't think its a problem with the code. But, I am a C++ noobie, so it's here anyways.
When I parse and print out the value:
Actual output: ${HOME}/folder1/
Expected/desired output: /home/my_dir/folder1/
Untested
You can use wordexp to do posix shell-like expansion of strings.
The function wordexp() performs a shell-like expansion of the string
s and returns the result in the structure pointed to by p.
You will need to #include <wordexp.h>
You also probably want to specify the flag WRDE_NOCMD to prevent subshell command execution.
http://man7.org/linux/man-pages/man3/wordexp.3.html
Is the following configuration syntax acceptable to you?
directory = getenv("HOME") + "/folder1/";
If so, then a configuration file parser library I wrote called Config4* can do what you want. You can find it on http://www.config4star.org.
I recommend you scroll down the web page to "Download the Manuals" and retrieve Config4* Getting Started Guide and Config4* C++ API Guide. Chapters 2 (overview of syntax) and 3 (overview of API) of the "Getting Started" guide should be more than sufficient to get you up and running.

Efficient and stable YAML parser for cocos2d-x

I am developing a game using cocos2d-x and C++, and I need to load a bunch of YAML files for this application. I tried using the yaml-cpp library with quite good results.
The problem is that this library seems to be very unstable (at least under cocos2d-x on iOS), since almost 20% of the time it fails loading the same YAML file throwing "end of map not found", "invalid map element", or errors like these ones.
I followed the HowToParseADocument guide, so I think I got it correct. But, since it's not 100% reliable, I am looking for something more stable. Eg:
long size = 0;
unsigned char *yaml = FileUtils::getInstance()->getFileData("file.yml", "r", &size);
std::stringstream is;
is << yaml;
YAML::Parser parser(is);
YAML::Node doc;
while(parser.GetNextDocument(doc)) {
instance->settings = doc.Clone();
}
The parser usally breaks at the parser.GetNextDocument(doc) call. The document I am trying to read is plain YAML with key: value lists in this simple form:
# Comment
section1:
param1: value1
param2: value2
# Comment
section2:
param1: value1
param2: value2
Edit
I am not allowed to disclose the content of the original YAML file, but I can give you some information:
It only contains maps, and not arrays, aliases or other particular constructs
Those values are integers, float or strings
It has been linted with this free tool, with success.
The code I used to read it, posted up there, it's always in that form, and I do not modify it to make the app run correctly. It's just that the app starts and works or starts and does not work. Since I am changing nothing in the middle, I really do not understand what's happening.
It's a bit hard to guess at the solution because you won't provide an actual example, but:
Who owns the data at the unsigned char* returned by getFileData? If that function itself owns the data, then it is no longer valid after the function returns, and so all sorts of crazy stuff might happen.
To validate what's happening here (beyond looking at the implementation of getFileData), you could print out is.string() before calling YAML::Parser parser(is); and see if that prints the expected YAML.

Compile a program with local file embedded as a string variable?

Question should say it all.
Let's say there's a local file "mydefaultvalues.txt", separated from the main project. In the main project I want to have something like this:
char * defaultvalues = " ... "; // here should be the contents of mydefaultvalues.txt
And let the compiler swap " ... " with the actual contents of mydefaultvalues.txt. Can this be done? Is there like a compiler directive or something?
Not exactly, but you could do something like this:
defaults.h:
#define DEFAULT_VALUES "something something something"
code.c:
#include "defaults.h"
char *defaultvalues = DEFAULT_VALUES;
Where defaults.h could be generated, or otherwise created however you were planning to do it. The pre-processor can only do so much. Making your files in a form that it will understand will make things much easier.
The trick I did, on Linux, was to have in the Makefile this line:
defaultvalues.h: defaultvalues.txt
xxd -i defaultvalues.txt > defaultvalues.h
Then you could include:
#include "defaultvalues.h"
There is defined both unsigned char defaultvalues_txt[]; with the contents of the file, and unsigned int defaultvalues_txt_len; with the size of the file.
Note that defaultvalues_txt is not null-terminated, thus, not considered a C string. But since you also have the size, this should not be a problem.
EDIT:
A small variation would allow me to have a null-terminated string:
echo "char defaultvalues[] = { " `xxd -i < defaultvalues.txt` ", 0x00 };" > defaultvalues.h
Obviously will not work very well if the null character is present inside the file defaultvalues.txt, but that won't happen if it is plain text.
One way to achieve compile-time trickery like this is to write a simple script in some interpreted programming language(e.g. Python, Ruby or Perl will do great) which does a simple search and replace. Then just run the script before compiling.
Define your own #pramga XYZ directive which the script looks for and replaces it with the code that declares the variable with file contents in a string.
char * defaultvalues = ...
where ... contains the text string read from the given text file. Be sure to compensate for line length, new lines, string formatting characters and other special characters.
Edit: lvella beat me to it with far superior approach - embrace the tools your environment supplies you. In this case a tool which does string search and replace and feed a file to it.
Late answer I know but I don't think any of the current answers address what the OP is trying to accomplish although zxcdw came really close.
All any 7 year old has to do is load your program into a hex editor and hit CTRL-S. If the text is in your executable code (or vicinity) or application resource they can find it and edit it.
If you want to prevent the general public from changing a resource or static data just encrypt it, stuff it in a resource then decrypt it at runtime. Try DES for something small to start with.

Embedded console tools functionality in application

I'm currently developing an application that happens to require some file preprocessing before actually reading the data.
Doing it externally was not a possibility so I came up with a fork & execve of "cut options filename | sort | uniq -c" etc... and I execute it like that.
However I thought that maybe there was already another option to reuse all those ancient and good working tools directly in my code and not having to invoke them through a shell.
I am currently looking at busybox to see if there is an easy way of statically link and programatically call those utils but no luck yet.
Arkaitz, the answer no, because of how you've phrased the question.
You ask for "another option to reuse all those ancient and good working tools directly in my code and not having to invoke them through a shell"
The problem with that is, the proper and accepted way of reusing all those ancient and good working tools is exactly what you're saying you want to avoid - invoking them via a shell (or at least, firing them up as child processes via popen for example) - and it's definitely not recommend to try to subsume, copy, or duplicate these tools into your code.
The UNIX (and Linux) model for data manipulation is robust and proven - why would you want to avoid it?
The 'ancient' tools were built for use by the shell, not to be built/linked into an executable. There are, however, more recent tools that kinda do lot of what you showed on your command line preprocessor: iostreams with extractors (to replace cut), std::sort and std::unique to replace the respective programs...
struct S { string col1, col3;
bool operator<( const S& s ) { return col1 < s.col1; }
};
vector<S> v;
while( cin ) {
S s;
string dummy;
cin >> s.col1 >> dummy >> col3 >> dummy;
v.push_back( s );
}
sort(v.begin(), v.end(), S::smaller );
unique( v.begin(), v.end() );
Not too complicated, I think.
Try popen().
char buffer [ BUFFER_SIZE ];
FILE * f = popen( "cut options filename | sort | uniq -c", "r" );
while( /*NOT*/! feof(f) )
fgets( buffer, BUFFER_SIZE, f );
pclose( f );
Reference: How to execute a command and get output of command within C++ using POSIX?
You have to do it through the shell, but it's easier to use "system" call.
while(something) {
int ret = system("foo");
if (WIFSIGNALED(ret) &&
(WTERMSIG(ret) == SIGINT || WTERMSIG(ret) == SIGQUIT))
break;
}
Just write another useful 'ancient and good' tool ;) and read all data from stdin and return it to stdout.
cat *.txt | grep 'blabla' | sort | my_new_tool | tee -o res_file
The nice way to do it is:
Create 2 pipes
Fork a new process
Replace stdin and stdout for child process with pipes using dup2 function
exec a command you'd like
Write and read from parent process using pipes
busybox was my first thought as well, although you might also want to consider embedding a scripting engine like Python and doing these kind of manipulations in Python scripts.
I would definitely not try to strip this kind of functionality out of GNU command line tools since they have grown significantly since the early UNIX days and sprouted an awful lot of options.
If the busybox code seems too hard to adapt, then the next place I would look would be Minix source code. Look under Previous Versions and pick one of the version 1 or 2 Minixes because those were written as teaching code so they tend to be clearer and simpler.
If you do not want to call external commands (whether by exec, popen or system etc) but do not want to modify the source of these utilities and compile them into your code (relatively easy, just change 'main' to 'main_cut' etc), then the only remaining option I see is to embed the utilities inside your code and either extract them at runtime or dynamically create a filing system by pointing at the data inside your code (eg using a floppy or cd image and writing a FUSE module that picks up the disk image data from a ram address). All of which seems like a lot of work just to make this look like a single neatly-packaged utility.
Personally, if i really had to do this, I'd get the source of all those utils and compile them in as external calls. Of course you'd no longer have pipes easily available, you'd either have to use temp files for preprocessing, or something more complicated involving co-routines. Or maybe sockets. Lots of work and messy whatever you do!