-
Notifications
You must be signed in to change notification settings - Fork 2
/
configparam.cc
491 lines (367 loc) · 10.9 KB
/
configparam.cc
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
* H.265 video codec.
* Copyright (c) 2013-2014 struktur AG, Dirk Farin <[email protected]>
*
* Authors: struktur AG, Dirk Farin <[email protected]>
*
* This file is part of libde265.
*
* libde265 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* libde265 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libde265. If not, see <http://www.gnu.org/licenses/>.
*/
#include "configparam.h"
#include <string.h>
#include <ctype.h>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <typeinfo>
#ifndef RTTI_ENABLED
#error "Need to compile with RTTI enabled."
#endif
static void remove_option(int* argc,char** argv,int idx, int n=1)
{
for (int i=idx+n;i<*argc;i++) {
argv[i-n] = argv[i];
}
*argc-=n;
}
bool option_string::processCmdLineArguments(char** argv, int* argc, int idx)
{
if (argv==NULL) { return false; }
if (idx >= *argc) { return false; }
value = argv[idx];
value_set = true;
remove_option(argc,argv,idx,1);
return true;
}
void option_int::set_range(int mini,int maxi)
{
have_low_limit =true;
have_high_limit=true;
low_limit =mini;
high_limit=maxi;
}
std::string option_int::getTypeDescr() const
{
std::stringstream sstr;
sstr << "(int)";
if (have_low_limit || have_high_limit) { sstr << " "; }
if (have_low_limit) { sstr << low_limit << " <= "; }
if (have_low_limit || have_high_limit) { sstr << "x"; }
if (have_high_limit) { sstr << " <= " << high_limit; }
if (!valid_values_set.empty()) {
sstr << " {";
bool first=true;
FOR_LOOP(int, v, valid_values_set) {
if (!first) sstr << ","; else first=false;
sstr << v;
}
sstr << "}";
}
return sstr.str();
}
bool option_int::processCmdLineArguments(char** argv, int* argc, int idx)
{
if (argv==NULL) { return false; }
if (idx >= *argc) { return false; }
int v = atoi(argv[idx]);
if (!is_valid(v)) { return false; }
value = v;
value_set = true;
remove_option(argc,argv,idx,1);
return true;
}
bool option_int::is_valid(int v) const
{
if (have_low_limit && v<low_limit) { return false; }
if (have_high_limit && v>high_limit) { return false; }
if (!valid_values_set.empty()) {
auto iter = std::find(valid_values_set.begin(), valid_values_set.end(), v);
if (iter==valid_values_set.end()) { return false; }
}
return true;
}
std::string option_int::get_default_string() const
{
std::stringstream sstr;
sstr << default_value;
return sstr.str();
}
std::string choice_option_base::getTypeDescr() const
{
std::vector<std::string> choices = get_choice_names();
std::stringstream sstr;
sstr << "{";
bool first=true;
#ifdef FOR_LOOP_AUTO_SUPPORT
FOR_LOOP(auto, c, choices) {
#else
FOR_LOOP(std::string, c, choices) {
#endif
if (first) { first=false; }
else { sstr << ","; }
sstr << c;
}
sstr << "}";
return sstr.str();
}
bool choice_option_base::processCmdLineArguments(char** argv, int* argc, int idx)
{
if (argv==NULL) { return false; }
if (idx >= *argc) { return false; }
std::string value = argv[idx];
std::cout << "set " << value << "\n";
bool success = set_value(value);
std::cout << "success " << success << "\n";
remove_option(argc,argv,idx,1);
return success;
}
static char* fill_strings_into_memory(const std::vector<std::string>& strings_list)
{
// calculate memory requirement
int totalStringLengths = 0;
#ifdef FOR_LOOP_AUTO_SUPPORT
FOR_LOOP(auto, str, strings_list) {
#else
FOR_LOOP(std::string, str, strings_list) {
#endif
totalStringLengths += str.length() +1; // +1 for null termination
}
int numStrings = strings_list.size();
int pointersSize = (numStrings+1) * sizeof(const char*);
char* memory = new char[pointersSize + totalStringLengths];
// copy strings to memory area
char* stringPtr = memory + (numStrings+1) * sizeof(const char*);
const char** tablePtr = (const char**)memory;
#ifdef FOR_LOOP_AUTO_SUPPORT
FOR_LOOP(auto, str, strings_list) {
#else
FOR_LOOP(std::string, str, strings_list) {
#endif
*tablePtr++ = stringPtr;
strcpy(stringPtr, str.c_str());
stringPtr += str.length()+1;
}
*tablePtr = NULL;
return memory;
}
const char** choice_option_base::get_choices_string_table() const
{
if (choice_string_table==NULL) {
choice_string_table = fill_strings_into_memory(get_choice_names());
}
return (const char**)choice_string_table;
}
bool config_parameters::parse_command_line_params(int* argc, char** argv, int* first_idx_ptr,
bool ignore_unknown_options)
{
int first_idx=1;
if (first_idx_ptr) { first_idx = *first_idx_ptr; }
for (int i=first_idx;i < *argc;i++) {
if (argv[i][0]=='-') {
// option
if (argv[i][1]=='-') {
// long option
bool option_found=false;
for (int o=0;o<mOptions.size();o++) {
if (mOptions[o]->hasLongOption() && strcmp(mOptions[o]->getLongOption().c_str(),
argv[i]+2)==0) {
option_found=true;
printf("FOUND %s\n",argv[i]);
bool success = mOptions[o]->processCmdLineArguments(argv,argc, i+1);
if (!success) {
if (first_idx_ptr) { *first_idx_ptr = i; }
return false;
}
remove_option(argc,argv,i);
i--;
break;
}
}
if (option_found == false && !ignore_unknown_options) {
return false;
}
}
else {
// short option
bool is_single_option = (argv[i][1] != 0 && argv[i][2]==0);
bool do_remove_option = true;
for (int n=1; argv[i][n]; n++) {
char option = argv[i][n];
bool option_found=false;
for (int o=0;o<mOptions.size();o++) {
if (mOptions[o]->getShortOption() == option) {
option_found=true;
bool success;
if (is_single_option) {
success = mOptions[o]->processCmdLineArguments(argv,argc, i+1);
}
else {
success = mOptions[o]->processCmdLineArguments(NULL,NULL, 0);
}
if (!success) {
if (first_idx_ptr) { *first_idx_ptr = i; }
return false;
}
break;
}
}
if (!option_found) {
if (!ignore_unknown_options) {
fprintf(stderr, "unknown option -%c\n",option);
return false;
}
else {
do_remove_option=false;
}
}
} // all short options
if (do_remove_option) {
remove_option(argc,argv,i);
i--;
}
} // is short option
} // is option
} // all command line arguments
return true;
}
void config_parameters::print_params() const
{
for (int i=0;i<mOptions.size();i++) {
const option_base* o = mOptions[i];
std::stringstream sstr;
sstr << " ";
if (o->hasShortOption()) {
sstr << '-' << o->getShortOption();
} else {
sstr << " ";
}
if (o->hasShortOption() && o->hasLongOption()) {
sstr << ", ";
} else {
sstr << " ";
}
if (o->hasLongOption()) {
sstr << "--" << std::setw(12) << std::left << o->getLongOption();
} else {
sstr << " ";
}
sstr << " ";
sstr << o->getTypeDescr();
if (o->has_default()) {
sstr << ", default=" << o->get_default_string();
}
if (o->has_description()) {
sstr << " : " << o->get_description();
}
sstr << "\n";
std::cerr << sstr.str();
}
}
void config_parameters::add_option(option_base* o)
{
mOptions.push_back(o);
delete[] param_string_table; // delete old table, since we got a new parameter
param_string_table = NULL;
}
std::vector<std::string> config_parameters::get_parameter_IDs() const
{
std::vector<std::string> ids;
#ifdef FOR_LOOP_AUTO_SUPPORT
FOR_LOOP(auto, option, mOptions) {
#else
FOR_LOOP(option_base*, option, mOptions) {
#endif
ids.push_back(option->get_name());
}
return ids;
}
enum en265_parameter_type config_parameters::get_parameter_type(const char* param) const
{
option_base* option = find_option(param);
assert(option);
if (dynamic_cast<option_int*> (option)) { return en265_parameter_int; }
if (dynamic_cast<option_bool*> (option)) { return en265_parameter_bool; }
if (dynamic_cast<option_string*>(option)) { return en265_parameter_string; }
if (dynamic_cast<choice_option_base*>(option)) { return en265_parameter_choice; }
assert(false);
return en265_parameter_bool;
}
std::vector<std::string> config_parameters::get_parameter_choices(const char* param) const
{
option_base* option = find_option(param);
assert(option);
choice_option_base* o = dynamic_cast<choice_option_base*>(option);
assert(o);
return o->get_choice_names();
}
option_base* config_parameters::find_option(const char* param) const
{
#ifdef FOR_LOOP_AUTO_SUPPORT
FOR_LOOP(auto, o, mOptions) {
#else
FOR_LOOP(option_base*, o, mOptions) {
#endif
if (strcmp(o->get_name().c_str(), param)==0) { return o; }
}
return NULL;
}
bool config_parameters::set_bool(const char* param, bool value)
{
option_base* option = find_option(param);
assert(option);
option_bool* o = dynamic_cast<option_bool*>(option);
assert(o);
return o->set(value);
}
bool config_parameters::set_int(const char* param, int value)
{
option_base* option = find_option(param);
assert(option);
option_int* o = dynamic_cast<option_int*>(option);
assert(o);
return o->set(value);
}
bool config_parameters::set_string(const char* param, const char* value)
{
option_base* option = find_option(param);
assert(option);
option_string* o = dynamic_cast<option_string*>(option);
assert(o);
return o->set(value);
}
bool config_parameters::set_choice(const char* param, const char* value)
{
option_base* option = find_option(param);
assert(option);
choice_option_base* o = dynamic_cast<choice_option_base*>(option);
assert(o);
return o->set(value);
}
const char** config_parameters::get_parameter_choices_table(const char* param) const
{
option_base* option = find_option(param);
assert(option);
choice_option_base* o = dynamic_cast<choice_option_base*>(option);
assert(o);
return o->get_choices_string_table();
}
const char** config_parameters::get_parameter_string_table() const
{
if (param_string_table==NULL) {
param_string_table = fill_strings_into_memory(get_parameter_IDs());
}
return (const char**)param_string_table;
}