JacobiHPC/src/config.c

60 lines
1.5 KiB
C

#include <stdio.h>
#include <string.h>
typedef struct configuration {
int n;
float north;
float east;
float south;
float west;
float init_value;
float threshold;
} configuration;
int load_config(configuration *config) {
char property[100];
char *value;
FILE *fp;
fp = fopen("jacobi.conf", "r");
if (fp == NULL) {
perror("Error opening file jacobi.conf");
return 1;
}
while (fgets(property, 100, fp) != NULL) {
if (property[0] == '\n' || property[0] == '#') {
/* Skip empty lines and comments */
continue;
}
value = strchr(property, ' ');
if (value == NULL) {
fclose(fp);
perror("Error reading file jacobi.conf");
return 1;
}
value[0] = '\0';
value += sizeof(char);
value[strlen(value) - 1] = '\0';
if (strcmp(property, "N") == 0) {
sscanf(value, "%d", &(config->n));
} else if (strcmp(property, "NORTH") == 0) {
sscanf(value, "%f", &(config->north));
} else if (strcmp(property, "EAST") == 0) {
sscanf(value, "%f", &(config->east));
} else if (strcmp(property, "SOUTH") == 0) {
sscanf(value, "%f", &(config->south));
} else if (strcmp(property, "WEST") == 0) {
sscanf(value, "%f", &(config->west));
} else if (strcmp(property, "INIT_VALUE") == 0) {
sscanf(value, "%f", &(config->init_value));
} else if (strcmp(property, "THRESHOLD") == 0) {
sscanf(value, "%f", &(config->threshold));
} else {
printf("Unknown property %s\n", property);
}
}
fclose(fp);
return 0;
}