Changeset 1328 in 3DVCSoftware for branches/HTM-15.1-dev0/source/Lib/TAppCommon
- Timestamp:
- 14 Sep 2015, 19:41:29 (9 years ago)
- Location:
- branches/HTM-15.1-dev0/source/Lib/TAppCommon
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HTM-15.1-dev0/source/Lib/TAppCommon/program_options_lite.cpp
r1313 r1328 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 } -
branches/HTM-15.1-dev0/source/Lib/TAppCommon/program_options_lite.h
r1313 r1328 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 #else 238 408 template<> 239 409 inline void … … 272 442 } 273 443 } 274 275 444 #endif 445 446 #if NH_MV_SEI 447 448 template<> 449 inline void 450 Option< std::vector< char*> >::parse(const std::string& arg, const std::vector<int>& idcs, ErrorReporter& err ) 451 { 452 453 opt_storage[ idcs[ 0 ] ] = arg.empty() ? NULL : strdup(arg.c_str()) ; 454 }; 455 456 template<> 457 inline void 458 Option< BoolAry1d >::parse(const std::string& arg, const std::vector<int>& idcs, ErrorReporter& err) 459 { 460 xParseVec( arg, opt_storage ); 461 }; 462 463 template<> 464 inline void 465 Option< BoolAry2d >::parse(const std::string& arg, const IntAry1d& idcs, ErrorReporter& err) 466 { 467 xParseVec( arg, opt_storage[ idcs[0] ] ); 468 }; 469 470 template<> 471 inline void 472 Option< BoolAry3d >::parse(const std::string& arg, const IntAry1d& idcs, ErrorReporter& err ) 473 { 474 xParseVec( arg, opt_storage[idcs[0]][idcs[1]] ); 475 }; 476 #else 276 477 template<> 277 478 inline void … … 307 508 } 308 509 #endif 510 #endif 309 511 /** Option class for argument handling using a user provided function */ 310 512 struct OptionFunc : public OptionBase … … 316 518 {} 317 519 520 #if NH_MV_SEI 521 void parse(const std::string& arg, const std::vector<int>& idcs, ErrorReporter& error_reporter) 522 #else 318 523 void parse(const std::string& arg, ErrorReporter& error_reporter) 524 #endif 319 525 { 320 526 func(parent, arg, error_reporter); … … 388 594 operator()(const std::string& name, std::vector<T>& storage, T default_val, unsigned uiMaxNum, const std::string& desc = "" ) 389 595 { 596 #if NH_MV_SEI 597 std::vector<T> defVal; 598 defVal.resize( uiMaxNum, default_val ); 599 std::vector< int > maxSize; 600 maxSize.push_back( uiMaxNum ); 601 parent.addOption(new Option< std::vector<T> >( name, storage, defVal, desc, false, maxSize )); 602 603 return *this; 604 } 605 template<typename T> 606 OptionSpecific& 607 operator()(const std::string& name, std::vector< std::vector<T> >& storage, T default_val, unsigned uiMaxNumDim1, unsigned uiMaxNumDim2, const std::string& desc = "" ) 608 { 609 std::vector< std::vector<T> > defVal; 610 defVal.resize(uiMaxNumDim1); 611 for ( unsigned int idxDim1 = 0; idxDim1 < uiMaxNumDim1; idxDim1++ ) 612 { 613 defVal[ idxDim1 ].resize(uiMaxNumDim2, default_val ); 614 } 615 616 std::vector< int > maxSize; 617 maxSize.push_back( uiMaxNumDim1 ); 618 maxSize.push_back( uiMaxNumDim2 ); 619 620 parent.addOption(new Option< std::vector< std::vector<T> > >( name, storage, defVal, desc, false, maxSize )); 621 return *this; 622 } 623 #else 390 624 std::string cNameBuffer; 391 625 std::string cDescBuffer; … … 415 649 return *this; 416 650 } 651 #endif 417 652 #endif 418 653 /**
Note: See TracChangeset for help on using the changeset viewer.