3
* Copyright (c) 2013-2014 struktur AG, Dirk Farin <farin@struktur.de>
5
* Authors: struktur AG, Dirk Farin <farin@struktur.de>
7
* This file is part of libde265.
9
* libde265 is free software: you can redistribute it and/or modify
10
* it under the terms of the GNU Lesser General Public License as
11
* published by the Free Software Foundation, either version 3 of
12
* the License, or (at your option) any later version.
14
* libde265 is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU Lesser General Public License for more details.
19
* You should have received a copy of the GNU Lesser General Public License
20
* along with libde265. If not, see <http://www.gnu.org/licenses/>.
23
#include "configparam.h"
34
#error "Need to compile with RTTI enabled."
37
static void remove_option(int* argc,char** argv,int idx, int n=1)
39
for (int i=idx+n;i<*argc;i++) {
47
bool option_string::processCmdLineArguments(char** argv, int* argc, int idx)
49
if (argv==NULL) { return false; }
50
if (idx >= *argc) { return false; }
55
remove_option(argc,argv,idx,1);
61
void option_int::set_range(int mini,int maxi)
69
std::string option_int::getTypeDescr() const
71
std::stringstream sstr;
74
if (have_low_limit || have_high_limit) { sstr << " "; }
75
if (have_low_limit) { sstr << low_limit << " <= "; }
76
if (have_low_limit || have_high_limit) { sstr << "x"; }
77
if (have_high_limit) { sstr << " <= " << high_limit; }
79
if (!valid_values_set.empty()) {
82
FOR_LOOP(int, v, valid_values_set) {
83
if (!first) sstr << ","; else first=false;
92
bool option_int::processCmdLineArguments(char** argv, int* argc, int idx)
94
if (argv==NULL) { return false; }
95
if (idx >= *argc) { return false; }
97
int v = atoi(argv[idx]);
98
if (!is_valid(v)) { return false; }
103
remove_option(argc,argv,idx,1);
108
bool option_int::is_valid(int v) const
110
if (have_low_limit && v<low_limit) { return false; }
111
if (have_high_limit && v>high_limit) { return false; }
113
if (!valid_values_set.empty()) {
114
auto iter = std::find(valid_values_set.begin(), valid_values_set.end(), v);
115
if (iter==valid_values_set.end()) { return false; }
121
std::string option_int::get_default_string() const
123
std::stringstream sstr;
124
sstr << default_value;
129
std::string choice_option_base::getTypeDescr() const
131
std::vector<std::string> choices = get_choice_names();
133
std::stringstream sstr;
137
#ifdef FOR_LOOP_AUTO_SUPPORT
138
FOR_LOOP(auto, c, choices) {
140
FOR_LOOP(std::string, c, choices) {
142
if (first) { first=false; }
143
else { sstr << ","; }
153
bool choice_option_base::processCmdLineArguments(char** argv, int* argc, int idx)
155
if (argv==NULL) { return false; }
156
if (idx >= *argc) { return false; }
158
std::string value = argv[idx];
160
std::cout << "set " << value << "\n";
161
bool success = set_value(value);
162
std::cout << "success " << success << "\n";
164
remove_option(argc,argv,idx,1);
170
static char* fill_strings_into_memory(const std::vector<std::string>& strings_list)
172
// calculate memory requirement
174
int totalStringLengths = 0;
175
#ifdef FOR_LOOP_AUTO_SUPPORT
176
FOR_LOOP(auto, str, strings_list) {
178
FOR_LOOP(std::string, str, strings_list) {
180
totalStringLengths += str.length() +1; // +1 for null termination
183
int numStrings = strings_list.size();
185
int pointersSize = (numStrings+1) * sizeof(const char*);
187
char* memory = new char[pointersSize + totalStringLengths];
190
// copy strings to memory area
192
char* stringPtr = memory + (numStrings+1) * sizeof(const char*);
193
const char** tablePtr = (const char**)memory;
195
#ifdef FOR_LOOP_AUTO_SUPPORT
196
FOR_LOOP(auto, str, strings_list) {
198
FOR_LOOP(std::string, str, strings_list) {
200
*tablePtr++ = stringPtr;
202
strcpy(stringPtr, str.c_str());
203
stringPtr += str.length()+1;
212
const char** choice_option_base::get_choices_string_table() const
214
if (choice_string_table==NULL) {
215
choice_string_table = fill_strings_into_memory(get_choice_names());
218
return (const char**)choice_string_table;
223
bool config_parameters::parse_command_line_params(int* argc, char** argv, int* first_idx_ptr,
224
bool ignore_unknown_options)
227
if (first_idx_ptr) { first_idx = *first_idx_ptr; }
229
for (int i=first_idx;i < *argc;i++) {
231
if (argv[i][0]=='-') {
234
if (argv[i][1]=='-') {
237
bool option_found=false;
239
for (int o=0;o<mOptions.size();o++) {
240
if (mOptions[o]->hasLongOption() && strcmp(mOptions[o]->getLongOption().c_str(),
244
bool success = mOptions[o]->processCmdLineArguments(argv,argc, i+1);
246
if (first_idx_ptr) { *first_idx_ptr = i; }
250
remove_option(argc,argv,i);
257
if (option_found == false && !ignore_unknown_options) {
264
bool is_single_option = (argv[i][1] != 0 && argv[i][2]==0);
265
bool do_remove_option = true;
267
for (int n=1; argv[i][n]; n++) {
268
char option = argv[i][n];
270
bool option_found=false;
272
for (int o=0;o<mOptions.size();o++) {
273
if (mOptions[o]->getShortOption() == option) {
277
if (is_single_option) {
278
success = mOptions[o]->processCmdLineArguments(argv,argc, i+1);
281
success = mOptions[o]->processCmdLineArguments(NULL,NULL, 0);
285
if (first_idx_ptr) { *first_idx_ptr = i; }
294
if (!ignore_unknown_options) {
295
fprintf(stderr, "unknown option -%c\n",option);
299
do_remove_option=false;
303
} // all short options
305
if (do_remove_option) {
306
remove_option(argc,argv,i);
311
} // all command line arguments
317
void config_parameters::print_params() const
319
for (int i=0;i<mOptions.size();i++) {
320
const option_base* o = mOptions[i];
322
std::stringstream sstr;
324
if (o->hasShortOption()) {
325
sstr << '-' << o->getShortOption();
330
if (o->hasShortOption() && o->hasLongOption()) {
336
if (o->hasLongOption()) {
337
sstr << "--" << std::setw(12) << std::left << o->getLongOption();
343
sstr << o->getTypeDescr();
345
if (o->has_default()) {
346
sstr << ", default=" << o->get_default_string();
351
std::cerr << sstr.str();
356
void config_parameters::add_option(option_base* o)
358
mOptions.push_back(o);
359
delete[] param_string_table; // delete old table, since we got a new parameter
360
param_string_table = NULL;
364
std::vector<std::string> config_parameters::get_parameter_IDs() const
366
std::vector<std::string> ids;
368
#ifdef FOR_LOOP_AUTO_SUPPORT
369
FOR_LOOP(auto, option, mOptions) {
371
FOR_LOOP(option_base*, option, mOptions) {
373
ids.push_back(option->get_name());
380
enum en265_parameter_type config_parameters::get_parameter_type(const char* param) const
382
option_base* option = find_option(param);
385
if (dynamic_cast<option_int*> (option)) { return en265_parameter_int; }
386
if (dynamic_cast<option_bool*> (option)) { return en265_parameter_bool; }
387
if (dynamic_cast<option_string*>(option)) { return en265_parameter_string; }
388
if (dynamic_cast<choice_option_base*>(option)) { return en265_parameter_choice; }
391
return en265_parameter_bool;
395
std::vector<std::string> config_parameters::get_parameter_choices(const char* param) const
397
option_base* option = find_option(param);
400
choice_option_base* o = dynamic_cast<choice_option_base*>(option);
403
return o->get_choice_names();
407
option_base* config_parameters::find_option(const char* param) const
409
#ifdef FOR_LOOP_AUTO_SUPPORT
410
FOR_LOOP(auto, o, mOptions) {
412
FOR_LOOP(option_base*, o, mOptions) {
414
if (strcmp(o->get_name().c_str(), param)==0) { return o; }
421
bool config_parameters::set_bool(const char* param, bool value)
423
option_base* option = find_option(param);
426
option_bool* o = dynamic_cast<option_bool*>(option);
429
return o->set(value);
432
bool config_parameters::set_int(const char* param, int value)
434
option_base* option = find_option(param);
437
option_int* o = dynamic_cast<option_int*>(option);
440
return o->set(value);
443
bool config_parameters::set_string(const char* param, const char* value)
445
option_base* option = find_option(param);
448
option_string* o = dynamic_cast<option_string*>(option);
451
return o->set(value);
454
bool config_parameters::set_choice(const char* param, const char* value)
456
option_base* option = find_option(param);
459
choice_option_base* o = dynamic_cast<choice_option_base*>(option);
462
return o->set(value);
467
const char** config_parameters::get_parameter_choices_table(const char* param) const
469
option_base* option = find_option(param);
472
choice_option_base* o = dynamic_cast<choice_option_base*>(option);
475
return o->get_choices_string_table();
478
const char** config_parameters::get_parameter_string_table() const
480
if (param_string_table==NULL) {
481
param_string_table = fill_strings_into_memory(get_parameter_IDs());
484
return (const char**)param_string_table;