📄 environment_block.hpp
字号:
WINSTL_ASSERT(cch >= 3);
WINSTL_ASSERT(NULL != traits_type::find(variable, cch, '='));
size_type oldSize = m_buffer.size();
WINSTL_ASSERT(m_buffer.size() > 1);
WINSTL_ASSERT('\0' == m_buffer[m_buffer.size() - 1]);
WINSTL_ASSERT('\0' == m_buffer[m_buffer.size() - 2]);
if(!m_buffer.resize(oldSize + cch + 1))
{
return false;
}
else
{
traits_type::copy(&m_buffer[oldSize - 1], variable, cch);
m_buffer[m_buffer.size() - 2] = '\0';
m_buffer[m_buffer.size() - 1] = '\0';
WINSTL_ASSERT(m_buffer.size() > 1);
WINSTL_ASSERT('\0' == m_buffer[m_buffer.size() - 1]);
WINSTL_ASSERT('\0' == m_buffer[m_buffer.size() - 2]);
return true;
}
}
/// \brief Append a NAME= environment variable
///
/// \param variable The variable
///
/// \return An indication of success. This will always return true when
/// compiling with exception support.
///
/// \note The variable must contain an equal sign ('=')
///
/// \exception std::bad_alloc When compiling with exception support, this will throw
/// std::bad_alloc if memory cannot be acquired. When compiling absent
/// exception support, failure to acquire memory will cause the method
/// to return false.
template <ss_typename_param_k S>
ws_bool_t push_back(S const& variable)
{
return push_back(stlsoft_ns_qual(c_str_data)(variable), stlsoft_ns_qual(c_str_len)(variable));
}
/// \brief Append a full NAME=VALUE environment pair
///
/// \param name The variable name
/// \param cchName The length of the variable name
/// \param value The variable value
/// \param cchValue The length of the variable value
///
/// \return An indication of success. This will always return true when
/// compiling with exception support.
///
/// \exception std::bad_alloc When compiling with exception support, this will throw
/// std::bad_alloc if memory cannot be acquired. When compiling absent
/// exception support, failure to acquire memory will cause the method
/// to return false.
ws_bool_t push_back(char_type const* name, ws_size_t cchName, char_type const* value, ws_size_t cchValue)
{
WINSTL_ASSERT(NULL != name);
WINSTL_ASSERT(NULL != value);
WINSTL_ASSERT(cchName > 1);
// WINSTL_ASSERT(cchValue > 1);
size_type oldSize = m_buffer.size();
WINSTL_ASSERT(m_buffer.size() > 1);
WINSTL_ASSERT('\0' == m_buffer[m_buffer.size() - 1]);
WINSTL_ASSERT('\0' == m_buffer[m_buffer.size() - 2]);
if(!m_buffer.resize(oldSize + cchName + 1 + cchValue + 1))
{
return false;
}
else
{
traits_type::copy(&m_buffer[oldSize - 2], name, cchName);
m_buffer[oldSize - 2 + cchName] = '=';
traits_type::copy(&m_buffer[oldSize - 2 + cchName + 1], value, cchValue);
m_buffer[oldSize - 2 + cchName + 1 + cchValue] = '\0';
m_buffer[m_buffer.size() - 2] = '\0';
m_buffer[m_buffer.size() - 1] = '\0';
WINSTL_ASSERT(m_buffer.size() > 1);
WINSTL_ASSERT('\0' == m_buffer[m_buffer.size() - 1]);
WINSTL_ASSERT('\0' == m_buffer[m_buffer.size() - 2]);
return true;
}
}
/// \brief Append a full NAME=VALUE environment pair
///
/// \param name The variable name
/// \param value The variable value
///
/// \return An indication of success. This will always return true when
/// compiling with exception support.
///
/// \exception std::bad_alloc When compiling with exception support, this will throw
/// std::bad_alloc if memory cannot be acquired. When compiling absent
/// exception support, failure to acquire memory will cause the method
/// to return false.
template< ss_typename_param_k S1
, ss_typename_param_k S2
>
ws_bool_t push_back(S1 const& name, S2 const& value)
{
return push_back(stlsoft_ns_qual(c_str_data)(name), stlsoft_ns_qual(c_str_len)(name), stlsoft_ns_qual(c_str_data)(value), stlsoft_ns_qual(c_str_len)(value));
}
/// Empties the block of all variables
void clear()
{
m_buffer.resize(2);
m_buffer[0] = '\0';
m_buffer[1] = '\0';
}
/// Swaps the contents of the two instances
void swap(class_type& rhs) stlsoft_throw_0()
{
m_buffer.swap(rhs.m_buffer);
}
/// @}
/// \name Accessors
/// @{
public:
/// Returns a pointer to the block contents
void const *base() const
{
return m_buffer.data();
}
/// The number of characters in the block
size_type size() const
{
return m_buffer.size();
}
/// The number of characters in the block
///
/// \note This method is a synonym for size()
size_type length() const
{
return size();
}
/// @}
/** \brief Members
*
* \ingroup group__library__system
*/
private:
typedef stlsoft_ns_qual(auto_buffer_old)< char_type
, allocator_type
, 1024
> buffer_type_;
buffer_type_ m_buffer;
};
/* /////////////////////////////////////////////////////////////////////////
* Typedefs for commonly encountered types
*/
#ifdef STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT
/// Specialisation of the basic_path template for the ANSI character type \c char
typedef basic_environment_block<ws_char_a_t> environment_block_a;
/** \brief Specialisation of the basic_environment_block template for the Unicode character type \c wchar_t
*
* \ingroup group__library__system
*/
typedef basic_environment_block<ws_char_w_t> environment_block_w;
/** \brief Specialisation of the basic_environment_block template for the Win32 character type \c TCHAR
*
* \ingroup group__library__system
*/
typedef basic_environment_block<TCHAR> environment_block;
#endif /* STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT */
/* /////////////////////////////////////////////////////////////////////////
* Unit-testing
*/
#ifdef STLSOFT_UNITTEST
# include "./unittest/environment_block_unittest_.h"
#endif /* STLSOFT_UNITTEST */
/* /////////////////////////////////////////////////////////////////////////
* Implementation
*/
/* ////////////////////////////////////////////////////////////////////// */
#ifndef _WINSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
} // namespace winstl
# else
} // namespace winstl_project
} // namespace stlsoft
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_WINSTL_NO_NAMESPACE */
/* ////////////////////////////////////////////////////////////////////// */
#endif /* WINSTL_INCL_WINSTL_SYSTEM_HPP_ENVIRONMENT_BLOCK */
/* ////////////////////////////////////////////////////////////////////// */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -