XCSF 1.4.8
XCSF learning classifier system
Loading...
Searching...
No Matches
config.c
Go to the documentation of this file.
1/*
2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation, either version 3 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 */
15
24#include "config.h"
25#include "param.h"
26
27#define MAXLEN (127)
28
33static void
35{
36 const char *d = s;
37 do {
38 while (*d == ' ' || *d == '\t' || *d == '\n' || *d == '\r') {
39 ++d;
40 }
41 } while ((*s++ = *d++));
42}
43
49void
50config_read(struct XCSF *xcsf, const char *filename)
51{
52 FILE *f = fopen(filename, "rt");
53 if (f == NULL) {
54 printf("Warning: could not open %s. %s.\n", filename, strerror(errno));
55 return;
56 }
57 fseek(f, 0, SEEK_END);
58 const long len = ftell(f);
59 fseek(f, 0, SEEK_SET);
60 char file_buff[len];
61 file_buff[0] = '\0';
62 char line_buff[MAXLEN];
63 while (!feof(f)) {
64 if (fgets(line_buff, MAXLEN - 2, f) == NULL) {
65 break;
66 }
67 config_trim(line_buff);
68 if (strnlen(line_buff, MAXLEN) == 0 || line_buff[0] == '#') {
69 continue; // ignore empty lines and lines starting with '#'
70 }
71 char *ptr = strchr(line_buff, '#'); // remove anything after #
72 if (ptr != NULL) {
73 *ptr = '\0';
74 }
75 strncat(file_buff, line_buff, MAXLEN);
76 }
77 fclose(f);
78 param_json_import(xcsf, file_buff);
79}
void config_read(struct XCSF *xcsf, const char *filename)
Reads the specified configuration file.
Definition config.c:50
static void config_trim(char *s)
Removes tabs/spaces/lf/cr.
Definition config.c:34
#define MAXLEN
Maximum config file line length to read.
Definition config.c:27
Configuration file handling functions.
void param_json_import(struct XCSF *xcsf, const char *json_str)
Sets the parameters from a json formatted string.
Definition param.c:437
Functions for setting and printing parameters.
XCSF data structure.
Definition xcsf.h:85