LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 08-31-2004, 11:38 PM   #1
max_sipos
Member
 
Registered: Jul 2004
Posts: 96

Rep: Reputation: 15
configuration files


What is the best and most effective way to parse configuration files (standard Linux text files, # is a comment)? Is there a standard library that does that or do I have to implement my own parser (or lex for that matter)? Is it a good idea to use xml for configuration files, do you believe user would prefer that over usual text files? How would you go about parsing xml?

Thank you!
Max
 
Old 09-01-2004, 02:17 AM   #2
ugenn
Member
 
Registered: Apr 2002
Posts: 549

Rep: Reputation: 30
I think XML is a good idea, since it takes care of all the mundane text processing. It's a little awkward if your user is used to regular files. But I'd go for it anyway because it really handles a lot of the work for you.

edit: you don't have to manually parse xml yourself. There are libraries for that (expat and libxml2 for eg). You just provide a DTD. That's the reason why I recommended xml.

Last edited by ugenn; 09-01-2004 at 02:19 AM.
 
Old 09-24-2004, 10:04 AM   #3
jpbarto
Senior Member
 
Registered: Mar 2003
Location: Pittsburgh, PA
Distribution: Gentoo / NetBSD
Posts: 1,251

Rep: Reputation: 45
max_sipos, are you going to take the XML route? I was considering creating a library that would parse an apache like conf file (name/value pairs with sections contained within <></> pairs). Actually I was hoping Apache would have a separate library for this posted but it does not appear so... the search continues.

jpbarto
 
Old 09-24-2004, 11:18 AM   #4
max_sipos
Member
 
Registered: Jul 2004
Posts: 96

Original Poster
Rep: Reputation: 15
Actually, I thought that, for the purposes of my application, a custom built, tiny parser is good enough. It would be probably a good idea if something like that would already exist, but the little I have spent analyzing sources of popular programs, I have noticed that all of them use integrated parsers.
 
Old 09-24-2004, 12:17 PM   #5
jpbarto
Senior Member
 
Registered: Mar 2003
Location: Pittsburgh, PA
Distribution: Gentoo / NetBSD
Posts: 1,251

Rep: Reputation: 45
Well I think I'm going to try my hand at writing one. It won't be XML but will follow coding standards such as httpd.conf (name value pairs and <></> sections). It doesn't make sense not to have one common one (like getopt).
 
Old 09-24-2004, 01:41 PM   #6
max_sipos
Member
 
Registered: Jul 2004
Posts: 96

Original Poster
Rep: Reputation: 15
True, if there is a way I could help, don't hesitate pm-ing me
Best,
 
Old 09-24-2004, 03:24 PM   #7
jpbarto
Senior Member
 
Registered: Mar 2003
Location: Pittsburgh, PA
Distribution: Gentoo / NetBSD
Posts: 1,251

Rep: Reputation: 45
uhm ... max ... uhm ... you have private messages turn off ... hehe.

Anyway, I've just started creating a couple of structs for holding config data and I should have something a little more testable here in a few days. Once I do I'll let you know as I'd appreciate a tester and a second pair of eyes to make sure the code isn't completely nuts.
 
Old 09-24-2004, 03:40 PM   #8
max_sipos
Member
 
Registered: Jul 2004
Posts: 96

Original Poster
Rep: Reputation: 15
Oops I see you're supposed to be a contributing member to use PM . I'll have to contribute some time

Anyways, I'd be very happy test the code, be sure to post it here when you're ready!
 
Old 09-28-2004, 01:05 AM   #9
jpbarto
Senior Member
 
Registered: Mar 2003
Location: Pittsburgh, PA
Distribution: Gentoo / NetBSD
Posts: 1,251

Rep: Reputation: 45
Max, I've completed some initial tests on an alpha release for a library of sorts. I'll be posting it along with a README on Tuesday. Please download it and give it a try. It's not too well tested yet but its operable (at least on NetBSD). Please take a look and make any recommendations you'd like.

jpbarto
 
Old 09-28-2004, 11:14 AM   #10
jpbarto
Senior Member
 
Registered: Mar 2003
Location: Pittsburgh, PA
Distribution: Gentoo / NetBSD
Posts: 1,251

Rep: Reputation: 45
Ok a first alpha release has been made available. You can download it from:

http://jpbarto.freeshell.org/?id=code#conf4c

It still needs a few things like better error handling, proper configuration and makefiles, and portability testing but its operational so I thought I'd make it available for testing.

jpbarto
 
Old 09-28-2004, 12:45 PM   #11
jpbarto
Senior Member
 
Registered: Mar 2003
Location: Pittsburgh, PA
Distribution: Gentoo / NetBSD
Posts: 1,251

Rep: Reputation: 45
Ok ... you remember that whole 'download it and try it' thing ... yeah ... well uhm ... unless you're going to run it on NetBSD you might have some problems. I just got a chance to try it out on a Gentoo box and the thing just segfaults right out. Some of the tests complete successfully but the big 'test-everything' test fails immediately ... so back to the coding board I go.
 
Old 09-29-2004, 01:42 PM   #12
jpbarto
Senior Member
 
Registered: Mar 2003
Location: Pittsburgh, PA
Distribution: Gentoo / NetBSD
Posts: 1,251

Rep: Reputation: 45
A better C4C has been posted and is ready for general tester consumption. A couple of detailed readmes are included with the download. To get it up and running just download the tarball, unpack, configure, make.

You can then cd to the test directory and run make. But before you execute any of the test binaries you will have to define LD_LIBRARY_PATH (unless you installed the C4C library). I also included txt files which dictate what the output from these binaries *should* look like so you can just run a diff on the actual vs predicted to see if something went wrong.

Enjoy:
http://jpbarto.freeshell.org/dl.php?id=C4C.tar.gz
 
Old 09-29-2004, 10:20 PM   #13
max_sipos
Member
 
Registered: Jul 2004
Posts: 96

Original Poster
Rep: Reputation: 15
Results of the testing

OK, I've did a short test of the code. I had no problems making. BTW I'm running slackware 10.

****************** System info:
cat /proc/cpuinfo:

processor : 0
vendor_id : AuthenticAMD
cpu family : 6
model : 6
model name : AMD Athlon(tm) XP 1900+
stepping : 2
cpu MHz : 1595.492
cache size : 256 KB
fdiv_bug : no
hlt_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : yes
cpuid level : 1
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 mmx fxsr sse syscall mp mmxext 3dno
wext 3dnow
bogomips : 3185.04

cat /proc/meminfo:

total: used: free: shared: buffers: cached:
Mem: 527814656 522891264 4923392 0 38309888 336007168
Swap: 509923328 16453632 493469696
MemTotal: 515444 kB
MemFree: 4808 kB
MemShared: 0 kB
Buffers: 37412 kB
Cached: 326556 kB
SwapCached: 1576 kB
Active: 223372 kB
Inactive: 218632 kB
HighTotal: 0 kB
HighFree: 0 kB
LowTotal: 515444 kB
LowFree: 4808 kB
SwapTotal: 497972 kB
SwapFree: 481904 kB

cat /proc/version:
Linux version 2.4.26 (root@tree) (gcc version 3.3.4) #6 Mon Jun 14 19:07:27 PDT 2004
******************

I've executed the test programs one by one and compared the differences to .output files.

testc4c: OK
testc4cf: OK
testconffile: OK
testconfsection: diff output:
11c11
< stor 1- 3
---
> stor 1- 3
17c17
< stor 2- 3
---
> stor 2- 3
testconfstor: OK
testconfvalue: OK

Besides the diff at testconfsection, all programs worked properly without anything like segfault

Hope this helps, I will continue testing when I find time!
 
Old 09-30-2004, 02:44 PM   #14
jpbarto
Senior Member
 
Registered: Mar 2003
Location: Pittsburgh, PA
Distribution: Gentoo / NetBSD
Posts: 1,251

Rep: Reputation: 45
It does help, there seem to be some differences or disparities in how strings are handled between BSD and Linux. I have a couple of functions which trim non-printables and whitespace, so I'll be looking closely at those to see where improvements can be made. Also I'm going to be constructing a Conf4CReader object and functions which will allow a conf file to be easily traversed via line-by-line. After that its on to being able to easily create a configuration file and then write it to disk, instead of just reading from disk. Other improvements and suggestions are greatly appreciated of course.

Thanks,
jpbarto
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Search in configuration files fiomba Linux - Software 9 10-31-2004 05:45 PM
Grub configuration files dwessell Mandriva 2 06-22-2004 01:36 PM
mplayer configuration files...???? avols143 Linux - General 1 02-18-2004 07:59 AM
configuration files dryhte Slackware 5 06-03-2003 11:12 AM
Where are programs files and configuration files stored? nutshell Linux - General 2 03-09-2002 11:24 AM


All times are GMT -5. The time now is 06:45 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration