Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages   Examples  

CTBargv.hxx

Go to the documentation of this file.
00001 #ifndef _CTBargv_HXX
00002 #define _CTBargv_HXX 1
00003 
00008 /*----------------------------------------------------------------------------*/
00009 /* C Tool Box: Designed and implemented by:                                   */
00010 /*    Walter F.J. Mueller   Gesellschaft fuer Schwerionenforschung (GSI)      */
00011 /*                          Planckstrasse 1, D-64291 Darmstadt, Germany       */
00012 /*                  Email:  W.F.J.Mueller@gsi.de                              */
00013 /*                  WWW:    http://www-kp3.gsi.de/www/kp3/people/mueller.html */
00014 /*------------------------------------------+---------------------------------*/
00015 
00016 #include <strstream.h>
00017 
00018 class CTBargv {
00019 
00020   public:
00021 
00022                     CTBargv();
00023                     CTBargv(int argc, char* argv[]);
00024 
00025                     ~CTBargv();
00026 
00027     int             Argc() const;
00028     const char*     Argv(int i_ind) const;
00029 
00030     bool            Locate(const char* c_opt, int i_npar = 0);
00031 
00032     const char*     NextArg();
00033 
00034     void            CheckUnused();
00035     bool            CheckErrors();
00036     bool            CheckErrors(ostream& os);
00037 
00038     void            SetupStream(istrstream& istr);
00039     void            CheckStream(istrstream& istr);
00040 
00041     void            ToStream(ostream& os = cout) const;
00042   
00043     const char*     operator[](int i_ind) const;
00044 
00045   private:
00046 
00047     struct argdsc {                         // argument descriptor
00048                     argdsc();               // constructor
00049       char*         mc_arg;                 // pointer to argument
00050       int           mi_nchar;               // number of chars for argument
00051       int           mi_ncharleft;           // number of left over chars
00052       bool          mb_used;                // used flag
00053       int           mi_errorflags;          // error flags
00054     };
00055 
00056     static const int mi_error_conv   = 0x0001; // conversion error flag
00057     static const int mi_error_npar   = 0x0002; // insufficient parameters avail.
00058     static const int mi_error_unused = 0x0004; // unused arguent
00059   
00060     int             mi_argc;                // argument count
00061     char*           mc_args;                // compound argument string
00062     argdsc*         m_argv;                 // array of argument descriptors
00063     int             mi_curarg;              // current argument index
00064 };
00065 
00066     ostream&        operator<<(ostream& os, const CTBargv& av);
00067 
00068     template <class T>
00069     CTBargv&        operator>>(             // extraction operator for type T
00070                       CTBargv& av,          // argv >> obj
00071                       T& obj);
00072 
00073 //------------------------------------------+-----------------------------------
00074 // Definition of member functions and operators which are always inlined
00075 
00076 template <class T>
00077 inline CTBargv& operator>>(                 // extraction operator for type T
00078     CTBargv& av,
00079     T& obj)
00080 {
00081   const char* c_arg = av.NextArg();         // get pointer to next argument
00082   
00083   if (c_arg) {                              // if defined
00084     istrstream istr(c_arg);                 // create string stream
00085     av.SetupStream(istr);                   // setup stream attributes
00086     istr >> obj;                            // use stream extraction operator
00087     av.CheckStream(istr);                   // and check complete conversion
00088   }
00089   return av;                                // to allow: argv >> i >> j >> k;
00090 }
00091 
00092 //CTB_TemplateFullSpecialization    // ??? vms didn't like the template
00093 //
00094 //Note: the `specialization' of oper>> must be defined inline for G++
00095 //      or it will be utterly confused between template and explicit definition.
00096 //
00097 inline CTBargv& operator>>(                 // extraction operator for
00098     CTBargv& av,
00099     const char*& obj)                       //   C strings
00100 {
00101   const char* c_arg = av.NextArg();         // get pointer to next argument
00102   
00103   if (c_arg) {                              // if defined
00104     obj = c_arg;
00105   }
00106   return av;                                // to allow: argv >> i >> j >> k;
00107 }
00108 
00109 //------------------------------------------+-----------------------------------
00110 
00111 #if !(defined(CTB__OutLine) || defined(CTBargv__OutLine))
00112 #include "CTBargv.icc"
00113 #endif
00114 
00115 #endif

Generated at Fri Oct 24 18:11:27 2003 for CTBbase by doxygen1.2.9-20010812 written by Dimitri van Heesch, © 1997-2001