-
Notifications
You must be signed in to change notification settings - Fork 193
/
param.cpp
225 lines (189 loc) · 5.7 KB
/
param.cpp
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//
// CRF++ -- Yet Another CRF toolkit
//
// $Id: param.cpp 1587 2007-02-12 09:00:36Z taku $;
//
// Copyright(C) 2005-2007 Taku Kudo <[email protected]>
//
#include <fstream>
#include <cstdio>
#include "param.h"
#include "common.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
namespace CRFPP {
namespace {
void init_param(std::string *help,
std::string *version,
const std::string &system_name,
const Option *opts) {
*help = std::string(COPYRIGHT) + "\nUsage: " +
system_name + " [options] files\n";
*version = std::string(PACKAGE) + " of " + VERSION + '\n';
size_t max = 0;
for (size_t i = 0; opts[i].name; ++i) {
size_t l = 1 + std::strlen(opts[i].name);
if (opts[i].arg_description)
l += (1 + std::strlen(opts[i].arg_description));
max = std::max(l, max);
}
for (size_t i = 0; opts[i].name; ++i) {
size_t l = std::strlen(opts[i].name);
if (opts[i].arg_description)
l += (1 + std::strlen(opts[i].arg_description));
*help += " -";
*help += opts[i].short_name;
*help += ", --";
*help += opts[i].name;
if (opts[i].arg_description) {
*help += '=';
*help += opts[i].arg_description;
}
for (; l <= max; l++) *help += ' ';
*help += opts[i].description;
*help += '\n';
}
*help += '\n';
return;
}
} // namespace
void Param::dump_config(std::ostream *os) const {
for (std::map<std::string, std::string>::const_iterator it = conf_.begin();
it != conf_.end();
++it) {
*os << it->first << ": " << it->second << std::endl;
}
}
bool Param::load(const char *filename) {
std::ifstream ifs(WPATH(filename));
CHECK_FALSE(ifs) << "no such file or directory: " << filename;
std::string line;
while (std::getline(ifs, line)) {
if (!line.size() ||
(line.size() && (line[0] == ';' || line[0] == '#'))) continue;
size_t pos = line.find('=');
CHECK_FALSE(pos != std::string::npos) << "format error: " << line;
size_t s1, s2;
for (s1 = pos+1; s1 < line.size() && isspace(line[s1]); s1++);
for (s2 = pos-1; static_cast<long>(s2) >= 0 && isspace(line[s2]); s2--);
const std::string value = line.substr(s1, line.size() - s1);
const std::string key = line.substr(0, s2 + 1);
set<std::string>(key.c_str(), value, false);
}
return true;
}
bool Param::open(int argc, char **argv, const Option *opts) {
int ind = 0;
int _errno = 0;
#define GOTO_FATAL_ERROR(n) { \
_errno = n; \
goto FATAL_ERROR; } while (0)
if (argc <= 0) {
system_name_ = "unknown";
return true; // this is not error
}
system_name_ = std::string(argv[0]);
init_param(&help_, &version_, system_name_, opts);
for (size_t i = 0; opts[i].name; ++i) {
if (opts[i].default_value) set<std::string>
(opts[i].name, opts[i].default_value);
}
for (ind = 1; ind < argc; ind++) {
if (argv[ind][0] == '-') {
// long options
if (argv[ind][1] == '-') {
char *s;
for (s = &argv[ind][2]; *s != '\0' && *s != '='; s++);
size_t len = (size_t)(s - &argv[ind][2]);
if (len == 0) return true; // stop the scanning
bool hit = false;
size_t i = 0;
for (i = 0; opts[i].name; ++i) {
size_t nlen = std::strlen(opts[i].name);
if (nlen == len && std::strncmp(&argv[ind][2],
opts[i].name, len) == 0) {
hit = true;
break;
}
}
if (!hit) GOTO_FATAL_ERROR(0);
if (opts[i].arg_description) {
if (*s == '=') {
if (*(s+1) == '\0') GOTO_FATAL_ERROR(1);
set<std::string>(opts[i].name, s+1);
} else {
if (argc == (ind+1)) GOTO_FATAL_ERROR(1);
set<std::string>(opts[i].name, argv[++ind]);
}
} else {
if (*s == '=') GOTO_FATAL_ERROR(2);
set<int>(opts[i].name, 1);
}
// short options
} else if (argv[ind][1] != '\0') {
size_t i = 0;
bool hit = false;
for (i = 0; opts[i].name; ++i) {
if (opts[i].short_name == argv[ind][1]) {
hit = true;
break;
}
}
if (!hit) GOTO_FATAL_ERROR(0);
if (opts[i].arg_description) {
if (argv[ind][2] != '\0') {
set<std::string>(opts[i].name, &argv[ind][2]);
} else {
if (argc == (ind+1)) GOTO_FATAL_ERROR(1);
set<std::string>(opts[i].name, argv[++ind]);
}
} else {
if (argv[ind][2] != '\0') GOTO_FATAL_ERROR(2);
set<int>(opts[i].name, 1);
}
}
} else {
rest_.push_back(std::string(argv[ind])); // others
}
}
return true;
FATAL_ERROR:
switch (_errno) {
case 0: WHAT << "unrecognized option `" << argv[ind] << "`"; break;
case 1: WHAT << "`" << argv[ind] << "` requires an argument"; break;
case 2: WHAT << "`" << argv[ind] << "` doesn't allow an argument"; break;
}
return false;
}
void Param::clear() {
conf_.clear();
rest_.clear();
}
bool Param::open(const char *arg, const Option *opts) {
char str[BUF_SIZE];
std::strncpy(str, arg, sizeof(str));
char* ptr[64];
unsigned int size = 1;
ptr[0] = const_cast<char*>(PACKAGE);
for (char *p = str; *p;) {
while (isspace(*p)) *p++ = '\0';
if (*p == '\0') break;
ptr[size++] = p;
if (size == sizeof(ptr)) break;
while (*p && !isspace(*p)) p++;
}
return open(size, ptr, opts);
}
int Param::help_version() const {
if (get<bool>("help")) {
std::cout << help();
return 0;
}
if (get<bool>("version")) {
std::cout << version();
return 0;
}
return 1;
}
}