|
ParseConfig
- parseConfig.c
#include <stdio.h>
#include <string.h>
//einfaches Parserprogramm
char *get_config(char *name, FILE *file) {
char buffer[200];
char a_name[100];
static char value[100];
size_t i,len;
rewind(file);
while (!feof(file)) {
fscanf(file," %200[^\n]",buffer);
if (';' == buffer[0] )
continue;
sscanf(buffer," %99[^ =] = %99[^\n]",a_name,value);
len = strlen(a_name);
for (i=0;i<len;i++)
if ('_' == a_name[i] )
memmove(a_name+i,a_name+i+1,len-i);
if (!strcasecmp(a_name,name))
break;
else value[0] = '\0';
}
return value;
}
//--------------------------------------------
int main(void) {
FILE *config;
char *configfile="test.cfg";
char *pString;
if (NULL == (config = fopen(configfile,"r" )))
fprintf(stderr,"\nUnable to open configuration file %s\n",configfile);
//---------------
unsigned short i=0;
char *p[]={"name","wert","height","width"};
// printf("Anzahl der Elemente: %d\n", sizeof(p)/sizeof(int));
for (i=0;i<(sizeof(p)/sizeof(int));i++)
{
pString = get_config(p[i],config);//Abfrage , ob der Eintrag existiert
if (pString[0])
printf("Variable %s ist %s\n", p[i],get_config(p[i],config));
else
printf("Fehler: %s nicht in config %s gefunden\n",p[i],configfile);
}
fclose(config);
return 0;
}
//---------------------------------------------
/* test.cfg Datei zum parsen
#this a comment
;this a comment
name = test2
wert = 4711
height=80
*/
|
|