config-parser
a simple ini parser written in C
 
Loading...
Searching...
No Matches
test.c
Go to the documentation of this file.
1#include "config.h"
2#include <errno.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6#include "file.h"
7
8int main(void)
9{
10 char teststr[] = "sdafsdfsdf[12345asdfddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd]";
11 char *name = NULL;
12 char *sectionName=NULL;
13 int ret=0;
14 name = malloc(MAX_LEN_SECTIONNAME*sizeof(char));
15 getStrAtPos(teststr, 11, 15, &name, MAX_LEN_SECTIONNAME);
16
17 sectionName = malloc(MAX_LEN_SECTIONNAME);
18 memset(sectionName,0,MAX_LEN_SECTIONNAME);
19 ret=checkSection(teststr,'[',']', &sectionName);
20 if(ret == FOUND_SECTION)
21 {
22 printf("checkSection sucessfull\nsectionName=%s\n",sectionName);
23 }
24 else
25 {
26 free(sectionName);
27 printf("an error occured:%d\n",ret);
28 return 1;
29 }
30
31 char testpair[] = "asifdsfo=s1254124";
32 char *keyName=NULL;
33 char *keyValue=NULL;
34 char *content=NULL;
35
36 keyName = malloc(MAX_LEN_SECTIONNAME);
37 keyValue = malloc(MAX_LEN_SECTIONNAME);
38 memset(keyName,0,MAX_LEN_SECTIONNAME);
39 memset(keyValue,0,MAX_LEN_SECTIONNAME);
40
41 if((ret=getNameValuePair(testpair,
42 '=',0,
43 &keyName,&keyValue,
45 {
46 printf("keyname:%s Value: %s\n",keyName,keyValue);
47 }else{
48 printf("error getNameValuePair: %d\n",ret);
49 }
50
51 long neededSize=0;
52 if((ret=getFile("config-segfault.cfg",NULL,0,&neededSize))==NO_ERROR)
53 {
54 printf("sucessfull retrieved size:%ld\n",neededSize);
55 }
56 else {
57 printf("Error on getFile:%d\n",ret);
58 return 1;
59 }
60 content = malloc(neededSize);
61 printf("allocate buffer for filecontent size:%ld\n",neededSize);
62 if(content == NULL)
63 {
64 int error = errno;
65 printf("MALLOC: %d\n",error);
66 return error;
67 }
68 memset(content,0,neededSize);
69 if((ret=getFile("config-segfault.cfg",&content,neededSize,&neededSize))==NO_ERROR)
70 {
71 //printf("Sucessfull read file into buffer:%s|\n---\n",content);
72 }
73 else {
74 printf("Error on getFile:%d\n",ret);
75 free(content);
76 return 1;
77 }
78 struct configEntry *entry=NULL;
79 int returnedCount=0;
80 int i=0;
81 int count=10;
82
83 entry = malloc(count*sizeof(struct configEntry));
84 if(entry == NULL)
85 {
86 int error = errno;
87 printf("MALLOC: %d\n",error);
88 return 0;
89 }
90 ret = parseConfig(content,&entry,count,&returnedCount);
91 if(ret!=NO_ERROR)
92 {
93 printf("Error on parseConfig:%d\nReallocate from size %d, to %d\n",ret,count,returnedCount);
94 }
95 if(returnedCount > count)
96 {
97 for(i=0;i<count;i++)
98 {
99 free(entry[i].keyName);
100 free(entry[i].keyValue);
101 free(entry[i].sectionName);
102 }
103
104 printf("Error on parseConfig:%d\nReallocate from size %d, to %d\n",ret,count,returnedCount);
105 if((entry=realloc(entry,returnedCount*sizeof(struct configEntry)))==NULL)
106 {
107 printf("error could not reallocate from size %d to %d \n",count,returnedCount);
108 return 1;
109 }
110 ret = parseConfig(content,&entry,returnedCount,&returnedCount);
111 if(ret!=NO_ERROR)
112 {
113 printf("Error on parseConfig:%d\n",ret);
114 return ret;
115 }
116 printf("returnedCount:%d\n",returnedCount);
117 }
118
119
120
121 free(content);
122
123 for(i=0;i<returnedCount;i++)
124 {
125 printf("i:%d/%d, struct section: %s, keyname: %s, keyvalue: %s\n",i,returnedCount,entry[i].sectionName,entry[i].keyName,entry[i].keyValue);
126 free(entry[i].keyName);
127 free(entry[i].keyValue);
128 free(entry[i].sectionName);
129 }
130
131
132 free(keyValue);
133 free(keyName);
134 free(name);
135 free(entry);
136 free(sectionName);
137 return 0;
138}
139
int parseConfig(char *originalBuffer, struct configEntry **entry, int configSizeCount, int *returnedCount)
Definition config.c:215
int getStrAtPos(char *str, int fromPos, int toPos, char **name, int sizeName)
Here we get / cut from fromPos to toPos and write it to an address.
Definition config.c:98
int checkSection(char *str, char delimiterLeft, char delimiterRight, char **sectionName)
Here we check if the given string contains a section for example [SECTIONName] here delimiterLeft is ...
Definition config.c:24
int getNameValuePair(char *str, char leftDelimiterPos, char rightDelimiterPos, char **name, char **value, int sizeName, int sizeValue)
Input:
Definition config.c:151
#define FOUND_SECTION
Definition config.h:2
#define NO_ERROR
Definition config.h:5
#define MAX_LEN_SECTIONNAME
Definition config.h:25
int getFile(char *fname, char **strContent, int cbSize, long *neededSize)
Definition file.c:17
char * keyValue
Definition config.h:34
char * keyName
Definition config.h:33
char * sectionName
Definition config.h:32
int main(void)
Definition test.c:8