Changeset 1246 in SHVCSoftware for branches/SHM-dev/source/Lib/TAppCommon
- Timestamp:
- 14 Jul 2015, 00:26:07 (10 years ago)
- Location:
- branches/SHM-dev/source/Lib/TAppCommon
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/SHM-dev/source/Lib/TAppCommon/program_options_lite.cpp
r1029 r1246 2 2 * License, included below. This software may be subject to other third party 3 3 * and contributor rights, including patent rights, and no such rights are 4 * granted under this license. 4 * granted under this license. 5 5 * 6 6 * Copyright (c) 2010-2014, ITU/ISO/IEC … … 50 50 namespace program_options_lite 51 51 { 52 52 53 53 Options::~Options() 54 54 { … … 58 58 } 59 59 } 60 60 61 61 void Options::addOption(OptionBase *opt) 62 62 { … … 64 64 names->opt = opt; 65 65 string& opt_string = opt->opt_string; 66 66 67 67 size_t opt_start = 0; 68 68 for (size_t opt_end = 0; opt_end != string::npos;) … … 96 96 return OptionSpecific(*this); 97 97 } 98 98 99 99 static void setOptions(Options::NamesPtrList& opt_list, const string& value) 100 100 { … … 108 108 109 109 static const char spaces[41] = " "; 110 110 111 111 /* format help text for a single option: 112 112 * using the formatting: "-x, --long", … … 138 138 } 139 139 } 140 140 141 141 /* format the help text */ 142 142 void doHelp(ostream& out, Options& opts, unsigned columns) … … 154 154 unsigned opt_width = min(max_width+2, 28u + pad_short) + 2; 155 155 unsigned desc_width = columns - opt_width; 156 156 157 157 /* second pass: write out formatted option and help text. 158 158 * - align start of help text to start at opt_width … … 209 209 split_pos = opt_desc.find_last_not_of(' ', split_pos) + 1; 210 210 } 211 211 212 212 /* bad split if no suitable space to split at. fall back to width */ 213 213 bool bad_split = split_pos == string::npos || split_pos <= cur_pos; … … 217 217 } 218 218 line << opt_desc.substr(cur_pos, split_pos - cur_pos); 219 219 220 220 /* eat up any space for the start of the next line */ 221 221 if (!bad_split) … … 224 224 } 225 225 cur_pos = newline_pos = split_pos; 226 226 227 227 if (cur_pos >= opt_desc.size()) 228 228 { … … 235 235 } 236 236 } 237 237 238 238 bool storePair(Options& opts, bool allow_long, bool allow_short, const string& name, const string& value) 239 239 { … … 248 248 } 249 249 } 250 250 251 251 /* check for the short list */ 252 252 if (allow_short && !(found && allow_long)) … … 269 269 return true; 270 270 } 271 271 272 272 bool storePair(Options& opts, const string& name, const string& value) 273 273 { 274 274 return storePair(opts, true, true, name, value); 275 275 } 276 276 277 277 /** 278 278 * returns number of extra arguments consumed … … 288 288 size_t arg_opt_sep = arg.find_first_of('='); 289 289 string option = arg.substr(arg_opt_start, arg_opt_sep - arg_opt_start); 290 290 291 291 unsigned extra_argc_consumed = 0; 292 292 if (arg_opt_sep == string::npos) … … 299 299 * booleans */ 300 300 if (argc == 1) 301 { 301 302 return 0; /* run out of argv for argument */ 303 } 302 304 extra_argc_consumed = 1; 303 305 #endif … … 339 341 return 1; 340 342 } 341 343 342 344 list<const char*> 343 345 scanArgv(Options& opts, unsigned argc, const char* argv[]) … … 376 378 /* a lone double dash ends option processing */ 377 379 while (++i < argc) 380 { 378 381 non_option_arguments.push_back(argv[i]); 382 } 379 383 break; 380 384 } … … 386 390 return non_option_arguments; 387 391 } 388 392 389 393 void scanLine(Options& opts, string& line) 390 394 { … … 441 445 * any trailing whitespace will be removed shortly */ 442 446 value_end = line.find_first_not_of(" \t\n\r", value_end); 443 } 444 while (value_end != string::npos); 447 } while (value_end != string::npos); 445 448 /* strip any trailing space from value*/ 446 449 value_end = line.find_last_not_of(" \t\n\r", value_end); … … 468 471 getline(in, line); 469 472 scanLine(opts, line); 470 } 471 while(!!in); 473 } while(!!in); 472 474 } 473 475 -
branches/SHM-dev/source/Lib/TAppCommon/program_options_lite.h
r1029 r1246 2 2 * License, included below. This software may be subject to other third party 3 3 * and contributor rights, including patent rights, and no such rights are 4 * granted under this license. 4 * granted under this license. 5 5 * 6 6 * Copyright (c) 2010-2014, ITU/ISO/IEC … … 49 49 { 50 50 struct Options; 51 51 52 52 struct ParseFailure : public std::exception 53 53 { … … 73 73 void parseConfigFile(Options& opts, const std::string& filename); 74 74 bool storePair(Options& opts, const std::string& name, const std::string& value); 75 75 76 76 /** OptionBase: Virtual base class for storing information relating to a 77 77 * specific option This base class describes common elements. Type specific … … 82 82 : opt_string(name), opt_desc(desc) 83 83 {}; 84 84 85 85 virtual ~OptionBase() {} 86 86 87 87 /* parse argument arg, to obtain a value for the option */ 88 88 virtual void parse(const std::string& arg) = 0; 89 89 /* set the argument to the default value */ 90 90 virtual void setDefault() = 0; 91 91 92 92 std::string opt_string; 93 93 std::string opt_desc; 94 94 }; 95 95 96 96 /** Type specific option storage */ 97 97 template<typename T> … … 101 101 : OptionBase(name, desc), opt_storage(storage), opt_default_val(default_val) 102 102 {} 103 103 104 104 void parse(const std::string& arg); 105 105 106 106 void setDefault() 107 107 { 108 108 opt_storage = opt_default_val; 109 109 } 110 110 111 111 T& opt_storage; 112 112 T opt_default_val; 113 113 }; 114 114 115 115 /* Generic parsing */ 116 116 template<typename T> … … 129 129 } 130 130 } 131 131 132 132 /* string parsing is specialized -- copy the whole string, not just the 133 133 * first word */ … … 138 138 opt_storage = arg; 139 139 } 140 140 141 141 /** Option class for argument handling using a user provided function */ 142 142 struct OptionFunc : public OptionBase 143 143 { 144 144 typedef void (Func)(Options&, const std::string&); 145 145 146 146 OptionFunc(const std::string& name, Options& parent_, Func *func_, const std::string& desc) 147 147 : OptionBase(name, desc), parent(parent_), func(func_) 148 148 {} 149 149 150 150 void parse(const std::string& arg) 151 151 { 152 152 func(parent, arg); 153 153 } 154 154 155 155 void setDefault() 156 156 { 157 157 return; 158 158 } 159 159 160 160 private: 161 161 Options& parent; 162 162 void (*func)(Options&, const std::string&); 163 163 }; 164 164 165 165 class OptionSpecific; 166 166 struct Options 167 167 { 168 168 ~Options(); 169 169 170 170 OptionSpecific addOptions(); 171 171 172 172 struct Names 173 173 { … … 176 176 { 177 177 if (opt) 178 { 178 179 delete opt; 180 } 179 181 } 180 182 std::list<std::string> opt_long; … … 184 186 185 187 void addOption(OptionBase *opt); 186 188 187 189 typedef std::list<Names*> NamesPtrList; 188 190 NamesPtrList opt_list; 189 191 190 192 typedef std::map<std::string, NamesPtrList> NamesMap; 191 193 NamesMap opt_long_map; 192 194 NamesMap opt_short_map; 193 195 }; 194 196 195 197 /* Class with templated overloaded operator(), for use by Options::addOptions() */ 196 198 class OptionSpecific … … 198 200 public: 199 201 OptionSpecific(Options& parent_) : parent(parent_) {} 200 202 201 203 /** 202 204 * Add option described by name to the parent Options list, … … 281 283 Options& parent; 282 284 }; 283 285 284 286 } /* namespace: program_options_lite */ 285 287 } /* namespace: df */
Note: See TracChangeset for help on using the changeset viewer.