From 87dd764accfde63e0dfbe6e4994c45630abd6426 Mon Sep 17 00:00:00 2001 From: Screenager Date: Wed, 21 Aug 2019 19:38:16 +0200 Subject: [PATCH] rework as function --- main.c | 15 +++++++++++++++ makefile | 7 +++++++ read_cfg.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ read_cfg.h | 12 ++++++++++++ test.c | 22 ---------------------- test.cfg | 9 +++++++-- 6 files changed, 94 insertions(+), 24 deletions(-) create mode 100644 main.c create mode 100644 makefile create mode 100644 read_cfg.c create mode 100644 read_cfg.h delete mode 100644 test.c diff --git a/main.c b/main.c new file mode 100644 index 0000000..a19b813 --- /dev/null +++ b/main.c @@ -0,0 +1,15 @@ +#include +#include +#include"read_cfg.h" + +int main(){ + char file[]="test.cfg"; + options *option; + option=read_cfg(file); //returns pointer to struct with options + printf(option[1].name);printf("\n"); + printf(option[1].setting);printf("\n"); + printf(option[2].name);printf("\n"); + printf(option[2].setting);printf("\n"); + free(option); //freeing memory that got alloced in the function read_cfg + return 0; +} \ No newline at end of file diff --git a/makefile b/makefile new file mode 100644 index 0000000..6e4af67 --- /dev/null +++ b/makefile @@ -0,0 +1,7 @@ +CC=gcc +CFLAGS=-O2 +LIBS= + +all: main.c read_cfg.c read_cfg.h + $(CC) -o test.exe main.c read_cfg.c $(CFLAGS) $(LIBS) + diff --git a/read_cfg.c b/read_cfg.c new file mode 100644 index 0000000..9dfa3b2 --- /dev/null +++ b/read_cfg.c @@ -0,0 +1,53 @@ +/*reads a config file with the following format: + +name1=setting1 +name2=setting2 + +example: + + relay = FALSE +debug = 1 +mods=12345;98765;13456 + +returns a pointer to a struct wich needs to be freed later on +should be portable on pretty much every platform where there is a C compiler with malloc() and enough RAM, this is not optimized for ICs +constants and struct defined in header + +implement comments later +*/ + +#include +#include +#include +#include"read_cfg.h" +options *read_cfg(char filename[]) { + FILE *fpointer; + char str[LINEBUFF]; + char line[LINEBUFF]; + int i=0; + int ib=0; + int il=0; + options *option; + option = malloc(MAXLINE*sizeof(*option)); //160 KB HEAP needs to be freed later in main.c + if ((fpointer = fopen(filename,"r")) == NULL){ + printf("Error opening file"); + return 0; + } + while( fgets (str, LINEBUFF, fpointer)!=NULL || il<=MAXLINE ) { + while (i -#include -#include -int main() { - FILE *fpointer; - char str[60]; - char strb[60]; - int i=0; - int ib=0; - if ((fpointer = fopen("test.cfg","r")) == NULL){ - printf("Error opening file"); - exit(1); - } - while( fgets (str, 60, fpointer)!=NULL ) { - while (i<60){ - if(str[i]!=' '){strb[ib]=str[i];i++;ib++;}else{i++;} - } - puts(strb);i=0;ib=0; - } - fclose(fpointer); - return 0; -} \ No newline at end of file diff --git a/test.cfg b/test.cfg index f8be7bb..2f491de 100644 --- a/test.cfg +++ b/test.cfg @@ -1,2 +1,7 @@ -line1 -line2 \ No newline at end of file + line1 = alasdads + line2=lel + mods = asdasd;94343;deinemudder //testst + line4 = lerare + + + \ No newline at end of file