argv_traverser.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. // See http://www.boost.org/libs/test for the library home page.
  6. //
  7. // File : $RCSfile$
  8. //
  9. // Version : $Revision$
  10. //
  11. // Description : defines facility to hide input traversing details
  12. // ***************************************************************************
  13. #ifndef BOOST_TEST_UTILS_RUNTIME_CLA_ARGV_TRAVERSER_HPP
  14. #define BOOST_TEST_UTILS_RUNTIME_CLA_ARGV_TRAVERSER_HPP
  15. // Boost.Test Runtime parameters
  16. #include <boost/test/utils/runtime/fwd.hpp>
  17. #include <boost/test/detail/suppress_warnings.hpp>
  18. namespace boost {
  19. namespace runtime {
  20. namespace cla {
  21. // ************************************************************************** //
  22. // ************** runtime::cla::argv_traverser ************** //
  23. // ************************************************************************** //
  24. class argv_traverser {
  25. typedef char const** argv_type;
  26. public:
  27. /// Constructs traverser based on argc/argv pair
  28. /// argv is taken "by reference" and later can be
  29. /// updated in remainder method
  30. argv_traverser( int argc, argv_type argv )
  31. : m_argc( argc )
  32. , m_curr_token( 0 )
  33. , m_token_size( 0 )
  34. , m_argv( argv )
  35. {
  36. // save program name
  37. save_token();
  38. }
  39. /// Returns new argc
  40. int remainder()
  41. {
  42. return m_argc;
  43. }
  44. /// Returns true, if we reached end on input
  45. bool eoi() const
  46. {
  47. return m_curr_token == m_argc;
  48. }
  49. /// Returns current token in the input
  50. cstring current_token()
  51. {
  52. if( eoi() )
  53. return cstring();
  54. return cstring( m_argv[m_curr_token], m_token_size );
  55. }
  56. /// Saves current token for remainder
  57. void save_token()
  58. {
  59. ++m_curr_token;
  60. if( !eoi() )
  61. m_token_size = ::strlen( m_argv[m_curr_token] );
  62. }
  63. /// Commit current token and iterate to next one
  64. void next_token()
  65. {
  66. if( !eoi() ) {
  67. for( std::size_t i = m_curr_token; i < m_argc-1; ++i )
  68. m_argv[i] = m_argv[i + 1];
  69. --m_argc;
  70. m_token_size = ::strlen( m_argv[m_curr_token] );
  71. }
  72. }
  73. private:
  74. // Data members
  75. std::size_t m_argc; // total number of arguments
  76. std::size_t m_curr_token; // current token index in argv
  77. std::size_t m_token_size; // current token size
  78. argv_type m_argv; // all arguments
  79. };
  80. } // namespace cla
  81. } // namespace runtime
  82. } // namespace boost
  83. #include <boost/test/detail/enable_warnings.hpp>
  84. #endif // BOOST_TEST_UTILS_RUNTIME_CLA_ARGV_TRAVERSER_HPP