#include"config.h" #include #include #include /* * Here we check if the given string contains a section * for example [SECTIONName] here delimiterLeft is [ and delimiterRight is ] * if a section is found, the section name will be written to sectionName * * Input: * char **str: the string to check * char delimiterLeft: the left delimiter to check for * char delimiterRight: the right delimiter to check for * Output: * char **sectionName: if found the section Name of the section * Return: * int status_code: one of the following values: * */ int checkSection(char *str,char delimiterLeft,char delimiterRight,char **sectionName) { char section[MAX_LEN_SECTIONNAME]; int i = 0; //most outer loop -> character of string if(str == NULL) { 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: for(i=0;i<=len;i++) //find first (left) delimiter { if(str[i] == delimiterLeft) { state = ST_FOUND_LEFT_DELIMITER; leftDelimiterPos = i; break; } } if(state != ST_FOUND_LEFT_DELIMITER) { return ST_ERROR_NOT_FOUND_LEFT_DELIMITER; } break; case ST_FOUND_LEFT_DELIMITER: for(i=0;i<=len;i++) //find second (right) delimiter { if(str[i] == delimiterRight) { state = ST_FOUND_RIGHT_DELIMITER; rightDelimiterPos = i; break; } } if(state != ST_FOUND_RIGHT_DELIMITER) { return ST_ERROR_NOT_FOUND_RIGHT_DELIMITER; } break; case ST_FOUND_RIGHT_DELIMITER: int ret=0; if((ret=getStrAtPos(str,leftDelimiterPos,rightDelimiterPos,sectionName,MAX_LEN_SECTIONNAME)) == NO_ERROR) { state = ST_FINISH; }else{ return ret; } case ST_FINISH: return FOUND_SECTION; } } return NO_SECTION; } /* * Here we get / cut from fromPos to toPos and write it to an address * * Input: * char *str: the string to cut * int fromPos: from which position we want to copy, the pos is included and zero indexed * int toPos: to which position we want to copy, the pos is included and zero indexed * Output: * char **name: here we will write the section name to * Input: * int sizeName: the size of the user allocated buffer at **name */ int getStrAtPos(char *str,int fromPos,int toPos,char **name,int sizeName) { int i=fromPos; //character iterator, which starts from specified pos int j=0; //the character iterator for the target string int diffLen=toPos-fromPos; char *ptr_name=*name; if(diffLen > MAX_LEN_SECTIONNAME || diffLen>sizeName) { return ERROR_MAX_LEN; } for(i=fromPos,j=0;i<=toPos;i++,j++) { ptr_name[j] = str[i]; } ptr_name[j+1]='\0'; //printf("*name:%s\n",ptr_name); return NO_ERROR; } int getNameValuePair(char *str,char leftDelimiterPos,char rightDelimiterPos,char **name,char **value,int sizeName,int sizeValue) { int state=ST_INIT; char *ptr_name=*name; char *ptr_value=*value; int ret=0; char *ptrDelimiter=NULL; int posDelimiter=0; int posEnd=0; ptrDelimiter=strchr(str,leftDelimiterPos); if(ptrDelimiter==NULL) { return ERROR_DELIMITER_NOT_FOUND; } posDelimiter = (ptrDelimiter - str); printf("LenUntilDelimiter: %d",posDelimiter); if((ret=getStrAtPos(str,0,posDelimiter-1,&ptr_name,MAX_LEN_SECTIONNAME)) == NO_ERROR) { printf("ptr_name:%s",ptr_name); }else { return ret; } if(rightDelimiterPos == 0) { posEnd = strlen(str); }else{ posEnd = rightDelimiterPos; } if((ret=getStrAtPos(str,posDelimiter+1,posEnd,&ptr_value,MAX_LEN_SECTIONNAME)) == NO_ERROR) { printf("ptr_name:%s\n",ptr_value); }else { return ret; } return NO_ERROR; } int parseConfig(char *buffer,struct configEntry **entry,int configSizeCount,int *returnedCount) { int state=ST_INIT; int ret=0; int i=0; char *sectionName=NULL; struct configEntry *ptr_entry = *entry; char *keyName=NULL; char *keyValue=NULL; //read buffer line by line char *token; token = strtok(buffer,"\n"); while(token != NULL && state != ST_FINISH) { switch(state) { case ST_INIT: sectionName = malloc(MAX_LEN_SECTIONNAME); 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); 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) { ptr_entry[i].sectionName = strdup(sectionName); ptr_entry[i].keyValue= strdup(keyValue); ptr_entry[i].keyName = strdup(keyName); free(sectionName); free(keyName); free(keyValue); if(i