How better and easier to implement work with ini files? That implementation of the structure is of interest:

;comment [section] name=value 

Is it possible to write a parser using the function for working with files? PS Do not offer ready-made solutions other than built-in functions, since I want myself.

  • one
    ini - inconvenient outdated format, better take xml or json right away - zenden2k
  • I have a program that stores editable parameters as the user wants. What should i use xml or json now? and this is nothing to do with it. - Dimcheg
  • 3
    " ini - inconvenient outdated format " - who told you such nonsense? Take a look at your /etc directory to get started. - user6550
  • 2
    C:\Users\user>cd /etc system cannot find the path specified. - zenden2k
  • Moreover, "we learn to speak for all." However, under Windows, you probably have some php.ini or mysql source, look there. And the fact that you do not have / etc does not make the ini format obsolete or inconvenient. - user6550

4 answers 4

Here's another, pure C: iniparser . Able to understand quite complex cases ( documentation ).

 dictionary * ini = iniparser_load( "config.ini" ); char * one = dictionary_get( ini, "first_section:one", "default" ); char * two = dictionary_get( ini, "second_section:two", NULL ); iniparser_freedict(ini); 

The maximum length of the string is fixed, but it can be redefined at build time:

 #define ASCIILINESZ (1024) 

PS Something I already wanted to finish it ...

  • A separate plus would put in the last sentence :) - VladD
  • " [Section] Keyword = value ; comment is converted to the following key pair: ("section:keyword", "value") " - is this correct? In this embodiment, the values ​​obviously can not contain a semicolon, which looks like something crazy. - Qwertiy
  • one
    No one bothers to enclose values ​​in quotes. There are some other little things in the implementation itself. - user6550

I don’t know exactly how deeply you want yourself, and how low (in the sense of how low-level operations) you are ready to descend.

If you are writing for Windows and can use WinAPI, then you can use the GetPrivateProfileString and GetPrivateProfileInt .

More details:

If you're writing cross-platform and ready to use Boost, then just take Program_options.

If you want to fully implement the parser yourself, look for the inih project for inspiration . Better than what is written there, no one will write anyway.

And finally, in order to further expand the pluralism of opinions and the variety of choices, a similar question to SO, in which you will find many other equally interesting alternatives:

  • " Better than what is written there, no one will write anyway ": at a minimum, strict restrictions on the size of lines are INI_ALLOW_MULTILINE ... Considering INI_ALLOW_MULTILINE - inaccurate :) - user6550
  • GetPrivateProfileString and GetPrivateProfileInt is a disgusting option. They will re-parse the ini-file with each call. And during normal implementation, the entire file must be read and parsed 1 time. - Qwertiy

It seems to me that a really good configuration reading function should be closely related to parsing command line arguments and current environment variables (environment). Those. need the ability to modify the configuration file environment variables and arguments with which the program is called.

It is also attractive that, after reading the configuration, you can immediately get the program variables already initialized from it (taking into account the modification of the command line arguments and the environment).

If interested, try to design such a thing.

  • one
    Another fun is the recording of an inishnik. Preserving custom formatting, at least comments. Taking into account possible changes in the program itself: deletion, addition, modification of values ​​/ sections / comments. More than 15 years ago, struggled with this task and even did. Just how - I don't remember anymore :) - user6550
  • @klopp, about the record - that's for sure. I did not load the vehicle in full. By the way, the most difficult thing in the record (I don’t know for sure how to do it, at least to do it easily ) is the "merging" of changes to the configs while several instances of the program are working simultaneously. - avp
  • And one more funny feature: selfconfig. For example: ValidTrueValues = "Yes, True, Yep, Ok" ; влияет на метод get_bool() ValidTrueValues = "Yes, True, Yep, Ok" ; влияет на метод get_bool() , MultilineCommentPairs = "/*,*/,{{,}}" , etc. But it is already from the category of candy wrappers :) - user6550

It is the implementation of the structure that is of interest.

 map < string, map <string, string> > ini; 

The first key is the section name, the second is the key name, the value is the parameter value. This is for the case when the meaningful content is important, not everything that is in the file. If you wish to add comments here, you can use an empty string or ; as a key (and with special desire, strings starting with a semicolon). But the order of the records will still change when rewriting.

If there is a desire to preserve and order, you can simply save the numbering in a separate vector.