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