diff --git a/app/PlaYUVerApp.cpp b/app/PlaYUVerApp.cpp index e980754..11ee89a 100644 --- a/app/PlaYUVerApp.cpp +++ b/app/PlaYUVerApp.cpp @@ -34,7 +34,7 @@ #ifdef USE_FERVOR #include "fvupdater.h" #endif -#include "lib/PlaYUVerCmdParser.h" +#include "lib/PlaYUVerOptions.h" #include "PlaYUVerSubWindowHandle.h" #include "AboutDialog.h" #include "SubWindowAbstract.h" diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 18a34e4..32da862 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -15,7 +15,7 @@ set(PlaYUVer_Lib_SRCS StreamHandlerPortableMap.cpp # Command Parser PlaYUVerOptions.cpp - PlaYUVerCmdParser.cpp +# PlaYUVerCmdParser.cpp ) IF( USE_FFMPEG ) diff --git a/lib/PlaYUVerCmdParser.cpp b/lib/PlaYUVerCmdParser.cpp deleted file mode 100644 index 07e5172..0000000 --- a/lib/PlaYUVerCmdParser.cpp +++ /dev/null @@ -1,140 +0,0 @@ -/* This file is a part of plaYUVer project - * Copyright (C) 2014-2016 by Luis Lucas (luisfrlucas@gmail.com) - * Joao Carreira (jfmcarreira@gmail.com) - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -/** - * \file PlaYUVerCmdParser.cpp - * \brief Command parser - */ - -#include "config.h" -#include -#include -#include -#include -#include "PlaYUVerCmdParser.h" -#include "PlaYUVerFrame.h" - - -PlaYUVerCmdParser::PlaYUVerCmdParser() -{ - Opts().addOptions()/**/ - ( "help", "produce help message" )/**/ - ( "version", "show version and exit" )/**/ - ( "pel_fmts", "list pixel formats" ) /**/ - ( "quality_metrics", "list supported quality metrics" )/**/ - ( "module_list", "list supported modules" ) /**/ - ( "module_list_full", "detailed list supported modules" ); -} - -PlaYUVerCmdParser::~PlaYUVerCmdParser() -{ - -} - -Options& PlaYUVerCmdParser::Opts() -{ - return m_cParserOptions; -} - -std::list& PlaYUVerCmdParser::getNoArgs() -{ - return m_aUnhandledArgs; -} - -Bool PlaYUVerCmdParser::parse() -{ - try - { - // m_cParserOptions.setDefaults(); - m_aUnhandledArgs = m_cParserOptions.scanArgv( m_iArgc, ( const Char** )m_ppArgv ); - - if( checkListingOpts() ) - { - return false; - } - } - catch( std::exception& e ) - { - std::cerr << "error: " - << e.what() - << "\n"; - return false; - } - catch( ... ) - { - std::cerr << "Exception of unknown type!\n"; - } - return true; -} - -Bool PlaYUVerCmdParser::parse( Options& opts, Int argc, Char *argv[] ) -{ - try - { - // m_cParserOptions.setDefaults(); - m_aUnhandledArgs = opts.scanArgv( argc, ( const Char** )argv ); - - } - catch( std::exception& e ) - { - std::cerr << "error: " - << e.what() - << "\n"; - return false; - } - catch( ... ) - { - std::cerr << "Exception of unknown type!\n"; - } - return true; -} - -Bool PlaYUVerCmdParser::checkListingOpts() -{ - Bool bRet = false; - - if( m_cParserOptions["version"]->count() ) - { - std::cout << "PlaYUVer version " - << PLAYUVER_VERSION_STRING - << "\n"; - bRet |= true; - } - if( m_cParserOptions["pel_fmts"]->count() ) - { - printf( "PlaYUVer supported pixel formats: \n" ); - for( UInt i = 0; i < PlaYUVerFrame::supportedPixelFormatListNames().size(); i++ ) - { - printf( " %s\n", PlaYUVerFrame::supportedPixelFormatListNames()[i].c_str() ); - } - bRet |= true; - } - if( Opts()["quality_metrics"]->count() ) - { - printf( "PlaYUVer supported quality metrics: \n" ); - for( UInt i = 0; i < PlaYUVerFrame::supportedQualityMetricsList().size(); i++ ) - { - printf( " %s\n", PlaYUVerFrame::supportedQualityMetricsList()[i].c_str() ); - } - bRet |= true; - } - return bRet; -} - - diff --git a/lib/PlaYUVerCmdParser.h b/lib/PlaYUVerCmdParser.h deleted file mode 100644 index 3b12747..0000000 --- a/lib/PlaYUVerCmdParser.h +++ /dev/null @@ -1,56 +0,0 @@ -/* This file is a part of plaYUVer project - * Copyright (C) 2014-2016 by Luis Lucas (luisfrlucas@gmail.com) - * Joao Carreira (jfmcarreira@gmail.com) - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -/** - * \file PlaYUVerCmdParser.h - * \brief Command parser - */ - -#include "PlaYUVerDefs.h" -#include "PlaYUVerOptions.h" - -#ifndef __PLAYUVERCMDPARSER_H__ -#define __PLAYUVERCMDPARSER_H__ - -class PlaYUVerCmdParser -{ -public: - PlaYUVerCmdParser(); - ~PlaYUVerCmdParser(); - - Void config( Int argc, Char *argv[] ) - { - m_iArgc = argc; - m_ppArgv = argv; - } - Bool parse(); - Bool parse( Options& opts, Int argc, Char *argv[] ); - std::list& getNoArgs(); - Options& Opts(); -private: - Int m_iArgc; - Char** m_ppArgv; - Options m_cParserOptions; - std::list m_aUnhandledArgs; - - Bool checkListingOpts(); - Void listModules(); -}; - -#endif // __PLAYUVERCMDPARSER_H__ diff --git a/lib/PlaYUVerModuleIf.h b/lib/PlaYUVerModuleIf.h index cad1f7f..84bd99e 100644 --- a/lib/PlaYUVerModuleIf.h +++ b/lib/PlaYUVerModuleIf.h @@ -101,7 +101,7 @@ public: enum Module_NumberOfFrames m_uiNumberOfFrames; UInt m_uiModuleRequirements; - Options m_cModuleOptions; + PlaYUVerOptions m_cModuleOptions; PlaYUVerModuleIf() { diff --git a/lib/PlaYUVerOptions.cpp b/lib/PlaYUVerOptions.cpp index d134be3..58451c8 100644 --- a/lib/PlaYUVerOptions.cpp +++ b/lib/PlaYUVerOptions.cpp @@ -36,46 +36,273 @@ * \brief Input args handler */ +#include "config.h" +#include "PlaYUVerOptions.h" + #include #include #include #include #include +#include +#include #include #include #include -#include "PlaYUVerProgramOptions.h" + +#include "lib/PlaYUVerModuleIf.h" +#include "modules/PlaYUVerModuleFactory.h" + using namespace std; -Options::Options( const String& name ) +struct ParseFailure: public std::exception +{ + String arg; + String val; + ParseFailure( String arg0, String val0 ) throw() : + arg( arg0 ), + val( val0 ) { } + ~ParseFailure() throw() { } + const char* what() const throw() + { + return "Option Parse Failure"; + } +}; + + +/** OptionBase: Virtual base class for storing information relating to a + * specific option This base class describes common elements. Type specific + * information should be stored in a derived class. */ +class OptionBase +{ +public: + OptionBase( const String& name, const String& desc ) : + arg_count( 0 ), + opt_string( name ), + opt_desc( desc ) + { + } + + virtual ~OptionBase() + { + } + + /* parse argument arg, to obtain a value for the option */ + virtual void parse( const String& arg ) = 0; + /* set the argument to the default value */ + //virtual void setDefault() = 0; + int count() + { + return arg_count; + } + Bool isBinary() + { + return is_binary; + } + + int arg_count; + String opt_string; + String opt_desc; + Bool is_binary; +}; + +/** Type specific option storage */ +class BoolOption: public OptionBase +{ +public: + BoolOption( const String& name, Bool default_val, const String& desc ) : + OptionBase( name, desc ), + opt_storage( false ), + opt_default_val( default_val ) + { + is_binary = true; + } + + void parse( const String& arg ) + { + opt_storage = true; + arg_count++; + } + + void setDefault() + { + opt_storage = opt_default_val; + } + Bool opt_storage; + Bool opt_default_val; +}; + +/** Type specific option storage */ +template +class StandardOption: public OptionBase +{ +public: + StandardOption( const String& name, T& storage, const String& desc ) : + OptionBase( name, desc ), + opt_storage( storage ) + { + is_binary = false; + } + + void parse( const String& arg ); + T& opt_storage; +}; + +/* Generic parsing */ +template +inline void StandardOption::parse( const String& arg ) +{ + std::istringstream arg_ss( arg, std::istringstream::in ); + arg_ss.exceptions( std::ios::failbit ); + try + { + arg_ss >> opt_storage; + } + catch( ... ) + { + throw ParseFailure( opt_string, arg ); + } + arg_count++; +} + +template<> +inline void StandardOption< std::vector >::parse( const String& arg ) +{ + UInt aux_opt_storage; + std::istringstream arg_ss( arg, std::istringstream::in ); + arg_ss.exceptions( std::ios::failbit ); + try + { + arg_ss >> aux_opt_storage; + opt_storage.push_back( aux_opt_storage ); + } + catch( ... ) + { + throw ParseFailure( opt_string, arg ); + } + arg_count++; +} + +template<> +inline void StandardOption< std::vector >::parse( const String& arg ) +{ + Int aux_opt_storage; + std::istringstream arg_ss( arg, std::istringstream::in ); + arg_ss.exceptions( std::ios::failbit ); + try + { + arg_ss >> aux_opt_storage; + opt_storage.push_back( aux_opt_storage ); + } + catch( ... ) + { + throw ParseFailure( opt_string, arg ); + } + arg_count++; +} + +/* string parsing is specialized */ +template<> +inline void StandardOption::parse( const String& arg ) +{ + opt_storage = arg; + arg_count++; +} + +/* string vector parsing is specialized */ +template<> +inline void StandardOption >::parse( const String& arg ) +{ + opt_storage.push_back( arg ); + arg_count++; +} + +/** Option class for argument handling using a user provided function */ +struct FunctionOption: public OptionBase +{ +public: + typedef Void( Func )( PlaYUVerOptions&, const String& ); + + FunctionOption( const String& name, PlaYUVerOptions& parent_, Func *func_, const String& desc ) : + OptionBase( name, desc ), + parent( parent_ ), + func( func_ ) + { + is_binary = false; + } + + void parse( const String& arg ) + { + func( parent, arg ); + arg_count++; + } + + void setDefault() + { + return; + } + +private: + PlaYUVerOptions& parent; + void ( *func )( PlaYUVerOptions&, const String& ); +}; + +struct PlaYUVerOptions::Option +{ + Option() : + opt( 0 ) + { + } + ~Option() + { + if( opt ) + delete opt; + } + std::list opt_long; + std::list opt_short; + OptionBase* opt; +}; + +PlaYUVerOptions::PlaYUVerOptions( const String& name ) { m_cOptionGroupName = name; m_bAllowUnkonw = true; + + /** + * Add default options + */ + addOptions()/**/ + ( "help", "produce help message" )/**/ + ( "version", "show version and exit" )/**/ + ( "pel_fmts", "list pixel formats" ) /**/ + ( "quality_metrics", "list supported quality metrics" )/**/ + ( "module_list", "list supported modules" ) /**/ + ( "module_list_full", "detailed list supported modules" ); } -Options::~Options() +PlaYUVerOptions::~PlaYUVerOptions() { - for( Options::OptionsList::iterator it = opt_list.begin(); it != opt_list.end(); it++ ) + for( PlaYUVerOptions::OptionsList::iterator it = opt_list.begin(); it != opt_list.end(); it++ ) { delete *it; } } -OptionBase* Options::operator[]( const String& optName ) +OptionBase* PlaYUVerOptions::operator[]( const String& optName ) { return getOption( optName ); } -OptionBase* Options::getOption( const String& optName ) +OptionBase* PlaYUVerOptions::getOption( const String& optName ) { - Options::OptionMap::iterator opt_it; + PlaYUVerOptions::OptionMap::iterator opt_it; opt_it = opt_short_map.find( optName ); if( opt_it != opt_short_map.end() ) { OptionsList opt_list = ( *opt_it ).second; - for( Options::OptionsList::iterator it = opt_list.begin(); it != opt_list.end(); ++it ) + for( PlaYUVerOptions::OptionsList::iterator it = opt_list.begin(); it != opt_list.end(); ++it ) { return ( *it )->opt; } @@ -84,7 +311,7 @@ OptionBase* Options::getOption( const String& optName ) if( opt_it != opt_long_map.end() ) { OptionsList opt_list = ( *opt_it ).second; - for( Options::OptionsList::iterator it = opt_list.begin(); it != opt_list.end(); ++it ) + for( PlaYUVerOptions::OptionsList::iterator it = opt_list.begin(); it != opt_list.end(); ++it ) { return ( *it )->opt; } @@ -92,12 +319,17 @@ OptionBase* Options::getOption( const String& optName ) return NULL; } -Bool Options::hasOpt( const String& optName ) +Bool PlaYUVerOptions::hasOpt( const String& optName ) { return getOption( optName )->count() > 0 ? true : false; } -void Options::addOption( OptionBase *opt ) +OptionSpecific PlaYUVerOptions::addOptions() +{ + return OptionSpecific( *this ); +} + +void PlaYUVerOptions::addOption( OptionBase *opt ) { Option* names = new Option(); names->opt = opt; @@ -130,35 +362,81 @@ void Options::addOption( OptionBase *opt ) } /* Helper method to initiate adding options to Options */ -OptionSpecific Options::addOptions() + +/** + * Add option described by name to the parent Options list, + * with storage for the option's value + * with default_val as the default value + * with desc as an optional help description + */ +template +OptionSpecific& OptionSpecific::operator()( const String& name, T& storage, const String& desc ) { - return OptionSpecific( *this ); + parent.addOption( new StandardOption( name, storage, desc ) ); + return *this; } +/** + * Add option described by name to the parent Options list, + * with storage for the option's value + * with default_val as the default value + * with desc as an optional help description + */ +OptionSpecific& OptionSpecific::operator()( const String& name, const String& desc ) +{ + parent.addOption( new BoolOption( name, false, desc ) ); + return *this; +} + +/** + * Add option described by name to the parent Options list, + * with storage for the option's value + * with default_val as the default value + * with desc as an optional help description + */ +OptionSpecific& OptionSpecific::operator()( const String& name, Bool default_val, const String& desc ) +{ + parent.addOption( new BoolOption( name, default_val, desc ) ); + return *this; +} + +/** + * Add option described by name to the parent Options list, + * with desc as an optional help description + * instead of storing the value somewhere, a function of type + * FunctionOption::Func is called. It is upto this function to correctly + * handle evaluating the option's value. + */ +// OptionSpecific& OptionSpecific::operator()( const String& name, FunctionOption::Func *func, const String& desc ) +// { +// parent.addOption( new FunctionOption( name, parent, func, desc ) ); +// return *this; +// } + /* for all options in opts, set their storage to their specified * default value */ -Void Options::setDefaults() +Void PlaYUVerOptions::setDefaults() { -// for( Options::OptionsList::iterator it = opt_list.begin(); it != opt_list.end(); it++ ) +// for( PlaYUVerOptions::OptionsList::iterator it = opt_list.begin(); it != opt_list.end(); it++ ) // { // ( *it )->opt->setDefault(); // } } -static void setOptions( Options::OptionsList& opt_list, const string& value ) +static void setOptions( PlaYUVerOptions::OptionsList& opt_list, const string& value ) { /* multiple options may be registered for the same name: * allow each to parse value */ - for( Options::OptionsList::iterator it = opt_list.begin(); it != opt_list.end(); ++it ) + for( PlaYUVerOptions::OptionsList::iterator it = opt_list.begin(); it != opt_list.end(); ++it ) { ( *it )->opt->parse( value ); } } -Bool Options::storePair( Bool allow_long, Bool allow_short, const string& name, const string& value ) +Bool PlaYUVerOptions::storePair( Bool allow_long, Bool allow_short, const string& name, const string& value ) { bool found = false; - Options::OptionMap::iterator opt_it; + PlaYUVerOptions::OptionMap::iterator opt_it; if( allow_long ) { opt_it = opt_long_map.find( name ); @@ -197,7 +475,7 @@ Bool Options::storePair( Bool allow_long, Bool allow_short, const string& name, return true; } -Bool Options::storePair( const string& name, const string& value ) +Bool PlaYUVerOptions::storePair( const string& name, const string& value ) { return storePair( true, true, name, value ); } @@ -205,7 +483,7 @@ Bool Options::storePair( const string& name, const string& value ) /** * returns number of extra arguments consumed */ -UInt Options::parseLONG( unsigned argc, const char* argv[] ) +UInt PlaYUVerOptions::parseLONG( unsigned argc, const char* argv[] ) { /* gnu style long options can take the forms: * --option=arg @@ -225,8 +503,8 @@ UInt Options::parseLONG( unsigned argc, const char* argv[] ) /* commented out, to return to true GNU style processing * where longopts have to include an =, otherwise they are * booleans */ - if (argc == 1) - return 0; /* run out of argv for argument */ + if( argc == 1 ) + return 0; /* run out of argv for argument */ extra_argc_consumed = 1; #endif if( !storePair( true, false, option, "1" ) ) @@ -246,7 +524,7 @@ UInt Options::parseLONG( unsigned argc, const char* argv[] ) /** * returns number of extra arguments consumed */ -UInt Options::parseLONG( string arg ) +UInt PlaYUVerOptions::parseLONG( string arg ) { /* gnu style long options can take the forms: * --option=arg @@ -275,7 +553,7 @@ UInt Options::parseLONG( string arg ) return extra_argc_consumed; } -unsigned Options::parseSHORT( unsigned argc, const char* argv[] ) +unsigned PlaYUVerOptions::parseSHORT( unsigned argc, const char* argv[] ) { /* short options can take the forms: * --option arg @@ -301,7 +579,33 @@ unsigned Options::parseSHORT( unsigned argc, const char* argv[] ) return 1; } -list Options::scanArgv( unsigned argc, const char* argv[] ) +Bool PlaYUVerOptions::parse( Int argc, Char *argv[] ) +{ + try + { + m_aUnhandledArgs = scanArgv( argc, ( const Char** )argv ); + + if( checkListingOpts() ) + { + return false; + } + } + catch( std::exception& e ) + { + std::cerr << "error: " + << e.what() + << "\n"; + return false; + } + catch( ... ) + { + std::cerr << "Exception of unknown type!\n"; + } + return true; +} + + +list PlaYUVerOptions::scanArgv( unsigned argc, const char* argv[] ) { /* a list for anything that didn't get handled as an option */ list non_option_arguments; @@ -325,7 +629,7 @@ list Options::scanArgv( unsigned argc, const char* argv[] ) { /* handle short (single dash) options */ #if 0 - i += parsePOSIX(opts, argc - i, &argv[i]); + i += parsePOSIX( opts, argc - i, &argv[i] ); #else i += parseSHORT( argc - i, &argv[i] ); #endif @@ -347,7 +651,7 @@ list Options::scanArgv( unsigned argc, const char* argv[] ) return non_option_arguments; } -Void Options::scanArgs( std::vector args_array) +Void PlaYUVerOptions::scanArgs( std::vector args_array ) { for( unsigned i = 0; i < args_array.size(); i++ ) { @@ -362,7 +666,7 @@ static const char spaces[41] = " "; * using the formatting: "-x, --long", * if a short/long option isn't specified, it is not printed */ -static void doHelpOpt( ostream& out, const Options::Option& entry, unsigned pad_short = 0 ) +static void doHelpOpt( ostream& out, const PlaYUVerOptions::Option& entry, unsigned pad_short = 0 ) { pad_short = min( pad_short, 8u ); @@ -391,12 +695,12 @@ static void doHelpOpt( ostream& out, const Options::Option& entry, unsigned pad_ } /* format the help text */ -Void Options::doHelp( ostream& out, unsigned columns ) +Void PlaYUVerOptions::doHelp( ostream& out, unsigned columns ) { const unsigned pad_short = 3; /* first pass: work out the longest option name */ unsigned max_width = 0; - for( Options::OptionsList::iterator it = opt_list.begin(); it != opt_list.end(); it++ ) + for( PlaYUVerOptions::OptionsList::iterator it = opt_list.begin(); it != opt_list.end(); it++ ) { ostringstream line( ios_base::out ); doHelpOpt( line, **it, pad_short ); @@ -411,7 +715,7 @@ Void Options::doHelp( ostream& out, unsigned columns ) * - if the option text is longer than opt_width, place the help * text at opt_width on the next line. */ - for( Options::OptionsList::iterator it = opt_list.begin(); it != opt_list.end(); it++ ) + for( PlaYUVerOptions::OptionsList::iterator it = opt_list.begin(); it != opt_list.end(); it++ ) { ostringstream line( ios_base::out ); line << " "; @@ -489,5 +793,97 @@ Void Options::doHelp( ostream& out, unsigned columns ) } } +Bool PlaYUVerOptions::checkListingOpts() +{ + Bool bRet = false; + + if( hasOpt( "version" ) ) + { + std::cout << "PlaYUVer version " + << PLAYUVER_VERSION_STRING + << "\n"; + bRet |= true; + } + if( hasOpt( "pel_fmts" ) ) + { + printf( "PlaYUVer supported pixel formats: \n" ); + for( UInt i = 0; i < PlaYUVerFrame::supportedPixelFormatListNames().size(); i++ ) + { + printf( " %s\n", PlaYUVerFrame::supportedPixelFormatListNames()[i].c_str() ); + } + bRet |= true; + } + if( hasOpt( "quality_metrics" ) ) + { + printf( "PlaYUVer supported quality metrics: \n" ); + for( UInt i = 0; i < PlaYUVerFrame::supportedQualityMetricsList().size(); i++ ) + { + printf( " %s\n", PlaYUVerFrame::supportedQualityMetricsList()[i].c_str() ); + } + bRet |= true; + } + if( hasOpt( "module_list" ) || hasOpt( "module_list_full" ) ) + { + listModules(); + bRet |= true; + } + return bRet; +} + +Void PlaYUVerOptions::listModules() +{ + Bool bDetailed = false; + + if( hasOpt( "module_list_full" ) ) + bDetailed = true; + + PlaYUVerModuleIf* pcCurrModuleIf; + PlaYUVerModuleFactoryMap& PlaYUVerModuleFactoryMap = PlaYUVerModuleFactory::Get()->getMap(); + PlaYUVerModuleFactoryMap::iterator it = PlaYUVerModuleFactoryMap.begin(); + + printf( "PlaYUVer available modules: \n" ); + printf( " [Internal Name] " ); + if( bDetailed ) + { + printf( " [Full Name] " ); + printf( " [Type] " ); + printf( " [Description]" ); + } + printf( " \n" ); + + Char ModuleNameString[40]; + + for( UInt i = 0; it != PlaYUVerModuleFactoryMap.end(); ++it, i++ ) + { + printf( " " ); + printf( "%-30s", it->first ); + if( bDetailed ) + { + ModuleNameString[0] = '\0'; + pcCurrModuleIf = it->second(); + + if( pcCurrModuleIf->m_pchModuleCategory ) + { + strcat( ModuleNameString, pcCurrModuleIf->m_pchModuleCategory ); + strcat( ModuleNameString, "/" ); + } + strcat( ModuleNameString, pcCurrModuleIf->m_pchModuleName ); + printf( " %-40s", ModuleNameString ); + switch( pcCurrModuleIf->m_iModuleType ) + { + case FRAME_PROCESSING_MODULE: + printf( " Processing " ); + break; + case FRAME_MEASUREMENT_MODULE: + printf( " Measurement " ); + break; + } + printf( " %s", pcCurrModuleIf->m_pchModuleTooltip ); + pcCurrModuleIf->Delete(); + } + printf( "\n" ); + } +} + diff --git a/lib/PlaYUVerOptions.h b/lib/PlaYUVerOptions.h index b686a96..75f19e7 100644 --- a/lib/PlaYUVerOptions.h +++ b/lib/PlaYUVerOptions.h @@ -45,282 +45,19 @@ #include #include #include + #include "PlaYUVerDefs.h" - -struct Options; - -struct ParseFailure: public std::exception -{ - ParseFailure( String arg0, String val0 ) throw() : - arg( arg0 ), - val( val0 ) - { - } - - ~ParseFailure() throw() - { - } - - String arg; - String val; - - const char* what() const throw() - { - return "Option Parse Failure"; - } -}; - -/** OptionBase: Virtual base class for storing information relating to a - * specific option This base class describes common elements. Type specific - * information should be stored in a derived class. */ -struct OptionBase -{ - OptionBase( const String& name, const String& desc ) : - arg_count( 0 ), - opt_string( name ), - opt_desc( desc ) - { - } - - virtual ~OptionBase() - { - } - - /* parse argument arg, to obtain a value for the option */ - virtual void parse( const String& arg ) = 0; - /* set the argument to the default value */ - //virtual void setDefault() = 0; - int count() - { - return arg_count; - } - Bool isBinary() - { - return is_binary; - } - - int arg_count; - String opt_string; - String opt_desc; - Bool is_binary; -}; - -/** Type specific option storage */ -struct FlagOpt: public OptionBase -{ - FlagOpt( const String& name, Bool default_val, const String& desc ) : - OptionBase( name, desc ), - opt_storage( false ), - opt_default_val( default_val ) - { - is_binary = true; - } - - void parse( const String& arg ) - { - opt_storage = true; - arg_count++; - } - - void setDefault() - { - opt_storage = opt_default_val; - } - Bool opt_storage; - Bool opt_default_val; -}; - -/** Type specific option storage */ -template -struct StandardOpt: public OptionBase -{ - StandardOpt( const String& name, T& storage, const String& desc ) : - OptionBase( name, desc ), - opt_storage( storage ) - { - is_binary = false; - } - - void parse( const String& arg ); - -// void setDefault() -// { -// opt_storage = opt_default_val; -// } - - T& opt_storage; -}; - -/* Generic parsing */ -template -inline void StandardOpt::parse( const String& arg ) -{ - std::istringstream arg_ss( arg, std::istringstream::in ); - arg_ss.exceptions( std::ios::failbit ); - try - { - arg_ss >> opt_storage; - } - catch( ... ) - { - throw ParseFailure( opt_string, arg ); - } - arg_count++; -} - -template<> -inline void StandardOpt< std::vector >::parse( const String& arg ) -{ - UInt aux_opt_storage; - std::istringstream arg_ss( arg, std::istringstream::in ); - arg_ss.exceptions( std::ios::failbit ); - try - { - arg_ss >> aux_opt_storage; - opt_storage.push_back( aux_opt_storage ); - } - catch( ... ) - { - throw ParseFailure( opt_string, arg ); - } - arg_count++; -} - -template<> -inline void StandardOpt< std::vector >::parse( const String& arg ) -{ - Int aux_opt_storage; - std::istringstream arg_ss( arg, std::istringstream::in ); - arg_ss.exceptions( std::ios::failbit ); - try - { - arg_ss >> aux_opt_storage; - opt_storage.push_back( aux_opt_storage ); - } - catch( ... ) - { - throw ParseFailure( opt_string, arg ); - } - arg_count++; -} - -/* string parsing is specialized */ -template<> -inline void StandardOpt::parse( const String& arg ) -{ - opt_storage = arg; - arg_count++; -} - -/* string vector parsing is specialized */ -template<> -inline void StandardOpt >::parse( const String& arg ) -{ - opt_storage.push_back( arg ); - arg_count++; -} - -/** Option class for argument handling using a user provided function */ -struct OptionFunc: public OptionBase -{ - typedef Void (Func)( Options&, const String& ); - - OptionFunc( const String& name, Options& parent_, Func *func_, const String& desc ) : - OptionBase( name, desc ), - parent( parent_ ), - func( func_ ) - { - is_binary = false; - } - - void parse( const String& arg ) - { - func( parent, arg ); - arg_count++; - } - - void setDefault() - { - return; - } - -private: - Options& parent; - void (*func)( Options&, const String& ); -}; - -class OptionSpecific; -class Options -{ -public: - - struct Option - { - Option() : - opt( 0 ) - { - } - ; - ~Option() - { - if( opt ) - delete opt; - } - std::list opt_long; - std::list opt_short; - OptionBase* opt; - }; - typedef std::list OptionsList; - - Options( const String& name = "" ); - ~Options(); - - std::list scanArgv( unsigned argc, const char* argv[] ); - Void scanArgs( std::vector args_array); - Void doHelp( std::ostream& out, unsigned columns = 80 ); - - OptionSpecific addOptions(); - Void setDefaults(); - - OptionBase* operator[]( const String& optName ); - OptionBase* getOption( const String& optName ); - - OptionsList& getOptionList() - { - return opt_list; - } - - Bool hasOpt( const String& optName ); - - void addOption( OptionBase *opt ); - -private: - - typedef std::map OptionMap; - OptionMap opt_long_map; - OptionMap opt_short_map; - - OptionsList opt_list; - - String m_cOptionGroupName; - Bool m_bAllowUnkonw; - - Bool storePair( Bool allow_long, Bool allow_short, const String& name, const String& value ); - Bool storePair( const String& name, const String& value ); - UInt parseLONG( unsigned argc, const char* argv[] ); - UInt parseLONG( String arg ); - unsigned parseSHORT( unsigned argc, const char* argv[] ); -}; +struct OptionBase; +class PlaYUVerOptions; /* Class with templated overloaded operator(), for use by Options::addOptions() */ class OptionSpecific { public: - OptionSpecific( Options& parent_ ) : - parent( parent_ ) - { - } + OptionSpecific( PlaYUVerOptions& parent_ ) : + parent( parent_ ) + { } /** * Add option described by name to the parent Options list, @@ -330,11 +67,7 @@ public: */ template OptionSpecific& - operator()( const String& name, T& storage, const String& desc ) - { - parent.addOption( new StandardOpt( name, storage, desc ) ); - return *this; - } + operator()( const String& name, T& storage, const String& desc ); /** * Add option described by name to the parent Options list, @@ -343,11 +76,7 @@ public: * with desc as an optional help description */ OptionSpecific& - operator()( const String& name, const String& desc ) - { - parent.addOption( new FlagOpt( name, false, desc ) ); - return *this; - } + operator()( const String& name, const String& desc ); /** * Add option described by name to the parent Options list, @@ -356,11 +85,7 @@ public: * with desc as an optional help description */ OptionSpecific& - operator()( const String& name, Bool default_val, const String& desc ) - { - parent.addOption( new FlagOpt( name, default_val, desc ) ); - return *this; - } + operator()( const String& name, Bool default_val, const String& desc ); /** * Add option described by name to the parent Options list, @@ -369,14 +94,60 @@ public: * OptionFunc::Func is called. It is upto this function to correctly * handle evaluating the option's value. */ - OptionSpecific& - operator()( const String& name, OptionFunc::Func *func, const String& desc ) - { - parent.addOption( new OptionFunc( name, parent, func, desc ) ); - return *this; - } +// OptionSpecific& +// operator()( const String& name, OptionFunc::Func *func, const String& desc ) private: - Options& parent; + PlaYUVerOptions& parent; }; + +class PlaYUVerOptions +{ +public: + + struct Option; + + typedef std::list OptionsList; + + PlaYUVerOptions( const String& name = "" ); + ~PlaYUVerOptions(); + + Bool parse( Int argc, Char *argv[] ); + std::list scanArgv( unsigned argc, const char* argv[] ); + Void scanArgs( std::vector args_array ); + Void doHelp( std::ostream& out, unsigned columns = 80 ); + + Bool checkListingOpts(); + Void listModules(); + Void setDefaults(); + + OptionBase* operator[]( const String& optName ); + OptionBase* getOption( const String& optName ); + + Bool hasOpt( const String& optName ); + + OptionSpecific addOptions(); + void addOption( OptionBase *opt ); + +private: + typedef std::map OptionMap; + OptionMap opt_long_map; + OptionMap opt_short_map; + + OptionsList opt_list; + + String m_cOptionGroupName; + Bool m_bAllowUnkonw; + std::list m_aUnhandledArgs; + + Bool storePair( Bool allow_long, Bool allow_short, const String& name, const String& value ); + Bool storePair( const String& name, const String& value ); + UInt parseLONG( unsigned argc, const char* argv[] ); + UInt parseLONG( String arg ); + UInt parseSHORT( unsigned argc, const char* argv[] ); + +}; + + + #endif // __PROGRAMOPTIONS_H__ diff --git a/tools/PlaYUVerToolsCmdParser.cpp b/tools/PlaYUVerToolsCmdParser.cpp index 2af415a..1d5c1c1 100644 --- a/tools/PlaYUVerToolsCmdParser.cpp +++ b/tools/PlaYUVerToolsCmdParser.cpp @@ -58,26 +58,20 @@ Int PlaYUVerToolsCmdParser::parseToolsArgs( Int argc, Char *argv[] ) { Int iRet = 0; - Opts().addOptions()/**/ - ( "quiet,q", m_bQuiet, "disable verbose" ); - - Opts().addOptions()/**/ + m_cOptions.addOptions()/**/ + ( "quiet,q", m_bQuiet, "disable verbose" ) ( "input,i", m_apcInputs, "input file" ) /**/ - //( "input,i", m_strInput, "input file" ) /**/ ( "output,o", m_strOutput, "output file" ) /**/ ( "size,s", m_strResolution, "size (WxH)" ) /**/ ( "pel_fmt,p", m_strPelFmt, "pixel format" ) /**/ ( "bits_pel", m_uiBitsPerPixel, "bits per pixel" ) /**/ ( "endianness", m_strEndianness, "File endianness (big, little)" ) /**/ - ( "frames,f", m_iFrames, "number of frames to parse" ); - - Opts().addOptions()/**/ + ( "frames,f", m_iFrames, "number of frames to parse" )/**/ ( "quality", m_strQualityMetric, "select a quality metric" ) /**/ ( "module", m_strModule, "select a module (use internal name)" ) /**/ ( "save", "save a specific frame" ); - config( argc, argv ); - if( !parse() ) + if( !m_cOptions.parse( argc, argv ) ) { iRet = 1; } @@ -87,82 +81,26 @@ Int PlaYUVerToolsCmdParser::parseToolsArgs( Int argc, Char *argv[] ) m_uiLogLevel = LOG_RESULT; } - if( Opts().hasOpt("module") && Opts().hasOpt("help") ) + if( m_cOptions.hasOpt("module") && m_cOptions.hasOpt("help") ) { listModuleHelp(); iRet = 1; } - else if( Opts().hasOpt("help") ) + else if( m_cOptions.hasOpt("help") ) { printf( "Usage: %s modules/quality/save [options] -input=input_file [--output=output_file]\n", argv[0] ); - Opts().doHelp( std::cout ); + m_cOptions.doHelp( std::cout ); iRet = 1; } - if( Opts().hasOpt("module_list") || Opts().hasOpt("module_list_full") ) + if( m_cOptions.hasOpt("module_list") || m_cOptions.hasOpt("module_list_full") ) { - listModules(); + m_cOptions.listModules(); iRet = 1; } return iRet; } -Void PlaYUVerToolsCmdParser::listModules() -{ - Bool bDetailed = false; - - if( Opts().hasOpt("module_list_full") ) - bDetailed = true; - - PlaYUVerModuleIf* pcCurrModuleIf; - PlaYUVerModuleFactoryMap& PlaYUVerModuleFactoryMap = PlaYUVerModuleFactory::Get()->getMap(); - PlaYUVerModuleFactoryMap::iterator it = PlaYUVerModuleFactoryMap.begin(); - - printf( "PlaYUVer available modules: \n" ); - //printf( " " ); - printf( " [Internal Name] " ); - if( bDetailed ) - { - printf( " [Full Name] " ); - printf( " [Type] " ); - printf( " [Description]" ); - } - printf( " \n" ); - - Char ModuleNameString[40]; - - for( UInt i = 0; it != PlaYUVerModuleFactoryMap.end(); ++it, i++ ) - { - printf( " " ); - printf( "%-30s", it->first ); - if( bDetailed ) - { - ModuleNameString[0] = '\0'; - pcCurrModuleIf = it->second(); - - if( pcCurrModuleIf->m_pchModuleCategory ) - { - strcat( ModuleNameString, pcCurrModuleIf->m_pchModuleCategory ); - strcat( ModuleNameString, "/" ); - } - strcat( ModuleNameString, pcCurrModuleIf->m_pchModuleName ); - printf( " %-40s", ModuleNameString ); - switch( pcCurrModuleIf->m_iModuleType ) - { - case FRAME_PROCESSING_MODULE: - printf( " Processing " ); - break; - case FRAME_MEASUREMENT_MODULE: - printf( " Measurement " ); - break; - } - printf( " %s", pcCurrModuleIf->m_pchModuleTooltip ); - pcCurrModuleIf->Delete(); - } - printf( "\n" ); - } -} - Void PlaYUVerToolsCmdParser::listModuleHelp() { String moduleName = m_strModule; diff --git a/tools/PlaYUVerToolsCmdParser.h b/tools/PlaYUVerToolsCmdParser.h index adfde00..0796fd2 100644 --- a/tools/PlaYUVerToolsCmdParser.h +++ b/tools/PlaYUVerToolsCmdParser.h @@ -27,12 +27,12 @@ #include "config.h" #include "lib/PlaYUVerDefs.h" -#include "lib/PlaYUVerCmdParser.h" +#include "lib/PlaYUVerOptions.h" class PlaYUVerStream; -class PlaYUVerToolsCmdParser: public PlaYUVerCmdParser +class PlaYUVerToolsCmdParser { friend class PlaYUVerTools; public: @@ -60,8 +60,13 @@ public: Int parseToolsArgs( Int argc, Char *argv[] ); + PlaYUVerOptions& Opts() + { + return m_cOptions; + } + private: - Options m_cParserOptions; + PlaYUVerOptions m_cOptions; UInt m_uiLogLevel; /**