fetch.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // (C) Copyright Gennadiy Rozental 2001.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // 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 : implements fetching absent parameter athuments from environment
  12. // ***************************************************************************
  13. #ifndef BOOST_TEST_UTILS_RUNTIME_ENV_FETCH_HPP
  14. #define BOOST_TEST_UTILS_RUNTIME_ENV_FETCH_HPP
  15. // Boost.Test Runtime parameters
  16. #include <boost/test/utils/runtime/parameter.hpp>
  17. #include <boost/test/utils/runtime/argument.hpp>
  18. #include <boost/test/detail/suppress_warnings.hpp>
  19. // C Runtime
  20. #include <stdlib.h>
  21. namespace boost {
  22. namespace runtime {
  23. namespace env {
  24. namespace env_detail {
  25. #ifndef UNDER_CE
  26. #ifdef BOOST_MSVC
  27. #pragma warning(push)
  28. #pragma warning(disable:4996) // getenv
  29. #endif
  30. inline std::pair<cstring,bool>
  31. sys_read_var( cstring var_name )
  32. {
  33. using namespace std;
  34. char const* res = getenv( var_name.begin() );
  35. return std::make_pair( cstring(res), res != NULL );
  36. }
  37. #ifdef BOOST_MSVC
  38. #pragma warning(pop)
  39. #endif
  40. #else
  41. inline std::pair<cstring,bool>
  42. sys_read_var( cstring var_name )
  43. {
  44. return std::make_pair( cstring(), false );
  45. }
  46. #endif
  47. //____________________________________________________________________________//
  48. template<typename ReadFunc>
  49. inline void
  50. fetch_absent( parameters_store const& params, runtime::arguments_store& args, ReadFunc read_func )
  51. {
  52. BOOST_TEST_FOREACH( parameters_store::storage_type::value_type const&, v, params.all() ) {
  53. basic_param_ptr param = v.second;
  54. if( args.has( param->p_name ) || param->p_env_var.empty() )
  55. continue;
  56. std::pair<cstring,bool> value = read_func( param->p_env_var );
  57. if( !value.second )
  58. continue;
  59. // Validate against unexpected empty value
  60. BOOST_TEST_I_ASSRT( !value.first.is_empty() || param->p_has_optional_value,
  61. format_error( param->p_name )
  62. << "Missing an argument value for the parameter " << param->p_name
  63. << " in the environment." );
  64. // Produce argument value
  65. param->produce_argument( value.first, false, args );
  66. }
  67. }
  68. //____________________________________________________________________________//
  69. } // namespace env_detail
  70. inline void
  71. fetch_absent( parameters_store const& params, runtime::arguments_store& args )
  72. {
  73. env_detail::fetch_absent( params, args, &env_detail::sys_read_var );
  74. }
  75. } // namespace env
  76. } // namespace runtime
  77. } // namespace boost
  78. #include <boost/test/detail/enable_warnings.hpp>
  79. #endif // BOOST_TEST_UTILS_RUNTIME_ENV_FETCH_HPP