1//  (C) Copyright Gennadiy Rozental 2005-2008.
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
6//  See http://www.boost.org/libs/test for the library home page.
7//
8//  File        : $RCSfile$
9//
10//  Version     : $Revision: 57992 $
11//
12//  Description : implements model of program environment
13// ***************************************************************************
14
15#ifndef BOOST_RT_ENV_ENVIRONMENT_IPP_062904GER
16#define BOOST_RT_ENV_ENVIRONMENT_IPP_062904GER
17
18// Boost.Runtime.Parameter
19#include <boost/test/utils/runtime/config.hpp>
20#include <boost/test/utils/runtime/validation.hpp>
21
22#include <boost/test/utils/runtime/env/variable.hpp>
23
24// Boost.Test
25#include <boost/test/utils/basic_cstring/compare.hpp>
26#include <boost/test/utils/basic_cstring/io.hpp>
27
28// STL
29#include <map>
30#include <list>
31
32namespace boost {
33
34namespace BOOST_RT_PARAM_NAMESPACE {
35
36namespace environment {
37
38// ************************************************************************** //
39// **************             runtime::environment             ************** //
40// ************************************************************************** //
41
42namespace rt_env_detail {
43
44typedef std::map<cstring,rt_env_detail::variable_data> registry;
45typedef std::list<dstring> keys;
46
47BOOST_RT_PARAM_INLINE registry& s_registry()    { static registry instance; return instance; }
48BOOST_RT_PARAM_INLINE keys&     s_keys()        { static keys instance; return instance; }
49
50BOOST_RT_PARAM_INLINE variable_data&
51new_var_record( cstring var_name )
52{
53    // save the name in list of keys
54    s_keys().push_back( dstring() );
55    dstring& key = s_keys().back();
56    assign_op( key, var_name, 0 );
57
58    // create and return new record
59    variable_data& new_var_data = s_registry()[key];
60
61    new_var_data.m_var_name = key;
62
63    return new_var_data;
64}
65
66//____________________________________________________________________________//
67
68BOOST_RT_PARAM_INLINE variable_data*
69find_var_record( cstring var_name )
70{
71    registry::iterator it = s_registry().find( var_name );
72
73    return it == s_registry().end() ? 0 : &(it->second);
74}
75
76//____________________________________________________________________________//
77
78#ifdef BOOST_MSVC
79#pragma warning(push)
80#pragma warning(disable:4996) // getenv
81#endif
82
83BOOST_RT_PARAM_INLINE cstring
84sys_read_var( cstring var_name )
85{
86    using namespace std;
87    return BOOST_RT_PARAM_GETENV( var_name.begin() );
88}
89
90#ifdef BOOST_MSVC
91#pragma warning(pop)
92#endif
93//____________________________________________________________________________//
94
95BOOST_RT_PARAM_INLINE void
96sys_write_var( cstring var_name, format_stream& var_value )
97{
98    BOOST_RT_PARAM_PUTENV( var_name, cstring( var_value.str() ) );
99}
100
101//____________________________________________________________________________//
102
103} // namespace rt_env_detail
104
105BOOST_RT_PARAM_INLINE variable_base
106var( cstring var_name )
107{
108    rt_env_detail::variable_data* vd = rt_env_detail::find_var_record( var_name );
109
110    BOOST_RT_PARAM_VALIDATE_LOGIC( !!vd,
111                                   BOOST_RT_PARAM_LITERAL( "First access to the environment variable " )
112                                        << var_name << BOOST_RT_PARAM_LITERAL( " should be typed" ) );
113
114    return variable_base( *vd );
115}
116
117//____________________________________________________________________________//
118
119} // namespace environment
120
121} // namespace BOOST_RT_PARAM_NAMESPACE
122
123} // namespace boost
124
125#endif // BOOST_RT_ENV_ENVIRONMENT_IPP_062904GER
126