| 1 |
// Start of tuning.h. |
| 2 |
|
| 3 |
|
| 4 |
int is_blank_line_or_comment(const char *s) { |
| 5 |
size_t i = strspn(s, " \t\n"); |
| 6 |
return s[i] == '\0' || // Line is blank. |
| 7 |
strncmp(s + i, "--", 2) == 0; // Line is comment. |
| 8 |
} |
| 9 |
|
| 10 |
static char* load_tuning_file(const char *fname, |
| 11 |
void *cfg, |
| 12 |
int (*set_tuning_param)(void*, const char*, size_t)) { |
| 13 |
const int max_line_len = 1024; |
| 14 |
char* line = (char*) malloc(max_line_len); |
| 15 |
|
| 16 |
FILE *f = fopen(fname, "r"); |
| 17 |
|
| 18 |
if (f == NULL) { |
| 19 |
snprintf(line, max_line_len, "Cannot open file: %s", strerror(errno)); |
| 20 |
return line; |
| 21 |
} |
| 22 |
|
| 23 |
int lineno = 0; |
| 24 |
while (fgets(line, max_line_len, f) != NULL) { |
| 25 |
lineno++; |
| 26 |
if (is_blank_line_or_comment(line)) { |
| 27 |
continue; |
| 28 |
} |
| 29 |
char *eql = strstr(line, "="); |
| 30 |
if (eql) { |
| 31 |
*eql = 0; |
| 32 |
char *endptr; |
| 33 |
int value = strtol(eql+1, &endptr, 10); |
| 34 |
if (*endptr && *endptr != '\n') { |
| 35 |
snprintf(line, max_line_len, "Invalid line %d (must be of form 'name=int').", |
| 36 |
lineno); |
| 37 |
return line; |
| 38 |
} |
| 39 |
if (set_tuning_param(cfg, line, (size_t)value) != 0) { |
| 40 |
char* err = (char*) malloc(max_line_len + 50); |
| 41 |
snprintf(err, max_line_len + 50, "Unknown name '%s' on line %d.", line, lineno); |
| 42 |
free(line); |
| 43 |
return err; |
| 44 |
} |
| 45 |
} else { |
| 46 |
snprintf(line, max_line_len, "Invalid line %d (must be of form 'name=int').", |
| 47 |
lineno); |
| 48 |
return line; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
free(line); |
| 53 |
|
| 54 |
return NULL; |
| 55 |
} |
| 56 |
|
| 57 |
// End of tuning.h. |