are there better ways to read config file? 
Author Message
 are there better ways to read config file?

hello.
I got a question the way, better than that I used to read config file or
article header, does exist. I think that my codes, under the such situation,
are tremendously ugly :-(. But I cannot device the better. Please help me.

I used codes like:

int readconf( char *conf )
{
    int i;
    char buf[BUF_SIZ], tmp[BUF_SIZ];
    char *cp;
    FILE *fp;

    if(!(fp = fopen(CONFIG_FNAME, "r"))) {
        handle_error_and_exit();
    }

    while(fgets(tmp, sizeof(buf), fp)) {
        strip_initial_space_chars_and_saveit_to_buf(buf, tmp);
        if(buf[0] == '#') continue;

        if(!strncmp( FLD_NAME1, buf, strlen(FLD_NAME1)) {
            cp = buf;
            cp += strlen(FLD_NAME1);
            strncpy(conf->fld1, strip_not_allowed_chars(cp), MAX_VAL_SIZ);
        }
        else if(!strncmp( FLD_NAME2, buf, strlen(FLD_NAME2)) {
            cp = buf;
            cp += strlen(FLD_NAME2);
            strncpy(conf->fld1, strip_not_allowed_chars(cp), MAX_VAL_SIZ);
        }
       ....
    }
    return 1;

Quote:
}

Do I continue the if, else if..., ever??
Is there another way?

thank you.



Thu, 20 Feb 2003 00:13:00 GMT  
 are there better ways to read config file?

Quote:

> hello.
> I got a question the way, better than that I used to read config file or
> article header, does exist. I think that my codes, under the such situation,
> are tremendously ugly :-(. But I cannot device the better. Please help me.

> I used codes like:

> int readconf( char *conf )
> {
>     int i;
>     char buf[BUF_SIZ], tmp[BUF_SIZ];
>     char *cp;
>     FILE *fp;

>     if(!(fp = fopen(CONFIG_FNAME, "r"))) {
>         handle_error_and_exit();
>     }

>     while(fgets(tmp, sizeof(buf), fp)) {
>         strip_initial_space_chars_and_saveit_to_buf(buf, tmp);
>         if(buf[0] == '#') continue;

>         if(!strncmp( FLD_NAME1, buf, strlen(FLD_NAME1)) {
>             cp = buf;
>             cp += strlen(FLD_NAME1);
>             strncpy(conf->fld1, strip_not_allowed_chars(cp), MAX_VAL_SIZ);
>         }
>         else if(!strncmp( FLD_NAME2, buf, strlen(FLD_NAME2)) {
>             cp = buf;
>             cp += strlen(FLD_NAME2);
>             strncpy(conf->fld1, strip_not_allowed_chars(cp), MAX_VAL_SIZ);
>         }
>        ....
>     }
>     return 1;
> }

> Do I continue the if, else if..., ever??
> Is there another way?

> thank you.

Good question, with a good answer.

Yes, there is another, much better, way.

Store your constants (the things to which you compare your input) in an
array, like this:

const char *FieldName[] =
{
  FLD_NAME1,
  FLD_NAME2,
  FLD_NAME3,
  /* ... etc ... */
  FLD_NAMEn

Quote:
};

Count them at run-time like this:

size_t numconfigs = sizeof FieldName / sizeof FieldName[0];
size_t thisconfig;
int found;

Then, inside your while loop, add this loop:

  found = 0;
  for(thisconfig = 0; !found && thisconfig < numconfigs; thisconfig++)
  {
    if(!strncmp( FieldName[thisconfig], buf,
strlen(FieldName[thisconfig]) {

    /* do something with value - you may need to give conf an array too
*/
  }

This will work fine if you think it through correctly, but it's still
not optimal. There is a variety of ways in which you could speed this
process up, but they're a little bit involved to discuss at this stage.
One day, look up binary search, trees, and so on. For now, just use the
loop.

--
Richard Heathfield
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
65 K&R Answers: http://users.powernet.co.uk/eton/kandr2/index.html (32
to go)



Thu, 20 Feb 2003 00:49:26 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Config files: best to use struct when reading in data

2. Best way to have config file for Assembly?

3. read Config-file module for C?

4. reading config files

5. basic config file read

6. How to read a config file in C

7. looking for function to read config file

8. Library for reading config files?

9. read config file

10. Updating the security.config file in the CONFIG directory

11. i am unable to read from a file having 26ascii character(ctrl+z)

12. Help with ideas, best ways etc...

 

 
Powered by phpBB® Forum Software