diff --git a/config.c b/config.c index 0f94625..c2b6a59 100644 --- a/config.c +++ b/config.c @@ -2,64 +2,6 @@ #include #include #include - -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 #include +#include #include "file.h" int main(void) @@ -56,6 +57,8 @@ int main(void) else { printf("Error on getFile:%d\n",ret); } + + parseConfig(content,&entry,sizeEntry);