feat: add key/val pair getting

This commit is contained in:
jonathan santis
2025-05-13 12:57:12 +02:00
parent f4d2c3f101
commit 3afd472bbd
3 changed files with 93 additions and 10 deletions

View File

@@ -60,16 +60,6 @@ int loadConfig(char *str_file, char **str_entry,char **host,int *intervall,int s
printf("Done reading config file\n"); printf("Done reading config file\n");
return 0; return 0;
} }
/*int parseConfig(File *file, char *str_entry)
{
char sub_str[256];
if(strcmp(str_entry,"[Host]")==0)//section
{
fgets(sub_str,256,file);
//do sub parsing
}
}*/
/* /*
* Here we check if the given string contains a section * Here we check if the given string contains a section
* for example [SECTIONName] here delimiterLeft is [ and delimiterRight is ] * for example [SECTIONName] here delimiterLeft is [ and delimiterRight is ]
@@ -179,3 +169,45 @@ int getStrAtPos(char *str,int fromPos,int toPos,char **name,int sizeName)
return NO_ERROR; 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;
}

View File

@@ -3,6 +3,8 @@
#define ERROR_STR 1 #define ERROR_STR 1
#define ERROR_MAX_LEN 2 #define ERROR_MAX_LEN 2
#define NO_ERROR 0 #define NO_ERROR 0
//ERROR DELIMITER
#define ERROR_DELIMITER_NOT_FOUND 40
//State Machine //State Machine
@@ -10,6 +12,8 @@
#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_ERROR_NOT_FOUND_RIGHT_DELIMITER 3
#define ST_
#define ST_FINISH 20 #define ST_FINISH 20
//state machine ERROR //state machine ERROR
@@ -24,3 +28,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 checkSection(char *str,char delimiterLeft,char delimiterRight,char **sectionName);
int getStrAtPos(char *str,int fromPos,int toPos, char **name,int sizeName); 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);

46
test.c Normal file
View File

@@ -0,0 +1,46 @@
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char teststr[] = "sdafsdfsdf[12345asdfddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd]";
char *name = NULL;
char *sectionName=NULL;
int ret=0;
name = malloc(MAX_LEN_SECTIONNAME*sizeof(char));
getStrAtPos(teststr, 11, 15, &name, MAX_LEN_SECTIONNAME);
sectionName = malloc(MAX_LEN_SECTIONNAME*sizeof(char));
if((ret=checkSection(teststr,'[',']', &sectionName)) == FOUND_SECTION)
{
printf("checkSection sucessfull\nsectionName=%s\n",sectionName);
}
else
{
printf("an error occured:%d\n",ret);
}
char testpair[] = "asifdsfo=s1254124";
char *keyName=NULL;
char *keyValue=NULL;
keyName = malloc(MAX_LEN_SECTIONNAME);
keyValue = malloc(MAX_LEN_SECTIONNAME);
if((ret=getNameValuePair(testpair,
'=',0,
&keyName,&keyValue,
MAX_LEN_SECTIONNAME,MAX_LEN_SECTIONNAME))==NO_ERROR)
{
printf("keyname:%s Value: %s\n",keyName,keyValue);
}else{
printf("error getNameValuePair: %d\n",ret);
}
free(keyValue);
free(keyName);
free(name);
}