2006-04-05 14:37:47 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: wx/convauto.h
|
|
|
|
// Purpose: wxConvAuto class declaration
|
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// Created: 2006-04-03
|
|
|
|
// Copyright: (c) 2006 Vadim Zeitlin
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef _WX_CONVAUTO_H_
|
|
|
|
#define _WX_CONVAUTO_H_
|
|
|
|
|
|
|
|
#include "wx/strconv.h"
|
2007-08-30 17:54:28 +00:00
|
|
|
#include "wx/fontenc.h"
|
2006-04-05 14:37:47 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxConvAuto: uses BOM to automatically detect input encoding
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2011-10-27 22:48:54 +00:00
|
|
|
// All currently recognized BOM values.
|
|
|
|
enum wxBOM
|
|
|
|
{
|
|
|
|
wxBOM_Unknown = -1,
|
|
|
|
wxBOM_None,
|
|
|
|
wxBOM_UTF32BE,
|
|
|
|
wxBOM_UTF32LE,
|
|
|
|
wxBOM_UTF16BE,
|
|
|
|
wxBOM_UTF16LE,
|
|
|
|
wxBOM_UTF8
|
|
|
|
};
|
|
|
|
|
2006-04-05 14:37:47 +00:00
|
|
|
class WXDLLIMPEXP_BASE wxConvAuto : public wxMBConv
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// default ctor, the real conversion will be created on demand
|
2007-08-30 17:54:28 +00:00
|
|
|
wxConvAuto(wxFontEncoding enc = wxFONTENCODING_DEFAULT)
|
|
|
|
{
|
2007-11-28 02:50:31 +00:00
|
|
|
Init();
|
|
|
|
|
2007-08-30 17:54:28 +00:00
|
|
|
m_encDefault = enc;
|
|
|
|
}
|
2006-04-05 14:37:47 +00:00
|
|
|
|
|
|
|
// copy ctor doesn't initialize anything neither as conversion can only be
|
|
|
|
// deduced on first use
|
2007-08-30 17:54:28 +00:00
|
|
|
wxConvAuto(const wxConvAuto& other) : wxMBConv()
|
|
|
|
{
|
2007-11-28 02:50:31 +00:00
|
|
|
Init();
|
|
|
|
|
2007-08-30 17:54:28 +00:00
|
|
|
m_encDefault = other.m_encDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~wxConvAuto()
|
|
|
|
{
|
|
|
|
if ( m_ownsConv )
|
|
|
|
delete m_conv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get/set the fall-back encoding used when the input text doesn't have BOM
|
|
|
|
// and isn't UTF-8
|
|
|
|
//
|
|
|
|
// special values are wxFONTENCODING_MAX meaning not to use any fall back
|
|
|
|
// at all (but just fail to convert in this case) and wxFONTENCODING_SYSTEM
|
|
|
|
// meaning to use the encoding of the system locale
|
|
|
|
static wxFontEncoding GetFallbackEncoding() { return ms_defaultMBEncoding; }
|
|
|
|
static void SetFallbackEncoding(wxFontEncoding enc);
|
|
|
|
static void DisableFallbackEncoding()
|
|
|
|
{
|
|
|
|
SetFallbackEncoding(wxFONTENCODING_MAX);
|
|
|
|
}
|
2006-04-05 14:37:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
// override the base class virtual function(s) to use our m_conv
|
|
|
|
virtual size_t ToWChar(wchar_t *dst, size_t dstLen,
|
2014-03-30 00:02:23 +00:00
|
|
|
const char *src, size_t srcLen = wxNO_LEN) const wxOVERRIDE;
|
2006-04-05 14:37:47 +00:00
|
|
|
|
|
|
|
virtual size_t FromWChar(char *dst, size_t dstLen,
|
2014-03-30 00:02:23 +00:00
|
|
|
const wchar_t *src, size_t srcLen = wxNO_LEN) const wxOVERRIDE;
|
2006-04-05 14:37:47 +00:00
|
|
|
|
2014-03-30 00:02:23 +00:00
|
|
|
virtual size_t GetMBNulLen() const wxOVERRIDE { return m_conv->GetMBNulLen(); }
|
2006-04-05 14:37:47 +00:00
|
|
|
|
2014-03-30 00:02:23 +00:00
|
|
|
virtual wxMBConv *Clone() const wxOVERRIDE { return new wxConvAuto(*this); }
|
2006-04-05 16:10:08 +00:00
|
|
|
|
2006-04-05 14:37:47 +00:00
|
|
|
// return the BOM type of this buffer
|
2011-10-27 22:48:54 +00:00
|
|
|
static wxBOM DetectBOM(const char *src, size_t srcLen);
|
2006-04-05 14:37:47 +00:00
|
|
|
|
2011-11-05 11:23:41 +00:00
|
|
|
// return the characters composing the given BOM.
|
|
|
|
static const char* GetBOMChars(wxBOM bomType, size_t* count);
|
|
|
|
|
2011-10-27 22:48:54 +00:00
|
|
|
wxBOM GetBOM() const
|
|
|
|
{
|
|
|
|
return m_bomType;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2007-11-28 02:50:31 +00:00
|
|
|
// common part of all ctors
|
|
|
|
void Init()
|
|
|
|
{
|
2011-10-27 22:48:54 +00:00
|
|
|
// We don't initialize m_encDefault here as different ctors do it
|
|
|
|
// differently.
|
2007-11-28 02:50:31 +00:00
|
|
|
m_conv = NULL;
|
2011-10-27 22:48:54 +00:00
|
|
|
m_bomType = wxBOM_Unknown;
|
2007-11-28 02:50:31 +00:00
|
|
|
m_ownsConv = false;
|
2011-10-27 22:48:54 +00:00
|
|
|
m_consumedBOM = false;
|
2007-11-28 02:50:31 +00:00
|
|
|
}
|
|
|
|
|
2007-08-30 17:54:28 +00:00
|
|
|
// initialize m_conv with the UTF-8 conversion
|
|
|
|
void InitWithUTF8()
|
2006-04-05 14:37:47 +00:00
|
|
|
{
|
|
|
|
m_conv = &wxConvUTF8;
|
|
|
|
m_ownsConv = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create the correct conversion object for the given BOM type
|
2011-10-27 22:48:54 +00:00
|
|
|
void InitFromBOM(wxBOM bomType);
|
2006-04-05 14:37:47 +00:00
|
|
|
|
|
|
|
// create the correct conversion object for the BOM present in the
|
2010-01-24 10:13:40 +00:00
|
|
|
// beginning of the buffer
|
2010-01-04 12:22:49 +00:00
|
|
|
//
|
|
|
|
// return false if the buffer is too short to allow us to determine if we
|
|
|
|
// have BOM or not
|
2010-01-24 10:13:40 +00:00
|
|
|
bool InitFromInput(const char *src, size_t len);
|
2006-04-05 14:37:47 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
|
2007-08-30 17:54:28 +00:00
|
|
|
// fall-back multibyte encoding to use, may be wxFONTENCODING_SYSTEM or
|
|
|
|
// wxFONTENCODING_MAX but not wxFONTENCODING_DEFAULT
|
|
|
|
static wxFontEncoding ms_defaultMBEncoding;
|
|
|
|
|
2006-04-05 14:37:47 +00:00
|
|
|
// conversion object which we really use, NULL until the first call to
|
|
|
|
// either ToWChar() or FromWChar()
|
|
|
|
wxMBConv *m_conv;
|
|
|
|
|
2007-08-30 17:54:28 +00:00
|
|
|
// the multibyte encoding to use by default if input isn't Unicode
|
|
|
|
wxFontEncoding m_encDefault;
|
|
|
|
|
2006-04-05 14:37:47 +00:00
|
|
|
// our BOM type
|
2011-10-27 22:48:54 +00:00
|
|
|
wxBOM m_bomType;
|
2006-04-05 14:37:47 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
|
2009-02-08 11:45:59 +00:00
|
|
|
wxDECLARE_NO_ASSIGN_CLASS(wxConvAuto);
|
2006-04-05 14:37:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // _WX_CONVAUTO_H_
|
|
|
|
|