add configparser
This commit is contained in:
121
config.c
121
config.c
@@ -2,64 +2,6 @@
|
|||||||
#include<stdio.h>
|
#include<stdio.h>
|
||||||
#include<stdlib.h>
|
#include<stdlib.h>
|
||||||
#include<string.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;
|
|
||||||
}
|
|
||||||
/*
|
/*
|
||||||
* 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 ]
|
||||||
@@ -211,3 +153,66 @@ int getNameValuePair(char *str,char leftDelimiterPos,char rightDelimiterPos,char
|
|||||||
return NO_ERROR;
|
return NO_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int parseConfig(char *buffer,struct configEntry **entry,int configSizeCount)
|
||||||
|
{
|
||||||
|
int state=ST_INIT;
|
||||||
|
int ret=0;
|
||||||
|
int i=0;
|
||||||
|
char *sectionName=NULL;
|
||||||
|
struct configEntry *ptr_entry = *entry;
|
||||||
|
char *keyName=NULL;
|
||||||
|
char *keyValue=NULL;
|
||||||
|
|
||||||
|
keyName = malloc(MAX_LEN_SECTIONNAME);
|
||||||
|
keyValue = malloc(MAX_LEN_SECTIONNAME);
|
||||||
|
//read buffer line by line
|
||||||
|
char *token;
|
||||||
|
token = strtok(buffer,"\n");
|
||||||
|
while(token != NULL && state != ST_FINISH)
|
||||||
|
{
|
||||||
|
switch(state)
|
||||||
|
{
|
||||||
|
case ST_INIT:
|
||||||
|
printf("token(line): %s\n",token);
|
||||||
|
if((ret=checkSection(token,'[',']',§ionName))==FOUND_SECTION)
|
||||||
|
{
|
||||||
|
state = ST_FOUND_SECTION;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ST_FOUND_SECTION:
|
||||||
|
if((ret=getNameValuePair(token,'=',0,
|
||||||
|
&keyName,&keyValue,
|
||||||
|
MAX_LEN_SECTIONNAME,MAX_LEN_SECTIONNAME
|
||||||
|
))==NO_ERROR)
|
||||||
|
{
|
||||||
|
ptr_entry[i].sectionName = strdup(sectionName);
|
||||||
|
ptr_entry[i].keyValue= strdup(keyValue);
|
||||||
|
ptr_entry[i].keyName = strdup(keyName);
|
||||||
|
if(i<configSizeCount-1)
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return ERROR_STR;
|
||||||
|
}
|
||||||
|
state = ST_INIT;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
state = ST_SKIP_READ;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(state != ST_SKIP_READ)
|
||||||
|
{
|
||||||
|
token = strtok(NULL,"\n");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
state = ST_INIT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free(keyName);
|
||||||
|
free(keyValue);
|
||||||
|
return NO_ERROR;
|
||||||
|
}
|
||||||
|
|||||||
11
config.h
11
config.h
@@ -12,8 +12,9 @@
|
|||||||
#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_FOUND_SECTION 4
|
||||||
|
#define ST_SKIP_READ 5
|
||||||
|
|
||||||
#define ST_
|
|
||||||
#define ST_FINISH 20
|
#define ST_FINISH 20
|
||||||
|
|
||||||
//state machine ERROR
|
//state machine ERROR
|
||||||
@@ -22,6 +23,12 @@
|
|||||||
//LIMITS
|
//LIMITS
|
||||||
#define MAX_LEN_SECTIONNAME 128
|
#define MAX_LEN_SECTIONNAME 128
|
||||||
|
|
||||||
|
struct configEntry
|
||||||
|
{
|
||||||
|
char *sectionName;
|
||||||
|
char *keyName;
|
||||||
|
char *keyValue;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
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);
|
||||||
@@ -29,3 +36,5 @@ int checkSection(char *str,char delimiterLeft,char delimiterRight,char **section
|
|||||||
|
|
||||||
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);
|
int getNameValuePair(char *str,char leftDelimiterPos,char rightDelimiterPos,char **name,char **value,int sizeName,int sizeValue);
|
||||||
|
|
||||||
|
int parseConfig(char *buffer,struct configEntry **entry,int configSize);
|
||||||
|
|||||||
2
file.c
2
file.c
@@ -51,3 +51,5 @@ int getFile(char *fname, char **strContent,int cbSize,long *neededSize)
|
|||||||
fclose(hfile);
|
fclose(hfile);
|
||||||
return NO_ERROR;
|
return NO_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
3
test.c
3
test.c
@@ -1,6 +1,7 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include "file.h"
|
#include "file.h"
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
@@ -57,6 +58,8 @@ int main(void)
|
|||||||
printf("Error on getFile:%d\n",ret);
|
printf("Error on getFile:%d\n",ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
parseConfig(content,&entry,sizeEntry);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user