.:: Bots United ::.

.:: Bots United ::. (http://forums.bots-united.com/index.php)
-   General Programming (http://forums.bots-united.com/forumdisplay.php?f=25)
-   -   a simple .ini parser (http://forums.bots-united.com/showthread.php?t=3317)

Whistler 02-01-2005 07:36

a simple .ini parser
 
its code isn't very "clever"... but it's enough to be used with bots:
http://cvs.sourceforge.net/viewcvs.p...pp?view=markup
http://cvs.sourceforge.net/viewcvs.p....h?view=markup

(I'm licensing this code under GNU GPL with the special exception to link against the HLSDK)

using is very simple:

PHP Code:

#include "ini.h" // at the top of your source file

CIniFile ini("test.ini"); // load an ini file
printf("%s\n"ini.Get("SECTION""Item")); // read a item from the file
ini.Set("SECTION""Item""Value"); // write a value to the ini
ini.Save("filename.ini"); // save the ini file 


Rifleman 02-01-2005 09:12

Re: a simple .ini parser
 
well done man !

koraX 02-01-2005 12:37

Re: a simple .ini parser
 
Some notes :
  • Get rid of pragma once, you already have .h guard defined before it
  • I recommend using C++ constructs, if you are already using classes (malloc -> new, FILE* -> std::fstream, char* -> std::string
  • Wrap your parser in some napespace, so people can use it in their programs without potential conflicts
  • you should trim section name after you remove [ ], example
    Code:

      [  foo]
    will be loaded as " FOO" instead of "FOO"
  • implement removing items
  • realoc is too often, will fragment memory and slow things down. I recommend allocating in blocks, like I'll allocate 10 units at start and if eleventh unit will come, I'll allocate another 10 units, allocating everytime unit comes is IMO slow and ineffective
  • overall it is small good simple program :)

@$3.1415rin 02-01-2005 13:17

Re: a simple .ini parser
 
maybe use the STL, especially the vector<..> class, to get rid of all those explicit memory handling stuff, makes a lot of stuff a lot more simpler.

and depending on how you wanna use that ini class, if it's called often and needs to be fast, a hashtable might be useful

Whistler 08-01-2005 06:30

Re: a simple .ini parser
 
updating CVS to fix a memory leak and allocate memory in blocks (thanks Jozef Wagner)

P.S, I found the memory leak with this handy tool:
http://filebase.bots-united.com/inde...on=file&id=254


All times are GMT +2. The time now is 09:19.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.