1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
// (C) Copyright Gennadiy Rozental 2005-2008.
// Use, modification, and distribution are subject to the
// Boost Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
// See http://www.boost.org/libs/test for the library home page.
//
// File : $RCSfile$
//
// Version : $Revision: 49312 $
//
// Description : implements models configuration file, it's parameter and parameter namespaces
// ***************************************************************************
// Boost.Runtime.Parameter
#include <boost/test/utils/runtime/config.hpp>
#include <boost/test/utils/runtime/file/config_file.hpp>
#include <boost/test/utils/runtime/validation.hpp>
// Boost.Test
#include <boost/test/utils/foreach.hpp>
#include <boost/test/utils/basic_cstring/basic_cstring.hpp>
#include <boost/test/utils/basic_cstring/io.hpp>
#include <boost/test/utils/iterator/token_iterator.hpp>
namespace boost {
namespace BOOST_RT_PARAM_NAMESPACE {
namespace file {
// ************************************************************************** //
// ************** runtime::file::parameter ************** //
// ************************************************************************** //
parameter::parameter( cstring name, cstring value, param_namespace const& parent )
: m_parent( parent )
{
assign_op( p_name.value, name, 0 );
assign_op( p_value.value, value, 0 );
}
//____________________________________________________________________________//
std::ostream&
operator<<( std::ostream& os, parameter const& p )
{
p.m_parent.print_full_name( os );
return os << p.p_name << " = \"" << p.p_value << "\"";
}
//____________________________________________________________________________//
// ************************************************************************** //
// ************** runtime::file::param_namespace ************** //
// ************************************************************************** //
param_namespace::param_namespace( cstring name, param_namespace const* parent )
: p_parent( parent )
{
assign_op( p_name.value, name );
}
//____________________________________________________________________________//
void
param_namespace::insert_param( cstring param_name, cstring param_value )
{
BOOST_TEST_FOREACH( parameter const&, p, m_parameters )
BOOST_RT_PARAM_VALIDATE_LOGIC( p.p_name != param_name,
BOOST_RT_PARAM_LITERAL( "Duplicate parameter " ) << param_name );
m_parameters.push_back( parameter( param_name, param_value, *this ) );
}
//____________________________________________________________________________//
param_namespace&
param_namespace::subnamespace( cstring namespace_name )
{
BOOST_TEST_FOREACH( param_namespace&, subns, m_subnamespaces )
if( subns.p_name == namespace_name )
return subns;
m_subnamespaces.push_back( param_namespace( namespace_name, this ) );
return m_subnamespaces.back();
}
//____________________________________________________________________________//
void
param_namespace::clear()
{
m_parameters.clear();
m_subnamespaces.clear();
}
//____________________________________________________________________________//
void
param_namespace::print_full_name( std::ostream& os ) const
{
if( !p_parent )
return;
p_parent.get()->print_full_name( os );
os << p_name << "::";
}
//____________________________________________________________________________//
boost::optional<cstring>
get_param_value( param_namespace const& where_from,
cstring name_part1,
cstring name_part2,
cstring name_part3,
cstring name_part4,
cstring name_part5 )
{
if( name_part2.is_empty() ) {
boost::optional<cstring> res;
BOOST_TEST_FOREACH( parameter const&, p, where_from ) {
if( p.p_name == name_part1 ) {
res = cstring( p.p_value );
break;
}
}
return res;
}
param_namespace const* sns = get_param_subns( where_from, name_part1 );
return sns ? get_param_value( *sns, name_part2, name_part3, name_part4, name_part5 )
: boost::optional<cstring>();
}
//____________________________________________________________________________//
cstring
get_requ_param_value( param_namespace const& where_from,
cstring name_part1,
cstring name_part2,
cstring name_part3,
cstring name_part4,
cstring name_part5 )
{
boost::optional<cstring> v = get_param_value( where_from, name_part1, name_part2, name_part3, name_part4, name_part5 );
#define APPEND_PART( part ) (part.is_empty() ? "" : "::") << (part.is_empty() ? cstring() : part)
BOOST_RT_PARAM_VALIDATE_LOGIC( !!v, BOOST_RT_PARAM_LITERAL( "Required parameter " )
<< name_part1
<< APPEND_PART( name_part2 )
<< APPEND_PART( name_part3 )
<< APPEND_PART( name_part4 )
<< APPEND_PART( name_part5 )
<< BOOST_RT_PARAM_LITERAL( " value is missing" ) );
#undef APPEND_PART
return *v;
}
//____________________________________________________________________________//
param_namespace const*
get_param_subns( param_namespace const& where_from, cstring namespace_name )
{
param_namespace::sub_ns_const_iterator it = where_from.sub_ns_begin();
param_namespace::sub_ns_const_iterator end = where_from.sub_ns_end();
while( it != end ) {
if( it->p_name == namespace_name )
return &*it;
++it;
}
return 0;
}
//____________________________________________________________________________//
void
param_namespace::load_impl( config_file_iterator cf_it,
cstring value_marker, cstring value_delimeter, cstring namespace_delimeter )
{
using namespace unit_test;
while( cf_it != config_file_iterator() ) {
string_token_iterator ti( *cf_it, (max_tokens = 2,kept_delimeters = dt_none, dropped_delimeters = value_delimeter) );
cstring param_name = *ti;
cstring param_value = *(++ti);
param_value.trim( value_marker );
param_namespace* targ_ns = this;
while( !param_name.is_empty() ) {
cstring::size_type pos = param_name.find( namespace_delimeter );
cstring subname( param_name.begin(), pos == cstring::npos ? param_name.size() : pos );
if( subname.size() == param_name.size() ) {
targ_ns->insert_param( param_name, param_value );
break;
}
else {
targ_ns = &targ_ns->subnamespace( subname );
param_name.trim_left( subname.size() + namespace_delimeter.size() );
}
}
++cf_it;
}
}
//____________________________________________________________________________//
// ************************************************************************** //
// ************** runtime::file::config_file ************** //
// ************************************************************************** //
config_file::config_file()
: param_namespace( cstring() )
{
}
//____________________________________________________________________________//
config_file::config_file( cstring file_name )
: param_namespace( cstring() )
{
load( file_name );
}
//____________________________________________________________________________//
} // namespace file
} // namespace BOOST_RT_PARAM_NAMESPACE
} // namespace boost
// EOF
|