-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparameters.cpp
More file actions
109 lines (95 loc) · 2.34 KB
/
parameters.cpp
File metadata and controls
109 lines (95 loc) · 2.34 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* Vectorix -- line-based image vectorizer
* (c) 2016 Jan Hadrava <had@atrey.karlin.mff.cuni.cz>
*/
#include <cstdio>
#include "parameters.h"
#include <cstdlib>
#include <unordered_map>
#include <vector>
#include <cstring>
// Program parameters
namespace vectorix {
void parameters::load_params(FILE *fd) { // Load parameters from file
char *line;
int linenumber = 0;
while (fscanf(fd, "%m[^\n]\n", &line) >= 0) {
linenumber++;
if (!line) // Skip empty line
continue;
if (line[0] == '#') { // Skip commented line
free(line);
continue;
}
char *name;
char *value;
sscanf(line, "%m[^ ] %m[^\n]", &name, &value); // Split by first space
if ((!name) && (!value)) {
free(line);
continue;
}
else if ((!name) && (value)) {
free(value);
free(line);
continue;
}
else if ((name) && (!value)) {
value = (char *) malloc(1);
value[0] = '\0';
}
// Everything is ok
std::string n = name;
std::string v = value;
int count_loaders = 0;
for (auto &par: parameter_list) {
if (n == par->name) {
par->load_var(value);
count_loaders++;
}
}
if (!count_loaders) {
auto old = not_loaded.find(n);
if (old == not_loaded.end())
not_loaded.insert({n,v});
else {
old->second = v;
}
}
free(name);
free(value);
free(line);
}
}
void parameters::save_params(FILE *fd) const { // Write parameters (with simple help) to opened filedescriptor
for (auto const &par: parameter_list) {
par->save_var(fd);
}
}
void parameters::load_params(const std::string &filename) { // Load parameters from file given by name
FILE *fd;
if ((fd = fopen(filename.c_str(), "r")) == NULL) {
fprintf(stderr, "Couldn't open config file for reading.\n");
return;
}
load_params(fd);
fclose(fd);
}
void parameters::save_params(const std::string &filename, bool append) const { // Save parameters to file given by name
FILE *fd;
if ((fd = fopen(filename.c_str(), (append) ? "a" : "w")) == NULL) {
fprintf(stderr, "Couldn't open config file for writing.\n");
return;
}
save_params(fd);
fclose(fd);
}
void parameters::add_comment(const char *name) {
std::shared_ptr<comment> s = std::make_shared<comment>();
s->name = ((std::string) "# ") + name;
auto old = binded_list.find(s->name);
if (old == binded_list.end()) {
parameter_list.push_back(s);
binded_list.insert({s->name, s});
}
}
}; // namespace