initial commit
This commit is contained in:
154
config.c
Normal file
154
config.c
Normal file
@@ -0,0 +1,154 @@
|
||||
#include"config.h"
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#include<string.h>
|
||||
|
||||
int loadConfig(char *str_file, char **str_entry,char **host,int *intervall,int size)
|
||||
{
|
||||
int i=0;
|
||||
FILE *f_config;
|
||||
char temp_str[256];
|
||||
char *ret = NULL;
|
||||
|
||||
printf("\nopening config file\n");
|
||||
f_config = fopen(str_file,"r");
|
||||
if(f_config != NULL)
|
||||
{
|
||||
ret = fgets(temp_str,256,f_config);
|
||||
while(ret!=NULL)
|
||||
{
|
||||
temp_str[strcspn(temp_str,"\n")]=0;
|
||||
printf("first temp_str:%s\n",temp_str);
|
||||
if(strcmp(temp_str,"[Host]")==0)
|
||||
{
|
||||
printf("temp_str host:%s\n",temp_str);
|
||||
while(ret!=NULL)
|
||||
{
|
||||
printf("temp_str:%s\n",temp_str);
|
||||
ret = fgets(temp_str,256,f_config);
|
||||
if(strchr(temp_str,'[')==NULL)
|
||||
{
|
||||
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]==' ') //Potential segfault??
|
||||
{
|
||||
if(ptr_host < (temp_str + strlen(temp_str)))
|
||||
{
|
||||
ptr_host = ptr_host + 1;
|
||||
}else{
|
||||
break;
|
||||
}
|
||||
}
|
||||
*host = strdup(ptr_host);
|
||||
printf("my host is:%s\n",*host);
|
||||
}
|
||||
}else{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr,"error opening file[%s]\n",str_file);
|
||||
}
|
||||
fclose(f_config);
|
||||
printf("Done reading config file\n");
|
||||
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
|
||||
* for example [SECTIONName] here delimiterLeft is [ and delimiterRight is ]
|
||||
* if a section is found, the section name will be written to sectionName
|
||||
*
|
||||
* Input:
|
||||
* char **str: the string to check
|
||||
* char delimiterLeft: the left delimiter to check for
|
||||
* char delimiterRight: the right delimiter to check for
|
||||
* Output:
|
||||
* char **sectionName: if found the section Name of the section
|
||||
* Return:
|
||||
* int status_code: one of the following values:
|
||||
*
|
||||
*/
|
||||
|
||||
int checkSection(char *str,char delimiterLeft,char delimiterRight,char **sectionName)
|
||||
{
|
||||
char section[MAX_LEN_SECTIONNAME];
|
||||
int i = 0; //most outer loop -> character of string
|
||||
if(str == NULL)
|
||||
{
|
||||
return ERROR_STR;
|
||||
}
|
||||
len = strlen(str);
|
||||
int state = ST_INIT;
|
||||
while(state != ST_FINISH)
|
||||
{
|
||||
switch(state)
|
||||
{
|
||||
case ST_INIT:
|
||||
for(i=0;i<len-1;i++) //find first (left) delimiter
|
||||
{
|
||||
if(str[i] == delimiterLeft)
|
||||
{
|
||||
state = ST_FOUND_LEFT_DELIMITER;
|
||||
leftDelimiterPos = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ST_FOUND_LEFT_DELIMITER:
|
||||
for(i=0;i<len-1;i++) //find second (right) delimiter
|
||||
{
|
||||
if(str[i] == delimiterRight)
|
||||
{
|
||||
state = ST_FOUND_RIGHT_DELIMITER;
|
||||
rightDelimiterPos = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ST_FOUND_RIGHT_DELIMITER:
|
||||
getStrAtPos(*str,leftDelimiterPos,rightDelimiterPos,section);
|
||||
}
|
||||
return NO_SECTION;
|
||||
}
|
||||
/*
|
||||
* Here we get / cut from fromPos to toPos and write it to an address
|
||||
*
|
||||
* Input:
|
||||
* char *str: the string to cut
|
||||
* int fromPos: from 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:
|
||||
* char **name: here we will write the section name to
|
||||
*/
|
||||
|
||||
int getStrAtPos(char *str,int fromPos,int toPosm,char **name)
|
||||
{
|
||||
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)
|
||||
{
|
||||
return ERROR_MAX_LEN;
|
||||
}
|
||||
for(i=fromPos,j=0;i<=toPos;i++,j++)
|
||||
{
|
||||
*name[j] = str[i];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user