140 lines
3.8 KiB
C
140 lines
3.8 KiB
C
#include "config.h"
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "file.h"
|
|
|
|
int main(void)
|
|
{
|
|
char teststr[] = "sdafsdfsdf[12345asdfddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd]";
|
|
char *name = NULL;
|
|
char *sectionName=NULL;
|
|
int ret=0;
|
|
name = malloc(MAX_LEN_SECTIONNAME*sizeof(char));
|
|
getStrAtPos(teststr, 11, 15, &name, MAX_LEN_SECTIONNAME);
|
|
|
|
sectionName = malloc(MAX_LEN_SECTIONNAME);
|
|
memset(sectionName,0,MAX_LEN_SECTIONNAME);
|
|
ret=checkSection(teststr,'[',']', §ionName);
|
|
if(ret == FOUND_SECTION)
|
|
{
|
|
printf("checkSection sucessfull\nsectionName=%s\n",sectionName);
|
|
}
|
|
else
|
|
{
|
|
free(sectionName);
|
|
printf("an error occured:%d\n",ret);
|
|
return 1;
|
|
}
|
|
|
|
char testpair[] = "asifdsfo=s1254124";
|
|
char *keyName=NULL;
|
|
char *keyValue=NULL;
|
|
char *content=NULL;
|
|
|
|
keyName = malloc(MAX_LEN_SECTIONNAME);
|
|
keyValue = malloc(MAX_LEN_SECTIONNAME);
|
|
memset(keyName,0,MAX_LEN_SECTIONNAME);
|
|
memset(keyValue,0,MAX_LEN_SECTIONNAME);
|
|
|
|
if((ret=getNameValuePair(testpair,
|
|
'=',0,
|
|
&keyName,&keyValue,
|
|
MAX_LEN_SECTIONNAME,MAX_LEN_SECTIONNAME))==NO_ERROR)
|
|
{
|
|
printf("keyname:%s Value: %s\n",keyName,keyValue);
|
|
}else{
|
|
printf("error getNameValuePair: %d\n",ret);
|
|
}
|
|
|
|
long neededSize=0;
|
|
if((ret=getFile("config-segfault.cfg",NULL,0,&neededSize))==NO_ERROR)
|
|
{
|
|
printf("sucessfull retrieved size:%ld\n",neededSize);
|
|
}
|
|
else {
|
|
printf("Error on getFile:%d\n",ret);
|
|
return 1;
|
|
}
|
|
content = malloc(neededSize);
|
|
printf("allocate buffer for filecontent size:%ld\n",neededSize);
|
|
if(content == NULL)
|
|
{
|
|
int error = errno;
|
|
printf("MALLOC: %d\n",error);
|
|
return error;
|
|
}
|
|
memset(content,0,neededSize);
|
|
if((ret=getFile("config-segfault.cfg",&content,neededSize,&neededSize))==NO_ERROR)
|
|
{
|
|
//printf("Sucessfull read file into buffer:%s|\n---\n",content);
|
|
}
|
|
else {
|
|
printf("Error on getFile:%d\n",ret);
|
|
free(content);
|
|
return 1;
|
|
}
|
|
struct configEntry *entry=NULL;
|
|
int returnedCount=0;
|
|
int i=0;
|
|
int count=10;
|
|
|
|
entry = malloc(count*sizeof(struct configEntry));
|
|
if(entry == NULL)
|
|
{
|
|
int error = errno;
|
|
printf("MALLOC: %d\n",error);
|
|
return 0;
|
|
}
|
|
ret = parseConfig(content,&entry,count,&returnedCount);
|
|
if(ret!=NO_ERROR)
|
|
{
|
|
printf("Error on parseConfig:%d\nReallocate from size %d, to %d\n",ret,count,returnedCount);
|
|
}
|
|
if(returnedCount > count)
|
|
{
|
|
for(i=0;i<count;i++)
|
|
{
|
|
free(entry[i].keyName);
|
|
free(entry[i].keyValue);
|
|
free(entry[i].sectionName);
|
|
}
|
|
|
|
printf("Error on parseConfig:%d\nReallocate from size %d, to %d\n",ret,count,returnedCount);
|
|
if((entry=realloc(entry,returnedCount*sizeof(struct configEntry)))==NULL)
|
|
{
|
|
printf("error could not reallocate from size %d to %d \n",count,returnedCount);
|
|
return 1;
|
|
}
|
|
ret = parseConfig(content,&entry,returnedCount,&returnedCount);
|
|
if(ret!=NO_ERROR)
|
|
{
|
|
printf("Error on parseConfig:%d\n",ret);
|
|
return ret;
|
|
}
|
|
printf("returnedCount:%d\n",returnedCount);
|
|
}
|
|
|
|
|
|
|
|
free(content);
|
|
|
|
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);
|
|
free(entry[i].keyName);
|
|
free(entry[i].keyValue);
|
|
free(entry[i].sectionName);
|
|
}
|
|
|
|
|
|
free(keyValue);
|
|
free(keyName);
|
|
free(name);
|
|
free(entry);
|
|
free(sectionName);
|
|
return 0;
|
|
}
|
|
|