LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-26-2006, 12:14 PM   #1
introuble
Member
 
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700

Rep: Reputation: 31
Parse configuration files


My program needs to accept "settings" provided via a configuration file. The file must have the following properties:

#1. Lines beginning with "#" are considered comments.

#2. Empty lines are ignored. [A line filled with spaces = empty line too]

#3. Settings come in the form:

Code:
name=<value str>
<value str> can contain spaces.

Any known/recommended way to parse such a configuration file? Or should I just use fgets+strcmp etc.
 
Old 07-26-2006, 12:17 PM   #2
ugenn
Member
 
Registered: Apr 2002
Posts: 549

Rep: Reputation: 30
Get a good regex library, like pcre. If you can use C++, checkout boost::regex from the boost library.
 
Old 07-26-2006, 12:59 PM   #3
introuble
Member
 
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700

Original Poster
Rep: Reputation: 31
I forgot to mention: Using C89
 
Old 07-26-2006, 03:52 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
My vote's for "fgets()"/"strcmp()".

If you find the solution inadequate, I'd worry first about making the data format more sophisticated (for example, going to XML instead of flat file) before I'd worry about a more sophisticated library.

But from everything you've said: a flat file sounds fine, and fgets()/strcmp() is the ideal way to parse that flat file.

IMHO .. PSM
 
Old 07-26-2006, 04:10 PM   #5
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by paulsm4
My vote's for "fgets()"/"strcmp()".

If you find the solution inadequate, I'd worry first about making the data format more sophisticated (for example, going to XML instead of flat file) before I'd worry about a more sophisticated library.

But from everything you've said: a flat file sounds fine, and fgets()/strcmp() is the ideal way to parse that flat file.

IMHO .. PSM
yeh id agree.. regex would be nice but is a bit of an overkill.. you already know the format of the file, so it should not be too hard to write a small lib that parses that format..
 
Old 07-26-2006, 09:22 PM   #6
ugenn
Member
 
Registered: Apr 2002
Posts: 549

Rep: Reputation: 30
Quote:
Originally Posted by xhi
yeh id agree.. regex would be nice but is a bit of an overkill.. you already know the format of the file, so it should not be too hard to write a small lib that parses that format..
Unless the format is ridiculously simple, regex can not only aid in parsing the file, but also validate the token data, all in one fell swoop.
 
Old 07-27-2006, 09:04 AM   #7
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
it looked pretty simple to me

name = value

not many swoops to that
 
Old 07-27-2006, 09:19 AM   #8
ugenn
Member
 
Registered: Apr 2002
Posts: 549

Rep: Reputation: 30
Quote:
Originally Posted by xhi
it looked pretty simple to me

name = value

not many swoops to that
And did you consider what acceptable values 'name' can hold? Apps written to be robust and secure will not just swallow whatever external data fed to it. This is where regex is handy.
 
Old 07-27-2006, 09:29 AM   #9
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
im not disputing anything about regexs.. i am simply suggesting that if the configuration file is simple, then it will may be easier to implement your own parsing without regex..

do you have an example of using regexs easily with C89? i mean you suggested boost, and then came back with nothing helpful in the way of C89? i know regexes are great and i use them all the time, well that is when they are actually going to save me time..
 
Old 07-27-2006, 09:32 AM   #10
ugenn
Member
 
Registered: Apr 2002
Posts: 549

Rep: Reputation: 30
Quote:
Originally Posted by xhi
im not disputing anything about regexs.. i am simply suggesting that if the configuration file is simple, then it will may be easier to implement your own parsing without regex..

do you have an example of using regexs easily with C89? i mean you suggested boost, and then came back with nothing helpful in the way of C89? i know regexes are great and i use them all the time, well that is when they are actually going to save me time..
Read properly, I mentioned pcre. While not part of the std lib, it is widely ported on many platforms.

Last edited by ugenn; 07-27-2006 at 09:34 AM.
 
Old 07-27-2006, 10:03 AM   #11
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
> Read properly

yes i did read that, i also mentioned easy, and worth the time to implement.. maybe it is easier to implement than i have seen, have any examples?
 
Old 07-27-2006, 02:03 PM   #12
orhun
LQ Newbie
 
Registered: Jun 2006
Distribution: Fedora 7
Posts: 11

Rep: Reputation: 0
If C++ and boost is an option then I suggest boost.program_options library instead of boost.regex.

From boost.program_options documentation
Quote:
Introduction

The program_options library allows program developers to obtain program options, that is (name, value) pairs from the user, via conventional methods such as command line and config file.

Why would you use such a library, and why is it better than parsing your command line by straightforward hand-written code?

* It's easier. The syntax for declaring options is simple, and the library itself is small. Things like conversion of option values to desired type and storing into program variables are handled automatically.
* Error reporting is better. All the problems with the command line are reported, while hand-written code can just misparse the input. In addition, the usage message can be automatically generated, to avoid falling out of sync with the real list of options.
* Options can be read from anywhere. Sooner or later the command line will be not enough for your users, and you'll want config files or maybe even environment variables. These can be added without significant effort on your part.
 
Old 07-27-2006, 02:13 PM   #13
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
nice. i had never heard of that. i dont know if that is an option for OP but thats good info either way.
 
Old 07-27-2006, 11:49 PM   #14
ugenn
Member
 
Registered: Apr 2002
Posts: 549

Rep: Reputation: 30
For pcre, look at pcredemo.c in the pcre source. Essentially, it's just 2 functions, pcre_compile and pcre_exec.
 
Old 09-01-2006, 01:10 AM   #15
jineshkj
Member
 
Registered: Apr 2006
Distribution: Fedora
Posts: 33

Rep: Reputation: 15
Quote:
Originally Posted by introuble
My program needs to accept "settings" provided via a configuration file. The file must have the following properties:

#1. Lines beginning with "#" are considered comments.

#2. Empty lines are ignored. [A line filled with spaces = empty line too]

#3. Settings come in the form:

Code:
name=<value str>
<value str> can contain spaces.

Any known/recommended way to parse such a configuration file? Or should I just use fgets+strcmp etc.
I'm sorry that I've made this reply so lately. You can try the following:

http://sourceforge.net/projects/parser
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What's the meaning of "PHP Parse error: parse error, unexpected $ in..." frandalla Programming 23 03-04-2009 12:34 PM
where are the configuration files for X ? nivedhitha Red Hat 3 05-02-2006 11:01 PM
How to parse log files into text view using GLADE shandy^^^ Programming 8 02-07-2006 08:13 PM
configuration files max_sipos Programming 13 09-30-2004 01:44 PM
Where are programs files and configuration files stored? nutshell Linux - General 2 03-09-2002 10:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration