⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 environment_variable_traits.hpp

📁 新版本TR1的stl
💻 HPP
📖 第 1 页 / 共 2 页
字号:

namespace stlsoft
{

namespace platformstl_project
{
#endif /* _STLSOFT_NO_NAMESPACE */

/* /////////////////////////////////////////////////////////////////////////
 * Classes
 */

/** \brief Abstraction of the platform-dependent environment variable
 *   handling.
 *
 * \ingroup group__library__system
 */
struct environment_variable_traits
{
/// \name Member Types
/// @{
public:
    /// \brief The character type
    typedef char        char_type;
/// @}

/// \name Operations
/// @{
public:
#ifdef PLATFORMSTL_ENVVAR_HAS_ENVIRON
    /// \brief Returns a pointer to the environment block pointer.
    ///
    /// \note The returned pointer must be passed back to release_environ().
    static char_type const* *get_environ();
    /// \brief Releases any allocation performed by get_environ().
    ///
    /// \param env The pointer returned in a previous call to get_environ().
    static void release_environ(char_type const** env) throw();

#endif /* PLATFORMSTL_ENVVAR_HAS_ENVIRON */

    /// \brief Returns a pointer to the value of the given variable, or NULL if
    /// the variable does not exist
    ///
    /// \param name The name of the variable whose value will be retrieved
    static char_type const* get_variable(char_type const* name) throw();

#ifdef PLATFORMSTL_ENVVAR_SET_SUPPORTED
    /// \brief Creates or updates the given variable to the given value
    ///
    /// \param name The name of the variable to create or update
    /// \param value The new value of the variable
    ///
    /// \return A status code indicating success
    /// \retval 0 The operation completed successfully
    /// \retval !0 The operation failed
    static int set_variable(char_type const* name, char_type const* value) throw();
#endif /* PLATFORMSTL_ENVVAR_SET_SUPPORTED */
#ifdef PLATFORMSTL_ENVVAR_ERASE_SUPPORTED
    /// \brief Removed the given variable
    ///
    /// \param name The name of the variable to remove
    ///
    /// \note Erasing a variable that does not exist is a succesful operation
    ///
    /// \return A status code indicating success
    /// \retval 0 The operation completed successfully
    /// \retval !0 The operation failed
    static int erase_variable(char_type const* name) throw();
#endif /* PLATFORMSTL_ENVVAR_ERASE_SUPPORTED */
/// @}

/// \name Implementation
/// @{
private:
#if defined(PLATFORMSTL_ENVVAR_SET_BY_PUTENV) || \
    defined(PLATFORMSTL_ENVVAR_ERASE_BY_PUTENV)
    static int          call_putenv_(char_type const* str) throw();
    static int          call_putenv_(char_type const* name, char_type const* value) throw();
#endif /* putenv ? */
/// @}
};

/* /////////////////////////////////////////////////////////////////////////
 * Implementation
 */

#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION

#ifdef PLATFORMSTL_ENVVAR_HAS_ENVIRON
inline /* static */ environment_variable_traits::char_type const* *environment_variable_traits::get_environ()
{
# ifdef PLATFORMSTL_ENVVAR_ENVIRON_HAS_UNDERSCORE
    return const_cast<char_type const**>(_environ);
# else /* ? PLATFORMSTL_ENVVAR_ENVIRON_HAS_UNDERSCORE */
    return const_cast<char_type const**>(environ);
# endif /* PLATFORMSTL_ENVVAR_ENVIRON_HAS_UNDERSCORE */
}

inline /* static */ void environment_variable_traits::release_environ(environment_variable_traits::char_type const**) throw()
{}
#endif /* PLATFORMSTL_ENVVAR_HAS_ENVIRON */

inline /* static */ environment_variable_traits::char_type const* environment_variable_traits::get_variable(environment_variable_traits::char_type const* name) throw()
{
    STLSOFT_ASSERT(NULL != name);
    STLSOFT_MESSAGE_ASSERT("Name may not contain '='", NULL == ::strchr(name, '='));

    return const_cast<char_type const*>(::getenv(name));
}

#ifdef PLATFORMSTL_ENVVAR_SET_SUPPORTED
inline /* static */ int environment_variable_traits::set_variable(environment_variable_traits::char_type const* name, environment_variable_traits::char_type const* value) throw()
{
    STLSOFT_ASSERT(NULL != name);
    STLSOFT_ASSERT(NULL != value);
    STLSOFT_MESSAGE_ASSERT("Name may not contain '='", NULL == ::strchr(name, '='));

#if defined(PLATFORMSTL_ENVVAR_SET_BY_PUTENV)
    return call_putenv_(name, value);
#elif defined(PLATFORMSTL_ENVVAR_SET_BY_SETENV)
    return ::setenv(name, value, 1);
#endif /* unsetenv */
}
#endif /* PLATFORMSTL_ENVVAR_SET_SUPPORTED */

#ifdef PLATFORMSTL_ENVVAR_ERASE_SUPPORTED
inline /* static */ int environment_variable_traits::erase_variable(environment_variable_traits::char_type const* name) throw()
{
    STLSOFT_ASSERT(NULL != name);
    STLSOFT_MESSAGE_ASSERT("Name may not contain '='", NULL == ::strchr(name, '='));

#if defined(PLATFORMSTL_ENVVAR_ERASE_BY_UNSETENV)
    static_cast<void>(::unsetenv(name));    // Some definitions of unsetenv() return void

    return 0;
#else /* ? unsetenv */
    return call_putenv_(name, NULL);
#endif /* unsetenv */
}
#endif /* PLATFORMSTL_ENVVAR_ERASE_SUPPORTED */

#if defined(PLATFORMSTL_ENVVAR_SET_BY_PUTENV) || \
    defined(PLATFORMSTL_ENVVAR_ERASE_BY_PUTENV)

inline /* static */ int environment_variable_traits::call_putenv_(environment_variable_traits::char_type const* str) throw()
{
#ifdef PLATFORMSTL_ENVVAR_PUTENV_HAS_UNDERSCORE
    return ::_putenv(str);
#else /* ? compiler */
    return ::putenv(str);
#endif /* compiler */
}

inline /* static */ int environment_variable_traits::call_putenv_(environment_variable_traits::char_type const* name, environment_variable_traits::char_type const* value) throw()
{
    STLSOFT_ASSERT(NULL != name);
    STLSOFT_ASSERT(NULL == ::strchr(name, '='));

#ifndef PLATFORMSTL_ENVVAR_ERASE_BY_PUTENV_EQUALS
    if(NULL == value)
    {
        return call_putenv_(name);
    }
    else
#endif /* !PLATFORMSTL_ENVVAR_ERASE_BY_PUTENV_EQUALS */
    {
#ifdef STLSOFT_CF_THROW_BAD_ALLOC
        try
#endif /* STLSOFT_CF_THROW_BAD_ALLOC */
        {
            const ss_size_t                 cchName     =   ::strlen(name);
            const ss_size_t                 cchValue    =   stlsoft::c_str_len(value);
            stlsoft::auto_buffer<char_type> buff(cchName + 1 + cchValue + 1);

#ifdef STLSOFT_CF_THROW_BAD_ALLOC
            STLSOFT_ASSERT(!buff.empty());
#else /* ? STLSOFT_CF_THROW_BAD_ALLOC */
            if(buff.empty()) // This check worthwhile since implementation of ator may not support bad_alloc
            {
                errno = ENOMEM;
                return -1;
            }
            else
#endif /* STLSOFT_CF_THROW_BAD_ALLOC */
            {
                ::strncpy(&buff[0], name, cchName);
                buff[cchName] = '=';
                ::strncpy(&buff[cchName + 1], value, cchValue);
                buff[cchName + 1 + cchValue] = '\0';
                STLSOFT_ASSERT(::strlen(buff.data()) == buff.size() - 1);

                return call_putenv_(buff.data());
            }
        }
#ifdef STLSOFT_CF_THROW_BAD_ALLOC
        catch(std::bad_alloc &)
        {
            errno = ENOMEM;
            return -1;
        }
#endif /* STLSOFT_CF_THROW_BAD_ALLOC */
    }
}
#endif /* putenv ? */

#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */

/* ////////////////////////////////////////////////////////////////////// */

#if defined(_STLSOFT_NO_NAMESPACE) || \
    defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
} // namespace platformstl
#else
} // namespace platformstl_project
} // namespace stlsoft
#endif /* _STLSOFT_NO_NAMESPACE */

/* ////////////////////////////////////////////////////////////////////// */

#endif /* !PLATFORMSTL_INCL_PLATFORMSTL_SYSTEM_HPP_ENVIRONMENT_TRAITS */

/* ////////////////////////////////////////////////////////////////////// */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -