modify config file, todo: fix over using fgets by storing temp_str in seperate strs

This commit is contained in:
Jonathan Santis 2024-09-18 13:18:29 +02:00
parent 14f1ab7398
commit 10808df595
4 changed files with 61 additions and 25 deletions

2
config
View File

@ -1,3 +1,5 @@
[APP]
Intervall = 5
[Host]
Hostname = https://5ccppi.org:4433
[Services]

View File

@ -3,7 +3,7 @@
#include<stdlib.h>
#include<string.h>
int loadConfig(char *str_file, char **str_entry,char **host,int size)
int loadConfig(char *str_file, char **str_entry,char **host,int *intervall,int size)
{
int i=0;
FILE *f_config;
@ -16,19 +16,32 @@ int loadConfig(char *str_file, char **str_entry,char **host,int size)
while(fgets(temp_str,256,f_config)!=NULL)
{
temp_str[strcspn(temp_str,"\n")]=0;
printf("first temp_str:%s\n",temp_str);
if(strcmp(temp_str,"[Host]")==0)
{
if(fgets(temp_str,256,f_config)!=NULL)
printf("temp_str host:%s\n",temp_str);
while(fgets(temp_str,256,f_config)!=NULL)
{
char *ptr_host = strrchr(temp_str,'=')+1;//Without =
ptr_host[strcspn(ptr_host,"\n")]=0;
while(ptr_host[0]==' ')
printf("temp_str:%s\n",temp_str);
if(strchr(temp_str,'[')==NULL)
{
ptr_host = ptr_host + 1;
printf("strchr\n");
if(strstr(temp_str,"Hostname")!=NULL)
{
char *ptr_host = strrchr(temp_str,'=')+1;//Without =
ptr_host[strcspn(ptr_host,"\n")]=0;
while(ptr_host[0]==' ')
{
ptr_host = ptr_host + 1;
}
*host = strdup(ptr_host);
printf("my host is:%s\n",*host);
}else{
fprintf(stderr,"keyword Hostname not found in config");
}
}else{
break;
}
*host = strdup(ptr_host);
//strncpy(host,ptr_host,256);
printf("my host is:%s\n",*host);
}
}
else if(strcmp(temp_str,"[Services]")==0)
@ -51,7 +64,30 @@ int loadConfig(char *str_file, char **str_entry,char **host,int size)
else
{
break;
fclose(f_config);
}
}
}
else if(strcmp(temp_str,"[APP]")==0)
{
while(fgets(temp_str,256,f_config)!=NULL)
{
if(strchr(temp_str,'[')==NULL)
{
if(strstr(temp_str,"Intervall")!=NULL)
{
char *ptr_intervall = strrchr(temp_str,'=')+1;//Without =
ptr_intervall[strcspn(ptr_intervall,"p\n")]=0;
while(ptr_intervall[0]==' ')
{
ptr_intervall = ptr_intervall + 1;
}
*intervall = atoi(ptr_intervall);
printf("intervall:%d\n",*intervall);
}else{
fprintf(stderr,"keyword Intervall not found in config");
}
}else{
break;
}
}
}

View File

@ -1,2 +1,2 @@
int loadConfig(char *file, char **str_entry,char **host,int size);
int loadConfig(char *file, char **str_entry,char **host,int *intervall,int size);

26
main.c
View File

@ -36,13 +36,19 @@ int getPath(char *configPath,int size);
//gotify_message("https://5ccppi.org:4433/message?token=","anothertitle","anothermessage");
* */
int main(int argc,char *argv[])
{
DBusConnection *connection=NULL;
DBusError error;
clock_t time;
char *str_entry[64]={'\0'};
char *host=NULL;
int i=0;
int intervall=10;
Service services[64]={'\0'};
char *ptr_name=NULL;
char path[256];
time = clock();
@ -87,11 +93,8 @@ int main(int argc,char *argv[])
{
if(argc==3 && strcmp(argv[2],"-d")!=0)
{
char *str_entry[64]={'\0'};
char *host=NULL;
int i=0;
loadConfig(argv[2],str_entry,&host,64);
loadConfig(argv[2],str_entry,&host,&intervall,64);
printf("[main]intervall:%d\n",intervall);
for(i=0;str_entry[i];i++)
{
printf("entry in config:%s\n",str_entry[i]);
@ -101,18 +104,13 @@ int main(int argc,char *argv[])
else if(argc==3 && strcmp(argv[2],"-d")==0)
{
printf("Running as a daemon\n");
char *str_entry[64]={'\0'};
int i=0;
Service services[64]={'\0'};
char *ptr_name=NULL;
char path[256];
char *host=NULL;
getPath(path,256);
printf("[main]configPath:%s\n",path);
loadConfig(path,str_entry,&host,64);
loadConfig(path,str_entry,&host,&intervall,64);
printf("[main]host:%s\n",host);
printf("[main]intervall:%d\n",intervall);
for(i=0;str_entry[i];i++)
{
services[i].targetPath = strdup(str_entry[i]);
@ -140,7 +138,7 @@ int main(int argc,char *argv[])
printf("entry in config:%s\n",services[i].name);
ServiceGetSendStatus(connection,&services[i]);
}
sleep(10);
sleep(intervall);
}
}else
{