diff --git a/config.c b/config.c index cf0088d..5bca523 100644 --- a/config.c +++ b/config.c @@ -69,10 +69,12 @@ int checkSection(char *str,char delimiterLeft,char delimiterRight,char **section break; case ST_FOUND_RIGHT_DELIMITER: int ret=0; - sectionName2 = malloc(MAX_LEN_SECTIONNAME*sizeof(char)); + sectionName2 = malloc(MAX_LEN_SECTIONNAME); + memset(sectionName2,0,MAX_LEN_SECTIONNAME); if((ret=getStrAtPos(str,leftDelimiterPos,rightDelimiterPos,§ionName2,MAX_LEN_SECTIONNAME)) == NO_ERROR) { - *sectionName = sectionName2; + *sectionName = strdup(sectionName2); + free(sectionName2); state = ST_FINISH; }else{ return ret; @@ -111,7 +113,7 @@ int getStrAtPos(char *str,int fromPos,int toPos,char **name,int sizeName) ptr_name[j] = str[i]; } ptr_name[j+1]='\0'; - printf("*name:%s\n",*name); + //printf("*name:%s\n",ptr_name); return NO_ERROR; } @@ -167,8 +169,8 @@ int parseConfig(char *buffer,struct configEntry **entry,int configSizeCount,int char *keyName=NULL; char *keyValue=NULL; - keyName = malloc(MAX_LEN_SECTIONNAME); - keyValue = malloc(MAX_LEN_SECTIONNAME); + sectionName = malloc(MAX_LEN_SECTIONNAME); + //read buffer line by line char *token; token = strtok(buffer,"\n"); @@ -178,12 +180,15 @@ int parseConfig(char *buffer,struct configEntry **entry,int configSizeCount,int { 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: + keyName = malloc(MAX_LEN_SECTIONNAME); + keyValue = malloc(MAX_LEN_SECTIONNAME); if((ret=getNameValuePair(token,'=',0, &keyName,&keyValue, MAX_LEN_SECTIONNAME,MAX_LEN_SECTIONNAME @@ -192,12 +197,15 @@ int parseConfig(char *buffer,struct configEntry **entry,int configSizeCount,int ptr_entry[i].sectionName = strdup(sectionName); ptr_entry[i].keyValue= strdup(keyValue); ptr_entry[i].keyName = strdup(keyName); + free(keyName); + free(keyValue); if(i