59 lines
1.5 KiB
C
59 lines
1.5 KiB
C
|
#include <stdio.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
typedef struct configuration {
|
||
|
int n;
|
||
|
double north;
|
||
|
double east;
|
||
|
double south;
|
||
|
double west;
|
||
|
double init_value;
|
||
|
double 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, "%lf", &(config->north));
|
||
|
} else if (strcmp(property, "EAST") == 0) {
|
||
|
sscanf(value, "%lf", &(config->east));
|
||
|
} else if (strcmp(property, "SOUTH") == 0) {
|
||
|
sscanf(value, "%lf", &(config->south));
|
||
|
} else if (strcmp(property, "WEST") == 0) {
|
||
|
sscanf(value, "%lf", &(config->west));
|
||
|
} else if (strcmp(property, "INIT_VALUE") == 0) {
|
||
|
sscanf(value, "%lf", &(config->init_value));
|
||
|
} else if (strcmp(property, "THRESHOLD") == 0) {
|
||
|
sscanf(value, "%lf", &(config->threshold));
|
||
|
} else {
|
||
|
printf("Unknown property %s\n", property);
|
||
|
}
|
||
|
}
|
||
|
fclose(fp);
|
||
|
return 0;
|
||
|
}
|