From 27d1e03d7bdf8fcfe7292c06e40bc3e2fca9158e Mon Sep 17 00:00:00 2001 From: Denis Oliver Kropp Date: Tue, 19 Oct 2010 15:56:15 +0200 Subject: pluggit --- src/Options.cxx | 275 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 275 insertions(+) create mode 100644 src/Options.cxx (limited to 'src/Options.cxx') diff --git a/src/Options.cxx b/src/Options.cxx new file mode 100644 index 0000000..27e8924 --- /dev/null +++ b/src/Options.cxx @@ -0,0 +1,275 @@ +namespace PluggIt { + +static const DirectFBPixelFormatNames(m_formats); + +class Options +{ + class Option { + friend class Options; + + protected const char *m_short_name; + protected const char *m_long_name; + protected const char *m_arg_name; + protected const char *m_arg_desc; + protected bool m_need_arg; + + public Option( const char *short_name, + const char *long_name, + const char *arg_name, + const char *arg_desc ) + { + m_short_name = short_name; + m_long_name = long_name; + m_arg_name = arg_name; + m_arg_desc = arg_desc; + m_need_arg = true; + } + + public virtual bool Parse( const char *arg ) = 0; + }; + + public class OptionBool extends Option { + private bool &m_value; + + public OptionBool( const char *short_name, + const char *long_name, + const char *arg_name, + const char *arg_desc, + bool &value ) : Option( short_name, long_name, arg_name, arg_desc ), m_value( value ) + { + m_need_arg = false; + } + + public virtual bool Parse( const char *arg ) { + m_value = true; + + return true; + } + }; + + public class OptionLong extends Option { + private long &m_value; + private int m_base; + + public OptionLong( const char *short_name, + const char *long_name, + const char *arg_name, + const char *arg_desc, + long &value, + int base = 10 ) : Option( short_name, long_name, arg_name, arg_desc ), m_value( value ) + { + m_base = base; + } + + public virtual bool Parse( const char *arg ) { + long ret; + char *end; + + ret = strtol( arg, &end, m_base ); + + if (*end) { + D_ERROR( "Option/Long: Invalid argument to '%s' or '%s'!\n", m_short_name, m_long_name ); + return false; + } + + m_value = ret; + + return true; + } + }; + + public class OptionULong extends Option { + private unsigned long &m_value; + private int m_base; + + public OptionULong( const char *short_name, + const char *long_name, + const char *arg_name, + const char *arg_desc, + unsigned long &value, + int base = 10 ) : Option( short_name, long_name, arg_name, arg_desc ), m_value( value ) + { + m_base = base; + } + + public virtual bool Parse( const char *arg ) { + unsigned long ret; + char *end; + + ret = strtoul( arg, &end, m_base ); + + if (*end) { + D_ERROR( "Option/ULong: Invalid argument to '%s' or '%s'!\n", m_short_name, m_long_name ); + return false; + } + + m_value = ret; + + return true; + } + }; + + public class OptionFormat extends Option { + private DFBSurfacePixelFormat &m_value; + + public OptionFormat( const char *short_name, + const char *long_name, + const char *arg_name, + const char *arg_desc, + DFBSurfacePixelFormat &value ) : Option( short_name, long_name, arg_name, arg_desc ), m_value( value ) + { + } + + public virtual bool Parse( const char *arg ) { + unsigned int i; + + for (i=0; i m_options; + + public void addOption( Option *option ) { + m_options.push_back( option ); + } + + public bool parseCommandLine( int argc, char *argv[] ) { + int n; + + for (n = 1; n < argc; n++) { + bool ok = false; + const char *arg = argv[n]; + + if (strcmp (arg, "-h") == 0 || strcmp (arg, "--help") == 0) { + printUsage (argv[0]); + return false; + } + + if (strcmp (arg, "-v") == 0 || strcmp (arg, "--version") == 0) { +// fprintf (stderr, "dfbscreen version %s\n", DIRECTFB_VERSION); + return false; + } + + vector::iterator itr = m_options.begin(); + + for (; itr != m_options.end(); itr++) { + Option *option = *itr; + + if (!strcmp (arg, option->m_short_name) || !strcmp (arg, option->m_long_name)) { + if (option->m_need_arg && ++n == argc) { + printUsage (argv[0]); + return false; + } + + if (!option->Parse( argv[n] )) + return false; + + ok = true; + + break; + } + } + + if (!ok) { + printUsage (argv[0]); + return false; + } + } + + return true; + } + + public void printUsage( const char *prg_name ) { + //fprintf (stderr, "\nDirectFB Screen Configuration (version %s)\n\n", DIRECTFB_VERSION); + fprintf (stderr, "Usage: %s [options]\n\n", prg_name); + fprintf (stderr, "Options:\n"); + fprintf (stderr, " -h --help Show this help message\n"); + fprintf (stderr, " -v --version Print version information\n"); + + + vector::const_iterator itr = m_options.begin(); + + for (; itr != m_options.end(); itr++) { + const Option *option = *itr; + + fprintf( stderr, " %-3s %-16s %-12s %s\n", + option->m_short_name, option->m_long_name, option->m_arg_name, option->m_arg_desc ); + } + + fprintf( stderr, "\n" ); + } +}; + +} + -- cgit