Reputation: 2224
is there an elegant way of parsing .conf file in a c program? say, if i have normal text file -
param1 = 22
param2 = 99
param34 = 11
param11 = 15
...
it'd be nice to get access in one function call, smth like:
int c = xfunction(my.conf, param34);
and now c = 11. Many thanks in advance.
Upvotes: 0
Views: 179
Reputation: 40145
#include <stdio.h>
#include <stdlib.h>
#define xfunction(file, param) \
system("awk '/^" #param " = [0-9]+$/{ num = $3 };END { exit num }' " #file)
int main(void){
int c = xfunction(my.conf, param34);
printf("%d\n", c);
return 0;
}
Upvotes: 2
Reputation: 1746
It is better if you use a linked list as follow
struct node
{
char *key;
int value;
};
and assign all the key
value
pair to node and you can add as many node
during parsing of conf file to the linked list
.
later when you search you can simply traverse the linked list
and check for the key
by a simple strcmp()
and get the value.
Upvotes: 0