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;
|
return ERROR_STR;
|
||||||
}
|
}
|
||||||
len = strlen(str);
|
int len = strlen(str);
|
||||||
|
printf("strlen: %d",len);
|
||||||
int state = ST_INIT;
|
int state = ST_INIT;
|
||||||
|
int leftDelimiterPos=0;
|
||||||
|
int rightDelimiterPos=0;
|
||||||
|
char *sectionName2 = NULL;
|
||||||
while(state != ST_FINISH)
|
while(state != ST_FINISH)
|
||||||
{
|
{
|
||||||
|
printf("STATE:%i\n",state);
|
||||||
switch(state)
|
switch(state)
|
||||||
{
|
{
|
||||||
case ST_INIT:
|
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)
|
if(str[i] == delimiterLeft)
|
||||||
{
|
{
|
||||||
@@ -112,7 +117,7 @@ int checkSection(char *str,char delimiterLeft,char delimiterRight,char **section
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ST_FOUND_LEFT_DELIMITER:
|
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)
|
if(str[i] == delimiterRight)
|
||||||
{
|
{
|
||||||
@@ -121,9 +126,24 @@ int checkSection(char *str,char delimiterLeft,char delimiterRight,char **section
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if(state != ST_FOUND_RIGHT_DELIMITER)
|
||||||
|
{
|
||||||
|
return ST_ERROR_NOT_FOUND_RIGHT_DELIMITER;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case ST_FOUND_RIGHT_DELIMITER:
|
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;
|
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
|
* int toPos: to which position we want to copy, the pos is included and zero indexed
|
||||||
* Output:
|
* Output:
|
||||||
* char **name: here we will write the section name to
|
* 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 i=fromPos; //character iterator, which starts from specified pos
|
||||||
int j=0; //the character iterator for the target string
|
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;
|
return ERROR_MAX_LEN;
|
||||||
}
|
}
|
||||||
for(i=fromPos,j=0;i<=toPos;i++,j++)
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
8
config.h
8
config.h
@@ -1,14 +1,20 @@
|
|||||||
#define NO_SECTION 0
|
#define NO_SECTION 0
|
||||||
|
#define FOUND_SECTION 10
|
||||||
#define ERROR_STR 1
|
#define ERROR_STR 1
|
||||||
#define ERROR_MAX_LEN 2
|
#define ERROR_MAX_LEN 2
|
||||||
|
#define NO_ERROR 0
|
||||||
|
|
||||||
//State Machine
|
//State Machine
|
||||||
|
|
||||||
#define ST_INIT 0
|
#define ST_INIT 0
|
||||||
#define ST_FOUND_LEFT_DELIMITER 1
|
#define ST_FOUND_LEFT_DELIMITER 1
|
||||||
#define ST_FOUND_RIGHT_DELIMITER 2
|
#define ST_FOUND_RIGHT_DELIMITER 2
|
||||||
|
#define ST_ERROR_NOT_FOUND_RIGHT_DELIMITER 3
|
||||||
#define ST_FINISH 20
|
#define ST_FINISH 20
|
||||||
|
|
||||||
|
//state machine ERROR
|
||||||
|
#define ST_ERROR_GETSTRATPOS 30
|
||||||
|
|
||||||
//LIMITS
|
//LIMITS
|
||||||
#define MAX_LEN_SECTIONNAME 128
|
#define MAX_LEN_SECTIONNAME 128
|
||||||
|
|
||||||
@@ -17,4 +23,4 @@
|
|||||||
int loadConfig(char *file, char **str_entry,char **host,int *intervall,int size);
|
int loadConfig(char *file, char **str_entry,char **host,int *intervall,int size);
|
||||||
int checkSection(char *str,char delimiterLeft,char delimiterRight,char **sectionName);
|
int checkSection(char *str,char delimiterLeft,char delimiterRight,char **sectionName);
|
||||||
|
|
||||||
int getStrAtPos(char *str,int fromPos,int toPos, char **name);
|
int getStrAtPos(char *str,int fromPos,int toPos, char **name,int sizeName);
|
||||||
|
|||||||
Reference in New Issue
Block a user