Changeset 1356 in 3DVCSoftware for trunk/source/Lib/TAppCommon
- Timestamp:
- 27 Oct 2015, 11:33:16 (9 years ago)
- Location:
- trunk/source/Lib/TAppCommon
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/source/Lib/TAppCommon/program_options_lite.cpp
r1313 r1356 111 111 } 112 112 113 #if NH_MV_SEI 114 static void setOptions(Options::NamesPtrList& opt_list, const std::vector<int> idcs, const string& value, ErrorReporter& error_reporter) 115 #else 113 116 static void setOptions(Options::NamesPtrList& opt_list, const string& value, ErrorReporter& error_reporter) 117 #endif 114 118 { 115 119 /* multiple options may be registered for the same name: … … 117 121 for (Options::NamesPtrList::iterator it = opt_list.begin(); it != opt_list.end(); ++it) 118 122 { 123 #if NH_MV_SEI 124 Bool doParsing = (*it)->opt->checkDim( idcs, error_reporter ); 125 if ( doParsing ) 126 { 127 (*it)->opt->parse(value, idcs, error_reporter); 128 } 129 130 #else 119 131 (*it)->opt->parse(value, error_reporter); 132 #endif 120 133 } 121 134 } … … 277 290 bool OptionWriter::storePair(bool allow_long, bool allow_short, const string& name, const string& value) 278 291 { 292 #if NH_MV_SEI 293 std::vector<int> idcs; 294 295 std::size_t pos_underscore = name.find("_" ); 296 std::size_t pos_last_underscore_plus1 = pos_underscore+1; 297 std::size_t pos_first_underscore = pos_underscore; 298 299 while ( pos_underscore != string::npos ) 300 { 301 pos_underscore = name.find("_", pos_last_underscore_plus1 ); 302 size_t subStrlen = ( pos_underscore == string::npos ) ? string::npos : ( pos_underscore - pos_last_underscore_plus1 ); 303 string idx_str = name.substr( pos_last_underscore_plus1, subStrlen ); 304 idcs.push_back( atoi( idx_str.c_str())); 305 pos_last_underscore_plus1 = pos_underscore + 1; 306 } 307 308 string name_idcs = name.substr(0, pos_first_underscore ); 309 for (size_t i = 0; i < idcs.size(); i++ ) 310 { 311 name_idcs += "_%d"; 312 } 313 314 bool found_idcs = false; 315 Options::NamesMap::iterator opt_it_idcs; 316 #endif 279 317 bool found = false; 280 318 Options::NamesMap::iterator opt_it; … … 286 324 found = true; 287 325 } 326 #if NH_MV_SEI 327 if ( idcs.size() > 0 ) 328 { 329 opt_it_idcs = opts.opt_long_map.find(name_idcs); 330 if (opt_it_idcs != opts.opt_long_map.end() ) 331 { 332 assert( !found ); 333 found = true; 334 found_idcs = true; 335 opt_it = opt_it_idcs; 336 } 337 } 338 #endif 288 339 } 289 340 … … 296 347 found = true; 297 348 } 298 } 299 349 #if NH_MV_SEI 350 if ( idcs.size() > 0 ) 351 { 352 opt_it = opts.opt_short_map.find(name); 353 if (opt_it != opts.opt_short_map.end()) 354 { 355 assert( !found ); 356 found = true; 357 found_idcs = true; 358 opt_it = opt_it_idcs; 359 } 360 } 361 #endif 362 } 363 364 #if NH_MV_SEI 365 if ( !found_idcs ) 366 { 367 idcs.clear(); 368 } 369 #endif 300 370 if (!found) 301 371 { 372 #if NH_MV_SEI 373 if (error_reporter.output_on_unknow_parameter ) 374 { 375 #endif 376 302 377 error_reporter.error(where()) 303 378 << "Unknown option `" << name << "' (value:`" << value << "')\n"; 379 #if NH_MV_SEI 380 } 381 #endif 304 382 return false; 305 383 } 306 384 385 #if NH_MV_SEI 386 setOptions((*opt_it).second, idcs, value, error_reporter); 387 #else 307 388 setOptions((*opt_it).second, value, error_reporter); 389 #endif 308 390 return true; 309 391 } -
trunk/source/Lib/TAppCommon/program_options_lite.h
r1313 r1356 78 78 struct ErrorReporter 79 79 { 80 #if NH_MV_SEI 81 ErrorReporter() : is_errored(0), output_on_unknow_parameter(true) {} 82 #else 80 83 ErrorReporter() : is_errored(0) {} 84 #endif 81 85 virtual ~ErrorReporter() {} 82 86 virtual std::ostream& error(const std::string& where); 83 87 virtual std::ostream& warn(const std::string& where); 84 88 bool is_errored; 89 #if NH_MV_SEI 90 bool output_on_unknow_parameter; 91 #endif 85 92 }; 86 93 … … 98 105 { 99 106 #if NH_MV 107 #if NH_MV_SEI 108 OptionBase(const std::string& name, const std::string& desc, bool duplicate = false, std::vector< int > maxdim = std::vector< int >(0) ) 109 : opt_string(name), opt_desc(desc), opt_duplicate(duplicate), max_dim( maxdim ) 110 #else 100 111 OptionBase(const std::string& name, const std::string& desc, bool duplicate = false) 101 112 : opt_string(name), opt_desc(desc), opt_duplicate(duplicate) 113 #endif 102 114 #else 103 115 OptionBase(const std::string& name, const std::string& desc) … … 109 121 110 122 /* parse argument arg, to obtain a value for the option */ 123 #if NH_MV_SEI 124 virtual void parse(const std::string& arg, const std::vector<int>& idcs, ErrorReporter&) = 0; 125 126 bool checkDim( std::vector< int > dims, ErrorReporter& err ) 127 { 128 bool doParsing = true; 129 if ( dims.size() != max_dim.size() ) 130 { 131 err.error(" ") << "Number of indices of `" << opt_string << "' not matching. Should be " << max_dim.size() << std::endl; 132 doParsing = false; 133 } 134 135 for (size_t i = 0 ; i < dims.size(); i++ ) 136 { 137 if ( dims[i] >= max_dim[i] ) 138 { 139 if (err.output_on_unknow_parameter ) 140 { 141 err.warn(" ") << "Index " << i << " of " << opt_string << " should be less than " << max_dim[i] << std::endl; 142 doParsing = false; 143 } 144 } 145 } 146 return doParsing; 147 } 148 149 void xParseVec( const std::string& arg, BoolAry1d& storage ) 150 { 151 char* pcNextStart = (char*) arg.data(); 152 char* pcEnd = pcNextStart + arg.length(); 153 154 char* pcOldStart = 0; 155 156 size_t iIdx = 0; 157 158 while (pcNextStart < pcEnd) 159 { 160 if ( iIdx < storage.size() ) 161 { 162 storage[iIdx] = (strtol(pcNextStart, &pcNextStart,10) != 0); 163 } 164 else 165 { 166 storage.push_back(strtol(pcNextStart, &pcNextStart,10) != 0) ; 167 } 168 iIdx++; 169 170 if ( errno == ERANGE || (pcNextStart == pcOldStart) ) 171 { 172 std::cerr << "Error Parsing Bools: `" << arg << "'" << std::endl; 173 exit(EXIT_FAILURE); 174 }; 175 while( (pcNextStart < pcEnd) && ( *pcNextStart == ' ' || *pcNextStart == '\t' || *pcNextStart == '\r' ) ) pcNextStart++; 176 pcOldStart = pcNextStart; 177 } 178 } 179 180 void xParseVec( const std::string& arg, IntAry1d& storage ) 181 { 182 storage.clear(); 183 184 char* pcNextStart = (char*) arg.data(); 185 char* pcEnd = pcNextStart + arg.length(); 186 187 char* pcOldStart = 0; 188 189 size_t iIdx = 0; 190 191 192 while (pcNextStart < pcEnd) 193 { 194 195 if ( iIdx < storage.size() ) 196 { 197 storage[iIdx] = (int) strtol(pcNextStart, &pcNextStart,10); 198 } 199 else 200 { 201 storage.push_back( (int) strtol(pcNextStart, &pcNextStart,10)) ; 202 } 203 iIdx++; 204 if ( errno == ERANGE || (pcNextStart == pcOldStart) ) 205 { 206 std::cerr << "Error Parsing Integers: `" << arg << "'" << std::endl; 207 exit(EXIT_FAILURE); 208 }; 209 while( (pcNextStart < pcEnd) && ( *pcNextStart == ' ' || *pcNextStart == '\t' || *pcNextStart == '\r' ) ) pcNextStart++; 210 pcOldStart = pcNextStart; 211 } 212 } 213 #else 111 214 virtual void parse(const std::string& arg, ErrorReporter&) = 0; 215 #endif 112 216 /* set the argument to the default value */ 113 217 virtual void setDefault() = 0; … … 117 221 #if NH_MV 118 222 bool opt_duplicate; 223 #if NH_MV_SEI 224 std::vector<int> max_dim; 225 #endif 119 226 #endif 120 227 }; … … 125 232 { 126 233 #if NH_MV 234 #if NH_MV_SEI 235 Option(const std::string& name, T& storage, T default_val, const std::string& desc, bool duplicate = false, std::vector< int > maxdim = std::vector< int >(0) ) 236 : OptionBase(name, desc, duplicate, maxdim), opt_storage(storage), opt_default_val(default_val) 237 #else 127 238 Option(const std::string& name, T& storage, T default_val, const std::string& desc, bool duplicate = false) 128 239 : OptionBase(name, desc, duplicate), opt_storage(storage), opt_default_val(default_val) 240 #endif 129 241 #else 130 242 Option(const std::string& name, T& storage, T default_val, const std::string& desc) … … 133 245 {} 134 246 247 #if NH_MV_SEI 248 void parse(const std::string& arg, const std::vector<int>& idcs, ErrorReporter&); 249 #else 135 250 void parse(const std::string& arg, ErrorReporter&); 251 #endif 136 252 137 253 void setDefault() … … 147 263 template<typename T> 148 264 inline void 265 #if NH_MV_SEI 266 Option<T>::parse(const std::string& arg, const std::vector<int>& idcs, ErrorReporter&) 267 #else 149 268 Option<T>::parse(const std::string& arg, ErrorReporter&) 150 { 269 #endif 270 { 271 #if NH_MV_SEI 272 assert( idcs.size() == 0 ); 273 #endif 274 151 275 std::istringstream arg_ss (arg,std::istringstream::in); 152 276 arg_ss.exceptions(std::ios::failbit); … … 165 289 template<> 166 290 inline void 291 #if NH_MV_SEI 292 Option<std::string>::parse(const std::string& arg, const std::vector<int>& idcs, ErrorReporter&) 293 #else 167 294 Option<std::string>::parse(const std::string& arg, ErrorReporter&) 168 { 295 #endif 296 { 297 #if NH_MV_SEI 298 assert( idcs.size() == 0 ); 299 #endif 169 300 opt_storage = arg; 170 301 } … … 173 304 template<> 174 305 inline void 306 #if NH_MV_SEI 307 Option<char*>::parse(const std::string& arg, const std::vector<int>& idcs, ErrorReporter&) 308 #else 175 309 Option<char*>::parse(const std::string& arg, ErrorReporter&) 176 { 310 #endif 311 { 312 #if NH_MV_SEI 313 assert( idcs.size() == 0 ); 314 #endif 177 315 opt_storage = arg.empty() ? NULL : strdup(arg.c_str()) ; 178 316 } 317 318 #if !NH_MV_SEI 179 319 180 320 template<> … … 198 338 } 199 339 } 200 340 #endif 201 341 202 342 template<> 203 343 inline void 344 #if NH_MV_SEI 345 Option< std::vector<double> >::parse(const std::string& arg, const std::vector< int > & idcs, ErrorReporter&) 346 #else 204 347 Option< std::vector<double> >::parse(const std::string& arg, ErrorReporter&) 205 { 348 #endif 349 { 350 #if NH_MV_SEI 351 assert( idcs.size() == 0 ); 352 #endif 206 353 char* pcNextStart = (char*) arg.data(); 207 354 char* pcEnd = pcNextStart + arg.length(); … … 236 383 } 237 384 385 386 #if NH_MV_SEI 387 template<> 388 inline void 389 Option< IntAry1d >::parse(const std::string& arg, const IntAry1d& idcs, ErrorReporter& err) 390 { 391 xParseVec( arg, opt_storage ); 392 }; 393 394 template<> 395 inline void 396 Option< IntAry2d >::parse(const std::string& arg, const IntAry1d& idcs, ErrorReporter&) 397 { 398 xParseVec( arg, opt_storage[ idcs[0] ] ); 399 }; 400 401 template<> 402 inline void 403 Option< IntAry3d >::parse(const std::string& arg, const IntAry1d& idcs, ErrorReporter&) 404 { 405 xParseVec ( arg, opt_storage[ idcs[0] ][ idcs[1] ] ); 406 }; 407 #if SEI_DRI_F0169 408 template<> 409 inline void 410 Option< std::vector< std::vector<double> > >::parse(const std::string& arg, const IntAry1d& idcs, ErrorReporter&) 411 { 412 // xParseVec ( arg, opt_storage[ idcs[0] ] ); 413 char* pcNextStart = (char*) arg.data(); 414 char* pcEnd = pcNextStart + arg.length(); 415 416 char* pcOldStart = 0; 417 418 size_t iIdx = 0; 419 420 while (pcNextStart < pcEnd) 421 { 422 errno = 0; 423 424 if ( iIdx < opt_storage[idcs[0]].size() ) 425 { 426 opt_storage[idcs[0]][iIdx] = strtod(pcNextStart, &pcNextStart); 427 } 428 else 429 { 430 opt_storage[idcs[0]].push_back( strtod(pcNextStart, &pcNextStart)) ; 431 } 432 iIdx++; 433 434 if ( errno == ERANGE || (pcNextStart == pcOldStart) ) 435 { 436 std::cerr << "Error Parsing Doubles: `" << arg << "'" << std::endl; 437 exit(EXIT_FAILURE); 438 }; 439 while( (pcNextStart < pcEnd) && ( *pcNextStart == ' ' || *pcNextStart == '\t' || *pcNextStart == '\r' ) ) pcNextStart++; 440 pcOldStart = pcNextStart; 441 442 } 443 444 445 } 446 #endif 447 #else 238 448 template<> 239 449 inline void … … 272 482 } 273 483 } 274 275 484 #endif 485 486 #if NH_MV_SEI 487 488 template<> 489 inline void 490 Option< StringAry1d >::parse(const std::string& arg, const std::vector<int>& idcs, ErrorReporter& err ) 491 { 492 493 opt_storage[ idcs[ 0 ] ] = arg; 494 }; 495 496 template<> 497 inline void 498 Option< StringAry2d >::parse(const std::string& arg, const std::vector<int>& idcs, ErrorReporter& err ) 499 { 500 501 opt_storage[idcs[0]][idcs[1]] = arg; 502 }; 503 504 505 template<> 506 inline void 507 Option< std::vector< char*> >::parse(const std::string& arg, const std::vector<int>& idcs, ErrorReporter& err ) 508 { 509 510 opt_storage[ idcs[ 0 ] ] = arg.empty() ? NULL : strdup(arg.c_str()) ; 511 }; 512 513 template<> 514 inline void 515 Option< BoolAry1d >::parse(const std::string& arg, const std::vector<int>& idcs, ErrorReporter& err) 516 { 517 xParseVec( arg, opt_storage ); 518 }; 519 520 template<> 521 inline void 522 Option< BoolAry2d >::parse(const std::string& arg, const IntAry1d& idcs, ErrorReporter& err) 523 { 524 xParseVec( arg, opt_storage[ idcs[0] ] ); 525 }; 526 527 template<> 528 inline void 529 Option< BoolAry3d >::parse(const std::string& arg, const IntAry1d& idcs, ErrorReporter& err ) 530 { 531 xParseVec( arg, opt_storage[idcs[0]][idcs[1]] ); 532 }; 533 #else 276 534 template<> 277 535 inline void … … 307 565 } 308 566 #endif 567 #endif 309 568 /** Option class for argument handling using a user provided function */ 310 569 struct OptionFunc : public OptionBase … … 316 575 {} 317 576 577 #if NH_MV_SEI 578 void parse(const std::string& arg, const std::vector<int>& idcs, ErrorReporter& error_reporter) 579 #else 318 580 void parse(const std::string& arg, ErrorReporter& error_reporter) 581 #endif 319 582 { 320 583 func(parent, arg, error_reporter); … … 388 651 operator()(const std::string& name, std::vector<T>& storage, T default_val, unsigned uiMaxNum, const std::string& desc = "" ) 389 652 { 653 #if NH_MV_SEI 654 std::vector<T> defVal; 655 defVal.resize( uiMaxNum, default_val ); 656 std::vector< int > maxSize; 657 maxSize.push_back( uiMaxNum ); 658 parent.addOption(new Option< std::vector<T> >( name, storage, defVal, desc, false, maxSize )); 659 660 return *this; 661 } 662 663 template<typename T> 664 OptionSpecific& 665 operator()(const std::string& name, std::vector< std::vector<T> >& storage, T default_val, unsigned uiMaxNumDim1, unsigned uiMaxNumDim2, const std::string& desc = "" ) 666 { 667 std::vector< std::vector<T> > defVal; 668 defVal.resize(uiMaxNumDim1); 669 for ( unsigned int idxDim1 = 0; idxDim1 < uiMaxNumDim1; idxDim1++ ) 670 { 671 defVal[ idxDim1 ].resize(uiMaxNumDim2, default_val ); 672 } 673 674 std::vector< int > maxSize; 675 maxSize.push_back( uiMaxNumDim1 ); 676 maxSize.push_back( uiMaxNumDim2 ); 677 678 parent.addOption(new Option< std::vector< std::vector<T> > >( name, storage, defVal, desc, false, maxSize )); 679 return *this; 680 } 681 #else 390 682 std::string cNameBuffer; 391 683 std::string cDescBuffer; … … 415 707 return *this; 416 708 } 709 #endif 417 710 #endif 418 711 /**
Note: See TracChangeset for help on using the changeset viewer.