strconv.cpp

来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C++ 代码 · 共 2,176 行 · 第 1/5 页

CPP
2,176
字号
/////////////////////////////////////////////////////////////////////////////
// Name:        strconv.cpp
// Purpose:     Unicode conversion classes
// Author:      Ove Kaaven, Robert Roebling, Vadim Zeitlin, Vaclav Slavik,
//              Ryan Norton, Fredrik Roubert (UTF7)
// Modified by:
// Created:     29/01/98
// RCS-ID:      $Id: strconv.cpp,v 1.160.2.2 2006/01/18 16:32:46 JS Exp $
// Copyright:   (c) 1999 Ove Kaaven, Robert Roebling, Vaclav Slavik
//              (c) 2000-2003 Vadim Zeitlin
//              (c) 2004 Ryan Norton, Fredrik Roubert
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

// ============================================================================
// declarations
// ============================================================================

// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------

#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
  #pragma implementation "strconv.h"
#endif

// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
  #pragma hdrstop
#endif

#ifndef WX_PRECOMP
    #include "wx/intl.h"
    #include "wx/log.h"
#endif // WX_PRECOMP

#include "wx/strconv.h"

#if wxUSE_WCHAR_T

#ifdef __WINDOWS__
    #include "wx/msw/private.h"
    #include "wx/msw/missing.h"
#endif

#ifndef __WXWINCE__
#include <errno.h>
#endif

#include <ctype.h>
#include <string.h>
#include <stdlib.h>

#if defined(__WIN32__) && !defined(__WXMICROWIN__)
    #define wxHAVE_WIN32_MB2WC
#endif // __WIN32__ but !__WXMICROWIN__

// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------

#ifdef __SALFORDC__
    #include <clib.h>
#endif

#ifdef HAVE_ICONV
    #include <iconv.h>
    #include "wx/thread.h"
#endif

#include "wx/encconv.h"
#include "wx/fontmap.h"
#include "wx/utils.h"

#ifdef __WXMAC__
#ifndef __DARWIN__
#include <ATSUnicode.h>
#include <TextCommon.h>
#include <TextEncodingConverter.h>
#endif

#include  "wx/mac/private.h"  // includes mac headers
#endif

#define TRACE_STRCONV _T("strconv")

// ----------------------------------------------------------------------------
// macros
// ----------------------------------------------------------------------------

#define BSWAP_UCS4(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT32_SWAP_ALWAYS(str[_c]); }
#define BSWAP_UTF16(str, len) { unsigned _c; for (_c=0; _c<len; _c++) str[_c]=wxUINT16_SWAP_ALWAYS(str[_c]); }

#if SIZEOF_WCHAR_T == 4
    #define WC_NAME         "UCS4"
    #define WC_BSWAP         BSWAP_UCS4
    #ifdef WORDS_BIGENDIAN
      #define WC_NAME_BEST  "UCS-4BE"
    #else
      #define WC_NAME_BEST  "UCS-4LE"
    #endif
#elif SIZEOF_WCHAR_T == 2
    #define WC_NAME         "UTF16"
    #define WC_BSWAP         BSWAP_UTF16
    #define WC_UTF16
    #ifdef WORDS_BIGENDIAN
      #define WC_NAME_BEST  "UTF-16BE"
    #else
      #define WC_NAME_BEST  "UTF-16LE"
    #endif
#else // sizeof(wchar_t) != 2 nor 4
    // does this ever happen?
    #error "Unknown sizeof(wchar_t): please report this to wx-dev@lists.wxwindows.org"
#endif

// ============================================================================
// implementation
// ============================================================================

// ----------------------------------------------------------------------------
// UTF-16 en/decoding to/from UCS-4
// ----------------------------------------------------------------------------


static size_t encode_utf16(wxUint32 input, wxUint16 *output)
{
    if (input<=0xffff)
    {
        if (output)
            *output = (wxUint16) input;
        return 1;
    }
    else if (input>=0x110000)
    {
        return (size_t)-1;
    }
    else
    {
        if (output)
        {
            *output++ = (wxUint16) ((input >> 10)+0xd7c0);
            *output = (wxUint16) ((input&0x3ff)+0xdc00);
        }
        return 2;
    }
}

static size_t decode_utf16(const wxUint16* input, wxUint32& output)
{
    if ((*input<0xd800) || (*input>0xdfff))
    {
        output = *input;
        return 1;
    }
    else if ((input[1]<0xdc00) || (input[1]>0xdfff))
    {
        output = *input;
        return (size_t)-1;
    }
    else
    {
        output = ((input[0] - 0xd7c0) << 10) + (input[1] - 0xdc00);
        return 2;
    }
}


// ----------------------------------------------------------------------------
// wxMBConv
// ----------------------------------------------------------------------------

wxMBConv::~wxMBConv()
{
    // nothing to do here (necessary for Darwin linking probably)
}

const wxWCharBuffer wxMBConv::cMB2WC(const char *psz) const
{
    if ( psz )
    {
        // calculate the length of the buffer needed first
        size_t nLen = MB2WC(NULL, psz, 0);
        if ( nLen != (size_t)-1 )
        {
            // now do the actual conversion
            wxWCharBuffer buf(nLen);
            nLen = MB2WC(buf.data(), psz, nLen + 1); // with the trailing NULL
            if ( nLen != (size_t)-1 )
            {
                return buf;
            }
        }
    }

    wxWCharBuffer buf((wchar_t *)NULL);

    return buf;
}

const wxCharBuffer wxMBConv::cWC2MB(const wchar_t *pwz) const
{
    if ( pwz )
    {
        size_t nLen = WC2MB(NULL, pwz, 0);
        if ( nLen != (size_t)-1 )
        {
            wxCharBuffer buf(nLen+3);       // space for a wxUint32 trailing zero
            nLen = WC2MB(buf.data(), pwz, nLen + 4);
            if ( nLen != (size_t)-1 )
            {
                return buf;
            }
        }
    }

    wxCharBuffer buf((char *)NULL);

    return buf;
}

const wxWCharBuffer wxMBConv::cMB2WC(const char *szString, size_t nStringLen, size_t* pOutSize) const
{
    wxASSERT(pOutSize != NULL);

    const char* szEnd = szString + nStringLen + 1;
    const char* szPos = szString;
    const char* szStart = szPos;

    size_t nActualLength = 0;
    size_t nCurrentSize = nStringLen; //try normal size first (should never resize?)

    wxWCharBuffer theBuffer(nCurrentSize);

    //Convert the string until the length() is reached, continuing the
    //loop every time a null character is reached
    while(szPos != szEnd)
    {
        wxASSERT(szPos < szEnd); //something is _really_ screwed up if this rings true

        //Get the length of the current (sub)string
        size_t nLen = MB2WC(NULL, szPos, 0);

        //Invalid conversion?
        if( nLen == (size_t)-1 )
        {
            *pOutSize = 0;
            theBuffer.data()[0u] = wxT('\0');
            return theBuffer;
        }


        //Increase the actual length (+1 for current null character)
        nActualLength += nLen + 1;

        //if buffer too big, realloc the buffer
        if (nActualLength > (nCurrentSize+1))
        {
            wxWCharBuffer theNewBuffer(nCurrentSize << 1);
            memcpy(theNewBuffer.data(), theBuffer.data(), nCurrentSize * sizeof(wchar_t));
            theBuffer = theNewBuffer;
            nCurrentSize <<= 1;
        }

        //Convert the current (sub)string
        if ( MB2WC(&theBuffer.data()[szPos - szStart], szPos, nLen + 1) == (size_t)-1 )
        {
            *pOutSize = 0;
            theBuffer.data()[0u] = wxT('\0');
            return theBuffer;
        }

        //Increment to next (sub)string
        //Note that we have to use strlen instead of nLen here
        //because XX2XX gives us the size of the output buffer,
        //which is not necessarily the length of the string
        szPos += strlen(szPos) + 1;
    }

    //success - return actual length and the buffer
    *pOutSize = nActualLength;
    return theBuffer;
}

const wxCharBuffer wxMBConv::cWC2MB(const wchar_t *szString, size_t nStringLen, size_t* pOutSize) const
{
    wxASSERT(pOutSize != NULL);

    const wchar_t* szEnd = szString + nStringLen + 1;
    const wchar_t* szPos = szString;
    const wchar_t* szStart = szPos;

    size_t nActualLength = 0;
    size_t nCurrentSize = nStringLen << 2; //try * 4 first

    wxCharBuffer theBuffer(nCurrentSize);

    //Convert the string until the length() is reached, continuing the
    //loop every time a null character is reached
    while(szPos != szEnd)
    {
        wxASSERT(szPos < szEnd); //something is _really_ screwed up if this rings true

        //Get the length of the current (sub)string
        size_t nLen = WC2MB(NULL, szPos, 0);

        //Invalid conversion?
        if( nLen == (size_t)-1 )
        {
            *pOutSize = 0;
            theBuffer.data()[0u] = wxT('\0');
            return theBuffer;
        }

        //Increase the actual length (+1 for current null character)
        nActualLength += nLen + 1;

        //if buffer too big, realloc the buffer
        if (nActualLength > (nCurrentSize+1))
        {
            wxCharBuffer theNewBuffer(nCurrentSize << 1);
            memcpy(theNewBuffer.data(), theBuffer.data(), nCurrentSize);
            theBuffer = theNewBuffer;
            nCurrentSize <<= 1;
        }

        //Convert the current (sub)string
        if(WC2MB(&theBuffer.data()[szPos - szStart], szPos, nLen + 1) == (size_t)-1 )
        {
            *pOutSize = 0;
            theBuffer.data()[0u] = wxT('\0');
            return theBuffer;
        }

        //Increment to next (sub)string
        //Note that we have to use wxWcslen instead of nLen here
        //because XX2XX gives us the size of the output buffer,
        //which is not necessarily the length of the string
        szPos += wxWcslen(szPos) + 1;
    }

    //success - return actual length and the buffer
    *pOutSize = nActualLength;
    return theBuffer;
}

// ----------------------------------------------------------------------------
// wxMBConvLibc
// ----------------------------------------------------------------------------

size_t wxMBConvLibc::MB2WC(wchar_t *buf, const char *psz, size_t n) const
{
    return wxMB2WC(buf, psz, n);
}

size_t wxMBConvLibc::WC2MB(char *buf, const wchar_t *psz, size_t n) const
{
    return wxWC2MB(buf, psz, n);
}

#ifdef __UNIX__

// ----------------------------------------------------------------------------
// wxConvBrokenFileNames
// ----------------------------------------------------------------------------

wxConvBrokenFileNames::wxConvBrokenFileNames(const wxChar *charset)
{
    if ( !charset || wxStricmp(charset, _T("UTF-8")) == 0
                  || wxStricmp(charset, _T("UTF8")) == 0  )
        m_conv = new wxMBConvUTF8(wxMBConvUTF8::MAP_INVALID_UTF8_TO_OCTAL);
    else
        m_conv = new wxCSConv(charset);
}

size_t
wxConvBrokenFileNames::MB2WC(wchar_t *outputBuf,
                             const char *psz,
                             size_t outputSize) const
{
    return m_conv->MB2WC( outputBuf, psz, outputSize );
}

size_t
wxConvBrokenFileNames::WC2MB(char *outputBuf,
                             const wchar_t *psz,
                             size_t outputSize) const
{
    return m_conv->WC2MB( outputBuf, psz, outputSize );
}

#endif

// ----------------------------------------------------------------------------
// UTF-7
// ----------------------------------------------------------------------------

// Implementation (C) 2004 Fredrik Roubert

//
// BASE64 decoding table
//
static const unsigned char utf7unb64[] =
{
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
    0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
    0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
    0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
    0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
    0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
    0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
    0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
    0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

⌨️ 快捷键说明

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