# Standard INI parser



## Farhan Khan (Sep 26, 2018)

Hi all,
Is there a standard INI parser library commonly used in the BSD community, ideally one built into FreeBSD? I need to parse several INI files and would prefer not to import a 3rd party library into this application. This is a C application.
Thanks


----------



## kpa (Sep 26, 2018)

I think most new tools like ports-mgmt/pkg are using already or are moving to JSON or UCL. This is the parser used by PKG:

https://github.com/vstakhov/libucl

That library is not part of any public API in FreeBSD however.


----------



## ralphbsz (Sep 26, 2018)

Look at the wikipedia page for INI file.  If you have GTK installed, you would have one already.

If the ini file format is simple (no comments, no special characters, no continuation lines), then parsing it would take half hour for an experienced C programmer; less than trying to interface to an existing library.  This assumes that you have control over the ini file format and can legislate away complex and bizarre stuff in the input file.

If you have a choice to select a better file format (ilke kpa said), pretty much anything is better than INI.  Even XML.


----------



## Farhan Khan (Sep 27, 2018)

I do not have control over the INI format, it must be INI.
And while INI files are inherently simple, this could be arbitrarily complex.


----------



## ralphbsz (Sep 27, 2018)

Well, look at that Wikipedia page for libraries.  And obviously do a web search.

Do you have to do the parsing in C?  String handling in C is doable, but tedious, difficult, and error-prone.  It might be a better idea to use a language with better string handling tools (Python, Perl, ...) for pre-processing the INI into a super-safe and easy format, and then sucking that into C.


----------



## droeders (Oct 8, 2018)

This is a third party C library, but claims it was designed to be small and simple:

https://github.com/benhoyt/inih


----------



## Beastie (Oct 9, 2018)

ralphbsz said:


> It might be a better idea to use a language with better string handling tools (Python, Perl, ...) for pre-processing the INI into a super-safe and easy format, and then sucking that into C.


+1 for Python. Not only is it fairly easy to implement (ideally, as a dictionary), but Python 3 even provides a module (1, 2) just for that purpose.


----------



## ekingston (Oct 9, 2018)

[edit]Never mind, I can't read. My comments are not relevant.[/edit]

Perl has a built in INI parser. It is dead simple to use. Just point it at the INI file and you get a data structure.

People tend to pick their language first, then go find the libraries. I don't think you mentioned what language you want to use.


----------



## SirDice (Oct 9, 2018)

ekingston said:


> I don't think you mentioned what language you want to use.





Farhan Khan said:


> This is a C application.


----------

