You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

53 lines
1.2 KiB

/*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<stdio.h>
#include<stdlib.h>
#include<string.h>
#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<LINEBUFF){
if(str[i]!=' ' && str[i]!='\n' && str[i]!='\r'){line[ib]=str[i];i++;ib++;}
else{i++;}
}
if (line[0]!='\0'){
i=0;ib=0;
strcpy(option[il].name,strtok(line, "="));
strcpy(option[il].setting,strtok(NULL, "="));
}
else{}
il++;
}
fclose(fpointer);
return option;
}