Boost logo

Boost Users :

Subject: Re: [Boost-users] Cross platform library for reading/writing configuration files?
From: Ken Smith (kgsmith_at_[hidden])
Date: 2008-11-18 21:09:00


On Fri, Nov 14, 2008 at 11:21 AM, Jeff Dunlap <jeff_j_dunlap_at_[hidden]> wrote:
>
> I've used Windows specific functions for reading/writing configuration (INI)
> files in my application and would like to know if there is a cross platform
> library available that is recommended.
>
> I searched the boost libs for something and there are libs of course that can
> achieve this with some programming, but I'd like to know if there is something
> (boost or other library) with specific functionality to handle reading/writing
> INI files. Example:
>
> [General]
> Language=Français
>
> [Strings]
> String00001=Version
> String00002=Etes vous sûr ?
> String00003=Voulez-vous continuer ?

When I want to do something like this, I use Lua. Eg.

conf.lua
conf = {
   general = {
      language='Français',
   },
   strings = {
      string00001='Version',
      string00002='Etes vous sûr ?',
      string00003='Voulez-vous continuer ?',
   },
}
EOF

main.cpp
#include <iostream>
#include <lua.hpp>
#include <assert.h>

int main()
{
   lua_State* L = luaL_newstate();
   assert(!luaL_dofile(L, "conf.lua"));

   lua_getglobal(L, "conf");
   lua_getfield(L, lua_gettop(L), "general");
   lua_getfield(L, lua_gettop(L), "language");
   const char* language = lua_tostring(L, lua_gettop(L));
   lua_pop(L, 2);

   lua_getfield(L, lua_gettop(L), "strings");
   lua_getfield(L, lua_gettop(L), "string00001");
   const char* string00001 = lua_tostring(L, lua_gettop(L));
   lua_pop(L, 1);
   lua_getfield(L, lua_gettop(L), "string00002");
   const char* string00002 = lua_tostring(L, lua_gettop(L));
   lua_pop(L, 1);
   lua_getfield(L, lua_gettop(L), "string00003");
   const char* string00003 = lua_tostring(L, lua_gettop(L));

   std::cout << "language = " << language << std::endl;
   std::cout << "string00001 = " << string00001 << std::endl;
   std::cout << "string00002 = " << string00002 << std::endl;
   std::cout << "string00003 = " << string00003 << std::endl;
}
EOF

output
g++ -c -o main.o main.cpp -I<path to lua>/include
g++ -o main main.o -L<path to lua>/lib -llua
language = Français
string00001 = Version
string00002 = Etes vous sûr ?
string00003 = Voulez-vous continuer ?
EOF

Of course, you can get more clever with the organization of your data
and the iteration method than I have here (see lua_next over indexed
tables (lists) instead of string00001, ...2, ...3, etc.) but you get
the idea.

   Ken


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net