[complement-svn] SF.net SVN: complement: [1874] trunk/complement/explore
Status: Pre-Alpha
Brought to you by:
complement
From: <Dmi...@us...> - 2008-05-14 08:53:25
|
Revision: 1874 http://complement.svn.sourceforge.net/complement/?rev=1874&view=rev Author: DmitryOsmakov Date: 2008-05-14 01:53:23 -0700 (Wed, 14 May 2008) Log Message: ----------- fixed lib Opts : unit tests was corrected, reduction test was added, field "option cnt" in opt class was added, operands is now returned in argv , argc is modified Modified Paths: -------------- trunk/complement/explore/include/misc/opts.h trunk/complement/explore/lib/misc/opts.cpp trunk/complement/explore/lib/misc/ut/misc_test_suite.cc trunk/complement/explore/lib/misc/ut/opts_test.cc trunk/complement/explore/lib/misc/ut/opts_test.h Modified: trunk/complement/explore/include/misc/opts.h =================================================================== --- trunk/complement/explore/include/misc/opts.h 2008-05-06 09:05:14 UTC (rev 1873) +++ trunk/complement/explore/include/misc/opts.h 2008-05-14 08:53:23 UTC (rev 1874) @@ -13,6 +13,7 @@ class Opt { public: + Opt() { cnt = 0; } char shortname; std::string longname; std::string desc; @@ -20,6 +21,7 @@ bool has_arg; bool is_set; + int cnt; // number of times this option was encounterd in command line }; class Opts @@ -44,8 +46,11 @@ bool is_set( char _shortname ); bool is_set( const std::string& _longname ); + int get_cnt( char _shortname ) const; + int get_cnt( const std::string& _longname ) const; + // parse - void parse(int ac, const char** av); + void parse(int& ac, const char** av); // stuff void help(std::ostream& out); @@ -85,7 +90,7 @@ }; - std::vector< std::string > args; + //std::vector< std::string > args; private: // data std::vector< Opt > storage; Modified: trunk/complement/explore/lib/misc/opts.cpp =================================================================== --- trunk/complement/explore/lib/misc/opts.cpp 2008-05-06 09:05:14 UTC (rev 1873) +++ trunk/complement/explore/lib/misc/opts.cpp 2008-05-14 08:53:23 UTC (rev 1874) @@ -126,11 +126,28 @@ return false; } -void Opts::parse(int ac,const char** av) +int Opts::get_cnt(char _shortname) const { + for (int i = 0;i < storage.size();i++) + if (storage[i].shortname == _shortname) + return storage[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; + return 0; +} + +void Opts::parse(int& ac,const char** av) +{ pname = av[0]; int i = 1; + int j = 1; while (i < ac && !isterm(av[i])) { if (is_opt_name(av[i])) @@ -152,28 +169,21 @@ throw invalid_opt(opt); else { + storage[p].is_set = true; + storage[p].cnt++; if (storage[p].has_arg) { if (!arg.empty()) - { - storage[p].is_set = true; storage[p].args.push_back(arg); - } else if (i + 1 < ac) - { - storage[p].is_set = true; storage[p].args.push_back(av[++i]); - } else throw missing_arg(opt); } else - { - storage[p].is_set = true; - if (!arg.empty()) + if (!arg.empty()) //unexpected arg throw invalid_arg(opt,arg); - } } } else @@ -188,18 +198,23 @@ else { storage[p].is_set = true; + storage[p].cnt++; if (storage[p].has_arg) throw missing_arg( "-" + string(1,optgroup[j]) ); } } } else - args.push_back(av[i]); + { + av[j++] = av[i]; + //args.push_back(av[i]); + } i++; } i += (i < ac && isterm(av[i])); while (i < ac) - args.push_back(av[i++]); + av[j++] = av[i++]; //args.push_back(av[i++]); + ac = j; } Modified: trunk/complement/explore/lib/misc/ut/misc_test_suite.cc =================================================================== --- trunk/complement/explore/lib/misc/ut/misc_test_suite.cc 2008-05-06 09:05:14 UTC (rev 1873) +++ trunk/complement/explore/lib/misc/ut/misc_test_suite.cc 2008-05-14 08:53:23 UTC (rev 1874) @@ -71,5 +71,15 @@ t.add( &opts_test::user_defined, opts, "user-defined type" ); + t.add( &opts_test::compound, opts, "compound" ); + + t.add( &opts_test::multiple, opts,"multiple"); + + t.add( &opts_test::args, opts,"args"); + + t.add( &opts_test::stop, opts,"stop"); + + t.add( &opts_test::reduction, opts,"reduction"); + return t.girdle(); }; Modified: trunk/complement/explore/lib/misc/ut/opts_test.cc =================================================================== --- trunk/complement/explore/lib/misc/ut/opts_test.cc 2008-05-06 09:05:14 UTC (rev 1873) +++ trunk/complement/explore/lib/misc/ut/opts_test.cc 2008-05-14 08:53:23 UTC (rev 1874) @@ -68,7 +68,7 @@ Opts opts; - opts.add( 'p', "port", "listen tcp port" ); + opts.add( 'p', "port", "listen tcp port" , true); try { opts.parse( argc, argv ); @@ -93,7 +93,7 @@ Opts opts; - opts.add( 'p', "port", "listen tcp port" ); + opts.add( 'p', "port", "listen tcp port" , true ); try { opts.parse( argc, argv ); @@ -179,6 +179,8 @@ opts.add( 'v', "verbose", "more trace messages" ); opts.parse( argc, argv ); + + EXAM_CHECK( opts.get_cnt('v') == 3 ); } { @@ -190,6 +192,8 @@ opts.add( 'v', "verbose", "more trace messages" ); opts.parse( argc, argv ); + + EXAM_CHECK( opts.get_cnt('v') == 3 ); } { @@ -201,6 +205,8 @@ opts.add( 'v', "verbose", "more trace messages" ); opts.parse( argc, argv ); + + EXAM_CHECK( opts.get_cnt('v') == 3 ); } return EXAM_RESULT; @@ -219,8 +225,11 @@ opts.add( 'c', "c-option", "option c" ); opts.parse( argc, argv ); + + EXAM_CHECK(opts.is_set('a') && opts.is_set('b') && opts.is_set('c')); } + return EXAM_RESULT; } @@ -232,7 +241,7 @@ Opts opts; - opts.add( 'f', "config", "configuration file" ); + opts.add( 'f', "config", "configuration file",true ); opts.parse( argc, argv ); @@ -248,7 +257,7 @@ Opts opts; - opts.add( 'f', "config", "configuration file" ); + opts.add( 'f', "config", "configuration file",true ); opts.parse( argc, argv ); @@ -264,7 +273,7 @@ Opts opts; - opts.add( 'f', "config", "configuration file" ); + opts.add( 'f', "config", "configuration file",true ); opts.parse( argc, argv ); @@ -342,3 +351,23 @@ return EXAM_RESULT; } + +int EXAM_IMPL(opts_test::reduction) +{ + { + const char* argv[] = { "name" , "--num" , "4"}; + int argc = sizeof( argv ) / sizeof(argv[0]); + + Opts opts; + + opts.add('n',"number_of_processors","number of processors",true ); + + opts.parse( argc, argv ); + + int n; + opts.get('n',n); + EXAM_CHECK( n == 4 ); + } + + return EXAM_RESULT; +} Modified: trunk/complement/explore/lib/misc/ut/opts_test.h =================================================================== --- trunk/complement/explore/lib/misc/ut/opts_test.h 2008-05-06 09:05:14 UTC (rev 1873) +++ trunk/complement/explore/lib/misc/ut/opts_test.h 2008-05-14 08:53:23 UTC (rev 1874) @@ -30,6 +30,7 @@ int EXAM_DECL(args); int EXAM_DECL(stop); int EXAM_DECL(user_defined); + int EXAM_DECL(reduction); }; #endif // __MISC_TEST_H This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |