Add generic parser to cfg to struct.
This commit is contained in:
7
config.c
7
config.c
@@ -47,6 +47,10 @@ int checkSection(char *str,char delimiterLeft,char delimiterRight,char **section
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if(state != ST_FOUND_LEFT_DELIMITER)
|
||||||
|
{
|
||||||
|
return ST_ERROR_NOT_FOUND_LEFT_DELIMITER;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case ST_FOUND_LEFT_DELIMITER:
|
case ST_FOUND_LEFT_DELIMITER:
|
||||||
for(i=0;i<=len;i++) //find second (right) delimiter
|
for(i=0;i<=len;i++) //find second (right) delimiter
|
||||||
@@ -153,7 +157,7 @@ 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 parseConfig(char *buffer,struct configEntry **entry,int configSizeCount,int *returnedCount)
|
||||||
{
|
{
|
||||||
int state=ST_INIT;
|
int state=ST_INIT;
|
||||||
int ret=0;
|
int ret=0;
|
||||||
@@ -191,6 +195,7 @@ int parseConfig(char *buffer,struct configEntry **entry,int configSizeCount)
|
|||||||
if(i<configSizeCount-1)
|
if(i<configSizeCount-1)
|
||||||
{
|
{
|
||||||
i++;
|
i++;
|
||||||
|
*returnedCount = i;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return ERROR_STR;
|
return ERROR_STR;
|
||||||
|
|||||||
9
config.cfg
Normal file
9
config.cfg
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
[PRINTER]
|
||||||
|
ip=192.168.209.173
|
||||||
|
[dööner]
|
||||||
|
df=123
|
||||||
|
[anothersection]
|
||||||
|
a=192
|
||||||
|
b=111
|
||||||
|
c=213
|
||||||
|
|
||||||
7
config.h
7
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_ERROR_NOT_FOUND_LEFT_DELIMITER 4
|
||||||
#define ST_SKIP_READ 5
|
#define ST_FOUND_SECTION 5
|
||||||
|
#define ST_SKIP_READ 6
|
||||||
|
|
||||||
#define ST_FINISH 20
|
#define ST_FINISH 20
|
||||||
|
|
||||||
@@ -37,4 +38,4 @@ 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);
|
int parseConfig(char *buffer,struct configEntry **entry,int configSize,int *returnedCount);
|
||||||
|
|||||||
13
test.c
13
test.c
@@ -57,8 +57,17 @@ int main(void)
|
|||||||
else {
|
else {
|
||||||
printf("Error on getFile:%d\n",ret);
|
printf("Error on getFile:%d\n",ret);
|
||||||
}
|
}
|
||||||
|
struct configEntry *entry=NULL;
|
||||||
parseConfig(content,&entry,sizeEntry);
|
int returnedCount=0;
|
||||||
|
int i=0;
|
||||||
|
|
||||||
|
entry = malloc(10*sizeof(struct configEntry));
|
||||||
|
parseConfig(content,&entry,10*sizeof(struct configEntry),&returnedCount);
|
||||||
|
|
||||||
|
for(i=0;i<returnedCount;i++)
|
||||||
|
{
|
||||||
|
printf("i:%d/%d, struct section: %s, keyname: %s, keyvalue: %s\n",i,returnedCount,entry[i].sectionName,entry[i].keyName,entry[i].keyValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user