1. changed all "wxMBConv& conv" parameters to "const wxMBConv&"
2. this allows to use wxConvAuto() instead of wxConvUTF8 as default value for this parameter in the classes which read text from the file: wxConvAuto automatically recognizes the BOM at the start of file and uses the correct conversion 3. don't use Windows for UTF-7 conversions as there is no way to make it fail on invalid UTF-7 strings; use our own wxMBConvUtf7 instead git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@38570 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
99
include/wx/convauto.h
Normal file
99
include/wx/convauto.h
Normal file
@@ -0,0 +1,99 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Name: wx/convauto.h
|
||||
// Purpose: wxConvAuto class declaration
|
||||
// Author: Vadim Zeitlin
|
||||
// Created: 2006-04-03
|
||||
// RCS-ID: $Id$
|
||||
// Copyright: (c) 2006 Vadim Zeitlin
|
||||
// Licence: wxWindows licence
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef _WX_CONVAUTO_H_
|
||||
#define _WX_CONVAUTO_H_
|
||||
|
||||
#include "wx/strconv.h"
|
||||
|
||||
#if wxUSE_WCHAR_T
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// wxConvAuto: uses BOM to automatically detect input encoding
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
class WXDLLIMPEXP_BASE wxConvAuto : public wxMBConv
|
||||
{
|
||||
public:
|
||||
// default ctor, the real conversion will be created on demand
|
||||
wxConvAuto() { m_conv = NULL; /* the rest will be initialized later */ }
|
||||
|
||||
// copy ctor doesn't initialize anything neither as conversion can only be
|
||||
// deduced on first use
|
||||
wxConvAuto(const wxConvAuto& WXUNUSED(other)) { m_conv = NULL; }
|
||||
|
||||
virtual ~wxConvAuto() { if ( m_conv && m_ownsConv ) delete m_conv; }
|
||||
|
||||
// override the base class virtual function(s) to use our m_conv
|
||||
virtual size_t ToWChar(wchar_t *dst, size_t dstLen,
|
||||
const char *src, size_t srcLen = -1) const;
|
||||
|
||||
virtual size_t FromWChar(char *dst, size_t dstLen,
|
||||
const wchar_t *src, size_t srcLen = -1) const;
|
||||
|
||||
virtual size_t GetMBNulLen() const { return m_conv->GetMBNulLen(); }
|
||||
|
||||
private:
|
||||
// all currently recognized BOM values
|
||||
enum BOMType
|
||||
{
|
||||
BOM_None,
|
||||
BOM_UTF32BE,
|
||||
BOM_UTF32LE,
|
||||
BOM_UTF16BE,
|
||||
BOM_UTF16LE,
|
||||
BOM_UTF8
|
||||
};
|
||||
|
||||
// return the BOM type of this buffer
|
||||
static BOMType DetectBOM(const char *src, size_t srcLen);
|
||||
|
||||
// initialize m_conv with the conversion to use by default (UTF-8)
|
||||
void InitWithDefault()
|
||||
{
|
||||
m_conv = &wxConvUTF8;
|
||||
m_ownsConv = false;
|
||||
}
|
||||
|
||||
// create the correct conversion object for the given BOM type
|
||||
void InitFromBOM(BOMType bomType);
|
||||
|
||||
// create the correct conversion object for the BOM present in the
|
||||
// beginning of the buffer; adjust the buffer to skip the BOM if found
|
||||
void InitFromInput(const char **src, size_t *len);
|
||||
|
||||
// adjust src and len to skip over the BOM (identified by m_bomType) at the
|
||||
// start of the buffer
|
||||
void SkipBOM(const char **src, size_t *len) const;
|
||||
|
||||
|
||||
// conversion object which we really use, NULL until the first call to
|
||||
// either ToWChar() or FromWChar()
|
||||
wxMBConv *m_conv;
|
||||
|
||||
// our BOM type
|
||||
BOMType m_bomType;
|
||||
|
||||
// true if we allocated m_conv ourselves, false if we just use an existing
|
||||
// global conversion
|
||||
bool m_ownsConv;
|
||||
|
||||
// true if we already skipped BOM when converting (and not just calculating
|
||||
// the size)
|
||||
bool m_consumedBOM;
|
||||
|
||||
|
||||
DECLARE_NO_ASSIGN_CLASS(wxConvAuto);
|
||||
};
|
||||
|
||||
#endif // wxUSE_WCHAR_T
|
||||
|
||||
#endif // _WX_CONVAUTO_H_
|
||||
|
Reference in New Issue
Block a user