I have written the following C code to parse a configuration file for settings to my program. I denote comments with a #
and blank lines are allowed. e.g.:
# Total bytes per line
TBPL 70
# bar minimum
NUMIN 10.
# bar maximum
NUMAX 30000.
# input filename
LLIST_NAME foo.txt
Is there anything badly wrong here? I'm used to Python so finding this a bit of a chore...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CONFIG_VARIABLE_LEN 20
#define CONFIG_LINE_BUFFER_SIZE 100
#define MAX_LLIST_NAME_LEN 256
#define MAX_OUT_NAME_LEN 256
struct config_struct {
int bytes_per_line;
char llist_name[MAX_LLIST_NAME_LEN];
double numin, numax;
} config;
int read_int_from_config_line(char* config_line) {
char prm_name[MAX_CONFIG_VARIABLE_LEN];
int val;
sscanf(config_line, "%s %d\n", prm_name, &val);
return val;
}
void read_double_from_config_line(char* config_line, double* val) {
char prm_name[MAX_CONFIG_VARIABLE_LEN];
sscanf(config_line, "%s %lf\n", prm_name, val);
}
void read_str_from_config_line(char* config_line, char* val) {
char prm_name[MAX_CONFIG_VARIABLE_LEN];
sscanf(config_line, "%s %s\n", prm_name, val);
}
void read_config_file(char* config_filename, struct config_struct config) {
FILE *fp;
char buf[CONFIG_LINE_BUFFER_SIZE];
if ((fp=fopen(config_filename, "r")) == NULL) {
fprintf(stderr, "Failed to open config file %s", config_filename);
exit(EXIT_FAILURE);
}
while(! feof(fp)) {
fgets(buf, CONFIG_LINE_BUFFER_SIZE, fp);
if (buf[0] == '#' || strlen(buf) < 4) {
continue;
}
if (strstr(buf, "TBPL ")) {
config.bytes_per_line = read_int_from_config_line(buf);
}
if (strstr(buf, "NUMIN ")) {
read_double_from_config_line(buf, &config.numin);
}
if (strstr(buf, "NUMAX ")) {
read_double_from_config_line(buf, &config.numax);
}
if (strstr(buf, "LLIST_NAME ")) {
read_str_from_config_line(buf, config.llist_name);
}
}
printf("TBPL = %d\n", config.bytes_per_line);
printf("NUMIN = %f\nNUMAX = %f\n", config.numin, config.numax);
printf("LLIST_NAME = %s\n", config.llist_name);
}
int main(int argc, char *argv[]) {
read_config_file(argv[1], config);
return EXIT_SUCCESS;
}