diff --git a/config.c b/config.c index be50bb2..1256bc5 100644 --- a/config.c +++ b/config.c @@ -2,6 +2,7 @@ #include #include #include +#include /* * Here we check if the given string contains a section * for example [SECTIONName] here delimiterLeft is [ and delimiterRight is ] @@ -27,14 +28,12 @@ int checkSection(char *str,char delimiterLeft,char delimiterRight,char **section return ERROR_STR; } int len = strlen(str); - printf("strlen: %d",len); int state = ST_INIT; int leftDelimiterPos=0; int rightDelimiterPos=0; char *sectionName2 = NULL; while(state != ST_FINISH) { - printf("STATE:%i\n",state); switch(state) { case ST_INIT: @@ -96,6 +95,12 @@ int checkSection(char *str,char delimiterLeft,char delimiterRight,char **section int getStrAtPos(char *str,int fromPos,int toPos,char **name,int sizeName) { + if(*name == NULL) + { + int error = errno; + printf("Pointer NULL:%d\n",error); + return error; + } int i=fromPos; //character iterator, which starts from specified pos int j=0; //the character iterator for the target string int diffLen=toPos-fromPos; @@ -109,7 +114,6 @@ 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",ptr_name); return NO_ERROR; } /* @@ -144,6 +148,12 @@ int getStrAtPos(char *str,int fromPos,int toPos,char **name,int sizeName) int getNameValuePair(char *str,char leftDelimiterPos,char rightDelimiterPos,char **name,char **value,int sizeName,int sizeValue) { + if(*name == NULL || *value == NULL) + { + int error = errno; + printf("Pointer NULL:%d\n",error); + return error; + } int state=ST_INIT; char *ptr_name=*name; char *ptr_value=*value; @@ -159,11 +169,12 @@ int getNameValuePair(char *str,char leftDelimiterPos,char rightDelimiterPos,char return ERROR_DELIMITER_NOT_FOUND; } posDelimiter = (ptrDelimiter - str); - printf("LenUntilDelimiter: %d",posDelimiter); + printf("LenUntilDelimiter: %d\n",posDelimiter); if((ret=getStrAtPos(str,0,posDelimiter-1,&ptr_name,sizeName)) == NO_ERROR) { - printf("ptr_name:%s",ptr_name); + printf("ptr_name:%s\n",ptr_name); }else { + printf("Error at getStrAtPos:%d\n",ret); return ret; } if(rightDelimiterPos == 0) @@ -185,15 +196,33 @@ int getNameValuePair(char *str,char leftDelimiterPos,char rightDelimiterPos,char -int parseConfig(char *buffer,struct configEntry **entry,int configSizeCount,int *returnedCount) +int parseConfig(char *originalBuffer,struct configEntry **entry,int configSizeCount,int *returnedCount) { int state=ST_INIT; int ret=0; int i=0; + *returnedCount=0; char *sectionName=NULL; struct configEntry *ptr_entry = *entry; char *keyName=NULL; char *keyValue=NULL; + char *buffer=NULL; + + buffer = malloc(strlen(originalBuffer)+1); + if(buffer == NULL) + { + int error = errno; + printf("ERROR MALLOC:%d",error); + return ERR_PARSECONFIG_UNKNOWN; + } + memset(buffer,0,strlen(originalBuffer)+1); + strcpy(buffer,originalBuffer); + + //this step is necessary, because the strok function modifys the originalBuffer + //so we make a copy of it + strcpy(buffer,originalBuffer); + + printf("buffer:%s\n--",buffer); sectionName = malloc(MAX_LEN_SECTIONNAME); memset(sectionName,0,MAX_LEN_SECTIONNAME); @@ -203,47 +232,47 @@ int parseConfig(char *buffer,struct configEntry **entry,int configSizeCount,int 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; + printf("FOUND_SECTION:%s\n",sectionName); } break; case ST_FOUND_SECTION: keyName = malloc(MAX_LEN_SECTIONNAME); keyValue = malloc(MAX_LEN_SECTIONNAME); + if(keyName == NULL || keyValue == NULL) + { + int error = errno; + printf("MALLOC:%d\n",error); + //return error; + } memset(keyName,0,MAX_LEN_SECTIONNAME); memset(keyValue,0,MAX_LEN_SECTIONNAME); - if((ret=getNameValuePair(token,'=',0, - &keyName,&keyValue, - MAX_LEN_SECTIONNAME,MAX_LEN_SECTIONNAME - ))==NO_ERROR) + + ret=getNameValuePair(token,'=',0,&keyName,&keyValue,MAX_LEN_SECTIONNAME,MAX_LEN_SECTIONNAME); + if(ret==NO_ERROR) { - ptr_entry[i].sectionName = strdup(sectionName); - ptr_entry[i].keyValue= strdup(keyValue); - ptr_entry[i].keyName = strdup(keyName); - free(keyName); - free(keyValue); - if(i #include #include #include @@ -48,7 +49,7 @@ int main(void) } long neededSize=0; - if((ret=getFile("config.cfg",NULL,0,&neededSize))==NO_ERROR) + if((ret=getFile("config-segfault.cfg",NULL,0,&neededSize))==NO_ERROR) { printf("sucessfull retrieved size:%ld\n",neededSize); } @@ -57,10 +58,17 @@ int main(void) return 1; } content = malloc(neededSize); - memset(content,0,neededSize); - if((ret=getFile("config.cfg",&content,neededSize,&neededSize))==NO_ERROR) + printf("allocate buffer for filecontent size:%ld\n",neededSize); + if(content == NULL) { - printf("Sucessfull read file into buffer:%s|\n---\n",content); + int error = errno; + printf("MALLOC: %d\n",error); + return error; + } + memset(content,0,neededSize); + if((ret=getFile("config-segfault.cfg",&content,neededSize,&neededSize))==NO_ERROR) + { + //printf("Sucessfull read file into buffer:%s|\n---\n",content); } else { printf("Error on getFile:%d\n",ret); @@ -70,9 +78,46 @@ int main(void) struct configEntry *entry=NULL; int returnedCount=0; int i=0; + int count=10; - entry = malloc(10*sizeof(struct configEntry)); - parseConfig(content,&entry,10*sizeof(struct configEntry),&returnedCount); + entry = malloc(count*sizeof(struct configEntry)); + if(entry == NULL) + { + int error = errno; + printf("MALLOC: %d\n",error); + return 0; + } + ret = parseConfig(content,&entry,count,&returnedCount); + if(ret!=NO_ERROR) + { + printf("Error on parseConfig:%d\nReallocate from size %d, to %d\n",ret,count,returnedCount); + } + if(returnedCount > count) + { + for(i=0;i