[complement-svn] SF.net SVN: complement: [1880] trunk/complement/explore
Status: Pre-Alpha
Brought to you by:
complement
From: <Dmi...@us...> - 2008-05-23 10:41:16
|
Revision: 1880 http://complement.svn.sourceforge.net/complement/?rev=1880&view=rev Author: DmitryOsmakov Date: 2008-05-23 03:41:10 -0700 (Fri, 23 May 2008) Log Message: ----------- fixed lib Opts : access to opts storage is modified everywhere (using options_container_type to make it more generic) Modified Paths: -------------- trunk/complement/explore/include/misc/opts.h trunk/complement/explore/lib/misc/opts.cpp trunk/complement/explore/lib/misc/ut/opts_test.cc Modified: trunk/complement/explore/include/misc/opts.h =================================================================== --- trunk/complement/explore/include/misc/opts.h 2008-05-22 14:26:19 UTC (rev 1879) +++ trunk/complement/explore/include/misc/opts.h 2008-05-23 10:41:10 UTC (rev 1880) @@ -19,9 +19,10 @@ char shortname; std::string longname; std::string desc; - std::vector< std::string > args; std::string default_v; + std::vector< std::string > args; + bool has_arg; bool is_set; int cnt; // number of times this option was encounterd in command line @@ -29,6 +30,9 @@ class Opts { + private: + typedef std::vector< Opt > options_container_type; + options_container_type storage; public: Opts( const std::string& _brief = "", const std::string& _author = "", const std::string& _copyright = "") : brief(_brief), @@ -109,12 +113,7 @@ { } }; - //std::vector< std::string > args; private: - // data - typedef std::vector< Opt > options_container_type; - options_container_type storage; - std::string pname; std::string brief; std::string author; @@ -124,7 +123,7 @@ bool is_opt_name( const std::string& s ); bool is_flag_group( const std::string& s ); bool is_substr(const std::string& small, const std::string& big ); - int get_opt_index( std::string s ); + options_container_type::iterator get_opt_index( std::string s ); }; template <class T> @@ -170,26 +169,26 @@ template <class T> T Opts::get_default( char _shortname ) { - int i; + options_container_type::const_iterator i; T res; - for (i = 0;i < storage.size();i++) - if (storage[i].shortname == _shortname) + for (i = storage.begin();i != storage.end();++i) + if (i->shortname == _shortname) { - if (!storage[i].has_arg) + if (!i->has_arg) throw bad_usage("using Opts::get for option without arguments"); std::stringstream ss; - ss << storage[i].default_v; + ss << i->default_v; ss >> res; if (ss.fail()) - throw invalid_arg(std::string("-") + std::string(1,_shortname),storage[i].default_v); + throw invalid_arg(std::string("-") + std::string(1,_shortname),i->default_v); break; } - if (i == storage.size()) + if (i == storage.end()) throw unknown_option(std::string("-") + std::string(1,_shortname)); return res; } @@ -197,29 +196,26 @@ template <class T> T Opts::get( const std::string& _longname ) { - int i; + options_container_type::const_iterator i; T res; - for (i = 0;i < storage.size();i++) - if (storage[i].longname == _longname) + for (i = storage.begin();i != storage.end();++i) + if (i->longname == _longname) { - if (!storage[i].has_arg) + if (!i->has_arg) throw bad_usage("using Opts::get for option without arguments"); std::stringstream ss; - if (!storage[i].args.empty()) - ss << storage[i].args[0]; - else - ss << storage[i].default_v; + ss << (i->args.empty() ? i->default_v : i->args[0]); ss >> res; if (ss.fail()) // need to recover stream? - throw invalid_arg(std::string("--") + _longname,storage[i].args[0]); + throw invalid_arg(std::string("--") + _longname,i->args[0]); break; } - if (i == storage.size()) + if (i == storage.end()) throw unknown_option(std::string("--") + _longname); return res; } @@ -227,25 +223,25 @@ template <class T> T Opts::get_default( const std::string& _longname ) { - int i; + options_container_type::const_iterator i; T res; - for (i = 0;i < storage.size();i++) - if (storage[i].longname == _longname) + for (i = storage.begin();i != storage.end();++i) + if (i->longname == _longname) { - if (!storage[i].has_arg) + if (!i->has_arg) throw bad_usage("using Opts::get for option without arguments"); - std::stringstream ss(storage[i].default_v); + std::stringstream ss(i->default_v); ss >> res; if (ss.fail()) // need to recover stream? - throw invalid_arg(std::string("--") + _longname,storage[i].default_v); + throw invalid_arg(std::string("--") + _longname,i->default_v); break; } - if (i == storage.size()) + if (i == storage.end()) throw unknown_option(std::string("--") + _longname); return res; } @@ -253,82 +249,70 @@ template <class BackInsertIterator> void Opts::getemall( char _shortname , BackInsertIterator bi) { - int i; - for (i = 0;i < storage.size();i++) - if (storage[i].shortname == _shortname) + options_container_type::const_iterator i; + for (i = storage.begin();i != storage.end();++i) + if (i->shortname == _shortname) { - if (!storage[i].has_arg) + if (!i->has_arg) throw bad_usage("using Opts::getemall for option without arguments"); - if (!storage[i].default_v.empty()) - { - std::stringstream ss(storage[i].default_v); - ss >> *bi++; - } - - if (!storage[i].args.empty()) - for (int j = 0;j < storage[i].args.size();j++) + if (!i->args.empty()) + for (int j = 0;j < i->args.size();j++) { - std::stringstream ss(storage[i].args[j]); + std::stringstream ss(i->args[j]); try { ss >> *bi++; } catch(...) { - throw invalid_arg(std::string("-") + std::string(1,_shortname),storage[i].args[j]); + throw invalid_arg(std::string("-") + std::string(1,_shortname),i->args[j]); } if (ss.fail()) - throw invalid_arg(std::string("-") + std::string(1,_shortname),storage[i].args[j]); + throw invalid_arg(std::string("-") + std::string(1,_shortname),i->args[j]); } break; } - if (i == storage.size()) + if (i == storage.end()) throw unknown_option(std::string("-") + std::string(1,_shortname)); } template <class BackInsertIterator> void Opts::getemall( const std::string& _longname , BackInsertIterator bi) { - int i; - for (i = 0;i < storage.size();i++) - if (storage[i].longname == _longname) + options_container_type::const_iterator i; + for (i = storage.begin();i != storage.end();++i) + if (i->longname == _longname) { - if (!storage[i].has_arg) + if (!i->has_arg) throw bad_usage("using Opts::getemall for option without arguments"); - if (!storage[i].default_v.empty()) - { - std::stringstream ss(storage[i].default_v); - ss >> *bi++; - } - - if (!storage[i].args.empty()) - for (int j = 0;j < storage[i].args.size();j++) + if (!i->args.empty()) + for (int j = 0;j < i->args.size();j++) { - std::stringstream ss(storage[i].args[j]); + std::stringstream ss(i->args[j]); try { ss >> *bi++; } catch(...) { - throw invalid_arg(std::string("--") + _longname,storage[i].args[j]); + throw invalid_arg(std::string("--") + _longname,i->args[j]); } if (ss.fail()) - throw invalid_arg(std::string("-") + _longname,storage[i].args[j]); + throw invalid_arg(std::string("-") + _longname,i->args[j]); } break; } - if (i == storage.size()) + if (i == storage.end()) throw unknown_option(std::string("-") + _longname); } Modified: trunk/complement/explore/lib/misc/opts.cpp =================================================================== --- trunk/complement/explore/lib/misc/opts.cpp 2008-05-22 14:26:19 UTC (rev 1879) +++ trunk/complement/explore/lib/misc/opts.cpp 2008-05-23 10:41:10 UTC (rev 1880) @@ -2,6 +2,7 @@ #include <string> #include <sstream> #include <typeinfo> +#include <cassert> #include <misc/opts.h> @@ -47,39 +48,40 @@ } // this function assumes that is_opt_name(s) = true; -int Opts::get_opt_index(string s) +Opts::options_container_type::iterator Opts::get_opt_index(string s) { + assert(is_opt_name(s)); if (s.size() == 2 && isalnum(s[1]) ) // is short name { - int i; - for (i = 0;i < storage.size();i++) - if (storage[i].shortname == s[1]) + options_container_type::iterator i; + for (i = storage.begin();i != storage.end();++i) + if (i->shortname == s[1]) break; return i; } if (s.size() > 2 && s[1] == '-') { - int i; + options_container_type::iterator i; s = s.substr(2); // exact match - for (i = 0;i < storage.size();i++) - if (storage[i].longname == s) + for (i = storage.begin();i != storage.end();++i) + if (i->longname == s) return i; - vector<int> matches; - for (i = 0;i < storage.size();i++) - if (is_substr(s,storage[i].longname)) + vector< options_container_type::iterator > matches; + for (i = storage.begin();i != storage.end();++i) + if (is_substr(s,i->longname)) matches.push_back(i); if (matches.size() == 1) return matches[0]; else - return storage.size(); + return storage.end(); } - return storage.size(); + return storage.end(); } void Opts::help(ostream& out) @@ -94,8 +96,9 @@ out << "usage: " << endl; out << pname << " [option ...] [optiongoup ...] [end operands ...]" << endl; out << "available options:" << endl; - for (int i = 0;i < storage.size();i++) - out << "-" << storage[i].shortname << "\t[--" << storage[i].longname << "] [" << storage[i].default_v << "]\t-\t" << storage[i].desc << endl; + options_container_type::const_iterator i; + for (i = storage.begin();i != storage.end();++i) + out << "-" << i->shortname << "\t[--" << i->longname << "] [" << i->default_v << "]\t-\t" << i->desc << endl; } void Opts::addflag(char _shortname,const string& _longname,const string& _desc) @@ -112,33 +115,37 @@ bool Opts::is_set(char _shortname) { - for (int i = 0;i < storage.size();i++) - if (storage[i].shortname == _shortname) - return storage[i].is_set; + options_container_type::const_iterator i; + for (i = storage.begin();i != storage.end();++i) + if (i->shortname == _shortname) + return i->is_set; return false; } bool Opts::is_set(const string& _longname) { - for (int i = 0;i < storage.size();i++) - if (storage[i].longname == _longname) - return storage[i].is_set; + options_container_type::const_iterator i; + for (i = storage.begin();i != storage.end();++i) + if (i->longname == _longname) + return i->is_set; return false; } int Opts::get_cnt(char _shortname) const { - for (int i = 0;i < storage.size();i++) - if (storage[i].shortname == _shortname) - return storage[i].cnt; + options_container_type::const_iterator i; + for (i = storage.begin();i != storage.end();++i) + if (i->shortname == _shortname) + return i->cnt; return 0; } int Opts::get_cnt(const string& _longname) const { - for (int i = 0;i < storage.size();i++) - if (storage[i].longname == _longname) - return storage[i].cnt; + options_container_type::const_iterator i; + for (i = storage.begin();i != storage.end();++i) + if (i->longname == _longname) + return i->cnt; return 0; } @@ -163,21 +170,21 @@ opt = opt.substr(0,k); } - int p = get_opt_index(opt); + options_container_type::iterator p = get_opt_index(opt); - if (p == storage.size()) + if (p == storage.end()) throw unknown_option(opt); else { - storage[p].is_set = true; - storage[p].cnt++; - if (storage[p].has_arg) + p->is_set = true; + p->cnt++; + if (p->has_arg) { if (!arg.empty()) - storage[p].args.push_back(arg); + p->args.push_back(arg); else if (i + 1 < ac) - storage[p].args.push_back(av[++i]); + p->args.push_back(av[++i]); else throw missing_arg(opt); } @@ -192,29 +199,26 @@ string optgroup = av[i]; for (int j = 1;j < optgroup.size();j++) { - int p = get_opt_index(string("-") + optgroup[j]); - if (p == storage.size()) + options_container_type::iterator p = get_opt_index(string("-") + optgroup[j]); + if (p == storage.end()) throw unknown_option( "-" + string(1,optgroup[j]) ); else { - storage[p].is_set = true; - storage[p].cnt++; - if (storage[p].has_arg) + p->is_set = true; + p->cnt++; + if (p->has_arg) throw missing_arg( "-" + string(1,optgroup[j]) ); } } } else - { av[j++] = av[i]; - //args.push_back(av[i]); - } i++; } i += (i < ac && isterm(av[i])); while (i < ac) - av[j++] = av[i++]; //args.push_back(av[i++]); + av[j++] = av[i++]; ac = j; } Modified: trunk/complement/explore/lib/misc/ut/opts_test.cc =================================================================== --- trunk/complement/explore/lib/misc/ut/opts_test.cc 2008-05-22 14:26:19 UTC (rev 1879) +++ trunk/complement/explore/lib/misc/ut/opts_test.cc 2008-05-23 10:41:10 UTC (rev 1880) @@ -308,7 +308,7 @@ int EXAM_IMPL(opts_test::multiple_compound) { { - const char* argv[] = { "name", "-xf","--flag", "-f", "-p=second" ,"--pa","third" }; + const char* argv[] = { "name", "-xf","--flag", "-f", "-p=first" ,"--pa","second" }; int argc = sizeof( argv ) / sizeof(argv[0]); Opts opts; @@ -316,7 +316,7 @@ opts.addflag( 'x', "x-option", "option x" ); opts.addflag( 'f', "flag", "option f" ); - opts.add('p',"first","path","some path"); + opts.add('p',"defaultpath","path","some path"); opts.parse( argc, argv ); @@ -324,12 +324,11 @@ EXAM_CHECK(opts.is_set("flag")); EXAM_CHECK(opts.is_set('p')); EXAM_CHECK(opts.get_cnt("flag") == 3 && opts.get_cnt('f') == 3); - vector<string> vs(3); + vector<string> vs(2); opts.getemall("path",vs.begin()); EXAM_CHECK( vs[0] == "first" ); EXAM_CHECK( vs[1] == "second" ); - EXAM_CHECK( vs[2] == "third" ); } @@ -518,10 +517,11 @@ opts.getemall('I',vs.begin()); - EXAM_CHECK( vs[0] == "/usr/include" ); - EXAM_CHECK( vs[1] == "first" ); - EXAM_CHECK( vs[2] == "second" ); - EXAM_CHECK( vs[3] == "third" ); + EXAM_CHECK( opts.get_default<string>("include") == "/usr/include"); + + EXAM_CHECK( vs[0] == "first" ); + EXAM_CHECK( vs[1] == "second" ); + EXAM_CHECK( vs[2] == "third" ); } return EXAM_RESULT; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |