summaryrefslogtreecommitdiff
path: root/src/Options.cxx
blob: 27e89243133a4ad990ee7e1c142c5cfc96c35227 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
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" );
     }
};

}