LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 03-01-2004, 02:02 PM   #1
kubicon
Member
 
Registered: Aug 2003
Location: Limbo
Posts: 54

Rep: Reputation: 15
Suggestiions for config file parsing library


Wassa!

A couple of days ago I decided that I wanted to develop a network program, in Linux, in c++. I didn't know what I wanted to make, I just wanted to make one; just because I want to learn network programming, and for fun.

So I started thinking about what the program would offer. I gave it some thought but could come up with something quickly so I decide to start working on a parser for the soon to come config file. I didn't know what options I was gonna support, but I knew I wanted a config file in other to tweek the program options at runtime.

I looked online for a library that allowed me to easily parse config files but found none(I didn't look very hard to be honest). So I decided to right my own. I actually didn't mind all having to write my own config parser. I actually like it. As I wrote, I started thinking about the people that want to develop apps and that want to concentrate on the app itself, as opposed to a parser for the config file. And this is why I'm here.

I want to develop a parse libray for text based configuration files, for linux apps, in c++. I want to give it away when I'm finished. I would really appreciated if you guys give me some opinions and tell me about things I should consider.

This is some of the specs I've drafted thus far:

Notes:
white space is anything non-printable. e.i. spaces and tabs.


comments
1 - They will start with the well known '#' (pound/hash) character, much like convential shell programming
2 - comments don't have to start the line, there can be spaces before the comment.
3 - Empty lines are considered comments. In other words, they are ignored. Empty lines include lines that have nothing but white space and the new line character in it.
4 - Comments must appear on their own line. Thus:
directive = some value # bad comment
is not a directive with a value followed by a comment. Instead, it's a directive with a pound in the value
5 - A commented line ends at the end of the line.



Directives
1 - Directives -- an option with an associated value -- takes the form:
directiveName = value
2 - In each line, the only thing that can come before a directive is white space.
legal: directive = value
legal:directive = value
not llegal: foo directive = value
3 - There can be zero or more white space after the directive name and before the '=' sign.
legal: directive= value
legal: directive = value
legal: directive = value
4 - there can be zero or more white space after the = sign and before the value.
legal: directive =value
legal: direcive = value
legal: directive = value
5 - Rule 3 and 4 from above can be conbined:
legal:directive=value
legal: directive= value
legal: directive = value
legal: directive =value

6 - Each directive on the config file is consired optional. An optional directive will can be left empty, or out althoger from the config file. If a directive begins with an & (ampersan) character this directive is mandatoryy and must apear on the config file. Also, a mandatory directive cannot be empty If not, the parse will flag an error and exit.

# regular directive
name = foo

#mandatory directive
&greeting = something here


7 - If a normal directive doesn't have a value after the equal sign, it will be considered as an empty value, not an error. If a mandatory directive,doesn't have a value after the equal sign, it will be considered as an error;

8 - white space is trimmed from the start and end or the 'value'. White space between the 'value' is normailzed, meaning that if the value contains multiple spaces between words, those spaces become only one space. Consider the following(In this particular case, quotes are added to show space only, they are not actually part of the value):

"Hello, how are you?"

# same as above
" Hello, how are you? "

# the as the above two
" hello, how are you? "

9 - If you want to keep multiple white space, enclose the value in quotes (single or double, but there is a little difference).

greeting = hello, how are you?
# the above will yield: $greeting->"hello, how are you"

greeting = " hello, how are you? "
# the above will the following, without quotes: $greeting->" hello, how are you? "


10 - If you want to include quotes in the value, you must scape them with a backslash

qreeting = \"Hello, how are you?\"
# the above will yield the following, including quotes $greeting->"Hello, how are you?"
# space normalization will still apply to the above, if you want to include multiple spaces between words, you have to put quotes around the whole thing:

greeting = " \" Hello, how are you?\" "


11 - Single quotes are the same as double quotes, exept when it comes to variables substitution, see the 'Variables' section bellow.


Variables
1 - Normal variables begin with the '$' (dollar) sign. Environment variables have a '%' (percent) sign and must retein case.

2 - Legal variable characters are letter from a-z, including capitals, number from 0-9, the dash (-), and the underscore (_)
3 - Variables can appear only the right side of a directive:
directive_name = $foo
log = %HOME
4 - Normal variables must be defined before they are used.
5 - Normal variable are the values of previously defined directives. Thus, you can't define you own variables.

greeting = how are you?
reply = $greeting

6 - Variable substitution will take effect in double quoted values, but not in single quoted:

name = Happy Gilmore
greeting = "Welcome $name, how are you"
# the above will yield, without the quotes.
greeting->"Welcome Happy Gilmore, how are you"

name = Happy Gilmore
greeting = 'Welcome $name, how are you'
# the above will yield, without the quotes:
greeting->"Welcome $name, how are you"

7 - Multiple variable substituion can happen on one line:

user1 = Optimus
user2 = Galvatron
user3 = Bumble-Bee

userList = $user1, $user2, $user3
$userList->"Optimus,Galvatron,Bumble-Bee"


### --------------------- END -------------------------


I would love to hear suggestions and comments.


thanks
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
config file parsing in C/C++ kadissie Programming 12 08-26-2009 05:19 PM
HTML parsing library nodger Programming 1 09-01-2005 01:42 AM
Error Parsing Config file for X chiefreborn Suse/Novell 5 08-31-2005 06:30 PM
Parsing config files unholy Programming 5 09-18-2004 03:59 PM
can't locate parsing code for .config file coolwind Linux - General 1 12-08-2002 08:24 AM


All times are GMT -5. The time now is 12:02 AM.

Main Menu
 
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
Open Source Consulting | Domain Registration