Configuration files from hell...

Requirements, Design, UML, Project management and general software engineering discussions.

Moderator:Moderators

Post Reply
User avatar
linuxfreak
Posts:52
Joined:Tue Jul 26, 2011 7:36 pm
Location:Paarl, Western Cape, South Africa
Configuration files from hell...

Post by linuxfreak » Sun Mar 04, 2012 9:08 pm

Hi all:

I don't know if this topic fits in here or not, but lets get cracking...

Okay, I have this file called config.cfg that is 'kinda like windows inf/ini files. It stores all the user settings(stuff like, username, password, privlages, date last accessed etc). This file can not be edited by users.

Now the problem is, I need to parse the file for a spesific value, like the value 'username=root'. I need to search for "username=" and then get its value.

I can read a value from that file into a variable by reading the first line of the file by doing "volReadFile(&fname,<variable name>, 16);". This reads the first line of the file into a c++ variable, but the file then only contains the term "root" - wich is what i want, but what about multiple values.

How can i read from different lines?
Regards.

User avatar
Mike
Site Admin
Posts:465
Joined:Sat Oct 20, 2007 7:58 pm
Contact:

Re: Configuration files from hell...

Post by Mike » Fri Mar 16, 2012 7:40 pm

Hello,

It is recommended to read the entire file into a buffer and parse it using more traditional methods. Then we can write a function similar to GetProfileString() that would look through the buffer watching for matching tokens to locate where a section starts and a matching key name within that section. With a basic lexer the actual parsing becomes much easier. This would allow you to call a single routine to obtain any setting from the configuration file.

If requested, I can provide a working example that demonstrates this.

User avatar
linuxfreak
Posts:52
Joined:Tue Jul 26, 2011 7:36 pm
Location:Paarl, Western Cape, South Africa

Re: Configuration files from hell...

Post by linuxfreak » Sun Mar 18, 2012 6:45 am

Thanx mike.

Sorry I did not look at it this way. :roll: It would be very kind of you to give an example! :lol:

That code I used, was basically just to see if I can read text from a file into a variable and use it in the rest of the kernel as a conditional. But now that I go through the code again, I saw that it is already being done(read text file into buffer variable). :shock:

So then the file would be similar to an XML file? That would be cool! It will also make it a lot easier storing multiple variables.

Thanx again!!!!

Regards.
The Anti-Microsoft revolution has begun, and soon Apple will take over the universe. The world is primed for war - someone should just take the first blow.

User avatar
linuxfreak
Posts:52
Joined:Tue Jul 26, 2011 7:36 pm
Location:Paarl, Western Cape, South Africa

Re: Configuration files from hell...

Post by linuxfreak » Tue Mar 20, 2012 6:48 pm

Hey, ill take a slight shot at the lexer:

a basic(basic!) version of a lexer! Correct me if im wrong!

Code: Select all

FILE file = volOpenFile(filename);

//Test to see if the file is valid here:

// Read contents of file into buffer:
volReadFile(&file, buffer, buffsize) // buffsize = file.fileLenght

/* Loop through buffer to get token:
(this is where i get stuck, i dont know what to do here! also need to go beyond the "=" to get the value */

// return the value of the variable in the buffer if found, else continue searching!
-- well that is an outline of what i got. But I need to get the code to search the buffer for the token and get its value, and then continue searching if its not found!

Also, if I can get this 2 work, can I implement syntax highlighting if I read a source file inside my os? Just a stupid question!!! :)
The Anti-Microsoft revolution has begun, and soon Apple will take over the universe. The world is primed for war - someone should just take the first blow.

User avatar
Mike
Site Admin
Posts:465
Joined:Sat Oct 20, 2007 7:58 pm
Contact:

Re: Configuration files from hell...

Post by Mike » Sat Mar 31, 2012 10:45 pm

Hello,

I recommend writing the parser in a user mode environment first so you can utilize better debuggers. You do not technically need a lexer, but it does help out.

The goal of the lexer is to convert a buffer of data into tokens that can be easily parsed by the syntax analyzer. For example, the lexer can accept the buffer as input and output an array of tokens. (The lexer can also skip comments and whitespace.) A token can look something like this:

Code: Select all

#define TOKEN_INT 1
#define TOKEN_VALUE 2
#define TOKEN_SYMBOL 4
#define TOKEN_STRING 8

typedef struct _token {
   int type;
   union {
     int value;
     char symbol;
     char* str;
   }u;
}token;
Your current lexer source looks alright so far. In order to continue, you need to keep track of your current position in the buffer and loop through each character, skipping whitespace. The loop will also need to look at the characters it encounters to see what type of token it is. For example, if the token is a 'a' we can assume its a CHAR token. If its a '1' we can assume its a INT token. This is a simplification though because it does not take into consideration cases like "ab12". This is easy to fix though by just looking at the following character after the current one being looked at.

Sorry, I have not had the time to work on a working example yet. If it is still requested, Ill see about providing it this weekend. :)

User avatar
linuxfreak
Posts:52
Joined:Tue Jul 26, 2011 7:36 pm
Location:Paarl, Western Cape, South Africa

Re: Configuration files from hell...

Post by linuxfreak » Tue Apr 03, 2012 10:13 am

Hi.

Thanks mike, that was very helpful and it made a few thinks more clear. I will start working on it as soon as possible and give updates, although I would still like a working example if it is not too much work. :) And I am in no rush so, take your time. :P

Again, thanks!
Jean-Pierre:
The Anti-Microsoft revolution has begun, and soon Apple will take over the universe. The world is primed for war - someone should just take the first blow.

Post Reply