C program for reading .ini files 
Author Message
 C program for reading .ini files

Hi all,

I'm looking for C source code (or for directions of how to write it if
there's no ready made code available) that processes an .ini file that
looks something like this:

...
[measuringparameters]
apparatus1.float_parameter1 = 3.45
apparatus2.int_parameter1 = 67

[standarddir]
defaultdir = C:\temp
...

I would like the program to behave in a way that it reads a line, and
then determines the variable it belongs to, and sets the value of the
variable accordingly (so not that it already knows the variable and
searches the .ini file line to go with it, but the other way around).

The idea is that I have some global structures with measuring
parameters, and that these ini files are used to initialize these
parameters as the program starts up (parameters that are not in the .ini
file remain uninitialized or get a hard coded default value). Until now
I used comma separated value-only .ini files
(234.54,3,6,1,filename,12,etc). However these are highly unflexible and
also hard to understand which number means what, you have to have a .txt
file that describes the ini file. If you insert a number in the wrong
place all hell breaks loose because all the remaining parameters will
get the wrong value. It is far more convenient to see what you are doing
if you want to alter a parameter in the file by hand, and that the order
of apprearance doesn't matter.

I know how to open files and read strings etc, but I have no clue how a
program can determine the name of the variable it has to assign the
value to, dependent on the string that has just been read.

So I need a function that goes something like (syntax may be wrong as
I'm not doing C that much, but just to get the idea across):

char variablename[] = "whatever.int.variable";
int value = 123;

assign(char *variablename, int value)
{

...???

// result: whatever.int.variable=123;

Quote:
}

If you have ready made source code, or directions on how I can find
templates for this somewhere, or directions for how to do this myself
they are highly appreciated.

(crossposted to comp.lang.c and comp.sources.wanted, de-crosspost
according to content)

Greetings,

Erwin Timmerman

--
"Tell you one thing, when your solid state parts wear out you can't play
rocket ship with any of them." - Twang (re tubes vs transistors)

Links to a lot of recording FAQs: http://www.*-*-*.com/



Tue, 19 Oct 2004 23:17:01 GMT  
 C program for reading .ini files

Quote:

> I'm looking for C source code (or for directions of how to write it if
> there's no ready made code available) that processes an .ini file that
> looks something like this:

Back in my MS-DOS programming days, I loved the .INI file format and
wrote some code that allowed reading/writing and editing of them.  I
wrote this a long time ago (early '90s), and I've learned a lot since
then, but you're welcome to take a look at it:

     http://www.Sonnack.com/WillCall/ini_files.zip

It was part of a large library I wrote back then, and I haven't included
the whole thing, so bits are missing, but it shouldn't be too hard to
fill in (lot of it was home-grown string functions).  And I'm very, very
embarrassed to admit this,.... but I USED to do some very ugly things
like this:

#define   IS       ==
#define   ISNT     !=
#define   GT       >
#define   LT       <

You get the idea.  It makes me shudder now (and clc was instrumental
in setting me straight!).

--

|_ http://www.Sonnack.com/ ___________________| Call: 1-800-DEV-NULL  |
|_____________________________________________|_______________________|

Opinions expressed herein are my own and may not represent those of my employer.



Tue, 19 Oct 2004 23:29:46 GMT  
 C program for reading .ini files

Quote:
>I need a function that goes something like (syntax may be wrong as
>I'm not doing C that much, but just to get the idea across):

>char variablename[] = "whatever.int.variable";
>int value = 123;

>assign(char *variablename, int value)
>{

>...???

>// result: whatever.int.variable=123;

>}

this can't be done, as you describe it.  i suggest a linked list or tree
wherein you maintain name / value pairings, e.g.,

[warning: no error checking, c89]

#include <stdlib.h>
#include <string.h>

union var_types { /* all the types we want to use */
  int i;

Quote:
};

struct var_node {
  struct var_node *next;
  char            *name;
  union var_types  value;

Quote:
} *var_head = 0;

struct var_node * var_locate(char *varname) {
  struct var_node *p;
  if (0 != var_head)
    for(p = var_head; 0 != p->next; p = p->next)
      if (0 == strcmp(p->name, varname))
        return p;
  return 0;

Quote:
}

struct var_node * var_locate_or_create(char *varname) {
  struct var_node *p = var_locate(varname);
  if (0 == p) {
    p = malloc(sizeof *p);
    p->next = var_head;
    /* p->name = strdup(varname); */
    { size_t l = strlen(varname);
      char *s = malloc(l+1);
      p->name = strcpy(s, varname); }
    var_head = p;
  }
  return p;

Quote:
}

int var_assign_int(char *varname, int value) {
  return var_locate_or_create(varname)->value.i = value;

Quote:
}

int main(void) {
  return var_assign_int("foo.bar", 0);

Quote:
}

--
bringing you boring signatures for 17 years


Wed, 20 Oct 2004 19:37:40 GMT  
 C program for reading .ini files

Quote:
> Hi all,
> I'm looking for C source code (or for directions of how to write it if
> there's no ready made code available) that processes an .ini file that
> looks something like this:

Check at www.freshmeat.net, do a search for iniparse, or if that fails,
just search for ini. there was a library posted there not long ago for
this very purpose.

Quote:
> ...
> [measuringparameters]
> apparatus1.float_parameter1 = 3.45
> apparatus2.int_parameter1 = 67
> [standarddir]
> defaultdir = C:\temp
> ...

--

                    The Lord detests the way of the wicked
                  but he loves those who pursue righteousness.
----------------------------- Proverbs 15:9 (niv) -----------------------------


Wed, 20 Oct 2004 20:02:54 GMT  
 C program for reading .ini files

Quote:

> Hi all,

> I'm looking for C source code (or for directions of how to write it if
> there's no ready made code available) that processes an .ini file

Thanks all for the reactions and answers. It really has helped me.
Thanks for all the effort, *much* appreciated.

Greetings,

Erwin Timmerman

--
"Tell you one thing, when your solid state parts wear out you can't play
rocket ship with any of them." - Twang (re tubes vs transistors)

Links to a lot of recording FAQs: http://go.to/recordingfaq



Sat, 23 Oct 2004 17:15:14 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Newbie: separate big .cs file into small .cs files

2. 2nd try: need a C library file for reading Windows-style .INI files

3. Function to read a .ini file

4. How to read INI files (16bit DOS)

5. Need source to read INI files, help?

6. Looking for source to read/write INI files?

7. Reading .INI files

8. who to read ini file

9. how can read or write an INI file?

10. INI file reading and writing

11. code for reading/writing INI files

12. How to read "ini."-File ?

 

 
Powered by phpBB® Forum Software