cmtkCommandLineCallback.cxx

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //  Copyright 2004-2009 SRI International
00005 //
00006 //  This file is part of the Computational Morphometry Toolkit.
00007 //
00008 //  http://www.nitrc.org/projects/cmtk/
00009 //
00010 //  The Computational Morphometry Toolkit is free software: you can
00011 //  redistribute it and/or modify it under the terms of the GNU General Public
00012 //  License as published by the Free Software Foundation, either version 3 of
00013 //  the License, or (at your option) any later version.
00014 //
00015 //  The Computational Morphometry Toolkit is distributed in the hope that it
00016 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00017 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 //  GNU General Public License for more details.
00019 //
00020 //  You should have received a copy of the GNU General Public License along
00021 //  with the Computational Morphometry Toolkit.  If not, see
00022 //  <http://www.gnu.org/licenses/>.
00023 //
00024 //  $Revision: 2022 $
00025 //
00026 //  $LastChangedDate: 2010-07-21 15:26:03 -0700 (Wed, 21 Jul 2010) $
00027 //
00028 //  $LastChangedBy: torstenrohlfing $
00029 //
00030 */
00031 #include "cmtkCommandLine.h"
00032 
00033 void 
00034 cmtk::CommandLine::Callback::Evaluate
00035 ( const size_t argc, const char* argv[], size_t& index )
00036 {
00037   // callback with argument?
00038   if ( this->m_FuncArg ) 
00039     {
00040     if ( index+1 < argc ) 
00041       {
00042       try
00043         {
00044         this->m_FuncArg( argv[index+1] );
00045         }
00046       catch ( const char* error )
00047         {
00048         throw( Exception( error, index ) );
00049         } 
00050       ++index;
00051       } 
00052     else
00053       {
00054       throw( Exception( "Option needs an argument.", index ) );
00055       }
00056     } 
00057   else
00058     // callback with integer argument?
00059     if ( this->m_FuncIntArg ) 
00060       {
00061       if ( index+1 < argc ) 
00062         {
00063         try
00064           {
00065           this->m_FuncIntArg( ConvertStrToLong( argv[index+1] ) );
00066           }
00067         catch ( const char* error )
00068           {
00069           throw( Exception( error, index ) );
00070           } 
00071 
00072         ++index;
00073         } 
00074       else
00075         {
00076         throw( Exception( "Option needs an integer argument.", index ) );
00077         }
00078       } 
00079   else
00080     // callback with double argument?
00081     if ( this->m_FuncDblArg ) 
00082       {
00083       if ( index+1 < argc ) 
00084         {
00085         try
00086           {
00087           this->m_FuncDblArg( ConvertStrToDouble( argv[index+1] ) );
00088           }
00089         catch ( const char* error )
00090           {
00091           throw( Exception( error, index ) );
00092           } 
00093 
00094         ++index;
00095         } 
00096       else
00097         {
00098         throw( Exception( "Option needs a floating point argument.", index ) );
00099         }
00100       } 
00101   else
00102     // multiple arguments to callback?
00103     if ( this->m_FuncMultiArg ) 
00104       {
00105       if ( index+1 < argc ) 
00106         {
00107         int argsUsed = 0;
00108         try
00109           {
00110           this->m_FuncMultiArg( argv+index+1, argsUsed );
00111           }
00112         catch ( const char* error )
00113           {
00114           throw( Exception( error, index ) );
00115           } 
00116         index += argsUsed;
00117         } 
00118       else
00119         {
00120         throw( Exception( "Option needs an argument", index ) );
00121         }
00122       } 
00123     else
00124       {
00125       // no argument to callback
00126       try
00127         {
00128         this->m_Func();
00129         }
00130       catch ( const char* error )
00131         {
00132         throw( Exception( error, index ) );
00133         }
00134       }
00135 }
00136 
00137 mxml_node_t* 
00138 cmtk::CommandLine::Callback
00139 ::MakeXML(  mxml_node_t *const parent ) const 
00140 {
00141   mxml_node_t* node = NULL;
00142   if ( this->m_Func )
00143     {
00144     node = mxmlNewElement( parent, "boolean" );
00145     mxml_node_t *dflt = mxmlNewElement( node, "default" );
00146     mxmlNewText( dflt, 0, "false" );
00147     }
00148   else if ( this->m_FuncArg )
00149     {
00150     node = mxmlNewElement( parent, "string" );
00151     }
00152   else if ( this->m_FuncIntArg )
00153     {
00154     node = mxmlNewElement( parent, "integer" );
00155     }
00156   else if ( this->m_FuncDblArg )
00157     {
00158     node = mxmlNewElement( parent, "double" );
00159     }
00160   else if ( this->m_FuncMultiArg )
00161     {
00162     node = mxmlNewElement( parent, "string-vector" );
00163     }
00164 
00165   mxmlElementSetAttr( node, "multiple", "true" );
00166   return node;
00167 }
00168 
00169 std::string
00170 cmtk::CommandLine::Callback
00171 ::GetParamTypeString() const
00172 {
00173   if ( this->m_FuncArg )
00174     {
00175     return Item::Helper<std::string>::GetParamTypeString( this );
00176     }
00177   else if ( this->m_FuncIntArg )
00178     {
00179     return Item::Helper<int>::GetParamTypeString( this );
00180     }
00181   else if ( this->m_FuncDblArg )
00182     {
00183     return Item::Helper<double>::GetParamTypeString( this );
00184     }
00185   else if ( this->m_FuncMultiArg )
00186     {
00187     return "<string-vector>";
00188     }
00189   return "";
00190 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines