Last updated:
31. October 2001

User Interface Programming

(Start main menu) Home ­ Articles ­ Book ­ Resources (End main menu)

ArgumentList.cpp: Listing 30


(Start sub-menu)

Table of Contents

Cover

Sample Chapter

Changelog


 

Feedback…

My blog »

(End sub-menu)

Programming Industrial Strength Windows (cover)

/*
 * $Header: /Book/ArgumentList.cpp 9     20.08.99 16:33 Oslph312 $
 *
 * Implements command line handling.
 */
 
#include "precomp.h"
#include "ArgumentList.h"
 
ArgumentList::ArgumentList( __LPCTSTR /* pszCmdLine */ ) {
 
   assert( isGoodPtr( this ) );
   assert( isGoodReadPtr( &__argc, sizeof __argc ) );
   assert( isGoodReadPtr( &__targv, sizeof __targv ) );
   assert(
      isGoodReadPtr( __targv, __argc * sizeof( __targv[ 0 ] ) ) );
 
   m_argc = __argc;
 
   // Using the system-supplied array appears to work OK,
   // but I am wary of messing with parameters that really
   // should have been const in the first place.
   // Hence the semi-deep copy. Since the strings themselves are
   // const, a deep copy is not necessary.
 
#if 0
   m_argv = __targv;
   assert( 0 == m_argv[ m_argc ] );
#else
   m_argv.alloc( m_argc + 1 );
   for ( int iArg = 0; iArg < m_argc; ++iArg ) {
      m_argv[ iArg ] = __targv[ iArg ];
   }
   assert( 0 == __targv[ m_argc ] );
   m_argv[ m_argc ] = 0;
#endif
}
 
 
bool ArgumentList::hasOption( LPCTSTR pszOption ) {
 
   assert( isValid() );
 
   bool bOption = false;
   for ( int iArg = 1; iArg < m_argc; ++iArg ) {
      if ( isOption( iArg ) &&
         0 == _tcsicmp( pszOption, getArg( iArg ) + 1 ) )
      {
         bOption = true;
         consume( iArg-- );
      }
   }
   return bOption;
}
 
 
void ArgumentList::consume( int nArg ) {
 
   assert( isValid() );
   assert( 0 <= nArg && nArg < m_argc );
 
   for ( int iArg = nArg; iArg < m_argc; ++iArg ) {
      m_argv[ iArg ] = m_argv[ iArg + 1 ];
   }
 
   --m_argc;
   assert( 0 == m_argv[ m_argc ] );
}
 
 
#ifdef _DEBUG
 
bool ArgumentList::isValid( void ) const {
 
   bool bValid = isGoodConstPtr( this ) && 0 < m_argc &&
      isGoodReadPtr( m_argv, (m_argc + 1) * sizeof( *m_argv ) );
   if ( bValid ) {
      for ( int iArg = 0; iArg < m_argc; ++iArg ) {
         if ( !isGoodStringPtr( m_argv[ iArg ] ) ) {
            bValid = false;
            break;
         }
      }
      assert( 0 == m_argv[ m_argc ] );
   }
   return bValid;
}
 
#endif // _DEBUG
 
// end of file

(Start bottom menu)

TopHomeArticlesBookResources
Win­dows De­vel­oper Maga­zineR&D BooksCMP Books
Amazon.comAmazon.co.ukContact Petter Hesselberg

(End bottom menu)

Chapter 7 Listings

createNewFile.cpp

getLongPathName.cpp

resolveName.cpp

activateOldInstance.cpp

ArgumentList.h

ArgumentList.cpp

init.cpp