📄 char_conversions.hpp
字号:
/* /////////////////////////////////////////////////////////////////////////
* File: winstl/conversion/char_conversions.hpp (originally MLStrCnv.h, ::SynesisStd)
*
* Purpose: Type conversions for Windows.
*
* Created: 31st May 2003
* Updated: 22nd March 2007
*
* Home: http://stlsoft.org/
*
* Copyright (c) 2003-2007, 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/conversion/char_conversions.hpp
*
* \brief [C++ only] Definition of the winstl::multibyte2wide and
* winstl::wide2multibyte class templates
* (\ref group__library__conversion "Conversion" Library).
*/
#ifndef WINSTL_INCL_WINSTL_CONVERSION_HPP_CHAR_CONVERSIONS
#define WINSTL_INCL_WINSTL_CONVERSION_HPP_CHAR_CONVERSIONS
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define WINSTL_VER_WINSTL_CONVERSION_HPP_CHAR_CONVERSIONS_MAJOR 5
# define WINSTL_VER_WINSTL_CONVERSION_HPP_CHAR_CONVERSIONS_MINOR 1
# define WINSTL_VER_WINSTL_CONVERSION_HPP_CHAR_CONVERSIONS_REVISION 4
# define WINSTL_VER_WINSTL_CONVERSION_HPP_CHAR_CONVERSIONS_EDIT 85
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* Compatibility
*/
/*
[Incompatibilies-start]
STLSOFT_COMPILER_IS_GCC: __GNUC__<3
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 */
#if defined(STLSOFT_COMPILER_IS_GCC) && \
__GNUC__ < 3
# error winstl/conversion/char_conversions.hpp is not compatible with GNU C++ prior to 3.0
#endif /* compiler */
#if defined(STLSOFT_COMPILER_IS_MSVC) && \
_MSC_VER < 1100
# error winstl/conversion/char_conversions.hpp is not compatible with Visual C++ 5.0 or earlier
#endif /* compiler */
#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_SHIMS_ACCESS_HPP_STRING
# include <stlsoft/shims/access/string.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING */
#ifndef WINSTL_INCL_WINSTL_SHIMS_ACCESS_HPP_STRING
# include <winstl/shims/access/string.hpp>
#endif /* !WINSTL_INCL_WINSTL_SHIMS_ACCESS_HPP_STRING */
#ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_AUTO_BUFFER
# include <stlsoft/memory/auto_buffer.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_MEMORY_HPP_AUTO_BUFFER */
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
# ifndef WINSTL_INCL_WINSTL_ERROR_HPP_CONVERSION_ERROR
# include <winstl/error/conversion_error.hpp>
# endif /* !WINSTL_INCL_WINSTL_ERROR_HPP_CONVERSION_ERROR */
# include <errno.h>
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
#ifdef STLSOFT_UNITTEST
# include <wchar.h>
#endif /* STLSOFT_UNITTEST */
/* ////////////////////////////////////////////////////////////////////// */
#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
*/
/** \brief Converts a multibyte (<code>char</code>-based) string to a wide
* (<code>whar_t</code>-based) string.
*
* \ingroup group__library__conversion
*/
template <ws_size_t CCH>
class multibyte2wide
: public auto_buffer_old<ws_char_w_t, processheap_allocator<ws_char_w_t>, CCH>
{
/// \name Member Types
/// @{
private:
typedef auto_buffer_old<ws_char_w_t, processheap_allocator<ws_char_w_t>, CCH> parent_class_type;
public:
/// \brief The character type
typedef ws_char_w_t char_type;
/// \brief The alternate character type
typedef ws_char_a_t alt_char_type;
/// \brief The size type
typedef ss_typename_type_k parent_class_type::size_type size_type;
/// \brief The pointer type
typedef ss_typename_type_k parent_class_type::pointer pointer;
/// @}
/// \name Construction
/// @{
public:
#ifdef STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
template <ss_typename_param_k S>
ss_explicit_k multibyte2wide(S const& s)
#else
ss_explicit_k multibyte2wide(alt_char_type const* s)
#endif // STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
: parent_class_type(stlsoft_ns_qual(c_str_len)(s) + 1)
{
prepare_(stlsoft_ns_qual(c_str_ptr_a)(s));
}
#ifdef STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
template <ss_typename_param_k S>
multibyte2wide(S const& s, size_type cch)
#else
multibyte2wide(alt_char_type const* s, size_type cch)
#endif // STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
: parent_class_type(cch + 1)
{
prepare_(stlsoft_ns_qual(c_str_ptr_a)(s));
}
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
/// @}
/// \name Implementation
/// @{
private:
void prepare_(alt_char_type const* s)
{
const size_type size = parent_class_type::size();
const pointer data = parent_class_type::data();
// If the auto_buffer failed to allocate the required memory, and
// we're not in an exception-environment, then size() will be zero
if(0 == size)
{
// Since we know that auto_buffer's parameterising size must
// always be greater that 0, then
data[0] = '\0';
}
else
{
// Note: cannot use -1 for length, since s might be a type that
// could change length
if(0 == ::MultiByteToWideChar(0, 0, s, static_cast<int>(size), data, static_cast<int>(size)))
{
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
STLSOFT_THROW_X(conversion_error("failed to convert multibyte string to wide string", ::GetLastError()));
#else /* ? STLSOFT_CF_EXCEPTION_SUPPORT */
data[0] = '\0';
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
}
else
{
data[size - 1] = '\0';
}
}
}
/// @}
/// \name Accessors
/// @{
public:
char_type const* c_str() const
{
return parent_class_type::data();
}
/// @}
/// \name Operators
/// @{
public:
operator char_type const* () const
{
return parent_class_type::data();
}
/// @}
/// \name Not to be implemented
/// @{
private:
multibyte2wide(multibyte2wide const&);
multibyte2wide& operator =(multibyte2wide const&);
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/// @}
};
/** \brief Converts a wide (<code>whar_t</code>-based) string to a
* multibyte (<code>char</code>-based) string.
*
* \ingroup group__library__conversion
*/
template <ws_size_t CCH>
class wide2multibyte
: public auto_buffer_old<ws_char_a_t, processheap_allocator<ws_char_a_t>, CCH>
{
/// \name Member Types
/// @{
private:
typedef auto_buffer_old<ws_char_a_t, processheap_allocator<ws_char_a_t>, CCH> parent_class_type;
public:
/// \brief The character type
typedef ws_char_a_t char_type;
/// \brief The alternate character type
typedef ws_char_w_t alt_char_type;
/// \brief The size type
typedef ss_typename_type_k parent_class_type::size_type size_type;
/// \brief The pointer type
typedef ss_typename_type_k parent_class_type::pointer pointer;
/// @}
/// \name Construction
/// @{
public:
#ifdef STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
template <ss_typename_param_k S>
ss_explicit_k wide2multibyte(S const& s)
#else
ss_explicit_k wide2multibyte(alt_char_type const* s)
#endif // STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
: parent_class_type(stlsoft_ns_qual(c_str_len)(s) + 1)
{
prepare_(stlsoft_ns_qual(c_str_ptr_w)(s));
}
#ifdef STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
template <ss_typename_param_k S>
ss_explicit_k wide2multibyte(S const& s, size_type cch)
#else
ss_explicit_k wide2multibyte(alt_char_type const* s, size_type cch)
#endif // STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
: parent_class_type(cch + 1)
{
prepare_(stlsoft_ns_qual(c_str_ptr_w)(s));
}
/// @}
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
// Implementation
private:
void prepare_(alt_char_type const* s)
{
const size_type size = parent_class_type::size();
const pointer data = parent_class_type::data();
// If the auto_buffer failed to allocate the required memory, and
// we're not in an exception-environment, then size() will be zero
if(0 == size)
{
// Since we know that auto_buffer's parameterising size must
// always be greater that 0, then
data[0] = '\0';
}
else
{
// Note: cannot use -1 for length, since s might be a type that
// could change length
if(0 == ::WideCharToMultiByte(0, 0, s, static_cast<int>(size), data, static_cast<int>(size), NULL, NULL))
{
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
STLSOFT_THROW_X(conversion_error("failed to convert multibyte string to wide string", ::GetLastError()));
#else /* ? STLSOFT_CF_EXCEPTION_SUPPORT */
data[0] = '\0';
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
}
else
{
data[size - 1] = '\0';
}
}
}
/// \name Accessors
/// @{
public:
char_type const* c_str() const
{
return parent_class_type::data();
}
/// @}
/// \name Operators
/// @{
public:
operator char_type const* () const
{
return parent_class_type::data();
}
/// @}
// Not to be implemented
private:
wide2multibyte(wide2multibyte const&);
wide2multibyte& operator =(wide2multibyte const&);
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
};
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
template <ss_typename_param_k C>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -