config-parser
a simple ini parser written in C
 
Loading...
Searching...
No Matches
config.c
Go to the documentation of this file.
1#include"config.h"
2#include<stdio.h>
3#include<stdlib.h>
4#include<string.h>
5#include<errno.h>
6
24int checkSection(char *str,char delimiterLeft,char delimiterRight,char **sectionName)
25{
26 char section[MAX_LEN_SECTIONNAME];
27 int i = 0; //most outer loop -> character of string
28 if(str == NULL)
29 {
30 return ERROR_STR;
31 }
32 int len = strlen(str);
33 int state = ST_INIT;
34 int leftDelimiterPos=0;
35 int rightDelimiterPos=0;
36 char *sectionName2 = NULL;
37 while(state != ST_FINISH)
38 {
39 switch(state)
40 {
41 case ST_INIT:
42 for(i=0;i<=len;i++) //find first (left) delimiter
43 {
44 if(str[i] == delimiterLeft)
45 {
47 leftDelimiterPos = i;
48 break;
49 }
50 }
51 if(state != ST_FOUND_LEFT_DELIMITER)
52 {
54 }
55 break;
57 for(i=0;i<=len;i++) //find second (right) delimiter
58 {
59 if(str[i] == delimiterRight)
60 {
62 rightDelimiterPos = i;
63 break;
64 }
65 }
66 if(state != ST_FOUND_RIGHT_DELIMITER)
67 {
69 }
70 break;
72 int ret=0;
73 if((ret=getStrAtPos(str,leftDelimiterPos,rightDelimiterPos,sectionName,MAX_LEN_SECTIONNAME)) == NO_ERROR)
74 {
75 state = ST_FINISH;
76 }else{
77 return ret;
78 }
79 case ST_FINISH:
80 return FOUND_SECTION;
81 }
82 }
83 return NO_SECTION;
84}
85
98int getStrAtPos(char *str,int fromPos,int toPos,char **name,int sizeName)
99{
100 if(*name == NULL)
101 {
102 int error = errno;
103 printf("Pointer NULL:%d\n",error);
104 return error;
105 }
106 int i=fromPos; //character iterator, which starts from specified pos
107 int j=0; //the character iterator for the target string
108 int diffLen=toPos-fromPos;
109 char *ptr_name=*name;
110 if(diffLen > MAX_LEN_SECTIONNAME || diffLen>sizeName)
111 {
112 return ERROR_MAX_LEN;
113 }
114 for(i=fromPos,j=0;i<=toPos;i++,j++)
115 {
116 ptr_name[j] = str[i];
117 }
118 ptr_name[j+1]='\0';
119 return NO_ERROR;
120}
121
151int getNameValuePair(char *str,char leftDelimiterPos,char rightDelimiterPos,char **name,char **value,int sizeName,int sizeValue)
152{
153 if(*name == NULL || *value == NULL)
154 {
155 int error = errno;
156 printf("Pointer NULL:%d\n",error);
157 return error;
158 }
159 int state=ST_INIT;
160 char *ptr_name=*name;
161 char *ptr_value=*value;
162 int ret=0;
163
164 char *ptrDelimiter=NULL;
165 int posDelimiter=0;
166 int posEnd=0;
167
168 ptrDelimiter=strchr(str,leftDelimiterPos);
169 if(ptrDelimiter==NULL)
170 {
172 }
173 posDelimiter = (ptrDelimiter - str);
174 printf("LenUntilDelimiter: %d\n",posDelimiter);
175 if((ret=getStrAtPos(str,0,posDelimiter-1,&ptr_name,sizeName)) == NO_ERROR)
176 {
177 printf("ptr_name:%s\n",ptr_name);
178 }else {
179 printf("Error at getStrAtPos:%d\n",ret);
180 return ret;
181 }
182 if(rightDelimiterPos == 0)
183 {
184 posEnd = strlen(str);
185 }else{
186 posEnd = rightDelimiterPos;
187 }
188
189 if((ret=getStrAtPos(str,posDelimiter+1,posEnd,&ptr_value,sizeValue)) == NO_ERROR)
190 {
191 printf("ptr_name:%s\n",ptr_value);
192 }else {
193 return ret;
194 }
195
196 return NO_ERROR;
197}
198
215int parseConfig(char *originalBuffer,struct configEntry **entry,int configSizeCount,int *returnedCount)
216{
217 int state=ST_INIT;
218 int ret=0;
219 int i=0;
220 *returnedCount=0;
221 char *sectionName=NULL;
222 struct configEntry *ptr_entry = *entry;
223 char *keyName=NULL;
224 char *keyValue=NULL;
225 char *buffer=NULL;
226
227 buffer = malloc(strlen(originalBuffer)+1);
228 if(buffer == NULL)
229 {
230 int error = errno;
231 printf("ERROR MALLOC:%d",error);
233 }
234 memset(buffer,0,strlen(originalBuffer)+1);
235 strcpy(buffer,originalBuffer);
236
237 //this step is necessary, because the strok function modifys the originalBuffer
238 //so we make a copy of it
239 strcpy(buffer,originalBuffer);
240
241 printf("buffer:%s\n--",buffer);
242
245
246 //read buffer line by line
247 char *token;
248 token = strtok(buffer,"\n");
249 while(token != NULL && state != ST_FINISH)
250 {
251
252 switch(state)
253 {
254 case ST_INIT:
255 if((ret=checkSection(token,'[',']',&sectionName))==FOUND_SECTION)
256 {
257 state = ST_FOUND_SECTION;
258 printf("FOUND_SECTION:%s\n",sectionName);
259 }
260 break;
261 case ST_FOUND_SECTION:
264 if(keyName == NULL || keyValue == NULL)
265 {
266 int error = errno;
267 printf("MALLOC:%d\n",error);
268 return error;
269 }
272
274 if(ret==NO_ERROR)
275 {
276 printf("configSizeCount: %d, i:%d\n",configSizeCount,i);
277 if(i<configSizeCount)
278 {
279 ptr_entry[i].sectionName = strdup(sectionName);
280 ptr_entry[i].keyValue= strdup(keyValue);
281 ptr_entry[i].keyName = strdup(keyName);
282 }
283 i++;
284 *returnedCount = i;
285 state = ST_FOUND_SECTION;
286 }
287 else {
288 state = ST_SKIP_READ;
289 }
290 free(keyName);
291 free(keyValue);
292 break;
293 }
294
295 if(state != ST_SKIP_READ)
296 {
297 token = strtok(NULL,"\n");
298 }
299 else {
300 state = ST_INIT;
301 }
302 }
303 printf("token: %s\n",token);
304 printf("buffer:%s\n--",buffer);
305
306 free(sectionName);
307 free(buffer);
308 printf("finish exitting parsing\nSTATE:%d\n",state);
309 return NO_ERROR;
310}
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 ERROR_DELIMITER_NOT_FOUND
Definition config.h:7
#define FOUND_SECTION
Definition config.h:2
#define ST_INIT
Definition config.h:11
#define ST_FOUND_RIGHT_DELIMITER
Definition config.h:13
#define NO_ERROR
Definition config.h:5
#define ERROR_MAX_LEN
Definition config.h:4
#define ST_FOUND_LEFT_DELIMITER
Definition config.h:12
#define ST_FOUND_SECTION
Definition config.h:16
#define ST_FINISH
Definition config.h:19
#define ST_SKIP_READ
Definition config.h:17
#define MAX_LEN_SECTIONNAME
Definition config.h:25
#define ERROR_STR
Definition config.h:3
#define NO_SECTION
Definition config.h:1
#define ERR_PARSECONFIG_UNKNOWN
Definition config.h:28
#define ST_ERROR_NOT_FOUND_RIGHT_DELIMITER
Definition config.h:14
#define ST_ERROR_NOT_FOUND_LEFT_DELIMITER
Definition config.h:15
char * keyValue
Definition config.h:34
char * keyName
Definition config.h:33
char * sectionName
Definition config.h:32