Ini Parsing in C++
-
Okay, this has been driving me crazy for the past week. I’ve gone through a bunch of different libraries trying to find one that I can use and either I have too much difficulty in getting them set up, or they don’t work for my purposes. I keep working on my own parser on and off as I keep trying to find new libraries, but I hate string manipulation in C++, and I hate writing parsers, so I really would prefer to use an external solutoin.
Does anyone here recommend a good method or library for reading Freelancer style .ini files in C++?
-
LUA is a fairly popular parsing package but I don’t know how difficult it is to set up. Personally I use my own code but it is MFC based, if you are using MFC then I can help you out in that case simply by whitiling the code down to a parser class…
-
Libraries for example here http://www.codeproject.com/search.aspx?q=%2Bc%2B%2B+%2Bini&doctypeid=1%3B2%3B3 will not help just because FL Style ini file may contain same section names with different keys and values.
I’ve searched appropriate library too, finally just got search and replace function by regular expressions, constructed regular expression.
To be honest, I used C#, but for using RegEx in .NET programming language does not matter.
Also try to use INI_Reader class at FLCoreCommon from the last SDK:
http://svn.the-starport.net/flhookplugin/trunk/sdk/ -
Crazy wrote:
If it’s Freelancer related… What about using Freelancer’s IniReader ?I’m not sure what you’re referring to.
That also depends how Freelancer integrated it is. I want to use a Freelancer like structure with ini files, so as to make my stuff easy to modify, but it’s not really a Freelancer project. I do want to maintain all the features that Freelancer’s inis used, particularly having sections and keys with the same names.
Here is an example of what I want to parse correctly and in a very Freelancer like way. It’s nothing out of the ordinary, and it very closely resembles a Freelancer ship anyway.
[Ship] name = Corvette mesh = SuperRhino.mesh mass = 1500 linear_drag = 0.33 angular_drag_x = 5 angular_drag_y = 5 angular_drag z = 5 forward_thrust = 24000 reverse_fraction = 0.5 steering_thrust_x = 10000 steering_thrust_y = 10000 steering_thrust_z = 10000
-
Alright, well I will start working only a parser class that you can simply drop into your project and compile it. It will be based off of my parsing code so it won’t have any trouble handling FL style ini files. Its the way I do everything in Galaxy Empire such as the following
[model] nickname = testsph mesh = Skies\STARSPHERES\starsphere_500k.x [model] nickname = testball mesh = Lights\White.x ofs = 0,30,-1 hp = 0,0,0,0,engine,eq_test_engine [model] nickname = sol_Cobra mesh = Ships\Sol\Cobra\Cobra.x name = 66538 ;desc = ofs = 0,15,-100 hp = 1,-33,-1,-12,gun,eq_basicgun hp = 1,33,-1,-12,gun,eq_basicgun hp = 1,-16,-1,-8,gun,eq_basicgun hp = 1,16,-1,-8,gun,eq_basicgun hp = 1,-33,1,-12,gun,eq_basicgun hp = 1,33,1,-12,gun,eq_basicgun hp = 1,-16,1,-8,gun,eq_basicgun hp = 1,16,1,-8,gun,eq_basicgun hp = 1,0,0,8,torp;,eq_basic_torp hp = 0,0,0,0,engine,eq_engine_5;,eq_basic_engine hp = 0,0,0,0,lootb,eq_basic_lootbeam hp = 0,0,0,0,xptr,eq_basic_transporter hp = 0,0,0,0,tractor,eq_basic_tractor hp = 0,-7.5,-1,-16,fx,eng_flame_01 hp = 0,7.5,-1,-16,fx,eng_flame_01 ;hp = 0,0,0,-30,fx,fx_jumpgate_001 fuse = small_death_fuse mass = 20 [fuse] nickname = default_fuse end = 0.5 trig = steffct,test_explosion_08a trig = steffct,expl_flame trig = steffct,expl_fireball03 trig = steffct,fx_expl_ring_01 trig = steffct,fx_expl_flare_03 trig = steffct,fx_expl_flare_04 trig = steffct,fx_whtyel_flare_01,0.25
-
I think the reason why there is no such library is, that there are windows functions for ini handling and for other os you don’t need it since they all use different file structures.
I personally like writing parsers, it’s mostly rather fast developed and the scheme is always the same. You need some experience of course.
-
Ok, the class is built and also includes routines to convert to float, ULONG and int, also comes with an example dialog program to show how to use the class.
I can provide the source if others need it.