MOAB: Mesh Oriented datABase  (version 5.4.1)
CLArgFlag.cpp
Go to the documentation of this file.
00001 /* *****************************************************************
00002     MESQUITE -- The Mesh Quality Improvement Toolkit
00003 
00004     Copyright 2007 Sandia National Laboratories.  Developed at the
00005     University of Wisconsin--Madison under SNL contract number
00006     624796.  The U.S. Government and the University of Wisconsin
00007     retain certain rights to this software.
00008 
00009     This library is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU Lesser General Public
00011     License as published by the Free Software Foundation; either
00012     version 2.1 of the License, or (at your option) any later version.
00013 
00014     This library is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017     Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public License
00020     (lgpl.txt) along with this library; if not, write to the Free Software
00021     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 
00023     (2008) [email protected]
00024 
00025   ***************************************************************** */
00026 
00027 /** \file CLArgFlag.cpp
00028  *  \brief
00029  *  \author Jason Kraftcheck
00030  */
00031 
00032 #include "Mesquite.hpp"
00033 #include "CLArgFlag.hpp"
00034 #include "ManPage.hpp"
00035 #include <cctype>
00036 #include <cstdlib>
00037 #include <iostream>
00038 #include <sstream>
00039 
00040 template < typename T >
00041 static std::string stringify( T value )
00042 {
00043     std::ostringstream ss;
00044     ss << value;
00045     return ss.str();
00046 }
00047 
00048 /************************* CLArgFlag ********************************/
00049 
00050 CLArgFlag::~CLArgFlag() {}
00051 
00052 std::string CLArgFlag::make_man_string( int count, const char* names[] ) const
00053 {
00054     std::ostringstream ss;
00055     ManPage::begin_bold( ss ) << "-" << flag();
00056     ManPage::end_bold( ss );
00057     if( count )
00058     {
00059         ss << "<" << names[0];
00060         for( int i = 1; i < count; ++i )
00061         {
00062             ss << "|";
00063         }
00064         ss << ">";
00065     }
00066     return ss.str();
00067 }
00068 
00069 std::string CLArgFlag::make_literal_man_string( int count, const char* args[] ) const
00070 {
00071     std::ostringstream ss;
00072     ManPage::begin_bold( ss ) << "-" << flag();
00073     ManPage::end_bold( ss );
00074     if( count )
00075     {
00076         ss << args[0];
00077         for( int i = 1; i < count; ++i )
00078             ManPage::bold( ss, args[i] );
00079     }
00080     return ss.str();
00081 }
00082 
00083 bool CLArgFlag::is_toggle() const
00084 {
00085     return false;
00086 }
00087 
00088 bool CLArgFlag::add_set( int, const char* const* )
00089 {
00090     return false;
00091 }
00092 
00093 /************************* CLArgToggle ********************************/
00094 
00095 bool CLArgToggle::parse( const char* ) const
00096 {
00097     if( !mCallback->value( mValue ) ) return false;
00098     mCallback->set_seen();
00099     return true;
00100 }
00101 
00102 std::string CLArgToggle::brief() const
00103 {
00104     if( !mOpposite )
00105     {
00106         const char result[] = { '-', flag(), '\0' };
00107         return result;
00108     }
00109     else if( mOpposite->flag() < flag() )
00110     {
00111         return std::string();
00112     }
00113     else
00114     {
00115         const char result[] = { '-', flag(), '|', '-', mOpposite->flag(), '\0' };
00116         return result;
00117     }
00118 }
00119 
00120 std::string CLArgToggle::manstr() const
00121 {
00122     if( !mOpposite )
00123     {
00124         return make_man_string();
00125     }
00126     else if( mOpposite->flag() > flag() )
00127     {
00128         std::ostringstream result;
00129         ManPage::begin_bold( result );
00130         result << "-" << flag();
00131         ManPage::end_bold( result );
00132         result << "|";
00133         ManPage::begin_bold( result );
00134         result << "-" << mOpposite->flag();
00135         ManPage::end_bold( result );
00136         return result.str();
00137     }
00138     else
00139     {
00140         return std::string();
00141     }
00142 }
00143 
00144 /************************* CLArgString ***********************************/
00145 
00146 std::string CLArgString::brief() const
00147 {
00148     std::ostringstream ss;
00149     ss << '-' << flag() << "  <" << mName << '>';
00150     return ss.str();
00151 }
00152 
00153 std::string CLArgString::manstr() const
00154 {
00155     return make_man_string( mName.c_str() );
00156 }
00157 
00158 bool CLArgString::parse( const char* option ) const
00159 {
00160     if( !mCallback->value( option ) ) return false;
00161     mCallback->set_seen();
00162     return true;
00163 }
00164 
00165 /************************* CLArgLong ***********************************/
00166 
00167 std::string CLArgLong::brief() const
00168 {
00169     const char str[] = { '-', flag(), ' ', '<', '\0' };
00170     std::string result( str );
00171     result += mName;
00172     result += ">";
00173     return result;
00174 }
00175 std::string CLArgLong::manstr() const
00176 {
00177     return make_man_string( &mName[0] );
00178 }
00179 
00180 bool CLArgLong::parse( const char* option ) const
00181 {
00182     long l;
00183     char* end_ptr;
00184     l = strtol( option, &end_ptr, 0 );
00185     if( *end_ptr || !mCallback->value( l ) ) return false;
00186     mCallback->set_seen();
00187     return true;
00188 }
00189 
00190 /************************* CLArgInt ***********************************/
00191 
00192 std::string CLArgInt::brief() const
00193 {
00194     const char str[] = { '-', flag(), ' ', '<', '\0' };
00195     std::string result( str );
00196     result += mName;
00197     result += ">";
00198     return result;
00199 }
00200 std::string CLArgInt::manstr() const
00201 {
00202     return make_man_string( &mName[0] );
00203 }
00204 
00205 bool CLArgInt::parse( const char* option ) const
00206 {
00207     long l;
00208     char* end_ptr;
00209     l     = strtol( option, &end_ptr, 0 );
00210     int i = (int)l;
00211     if( *end_ptr || (long)i != l || !mCallback->value( i ) ) return false;
00212     mCallback->set_seen();
00213     return true;
00214 }
00215 
00216 /************************* CLArgDouble ********************************/
00217 
00218 std::string CLArgDouble::brief() const
00219 {
00220     const char str[] = { '-', flag(), ' ', '<', '\0' };
00221     std::string result( str );
00222     result += mName;
00223     result += ">";
00224     return result;
00225 }
00226 std::string CLArgDouble::manstr() const
00227 {
00228     return make_man_string( &mName[0] );
00229 }
00230 
00231 bool CLArgDouble::parse( const char* option ) const
00232 {
00233     char* endptr;
00234     double val = strtod( option, &endptr );
00235     if( *endptr || !mCallback->value( val ) ) return false;
00236     mCallback->set_seen();
00237     return true;
00238 }
00239 
00240 /************************* CLArgIDList ********************************/
00241 
00242 std::string CLArgIDList::brief() const
00243 {
00244     const char str[] = { '-', flag(), '\0' };
00245     std::string result( str );
00246     result += " <id_list>";
00247     return result;
00248 }
00249 std::string CLArgIDList::manstr() const
00250 {
00251     return make_man_string( "id_list" );
00252 }
00253 
00254 bool CLArgIDList::parse( const char* str ) const
00255 {
00256     std::vector< int > list;
00257     long v;
00258     int i, j;
00259     char* endptr;
00260     for( ;; )
00261     {
00262         while( isspace( *str ) )
00263             ++str;
00264         if( !*str ) break;
00265 
00266         v = strtol( str, &endptr, 10 );
00267         if( endptr == str || v <= 0 ) return false;
00268 
00269         i = (int)v;
00270         if( (long)i != v ) return false;
00271 
00272         list.push_back( i );
00273         str = endptr;
00274 
00275         while( isspace( *str ) )
00276             ++str;
00277 
00278         if( *str == '-' )
00279         {
00280             do
00281             {
00282                 ++str;
00283             } while( isspace( *str ) );
00284             v = strtol( str, &endptr, 10 );
00285             if( endptr == str || v < i ) return false;
00286             j = (int)v;
00287             if( (long)j != v ) return false;
00288             for( ++i; i < j; ++j )
00289                 list.push_back( i );
00290 
00291             str = endptr;
00292             while( isspace( *str ) )
00293                 ++str;
00294         }
00295 
00296         if( !*str ) break;
00297         if( *str != ',' ) return false;
00298         ++str;
00299     }
00300 
00301     if( list.empty() ) return false;
00302 
00303     if( !mCallback->value( list ) ) return false;
00304     mCallback->set_seen();
00305     return true;
00306 }
00307 
00308 /************************* CLArgListData ********************************/
00309 
00310 bool CLArgListData::add_set( int size, const char* const* names )
00311 {
00312     if( size < 0 || ( !mSets.empty() && acceptable_length( size ) ) ) return false;
00313 
00314     mSets.resize( mSets.size() + 1 );
00315     for( int i = 0; i < size; ++i )
00316         mSets.back().push_back( names[i] );
00317     return true;
00318 }
00319 
00320 bool CLArgListData::acceptable_length( unsigned len ) const
00321 {
00322     for( unsigned i = 0; i < mSets.size(); ++i )
00323         if( len == mSets[i].size() ) return true;
00324     return mSets.empty();
00325 }
00326 
00327 std::string CLArgListData::set_string( int i ) const
00328 {
00329     if( mSets[i].empty() ) return std::string();
00330 
00331     std::ostringstream result;
00332     result << mSets[i][0];
00333     for( unsigned j = 1; j < mSets[i].size(); ++j )
00334         result << "," << mSets[i][j];
00335     return result.str();
00336 }
00337 
00338 std::string CLArgListData::brief() const
00339 {
00340     if( mSets.empty() ) return std::string();
00341 
00342     std::ostringstream result;
00343     for( unsigned i = 0; i < mSets.size(); ++i )
00344     {
00345         if( i ) result << '|';
00346         result << '{' << set_string( i ) << "}";
00347     }
00348     return result.str();
00349 }
00350 
00351 std::string CLArgListData::manstr( char type_char, const CLArgFlag& f ) const
00352 {
00353     if( mSets.empty() )
00354     {
00355         const char argstr[] = { type_char, '1', ',', type_char, '2', ',', '.', '.', '.', '\0' };
00356         return f.make_man_string( argstr );
00357     }
00358     else
00359     {
00360         const char flagstr[] = { '-', f.flag(), '\0' };
00361         std::ostringstream ss;
00362         for( unsigned i = 0; i < mSets.size(); ++i )
00363         {
00364             if( i ) ss << "|";
00365             ManPage::begin_bold( ss );
00366             ss << flagstr;
00367             ManPage::end_bold( ss );
00368             ss << "<" << set_string( i ) << ">";
00369         }
00370         return ss.str();
00371     }
00372 }
00373 
00374 /************************* CLArgIntList ********************************/
00375 
00376 std::string CLArgIntList::brief() const
00377 {
00378     const char str[] = { '-', flag(), ' ', '\0' };
00379     std::string result( str );
00380 
00381     if( listData.accept_any_length() )
00382         result += "<n1,n2,...>";
00383     else
00384         result += listData.brief();
00385 
00386     return result;
00387 }
00388 
00389 std::string CLArgIntList::manstr() const
00390 {
00391     return listData.manstr( 'n', *this );
00392 }
00393 
00394 bool CLArgIntList::parse( const char* str ) const
00395 {
00396     long i;
00397     char* endptr;
00398     std::vector< int > list;
00399     for( ;; )
00400     {
00401         while( isspace( *str ) )
00402             ++str;
00403 
00404         if( !*str ) break;
00405 
00406         i = strtol( str, &endptr, 0 );
00407         list.push_back( i );
00408 
00409         while( isspace( *str ) )
00410             ++str;
00411 
00412         if( !*str ) break;
00413 
00414         if( *str != ',' ) return false;
00415 
00416         ++str;
00417     }
00418 
00419     if( !listData.acceptable_length( list.size() ) || !mCallback->value( list ) ) return false;
00420 
00421     mCallback->set_seen();
00422     return true;
00423 }
00424 
00425 /************************* CLArgDoubleList ********************************/
00426 
00427 std::string CLArgDoubleList::brief() const
00428 {
00429     const char str[] = { '-', flag(), ' ', '\0' };
00430     std::string result( str );
00431 
00432     if( listData.accept_any_length() )
00433         result += "<r1,r2,...>";
00434     else
00435         result += listData.brief();
00436 
00437     return result;
00438 }
00439 
00440 std::string CLArgDoubleList::manstr() const
00441 {
00442     return listData.manstr( 'r', *this );
00443 }
00444 
00445 bool CLArgDoubleList::parse( const char* str ) const
00446 {
00447     std::vector< double > list;
00448     char* endptr;
00449     for( ;; )
00450     {
00451         while( isspace( *str ) )
00452             ++str;
00453         if( !*str ) break;
00454 
00455         double d = strtod( str, &endptr );
00456         list.push_back( d );
00457         str = endptr;
00458 
00459         while( isspace( *str ) )
00460             ++str;
00461         if( !*str ) break;
00462 
00463         if( *str != ',' ) return false;
00464         ++str;
00465     }
00466 
00467     if( !listData.acceptable_length( list.size() ) || !mCallback->value( list ) ) return false;
00468 
00469     mCallback->set_seen();
00470     return true;
00471 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines