fshake3d  0.0.1
FreeformDensity3DSurfaceEditor
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines
options.hpp
Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include <iostream>
00003 
00004 // interface
00005 
00006 struct options_config;
00007 struct options_parser
00008 {
00009   options_parser(int argc, char* argv[])
00010     : mArgc(argc)
00011     , mArgv(argv)
00012     , mPos(0)
00013   {
00014   }
00015   void usage();
00016   bool empty() const { return (mPos >= mArgc); }
00017   int parse_int() { return atoi(mArgv[mPos++]); };
00018   const char* parse_string() { return mArgv[mPos++]; }
00019   double parse_double() { return atof(mArgv[mPos++]); }  
00020   bool expect(int n) { if ( mPos+n <= mArgc) { return true; } std::cerr << "invalid parameter count: expected " << n << " arguments\n"; return false; }
00021   void parse_argument(options_config& c);
00022   void skip(int i) { mPos++; }
00023   void parse_all(options_config& c)
00024   {
00025     skip(1);
00026     while ( ! empty() )
00027     {
00028       parse_argument(c);
00029     }
00030   }
00031   int mArgc;
00032   char** mArgv;
00033   int mPos;
00034 };
00035 
00036 // support math datatypes
00037 #include "math.hpp"
00038 
00039 struct options_config
00040 {
00041   options_config()
00042     : mSize(31,31)
00043     , mPosition(0,0)
00044     , mTitle("untitled")
00045     , mXLimits(0,100)
00046     , mYLimits(0,100)
00047   {
00048   }
00049   void size(options_parser& o)
00050   {
00051     if ( o.expect(2) )
00052     {
00053       mSize[0] = o.parse_int();
00054       mSize[1] = o.parse_int();
00055     }
00056   }
00057   void title(options_parser& o)
00058   {
00059     if ( o.expect(1) )
00060     {
00061       mTitle = o.parse_string();
00062     }
00063   }
00064   void position(options_parser& o)
00065   {
00066     if ( o.expect(2) )
00067     {
00068       mPosition[0] = o.parse_int();
00069       mPosition[1] = o.parse_int();
00070     }      
00071   }
00072   void limits(options_parser& o)
00073   {
00074     if ( o.expect(4) )
00075     {
00076       mXLimits[0] = o.parse_double();
00077       mXLimits[1] = o.parse_double();
00078       mYLimits[0] = o.parse_double();
00079       mYLimits[1] = o.parse_double();
00080     }
00081   }
00082   void help(options_parser& o)
00083   {
00084     o.usage();
00085   }
00086   Vec2i mSize;
00087   const char* mTitle;
00088   Vec2i mPosition;
00089   const char* mImage;
00090   Vec2d mXLimits;
00091   Vec2d mYLimits;
00092 };
00093