testing section logic
This commit is contained in:
41
config.c
41
config.c
@@ -94,14 +94,19 @@ int checkSection(char *str,char delimiterLeft,char delimiterRight,char **section
|
||||
{
|
||||
return ERROR_STR;
|
||||
}
|
||||
len = strlen(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-1;i++) //find first (left) delimiter
|
||||
for(i=0;i<=len;i++) //find first (left) delimiter
|
||||
{
|
||||
if(str[i] == delimiterLeft)
|
||||
{
|
||||
@@ -112,7 +117,7 @@ int checkSection(char *str,char delimiterLeft,char delimiterRight,char **section
|
||||
}
|
||||
break;
|
||||
case ST_FOUND_LEFT_DELIMITER:
|
||||
for(i=0;i<len-1;i++) //find second (right) delimiter
|
||||
for(i=0;i<=len;i++) //find second (right) delimiter
|
||||
{
|
||||
if(str[i] == delimiterRight)
|
||||
{
|
||||
@@ -121,9 +126,24 @@ int checkSection(char *str,char delimiterLeft,char delimiterRight,char **section
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(state != ST_FOUND_RIGHT_DELIMITER)
|
||||
{
|
||||
return ST_ERROR_NOT_FOUND_RIGHT_DELIMITER;
|
||||
}
|
||||
break;
|
||||
case ST_FOUND_RIGHT_DELIMITER:
|
||||
getStrAtPos(*str,leftDelimiterPos,rightDelimiterPos,section);
|
||||
int ret=0;
|
||||
sectionName2 = malloc(MAX_LEN_SECTIONNAME*sizeof(char));
|
||||
if((ret=getStrAtPos(str,leftDelimiterPos,rightDelimiterPos,§ionName2,MAX_LEN_SECTIONNAME)) == NO_ERROR)
|
||||
{
|
||||
*sectionName = sectionName2;
|
||||
state = ST_FINISH;
|
||||
}else{
|
||||
return ret;
|
||||
}
|
||||
case ST_FINISH:
|
||||
return FOUND_SECTION;
|
||||
}
|
||||
}
|
||||
return NO_SECTION;
|
||||
}
|
||||
@@ -136,19 +156,26 @@ int checkSection(char *str,char delimiterLeft,char delimiterRight,char **section
|
||||
* 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 toPosm,char **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
|
||||
if(toPos - fromPos > MAX_LEN_SECTIONNAME)
|
||||
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++)
|
||||
{
|
||||
*name[j] = str[i];
|
||||
ptr_name[j] = str[i];
|
||||
}
|
||||
ptr_name[j+1]='\0';
|
||||
printf("*name:%s\n",*name);
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user