testing section logic

This commit is contained in:
jonathan santis
2025-05-13 11:04:13 +02:00
parent 6e73734672
commit f4d2c3f101
2 changed files with 41 additions and 8 deletions

View File

@@ -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,&sectionName2,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;
}

View File

@@ -1,14 +1,20 @@
#define NO_SECTION 0
#define FOUND_SECTION 10
#define ERROR_STR 1
#define ERROR_MAX_LEN 2
#define NO_ERROR 0
//State Machine
#define ST_INIT 0
#define ST_FOUND_LEFT_DELIMITER 1
#define ST_FOUND_RIGHT_DELIMITER 2
#define ST_ERROR_NOT_FOUND_RIGHT_DELIMITER 3
#define ST_FINISH 20
//state machine ERROR
#define ST_ERROR_GETSTRATPOS 30
//LIMITS
#define MAX_LEN_SECTIONNAME 128
@@ -17,4 +23,4 @@
int loadConfig(char *file, char **str_entry,char **host,int *intervall,int size);
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);