MOAB: Mesh Oriented datABase  (version 5.2.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) kraftche@cae.wisc.edu
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 <ctype.h>
00036 #include <stdlib.h>
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 ) { return make_man_string(); }
00123     else if( mOpposite->flag() > flag() )
00124     {
00125         std::ostringstream result;
00126         ManPage::begin_bold( result );
00127         result << "-" << flag();
00128         ManPage::end_bold( result );
00129         result << "|";
00130         ManPage::begin_bold( result );
00131         result << "-" << mOpposite->flag();
00132         ManPage::end_bold( result );
00133         return result.str();
00134     }
00135     else
00136     {
00137         return std::string();
00138     }
00139 }
00140 
00141 /************************* CLArgString ***********************************/
00142 
00143 std::string CLArgString::brief() const
00144 {
00145     std::ostringstream ss;
00146     ss << '-' << flag() << "  <" << mName << '>';
00147     return ss.str();
00148 }
00149 
00150 std::string CLArgString::manstr() const
00151 {
00152     return make_man_string( mName.c_str() );
00153 }
00154 
00155 bool CLArgString::parse( const char* option ) const
00156 {
00157     if( !mCallback->value( option ) ) return false;
00158     mCallback->set_seen();
00159     return true;
00160 }
00161 
00162 /************************* CLArgLong ***********************************/
00163 
00164 std::string CLArgLong::brief() const
00165 {
00166     const char str[] = { '-', flag(), ' ', '<', '\0' };
00167     std::string result( str );
00168     result += mName;
00169     result += ">";
00170     return result;
00171 }
00172 std::string CLArgLong::manstr() const
00173 {
00174     return make_man_string( &mName[0] );
00175 }
00176 
00177 bool CLArgLong::parse( const char* option ) const
00178 {
00179     long l;
00180     char* end_ptr;
00181     l = strtol( option, &end_ptr, 0 );
00182     if( *end_ptr || !mCallback->value( l ) ) return false;
00183     mCallback->set_seen();
00184     return true;
00185 }
00186 
00187 /************************* CLArgInt ***********************************/
00188 
00189 std::string CLArgInt::brief() const
00190 {
00191     const char str[] = { '-', flag(), ' ', '<', '\0' };
00192     std::string result( str );
00193     result += mName;
00194     result += ">";
00195     return result;
00196 }
00197 std::string CLArgInt::manstr() const
00198 {
00199     return make_man_string( &mName[0] );
00200 }
00201 
00202 bool CLArgInt::parse( const char* option ) const
00203 {
00204     long l;
00205     char* end_ptr;
00206     l     = strtol( option, &end_ptr, 0 );
00207     int i = (int)l;
00208     if( *end_ptr || (long)i != l || !mCallback->value( i ) ) return false;
00209     mCallback->set_seen();
00210     return true;
00211 }
00212 
00213 /************************* CLArgDouble ********************************/
00214 
00215 std::string CLArgDouble::brief() const
00216 {
00217     const char str[] = { '-', flag(), ' ', '<', '\0' };
00218     std::string result( str );
00219     result += mName;
00220     result += ">";
00221     return result;
00222 }
00223 std::string CLArgDouble::manstr() const
00224 {
00225     return make_man_string( &mName[0] );
00226 }
00227 
00228 bool CLArgDouble::parse( const char* option ) const
00229 {
00230     char* endptr;
00231     double val = strtod( option, &endptr );
00232     if( *endptr || !mCallback->value( val ) ) return false;
00233     mCallback->set_seen();
00234     return true;
00235 }
00236 
00237 /************************* CLArgIDList ********************************/
00238 
00239 std::string CLArgIDList::brief() const
00240 {
00241     const char str[] = { '-', flag(), '\0' };
00242     std::string result( str );
00243     result += " <id_list>";
00244     return result;
00245 }
00246 std::string CLArgIDList::manstr() const
00247 {
00248     return make_man_string( "id_list" );
00249 }
00250 
00251 bool CLArgIDList::parse( const char* str ) const
00252 {
00253     std::vector< int > list;
00254     long v;
00255     int i, j;
00256     char* endptr;
00257     for( ;; )
00258     {
00259         while( isspace( *str ) )
00260             ++str;
00261         if( !*str ) break;
00262 
00263         v = strtol( str, &endptr, 10 );
00264         if( endptr == str || v <= 0 ) return false;
00265 
00266         i = (int)v;
00267         if( (long)i != v ) return false;
00268 
00269         list.push_back( i );
00270         str = endptr;
00271 
00272         while( isspace( *str ) )
00273             ++str;
00274 
00275         if( *str == '-' )
00276         {
00277             do
00278             {
00279                 ++str;
00280             } while( isspace( *str ) );
00281             v = strtol( str, &endptr, 10 );
00282             if( endptr == str || v < i ) return false;
00283             j = (int)v;
00284             if( (long)j != v ) return false;
00285             for( ++i; i < j; ++j )
00286                 list.push_back( i );
00287 
00288             str = endptr;
00289             while( isspace( *str ) )
00290                 ++str;
00291         }
00292 
00293         if( !*str ) break;
00294         if( *str != ',' ) return false;
00295         ++str;
00296     }
00297 
00298     if( list.empty() ) return false;
00299 
00300     if( !mCallback->value( list ) ) return false;
00301     mCallback->set_seen();
00302     return true;
00303 }
00304 
00305 /************************* CLArgListData ********************************/
00306 
00307 bool CLArgListData::add_set( int size, const char* const* names )
00308 {
00309     if( size < 0 || ( !mSets.empty() && acceptable_length( size ) ) ) return false;
00310 
00311     mSets.resize( mSets.size() + 1 );
00312     for( int i = 0; i < size; ++i )
00313         mSets.back().push_back( names[i] );
00314     return true;
00315 }
00316 
00317 bool CLArgListData::acceptable_length( unsigned len ) const
00318 {
00319     for( unsigned i = 0; i < mSets.size(); ++i )
00320         if( len == mSets[i].size() ) return true;
00321     return mSets.empty();
00322 }
00323 
00324 std::string CLArgListData::set_string( int i ) const
00325 {
00326     if( mSets[i].empty() ) return std::string();
00327 
00328     std::ostringstream result;
00329     result << mSets[i][0];
00330     for( unsigned j = 1; j < mSets[i].size(); ++j )
00331         result << "," << mSets[i][j];
00332     return result.str();
00333 }
00334 
00335 std::string CLArgListData::brief() const
00336 {
00337     if( mSets.empty() ) return std::string();
00338 
00339     std::ostringstream result;
00340     for( unsigned i = 0; i < mSets.size(); ++i )
00341     {
00342         if( i ) result << '|';
00343         result << '{' << set_string( i ) << "}";
00344     }
00345     return result.str();
00346 }
00347 
00348 std::string CLArgListData::manstr( char type_char, const CLArgFlag& f ) const
00349 {
00350     if( mSets.empty() )
00351     {
00352         const char argstr[] = { type_char, '1', ',', type_char, '2', ',', '.', '.', '.', '\0' };
00353         return f.make_man_string( argstr );
00354     }
00355     else
00356     {
00357         const char flagstr[] = { '-', f.flag(), '\0' };
00358         std::ostringstream ss;
00359         for( unsigned i = 0; i < mSets.size(); ++i )
00360         {
00361             if( i ) ss << "|";
00362             ManPage::begin_bold( ss );
00363             ss << flagstr;
00364             ManPage::end_bold( ss );
00365             ss << "<" << set_string( i ) << ">";
00366         }
00367         return ss.str();
00368     }
00369 }
00370 
00371 /************************* CLArgIntList ********************************/
00372 
00373 std::string CLArgIntList::brief() const
00374 {
00375     const char str[] = { '-', flag(), ' ', '\0' };
00376     std::string result( str );
00377 
00378     if( listData.accept_any_length() )
00379         result += "<n1,n2,...>";
00380     else
00381         result += listData.brief();
00382 
00383     return result;
00384 }
00385 
00386 std::string CLArgIntList::manstr() const
00387 {
00388     return listData.manstr( 'n', *this );
00389 }
00390 
00391 bool CLArgIntList::parse( const char* str ) const
00392 {
00393     long i;
00394     char* endptr;
00395     std::vector< int > list;
00396     for( ;; )
00397     {
00398         while( isspace( *str ) )
00399             ++str;
00400 
00401         if( !*str ) break;
00402 
00403         i = strtol( str, &endptr, 0 );
00404         list.push_back( i );
00405 
00406         while( isspace( *str ) )
00407             ++str;
00408 
00409         if( !*str ) break;
00410 
00411         if( *str != ',' ) return false;
00412 
00413         ++str;
00414     }
00415 
00416     if( !listData.acceptable_length( list.size() ) || !mCallback->value( list ) ) return false;
00417 
00418     mCallback->set_seen();
00419     return true;
00420 }
00421 
00422 /************************* CLArgDoubleList ********************************/
00423 
00424 std::string CLArgDoubleList::brief() const
00425 {
00426     const char str[] = { '-', flag(), ' ', '\0' };
00427     std::string result( str );
00428 
00429     if( listData.accept_any_length() )
00430         result += "<r1,r2,...>";
00431     else
00432         result += listData.brief();
00433 
00434     return result;
00435 }
00436 
00437 std::string CLArgDoubleList::manstr() const
00438 {
00439     return listData.manstr( 'r', *this );
00440 }
00441 
00442 bool CLArgDoubleList::parse( const char* str ) const
00443 {
00444     std::vector< double > list;
00445     char* endptr;
00446     for( ;; )
00447     {
00448         while( isspace( *str ) )
00449             ++str;
00450         if( !*str ) break;
00451 
00452         double d = strtod( str, &endptr );
00453         list.push_back( d );
00454         str = endptr;
00455 
00456         while( isspace( *str ) )
00457             ++str;
00458         if( !*str ) break;
00459 
00460         if( *str != ',' ) return false;
00461         ++str;
00462     }
00463 
00464     if( !listData.acceptable_length( list.size() ) || !mCallback->value( list ) ) return false;
00465 
00466     mCallback->set_seen();
00467     return true;
00468 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines