Line data Source code
1 : // (C) Copyright Gennadiy Rozental 2001. 2 : // Use, modification, and distribution are subject to the 3 : // Boost Software License, Version 1.0. (See accompanying file 4 : // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 5 : 6 : // See http://www.boost.org/libs/test for the library home page. 7 : // 8 : // File : $RCSfile$ 9 : // 10 : // Version : $Revision$ 11 : // 12 : // Description : defines facility to hide input traversing details 13 : // *************************************************************************** 14 : 15 : #ifndef BOOST_TEST_UTILS_RUNTIME_CLA_ARGV_TRAVERSER_HPP 16 : #define BOOST_TEST_UTILS_RUNTIME_CLA_ARGV_TRAVERSER_HPP 17 : 18 : // Boost.Test Runtime parameters 19 : #include <boost/test/utils/runtime/fwd.hpp> 20 : #include <cstring> 21 : 22 : #include <boost/test/detail/suppress_warnings.hpp> 23 : 24 : namespace boost { 25 : namespace runtime { 26 : namespace cla { 27 : 28 : // ************************************************************************** // 29 : // ************** runtime::cla::argv_traverser ************** // 30 : // ************************************************************************** // 31 : 32 : class argv_traverser { 33 : typedef char const** argv_type; 34 : public: 35 : /// Constructs traverser based on argc/argv pair 36 : /// argv is taken "by reference" and later can be 37 : /// updated in remainder method 38 292 : argv_traverser( int argc, argv_type argv ) 39 146 : : m_argc( argc ) 40 146 : , m_curr_token( 0 ) 41 146 : , m_token_size( 0 ) 42 146 : , m_argv( argv ) 43 146 : { 44 : // save program name 45 146 : save_token(); 46 292 : } 47 : 48 : /// Returns new argc 49 146 : int remainder() 50 : { 51 146 : return static_cast<int>(m_argc); 52 : } 53 : 54 : /// Returns true, if we reached end on input 55 2482 : bool eoi() const 56 : { 57 2482 : return m_curr_token == m_argc; 58 : } 59 : 60 : /// Returns current token in the input 61 876 : cstring current_token() 62 : { 63 876 : if( eoi() ) 64 0 : return cstring(); 65 : 66 876 : return cstring( m_argv[m_curr_token], m_token_size ); 67 876 : } 68 : 69 : /// Saves current token for remainder 70 146 : void save_token() 71 : { 72 146 : ++m_curr_token; 73 : 74 146 : if( !eoi() ) 75 146 : m_token_size = ::strlen( m_argv[m_curr_token] ); 76 146 : } 77 : 78 : /// Commit current token and iterate to next one 79 876 : void next_token() 80 : { 81 876 : if( !eoi() ) { 82 3942 : for( std::size_t i = m_curr_token; i < m_argc-1; ++i ) 83 3066 : m_argv[i] = m_argv[i + 1]; 84 : 85 876 : --m_argc; 86 : 87 876 : m_token_size = ::strlen( m_argv[m_curr_token] ); 88 876 : } 89 876 : } 90 : 91 : private: 92 : 93 : // Data members 94 : std::size_t m_argc; // total number of arguments 95 : std::size_t m_curr_token; // current token index in argv 96 : std::size_t m_token_size; // current token size 97 : argv_type m_argv; // all arguments 98 : }; 99 : 100 : } // namespace cla 101 : } // namespace runtime 102 : } // namespace boost 103 : 104 : #include <boost/test/detail/enable_warnings.hpp> 105 : 106 : #endif // BOOST_TEST_UTILS_RUNTIME_CLA_ARGV_TRAVERSER_HPP