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 
21 #include <boost/test/detail/suppress_warnings.hpp>
22 
23 namespace boost {
24 namespace runtime {
25 namespace cla {
26 
27 // ************************************************************************** //
28 // **************          runtime::cla::argv_traverser        ************** //
29 // ************************************************************************** //
30 
31 class argv_traverser {
32     typedef char const** argv_type;
33 public:
34     /// Constructs traverser based on argc/argv pair
35     /// argv is taken "by reference" and later can be
36     /// updated in remainder method
argv_traverser(int argc,argv_type argv)37     argv_traverser( int argc, argv_type argv )
38     : m_argc( argc )
39     , m_curr_token( 0 )
40     , m_token_size( 0 )
41     , m_argv( argv )
42     {
43         // save program name
44         save_token();
45     }
46 
47     /// Returns new argc
remainder()48     int         remainder()
49     {
50         return m_argc;
51     }
52 
53     /// Returns true, if we reached end on input
eoi() const54     bool        eoi() const
55     {
56         return m_curr_token == m_argc;
57     }
58 
59     /// Returns current token in the input
current_token()60     cstring     current_token()
61     {
62         if( eoi() )
63             return cstring();
64 
65         return cstring( m_argv[m_curr_token], m_token_size );
66     }
67 
68     /// Saves current token for remainder
save_token()69     void        save_token()
70     {
71         ++m_curr_token;
72 
73         if( !eoi() )
74             m_token_size = ::strlen( m_argv[m_curr_token] );
75     }
76 
77     /// Commit current token and iterate to next one
next_token()78     void        next_token()
79     {
80         if( !eoi() ) {
81             for( std::size_t i = m_curr_token; i < m_argc-1; ++i )
82                 m_argv[i] = m_argv[i + 1];
83 
84             --m_argc;
85 
86             m_token_size = ::strlen( m_argv[m_curr_token] );
87         }
88     }
89 
90 private:
91 
92     // Data members
93     std::size_t m_argc;         // total number of arguments
94     std::size_t m_curr_token;   // current token index in argv
95     std::size_t m_token_size;   // current token size
96     argv_type   m_argv;         // all arguments
97 };
98 
99 } // namespace cla
100 } // namespace runtime
101 } // namespace boost
102 
103 #include <boost/test/detail/enable_warnings.hpp>
104 
105 #endif // BOOST_TEST_UTILS_RUNTIME_CLA_ARGV_TRAVERSER_HPP
106