forked from christopherlovell/DensityGridder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread_config.h
67 lines (54 loc) · 1.77 KB
/
read_config.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CONFIG_VARIABLE_LEN 1024
#define CONFIG_LINE_BUFFER_SIZE 100
#define MAX_LLIST_NAME_LEN 1024
#define MAX_OUT_NAME_LEN 1024
//struct config_struct
//typedef struct
struct config_struct {
char input_dir[MAX_LLIST_NAME_LEN];
char output_dir[MAX_LLIST_NAME_LEN];
int grid_dims;
double sim_dims;
};
void read_int_from_config_line(char* config_line, int* val) {
char prm_name[MAX_CONFIG_VARIABLE_LEN];
sscanf(config_line, "%s %i\n", prm_name, 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* conf) {
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, "INPUT_DIR ")) {
read_str_from_config_line(buf, conf->input_dir);
}
if (strstr(buf, "OUTPUT_DIR ")) {
read_str_from_config_line(buf, conf->output_dir);
}
if (strstr(buf, "GRID_DIMS ")) {
read_int_from_config_line(buf, &conf->grid_dims);
}
if (strstr(buf, "SIM_DIMS ")) {
read_double_from_config_line(buf, &conf->sim_dims);
}
}
fclose(fp);
}