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

📄 searchpath_sequence.hpp

📁 用STL的方式封装了WindowsAPI、COM调用、ACE、ATL、MFC、WTL等多种组件
💻 HPP
📖 第 1 页 / 共 2 页
字号:

        traits_type::get_environment_variable(_disgusting_hack("PATH", L"PATH"), &buffer[0], buffer.size());

        for(; begin != end; ++begin)
        {
            if(*begin != ';')
            {
                break;
            }
        }

        for(last = begin; begin != end; ++begin)
        {
            if(*begin == ';')
            {
                if(1 < begin - last)
                {
                    ++cPaths;
                }

                last = begin + 1;
            }
        }

        if(1 < begin - last)
        {
            ++cPaths;
        }

        return cPaths;
    }

    static ws_bool_t is_curr_dir_last_()
    {
        if( system_version::winnt() &&
            system_version::major() >= 5 &&
            system_version::minor() == 1)
        {
            ws_bool_t   res     =   false;
            HKEY        hkey;
            LRESULT     lRes    =   ::RegOpenKeyExW(HKEY_LOCAL_MACHINE
                                                ,   L"SYSTEM\\CurrentControlSet\\Control\\Session Manager"
                                                ,   0
                                                ,   KEY_QUERY_VALUE
                                                ,   &hkey);

            if(ERROR_SUCCESS == lRes)
            {
                DWORD   type;
                DWORD   data;
                DWORD   cbData  =   sizeof(data);

                lRes = ::RegQueryValueExW(hkey, L"SafeDllSearchMode", NULL, &type, reinterpret_cast<LPBYTE>(&data), &cbData);
                if(ERROR_SUCCESS == lRes)
                {
                    if(1 == data)
                    {
                        res = true;
                    }
                }

                ::RegCloseKey(hkey);
            }

            return res;
        }
        else
        {
            return false;
        }
    }

    // One to be ashamed of. This will be replaced in the next version of the libraries
    static char_type const *_disgusting_hack(ws_char_a_t *literal_a, ws_char_w_t *literal_w)
    {
#if defined(STLSOFT_COMPILER_IS_DMC)
        if(sizeof(char_type) == sizeof(ws_char_w_t))
        {
            return static_cast<char_type*>(static_cast<void*>(literal_w));
        }
        else
        {
            return static_cast<char_type*>(static_cast<void*>(literal_a));
        }
#else /* ? compiler */
        return static_cast<char_type*>((sizeof(char_type) == sizeof(ws_char_w_t)) ? static_cast<void*>(literal_w) : static_cast<void*>(literal_a));
#endif /* compiler */
    }

// Not to be implemented
private:
    basic_searchpath_sequence(class_type const &);
    const basic_searchpath_sequence &operator =(class_type const &);
};

/* /////////////////////////////////////////////////////////////////////////
 * Typedefs for commonly encountered types
 */

/// Instantiation of the basic_searchpath_sequence template for the ANSI character type \c char
typedef basic_searchpath_sequence<ws_char_a_t, filesystem_traits<ws_char_a_t> > searchpath_sequence_a;
/// Instantiation of the basic_searchpath_sequence template for the Unicode character type \c wchar_t
typedef basic_searchpath_sequence<ws_char_w_t, filesystem_traits<ws_char_w_t> > searchpath_sequence_w;
/// Instantiation of the basic_searchpath_sequence template for the Win32 character type \c TCHAR
typedef basic_searchpath_sequence<TCHAR, filesystem_traits<TCHAR> >             searchpath_sequence;

////////////////////////////////////////////////////////////////////////////
// Unit-testing

#ifdef STLSOFT_UNITTEST
#include "./unittest/searchpath_sequence_unittest_.h"
#endif /* STLSOFT_UNITTEST */

////////////////////////////////////////////////////////////////////////////
// Implementation

#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION

// basic_searchpath_sequence

template<   ss_typename_param_k C
        ,   ss_typename_param_k T
        >
inline basic_searchpath_sequence<C, T>::basic_searchpath_sequence()
    : m_buffer(directories_total())
    , m_values(num_paths() + (system_version::winnt() ? 5 : 4))
{
    construct_(true, true, true);
}

template<   ss_typename_param_k C
        ,   ss_typename_param_k T
        >
inline basic_searchpath_sequence<C, T>::basic_searchpath_sequence(ws_bool_t bIncludeApplicationDirectory, ws_bool_t bIncludeCurrentDirectory, ws_bool_t bApplicationDirectoryFirst /* = true */)

    : m_buffer(directories_total())
    , m_values(num_paths() + (system_version::winnt() ? 5 : 4) - (!bIncludeApplicationDirectory + !bIncludeCurrentDirectory))
{
    construct_(bIncludeApplicationDirectory, bIncludeCurrentDirectory, bApplicationDirectoryFirst);
}

template<   ss_typename_param_k C
        ,   ss_typename_param_k T
        >
inline void basic_searchpath_sequence<C, T>::construct_(ws_bool_t   bIncludeApplicationDirectory
                                                    ,   ws_bool_t   bIncludeCurrentDirectory
                                                    ,   ws_bool_t   bApplicationDirectoryFirst)
{
    // Determine whether the current directory must be relegated
    ws_bool_t   bIncludeCurrentDirectoryLast    =   bIncludeCurrentDirectory && is_curr_dir_last_();

    // 6 - n. Path directories

    char_type       *psz    =   &m_buffer[0];
    char_type const **it    =   &m_values[0];
    char_type const *cwd    =   NULL;

    psz[0] = '\0';

    { for(int i = 0; i < 2; ++i) {

        if((i & 1) != static_cast<int>(bApplicationDirectoryFirst))
        {
            if(bIncludeApplicationDirectory)
            {
                *it++ = psz;

                // 1. Application directory - GetModuleFileName(NULL, ...);
                traits_type::str_copy(psz, get_application_directory());
                psz += traits_type::str_len(psz);
            }
        }
        else
        {
            if(bIncludeCurrentDirectory)
            {
                cwd =   psz;

                // 2. Current directory - GetCurrentDirectory
                psz += traits_type::get_current_directory(_MAX_PATH + 1, psz);

                if(!bIncludeCurrentDirectoryLast)
                {
                    *it++ = cwd;
                }
            }
        }

        ++psz;
    }}

    // 3. System directory
    *it++ = psz;
    traits_type::str_copy(psz, get_system_directory());
    psz += traits_type::str_len(psz);
    ++psz;

    // 4. NT-only: 16-bit system directory
    if(system_version::winnt())
    {
        *it++ = psz;
        traits_type::str_copy(psz, get_system16_directory());
        psz += traits_type::str_len(psz);
        ++psz;
    }

    // 5. Windows directory
    *it++ = psz;
    traits_type::str_copy(psz, get_windows_directory());
    psz += traits_type::str_len(psz);
    ++psz;

    // 2.b. Current directory last?
    if( bIncludeCurrentDirectory &&
        bIncludeCurrentDirectoryLast)
    {
        *it++ = cwd;
    }

    // 6. Paths
    char_type const         *begin  =   psz;
    char_type const * const end     =   begin + traits_type::get_environment_variable(_disgusting_hack("PATH", L"PATH"), psz, static_cast<DWORD>(m_buffer.end() - psz));
    char_type const         *last;

    // Move along to the first valid item
    for(; begin != end; ++begin)
    {
        if(*begin != ';')
        {
            break;
        }
    }

    for(last = begin; begin != end; ++begin)
    {
        if(*begin == ';')
        {
            if(1 < begin - last)
            {
                *it++ = last;
            }

            *const_cast<char_type*>(begin) = '\0';

            last = begin + 1;
        }
    }

    if(1 < begin - last)
    {
        *it++ = last;
    }

    m_end   =   it;
}

template<   ss_typename_param_k C
        ,   ss_typename_param_k T
        >
inline basic_searchpath_sequence<C, T>::~basic_searchpath_sequence() stlsoft_throw_0()
{}

template<   ss_typename_param_k C
        ,   ss_typename_param_k T
        >
inline ss_typename_type_k basic_searchpath_sequence<C, T>::const_iterator basic_searchpath_sequence<C, T>::begin() const
{
    return &m_values[0];
}

template<   ss_typename_param_k C
        ,   ss_typename_param_k T
        >
inline ss_typename_type_k basic_searchpath_sequence<C, T>::const_iterator basic_searchpath_sequence<C, T>::end() const
{
    return m_end;
}

#if defined(STLSOFT_CF_BIDIRECTIONAL_ITERATOR_SUPPORT)
template<   ss_typename_param_k C
        ,   ss_typename_param_k T
        >
inline ss_typename_type_k basic_searchpath_sequence<C, T>::const_reverse_iterator basic_searchpath_sequence<C, T>::rbegin() const
{
    return const_reverse_iterator(end());
}

template<   ss_typename_param_k C
        ,   ss_typename_param_k T
        >
inline ss_typename_type_k basic_searchpath_sequence<C, T>::const_reverse_iterator basic_searchpath_sequence<C, T>::rend() const
{
    return const_reverse_iterator(begin());
}
#endif /* STLSOFT_CF_BIDIRECTIONAL_ITERATOR_SUPPORT */

template<   ss_typename_param_k C
        ,   ss_typename_param_k T
        >
inline ss_typename_type_k basic_searchpath_sequence<C, T>::size_type basic_searchpath_sequence<C, T>::size() const
{
    return static_cast<size_type>(end() - begin());
}

template<   ss_typename_param_k C
        ,   ss_typename_param_k T
        >
inline ws_bool_t basic_searchpath_sequence<C, T>::empty() const
{
    return begin() == end();
}

template<   ss_typename_param_k C
        ,   ss_typename_param_k T
        >
inline /* static */ ss_typename_type_k basic_searchpath_sequence<C, T>::size_type basic_searchpath_sequence<C, T>::max_size()
{
    // Kind of kludgy, sigh.
    return static_cast<size_type>(-1) / _MAX_PATH;
}

template<   ss_typename_param_k C
        ,   ss_typename_param_k T
        >
inline ss_typename_type_k basic_searchpath_sequence<C, T>::value_type basic_searchpath_sequence<C, T>::operator [](ss_typename_type_k basic_searchpath_sequence<C, T>::size_type index) const
{
    WINSTL_MESSAGE_ASSERT("Invalid index in search path sequence", !(size() < index));

    return begin()[index];
}

#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */

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

#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_SEARCHPATH_SEQUENCE */

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

⌨️ 快捷键说明

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