📄 file_path_buffer.hpp
字号:
/* /////////////////////////////////////////////////////////////////////////
* File: winstl/filesystem/file_path_buffer.hpp (originally winstl_file_path_buffer.h)
*
* Purpose: Contains the basic_file_path_buffer template class, and ANSI
* and Unicode specialisations thereof.
*
* Created: 7th February 2002
* Updated: 13th June 2006
*
* Thanks to: Pablo Aguilar for discovering the Borland weirdness which is now
* addressed with the calc_path_max_() method.
*
* Home: http://stlsoft.org/
*
* Copyright (c) 2002-2006, Matthew Wilson and Synesis Software
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
* any contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* ////////////////////////////////////////////////////////////////////// */
/** \file winstl/filesystem/file_path_buffer.hpp
*
* \brief [C++ only] Definition of the winstl::basic_file_path_buffer class
* template.
* (\ref group__library__file_system "File System" Library.)
*/
#ifndef WINSTL_INCL_WINSTL_FILESYSTEM_HPP_FILE_PATH_BUFFER
#define WINSTL_INCL_WINSTL_FILESYSTEM_HPP_FILE_PATH_BUFFER
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define WINSTL_VER_WINSTL_FILESYSTEM_HPP_FILE_PATH_BUFFER_MAJOR 4
# define WINSTL_VER_WINSTL_FILESYSTEM_HPP_FILE_PATH_BUFFER_MINOR 1
# define WINSTL_VER_WINSTL_FILESYSTEM_HPP_FILE_PATH_BUFFER_REVISION 1
# define WINSTL_VER_WINSTL_FILESYSTEM_HPP_FILE_PATH_BUFFER_EDIT 100
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* Compatibility
*/
/*
[Incompatibilies-start]
STLSOFT_COMPILER_IS_MSVC: _MSC_VER<1200
[Incompatibilies-end]
*/
/* /////////////////////////////////////////////////////////////////////////
* Includes
*/
#ifndef WINSTL_INCL_WINSTL_H_WINSTL
# include <winstl/winstl.h>
#endif /* !WINSTL_INCL_WINSTL_H_WINSTL */
#define WINSTL_FILE_PATH_BUFFER_USE_AUTO_BUFFER
#if defined(STLSOFT_COMPILER_IS_MSVC)
# if _MSC_VER < 1100
# error winstl/filesystem/file_path_buffer.hpp is not compatible with Visual C++ prior to version 5.0
# elif _MSC_VER < 1200
# undef WINSTL_FILE_PATH_BUFFER_USE_AUTO_BUFFER
# else /* ? _MSC_VER */
/* Nothing to do */
# endif /* _MSC_VER */
#endif /* compiler */
#ifndef STLSOFT_INCL_STLSOFT_HPP_STRING_ACCESS
# include <stlsoft/string_access.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_HPP_STRING_ACCESS */
#ifdef WINSTL_FILE_PATH_BUFFER_USE_AUTO_BUFFER
# ifndef STLSOFT_INCL_STLSOFT_HPP_MEMORY_AUTO_BUFFER
# include <stlsoft/memory/auto_buffer.hpp>
# endif /* !STLSOFT_INCL_STLSOFT_HPP_MEMORY_AUTO_BUFFER */
#else /* ? WINSTL_FILE_PATH_BUFFER_USE_AUTO_BUFFER */
# include <vector>
#endif /* WINSTL_FILE_PATH_BUFFER_USE_AUTO_BUFFER */
#ifndef WINSTL_INCL_WINSTL_MEMORY_HPP_PROCESSHEAP_ALLOCATOR
# include <winstl/memory/processheap_allocator.hpp>
#endif /* !WINSTL_INCL_WINSTL_MEMORY_HPP_PROCESSHEAP_ALLOCATOR */
#ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_COPY_FUNCTIONS
# include <stlsoft/string/copy_functions.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_COPY_FUNCTIONS */
/* /////////////////////////////////////////////////////////////////////////
* Namespace
*/
#ifndef _WINSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
/* There is no stlsoft namespace, so must define ::winstl */
namespace winstl
{
# else
/* Define stlsoft::winstl_project */
namespace stlsoft
{
namespace winstl_project
{
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_WINSTL_NO_NAMESPACE */
/* /////////////////////////////////////////////////////////////////////////
* Classes
*/
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
template <ss_typename_param_k C>
struct is_ansi
{
enum { value = 0 };
};
STLSOFT_TEMPLATE_SPECIALISATION
struct is_ansi<ws_char_a_t>
{
enum { value = 1 };
};
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
// class basic_file_path_buffer
/** \brief Acts as a buffer with sufficient size for any drive on the host machine
*
* \ingroup group__library__file_system
*
* This class is a non-template class primarily so that separate instantiations
* are not created for each instantiation of the basic_file_path_buffer.
*
* This class provides a simple function, which is to provide the maximum path
* length for the host. This information is then cached due to the static nature
* of the get_drivesvar_() method, although it can be reset by calling the
* refresh() method on the buffer class.
*
* \param C The character type
* \param A The allocator type
* \param CCH The size of the internal member path structure. On translators that support default template arguments this default to (\c 1 + \c _MAX_PATH)
*/
template< ss_typename_param_k C
#ifdef STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT
, ss_typename_param_k A = processheap_allocator<C>
#else /* ? STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT */
, ss_typename_param_k A /* = processheap_allocator<C> */
#endif /* STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT */
>
class basic_file_path_buffer
{
/// \name Member Constants
/// @{
private:
enum
{
internalBufferSize = 1 + _MAX_PATH
};
enum
{
CCH_9x = _MAX_PATH
, CCH_NT = (is_ansi<C>::value ? _MAX_PATH : CONST_NT_MAX_PATH)
};
/// \brief The buffer type
#ifdef WINSTL_FILE_PATH_BUFFER_USE_AUTO_BUFFER
typedef stlsoft_ns_qual(auto_buffer)< C
# if defined(STLSOFT_COMPILER_IS_BORLAND)
// This is necessary, since Borland will attempt an auto_buffer with what
// seems like 0 size, or maybe it just can't define the type. Who can tell?
, 1 + _MAX_PATH
# else /* ? compiler */
, internalBufferSize
# endif /* compiler */
, A
> buffer_type;
#else /* ? WINSTL_FILE_PATH_BUFFER_USE_AUTO_BUFFER */
typedef stlsoft_ns_qual_std(vector)<C> buffer_type;
#endif /* WINSTL_FILE_PATH_BUFFER_USE_AUTO_BUFFER */
/// @}
/// \name Member Types
/// @{
public:
/// \brief The character type
typedef C char_type;
/// \brief The allocator type
typedef A allocator_type;
/// \brief The current parameterisation of the type
typedef basic_file_path_buffer<C, A> class_type;
/// \brief The value type
typedef ss_typename_type_k buffer_type::value_type value_type;
/// \brief The reference type
typedef value_type &reference;
/// \brief The non-mutating (const) reference type
typedef value_type const &const_reference;
/// \brief The size type
typedef ss_typename_type_k buffer_type::size_type size_type;
/// @}
/// \name Construction
/// @{
public:
/// \brief Default constructor
basic_file_path_buffer()
: m_buffer(1 + calc_path_max_())
{
#ifdef _DEBUG
::memset(&m_buffer[0], '?', m_buffer.size());
m_buffer[m_buffer.size() - 1] = '\0';
#endif /* _DEBUG */
}
/// \brief Copy constructor
basic_file_path_buffer(class_type const &rhs)
: m_buffer(rhs.size())
{
stlsoft_ns_qual(pod_copy_n)(data(), rhs.data(), m_buffer.size());
}
/// \brief Copy assignment operator
class_type &operator =(class_type const &rhs)
{
m_buffer.resize(rhs.size());
stlsoft_ns_qual(pod_copy_n)(data(), rhs.data(), m_buffer.size());
return *this;
}
/// @}
/// \name Operations
/// @{
public:
/// \brief Swaps the contents with those of another instance
///
/// \param rhs The instance whose contents will be swapped with the
/// callee
///
/// \note The complexity of this operation is not guaranteed
/// to be constant-time. See the documentation for
/// \link stlsoft::auto_buffer auto_buffer\endlink for
/// further details.
void swap(class_type &rhs) stlsoft_throw_0()
{
m_buffer.swap(rhs.m_buffer);
}
/// @}
/// \name Accessors
/// @{
public:
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
value_type const *data() const
{
#ifdef WINSTL_FILE_PATH_BUFFER_USE_AUTO_BUFFER
return m_buffer.data();
#else /* ? WINSTL_FILE_PATH_BUFFER_USE_AUTO_BUFFER */
return &m_buffer[0];
#endif /* WINSTL_FILE_PATH_BUFFER_USE_AUTO_BUFFER */
}
value_type *data()
{
#ifdef WINSTL_FILE_PATH_BUFFER_USE_AUTO_BUFFER
return m_buffer.data();
#else /* ? WINSTL_FILE_PATH_BUFFER_USE_AUTO_BUFFER */
return &m_buffer[0];
#endif /* WINSTL_FILE_PATH_BUFFER_USE_AUTO_BUFFER */
}
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/// \brief Returns a pointer to a nul-terminated string
value_type const *c_str() const
{
return this->data();
}
/// \brief Returns a mutable (non-const) pointer to the internal buffer
reference operator [](size_t index)
{
buffer_type &this_ = m_buffer;
return this_[index];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -