add configparser
This commit is contained in:
121
config.c
121
config.c
@@ -2,64 +2,6 @@
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#include<string.h>
|
||||
|
||||
int loadConfig(char *str_file, char **str_entry,char **host,int *intervall,int size)
|
||||
{
|
||||
int i=0;
|
||||
FILE *f_config;
|
||||
char temp_str[256];
|
||||
char *ret = NULL;
|
||||
|
||||
printf("\nopening config file\n");
|
||||
f_config = fopen(str_file,"r");
|
||||
if(f_config != NULL)
|
||||
{
|
||||
ret = fgets(temp_str,256,f_config);
|
||||
while(ret!=NULL)
|
||||
{
|
||||
temp_str[strcspn(temp_str,"\n")]=0;
|
||||
printf("first temp_str:%s\n",temp_str);
|
||||
if(strcmp(temp_str,"[Host]")==0)
|
||||
{
|
||||
printf("temp_str host:%s\n",temp_str);
|
||||
while(ret!=NULL)
|
||||
{
|
||||
printf("temp_str:%s\n",temp_str);
|
||||
ret = fgets(temp_str,256,f_config);
|
||||
if(strchr(temp_str,'[')==NULL)
|
||||
{
|
||||
printf("strchr\n");
|
||||
if(strstr(temp_str,"Hostname")!=NULL)
|
||||
{
|
||||
char *ptr_host = strrchr(temp_str,'=')+1;//Without =
|
||||
ptr_host[strcspn(ptr_host,"\n")]=0;
|
||||
while(ptr_host[0]==' ') //Potential segfault??
|
||||
{
|
||||
if(ptr_host < (temp_str + strlen(temp_str)))
|
||||
{
|
||||
ptr_host = ptr_host + 1;
|
||||
}else{
|
||||
break;
|
||||
}
|
||||
}
|
||||
*host = strdup(ptr_host);
|
||||
printf("my host is:%s\n",*host);
|
||||
}
|
||||
}else{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr,"error opening file[%s]\n",str_file);
|
||||
}
|
||||
fclose(f_config);
|
||||
printf("Done reading config file\n");
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
* Here we check if the given string contains a section
|
||||
* for example [SECTIONName] here delimiterLeft is [ and delimiterRight is ]
|
||||
@@ -211,3 +153,66 @@ int getNameValuePair(char *str,char leftDelimiterPos,char rightDelimiterPos,char
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int parseConfig(char *buffer,struct configEntry **entry,int configSizeCount)
|
||||
{
|
||||
int state=ST_INIT;
|
||||
int ret=0;
|
||||
int i=0;
|
||||
char *sectionName=NULL;
|
||||
struct configEntry *ptr_entry = *entry;
|
||||
char *keyName=NULL;
|
||||
char *keyValue=NULL;
|
||||
|
||||
keyName = malloc(MAX_LEN_SECTIONNAME);
|
||||
keyValue = malloc(MAX_LEN_SECTIONNAME);
|
||||
//read buffer line by line
|
||||
char *token;
|
||||
token = strtok(buffer,"\n");
|
||||
while(token != NULL && state != ST_FINISH)
|
||||
{
|
||||
switch(state)
|
||||
{
|
||||
case ST_INIT:
|
||||
printf("token(line): %s\n",token);
|
||||
if((ret=checkSection(token,'[',']',§ionName))==FOUND_SECTION)
|
||||
{
|
||||
state = ST_FOUND_SECTION;
|
||||
}
|
||||
break;
|
||||
case ST_FOUND_SECTION:
|
||||
if((ret=getNameValuePair(token,'=',0,
|
||||
&keyName,&keyValue,
|
||||
MAX_LEN_SECTIONNAME,MAX_LEN_SECTIONNAME
|
||||
))==NO_ERROR)
|
||||
{
|
||||
ptr_entry[i].sectionName = strdup(sectionName);
|
||||
ptr_entry[i].keyValue= strdup(keyValue);
|
||||
ptr_entry[i].keyName = strdup(keyName);
|
||||
if(i<configSizeCount-1)
|
||||
{
|
||||
i++;
|
||||
}
|
||||
else {
|
||||
return ERROR_STR;
|
||||
}
|
||||
state = ST_INIT;
|
||||
}
|
||||
else {
|
||||
state = ST_SKIP_READ;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if(state != ST_SKIP_READ)
|
||||
{
|
||||
token = strtok(NULL,"\n");
|
||||
}
|
||||
else {
|
||||
state = ST_INIT;
|
||||
}
|
||||
}
|
||||
|
||||
free(keyName);
|
||||
free(keyValue);
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user