summaryrefslogtreecommitdiff
path: root/src/Options.cxx
diff options
context:
space:
mode:
Diffstat (limited to 'src/Options.cxx')
-rw-r--r--src/Options.cxx275
1 files changed, 275 insertions, 0 deletions
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<D_ARRAY_SIZE(m_formats); i++) {
+ if (!strcasecmp( m_formats[i].name, arg )) {
+ m_value = m_formats[i].format;
+
+ return true;
+ }
+ }
+
+ D_ERROR( "Option/Format: Unknown format '%s' passed to '%s' or '%s'!\n", arg, m_short_name, m_long_name );
+
+ return false;
+ }
+ };
+
+ public class OptionString extends Option {
+ private std::string &m_value;
+
+ public OptionString( const char *short_name,
+ const char *long_name,
+ const char *arg_name,
+ const char *arg_desc,
+ std::string &value ) : Option( short_name, long_name, arg_name, arg_desc ), m_value( value )
+ {
+ }
+
+ public virtual bool Parse( const char *arg ) {
+ m_value = arg;
+ return true;
+ }
+ };
+
+ public class OptionPoint extends Option {
+ private DFBPoint &m_value;
+
+ public OptionPoint( const char *short_name,
+ const char *long_name,
+ const char *arg_name,
+ const char *arg_desc,
+ DFBPoint &value ) : Option( short_name, long_name, arg_name, arg_desc ), m_value( value )
+ {
+ }
+
+ public virtual bool Parse( const char *arg ) {
+ if (sscanf( arg, "%d,%d", &m_value.x, &m_value.y ) != 2) {
+ D_ERROR( "Option/Point: Invalid argument to '%s' or '%s'!\n", m_short_name, m_long_name );
+ return false;
+ }
+
+ return true;
+ }
+ };
+
+ public class OptionDimension extends Option {
+ private DFBDimension &m_value;
+
+ public OptionDimension( const char *short_name,
+ const char *long_name,
+ const char *arg_name,
+ const char *arg_desc,
+ DFBDimension &value ) : Option( short_name, long_name, arg_name, arg_desc ), m_value( value )
+ {
+ }
+
+ public virtual bool Parse( const char *arg ) {
+ if (sscanf( arg, "%dx%d", &m_value.w, &m_value.h ) != 2) {
+ D_ERROR( "Option/Dimension: Invalid argument to '%s' or '%s'!\n", m_short_name, m_long_name );
+ return false;
+ }
+
+ return true;
+ }
+ };
+
+
+
+ private vector<Option*> 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<Option*>::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<Option*>::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" );
+ }
+};
+
+}
+