Good, simple configuration library for large c++ project? - c++

We are developing a rather large project in C++, where many components require configuration parameters. We would like to use a central place to configure everything (like a registry), preferably with a nice and simple GUI (e.g. like Firefox's about:config) and a simple API.
I am pretty sure this that many applications have this kind of problem, but could not find any libraries available that can be readily used for this. Does anyone know of a good (preferably free) library to use for this?
This should work cross platform in Windows and Linux.

boost::program_options provides unified (and cross platform) support for configuration from command line, environment variables and configuration files. It seems like it ought to scale to multiple bits of a large software system registering an interest in various parameters (e.g option groups). Not much help with the GUI or persistence side of things though (but then what's wrong with editing a config file with a text editor ?).

I've used libconfig before, works well easy and LGPL.
http://www.hyperrealm.com/libconfig/

I've used a modified version of John Torjo code from TechRepublic/DDJ (source)

The multi platform ACE library has a configuration class that uses config files that have the Windows .ini format.

I've often used a simple wrapper around pugxml. I find that creating a configuration class with parameter validation for enumerated types and so on makes the rest of the code much cleaner. If you are just dealing with key/value pairs you will have to validate the data all throughout your code. By writing a custom class for each application you can put all that in one place.

Try Configurator. There is no GUI, but it's easy-to-use and flexible C++ library for configuration file parsing (from simplest INI to complex files with arbitrary nesting and semantic checking). Header-only and cross-platform. Uses Boost C++ libraries.
See: http://opensource.dshevchenko.biz/configurator

Related

C++ Parse library for Qt applications

I'm aware there is no official C++ library for Parse (https://parse.com). I've got a desktop variant of an app written using Qt C++ for OSX/Windows that needs to talk to Parse. I'm embarking on writing my own client for the Parse REST API based on the PHP library (https://github.com/apotropaic/parse.com-php-library) and the Qt network classes. Would you recommend a different approach?
The data I am saving is pretty basic, some objects and some geo data, but it would be nice to have a general solution to share with others. I will happily release whatever I come up with via GitHub, and all the better if this can lead to a group of us sharing the load.
Cheers!

How to store preferences (and user settings) in a cross-platform application?

I'm working on a GUI desktop application that should run natively on Windows, Mac OS X and Linux. What is the preferred way to store preferences in a cross-platform application? I'm using C++, but the question (and its answers) should be valid for any natively compiled language. (Solutions for dynamic languages and Java can be seen here.)
My research so far tells me, that there are at least two strategies:
(A) Use the OS-specific API preferences functions.
(B) Store the preferences in a file within an appropriate (OS-specific) folder.
Let's consider method (A): I assume NSUserDefaults is the correct method for Mac OS X. On Windows systems, I'd write to the registry via RegOpenKeyEx. But there arise some questions: Is there any comparable and portable Linux API for that? Is writing to the Windows registry really a future-proof solution?
To keep things simple, I'm inclined to follow method (B). Thus I just have OS-specific code to get the appropriate directory where I can store my data in a format of my choice. On Windows, I've learned SHGetFolderPath (or SHGetKnownFolderPath for recent Windows systems) and CSIDL_LOCAL_APPDATA is the way to go. On Macs, the NSSearchPathForDirectoriesInDomains API call should do the same; it's an Objective-C API though making things more complicated. Finally, for the Linux version using getenv("HOME") (and getpwuid() as a fallback solution) seems to be recommend.
To summarize my questions:
1. Are there any patterns considered as best-practice for this task?
2. Is there any C++ class abstracting all the dirty things like finding the correct folder away out there? (I came across QSetting, but I'm using FLTK and I don't want to change my GUI toolkit.)
Edit:
By "preferences" I mean data that may be changed by the application and the user, e.g. a list of recent files, the preferred window size and so on.
Since z80crew says he's using Fltk in his project, I think the best way to store small amounts of user data is through the Fltk Fl_Preferences class. (http://www.fltk.org/doc-1.3/classFl__Preferences.html).
That way, you don't have to care about where the user data is actually stored in the filesystem. You just create a Fl_Preferences object identified by the name of the application and its vendor (e.g., "CoolApp" and "AuthorOfCoolApp") and Fltk stores the data somewhere.
Under Linux, the preferences are stored in ~/.fltk/{vendor}/{application}.prefs where {vendor} and {application} are the same strings you've passed the constructor of Fl_Preferences. But you aren't supposed to worry about that.
I'm also developing a set of cross-platform plugins and did exactly what you describe in method (B):
Find the proper folder using platform specific code.
Write the preferences to that folder with cross-platform code.
I would also note that:
Using the Windows registry is very limited and problematic. I try to avoid that as much as possible.
Lately apple has a new song: that you should not access the ~/Library/Preferences folder directly but rather use their API for saving preferences value. In effect this turns the preferences into registry like mechanism. I very much resent this approach.
I do not know of any library that implements finding the currect folder in cross-platform manner. Such function is not very hard to write using SHGetKnownFolderPath and NSSearchPathForDirectoriesInDomains, as you described.

Looking for a way to translate an application

I'm starting to design an application and I'd like it to support several languages from the beginning to avoid having to change big amounts of code later on to enable multiple languages.
I've written early a C application for which the messages were in a struct so I'd have several header files with the translated strings which would be integrated into the application itself, so to add a new language I'd have to recompile.
What I'd like is to be able to have a localization system that allows me to translate the application and add new languages easily without having to recompile the application. Ideally with a pretty straightforward way of translating and testing the translation (like editing a text file and seeing the results back in the app), without having to depend on 3rd party tools for the translation.
I'm thinking about writing my own system for it, maybe based on XML for the files containing the translations for example, but I wonder if anybody has experience with this and would recommend a lightweight library that provides that I'd like (even if the translation is not so direct as editing a text file). I emphasize lightweight because I think the application itself is not going to weight more than a couple of megabytes.
It's going to be a Windows application, if that's relevant for the matter. As of now I still haven't decided on the graphic toolkit I use but it's probably that I'll use the default Windows offers without using a cross platform one (like wxWidgets, Qt, GTK+, ...). But it's not set in stone, if one offered significant advantages it'd consider it.
I am not sure how lightweight is, but qt has a good support for internationalization.
Then you can use QtLinguist to simply translate your text.
Don't invent your own system. Internationalization (i18n) and localization (l10n) are almost always covered in some existing library.
If you develop for Windows, this link, Globalizing and Localizing .NET Framework Applications, might prove useful.

Internationalization in MFC

It's finally (after years of postponing) the time to localize my app in a few other languages other than English.
The first challenge is to design the integration into my C++ / MFC application that has dozens of dialogs and countless strings. I came across two possible alternative implementations:
Compile and deploy localized resource files as DLLs
Extract and replace all strings with the localized version. For each
language there will be an XML (or simple text) file.
Personally I opt for the second alternative since it seems to me more flexible. The changes are many but not hard to make, and very importantly the XML files will be very easy to modify for the translators.
Any advise is greatly appreciated.
Regards,
Cosmin Unguru
http://www.batchphoto.com/
I did some long-living MFC projects with different languages.
I strongly recommend the first approach with resource-only DLLs.
The reasons:
(1) If the user does a XCOPY install, he always has the default language (English) in the main executables.
(2) If you don't translate everything (e.g. you're late with your release or forget some strings), the Windows resource functions if properly used return the resource in the default language automatically - you don't have to implement it on your own.
(3) My very person opinion: (a) Line breaks, tabs, whitespaces in XML files are a pain in your a**. (b) Merging XML files is even worse...
(4) Don't forget the encoding. It's okay in XML but your translators might use an unsuitable editor and damage the file.
And now for the main reason:
(5) You will have to rearrange many of your dialogs, because many strings are longer in e.g. French or German than in English. And making all statics, buttons, ... larger "just in case" looks crappy.
Another hint: Spend some bucks and buy one of the translation tools which import your projects / binaries and build up a translation database. This will be amortized after the first release.
Another hint (2): If possible make a release which doesn't contain any changes but only the multi-language feature. Also in future, if possible: Release your product in English. Then do the translation in one single step (per language) and release the other languages.
My good and friendly suggestion from somebody who worked a lot with localization:
Grab GNU Gettext,
Mark all your strings as _("English").
Extract all strings using gettext tool xgettext and compile dictionalries
Translate string using great tools like poedit.
Use gettext in your project and make your localization life simpler!
You can also use boost::locale for same purpose - it uses GNU Gettext dictionaries and approach but provides different and more powerful runtime and for windows developer it has very good addon - it supports wide strings that MFC requires to use for normal Unicode support.
Don't use resources and other "translation" tools that are total crap from linguistic point of view (and developer's point of view as well).
Further reading: http://cppcms.sourceforge.net/boost_locale/html/tutorial.html
Using a DLL resource library is a relatively straightforward operation, and allows you to manage not only strings, but other resources as well. And this is its main advantage, because i18n is not only about string translation.
However, depending on your needs, a text-based solution may be a better decision, because of its easier handling - resource scripts being more complex than xml files, especially for the average translator.
I would suggest creating your own abstraction layer, something like "LoadLocalizedString", etc.; in this way, you can start implementing it just with text files, and then change to something more complex when and if required in a transparent way - all the effort for making your software i18n aware would still be valid.
In our case we had diffrent dialogues per Language. The resource file was the same as the multiple laguages were implemented at development time. You could basically append on existing resource files the diferent languages. I hope it helps to find your way.
The DLL option is commonly used for this since the resource loading procedure (e.g. LoadLibrary) is already written - meaning you don't have to write any parsing/loading code. While XML is easier to edit, DLLs have a bit more security (users won't be able to easily edit them) and will allow the developer (meaning you) more time to work on application logic instead of writing a language loading system.
HMODULE hLangDLL = LoadLibrary("text_en.dll");
// more stuff
TCHAR mybuffer[1024] = {0};
LoadString(hLangDLL, IDS_MYSTRING, mybuffer, 1023);
If it is just the strings that are changing then I agree that XML is the way forward here for the exact reasons you outline. Easy for other people to edit, easy to change language at runtime, etc.
The only reason (in my eyes) that you'd choose option 1 is if things other than strings are being localized such as needing different icons.
If it's just text? I say go with the XML.

Stock Charts in C++

I am trying to create some charts of data (eg http://www.amibroker.com/). Is there a C++ library that can do this without a lot of extra work? I'm thinking Qt or wxWindows would have something like it, but it wasn't immediately obvious.
Thanks!
FLTK is a light and portable C++ toolkit for GUI. There's a chart class. Sample.
Qwt does at least some of the things you are trying to achieve (basic plots, bar charts and so on), and integrates well with Qt.
I think you need to pick your GUI framework first, then find a charting control for the given framework, since that affects what charting controls you could feasibly use. For example, must this be portable?
For the project I'm working on (a large MFC application) we use Cedric Moonen's ChartCtrl. We've had a pretty good experience with it so far.
I've done some graph plotting with gnuplot lately, which is quite powerful. Although I think it is Linux only, which may or may not be a problem.
Also, believe it or not, some amazing things can be done with LaTeX. I've used the tkz (tikz) library to produce some awesome graphs.
Both solutions plot to a file which you can include in your interface.
They aren't C++ libraries but you can easily create the datafiles and call the necessary programs from within your C++ program (Being creative with system() and possible some shell scripts).