2010-01-24 19:56:51 +01:00
|
|
|
// Copyright (c) 2010, Thomas Goyne
|
2009-07-14 23:28:49 +02:00
|
|
|
// 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 of the Aegisub Group nor the names of its 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.
|
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
2009-07-14 23:28:49 +02:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/// @file charset_conv.h
|
|
|
|
/// @see charset_conv.cpp
|
|
|
|
/// @ingroup utility
|
|
|
|
///
|
2009-07-14 23:28:49 +02:00
|
|
|
|
2009-09-11 04:36:34 +02:00
|
|
|
#ifndef AGI_PRE
|
2009-07-14 23:28:49 +02:00
|
|
|
#include <iconv.h>
|
|
|
|
#include <wchar.h>
|
2009-09-11 04:36:34 +02:00
|
|
|
|
2009-07-14 23:28:49 +02:00
|
|
|
#include <wx/arrstr.h>
|
2009-07-18 02:58:13 +02:00
|
|
|
#include <wx/string.h>
|
|
|
|
#include <wx/strconv.h>
|
2009-09-11 04:36:34 +02:00
|
|
|
#include <wx/thread.h>
|
|
|
|
#endif
|
2009-07-14 23:28:49 +02:00
|
|
|
|
|
|
|
#include "aegisub_endian.h"
|
|
|
|
|
|
|
|
#if !defined(_LIBICONV_VERSION) || _LIBICONV_VERSION < 0x010A || defined(LIBICONV_PLUG)
|
|
|
|
#define ICONV_POSIX
|
|
|
|
#endif
|
|
|
|
|
2010-01-24 19:56:51 +01:00
|
|
|
/// @class iconv_wrapper
|
|
|
|
/// @brief RAII wrapper for iconv
|
|
|
|
class iconv_wrapper {
|
|
|
|
private:
|
|
|
|
iconv_t conv;
|
|
|
|
public:
|
|
|
|
iconv_wrapper(const char *to, const char *from)
|
|
|
|
: conv(iconv_open(to, from))
|
|
|
|
{ }
|
|
|
|
iconv_wrapper(wxString const& to, wxString const& from)
|
|
|
|
: conv(iconv_open(to.ToAscii(), from.ToAscii()))
|
|
|
|
{ }
|
|
|
|
iconv_wrapper(const char *to, wxString const& from)
|
|
|
|
: conv(iconv_open(to, from.ToAscii()))
|
|
|
|
{ }
|
|
|
|
iconv_wrapper(wxString const& to, const char *from)
|
|
|
|
: conv(iconv_open(to.ToAscii(), from))
|
|
|
|
{ }
|
|
|
|
~iconv_wrapper() {
|
|
|
|
if (conv != (iconv_t)-1) iconv_close(conv);
|
|
|
|
}
|
|
|
|
operator iconv_t() {
|
|
|
|
return conv;
|
|
|
|
}
|
|
|
|
operator const iconv_t() const {
|
|
|
|
return conv;
|
|
|
|
}
|
|
|
|
};
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
|
|
|
/// @class AegisubCSConv
|
2010-01-24 19:56:51 +01:00
|
|
|
/// @brief wxMBConv implementation for converting to and from unicode
|
2009-07-14 23:28:49 +02:00
|
|
|
class AegisubCSConv : public wxMBConv {
|
|
|
|
public:
|
2010-01-24 19:56:51 +01:00
|
|
|
/// @param mbEncName Multibyte encoding to convert to/from
|
|
|
|
/// @param enableSubst Whether to substitute characters when needed.
|
|
|
|
/// By default, any conversion that would be lossy will fail
|
|
|
|
/// When enableSubst is true, conversions to multibyte with a sufficiently
|
|
|
|
/// large buffer are guaranteed to succeed, with characters dropped or
|
|
|
|
/// changed as needed to fit the string into the target encoding.
|
2009-07-14 23:28:49 +02:00
|
|
|
AegisubCSConv(const wxChar *mbEncName, bool enableSubst = false);
|
|
|
|
|
|
|
|
// wxMBConv implementation; see strconv.h for usage details
|
2010-01-24 19:56:51 +01:00
|
|
|
size_t ToWChar(wchar_t *dst, size_t dstLen, const char *src, size_t srcLen = wxNO_LEN) const;
|
|
|
|
size_t FromWChar(char *dst, size_t dstLen, const wchar_t *src, size_t srcLen = wxNO_LEN) const;
|
|
|
|
size_t GetMBNulLen() const;
|
|
|
|
wxMBConv *Clone() const;
|
2009-07-14 23:28:49 +02:00
|
|
|
|
2010-01-24 19:56:51 +01:00
|
|
|
/// @brief Multibyte-aware strlen
|
|
|
|
/// @return Length in bytes of str (excluding terminator)
|
2009-07-14 23:28:49 +02:00
|
|
|
size_t MBBuffLen(const char *str) const;
|
|
|
|
|
2010-01-24 19:56:51 +01:00
|
|
|
/// @brief Get a list of support encodings with user-friendly names
|
2009-07-14 23:28:49 +02:00
|
|
|
static wxArrayString GetEncodingsList();
|
2010-01-24 19:56:51 +01:00
|
|
|
/// @brief Get a list of all encodings supported by iconv
|
|
|
|
/// Requires GNU iconv for useful results
|
2009-07-14 23:28:49 +02:00
|
|
|
static wxArrayString GetAllSupportedEncodings();
|
2010-01-24 19:56:51 +01:00
|
|
|
/// @brief Map a user-friendly encoding name to the real encoding name
|
2009-07-14 23:28:49 +02:00
|
|
|
static wxString GetRealEncodingName(wxString name);
|
|
|
|
|
|
|
|
private:
|
2010-01-24 19:56:51 +01:00
|
|
|
// The smattering of mutable variables here are due to that ToWChar and
|
|
|
|
// FromWChar are const in wxMBConv, but we require minor mutation for
|
|
|
|
// things like locks (as iconv is not thread-safe)
|
2009-07-14 23:28:49 +02:00
|
|
|
wxString wcCharsetName;
|
|
|
|
wxString mbCharsetName;
|
2010-01-24 19:56:51 +01:00
|
|
|
mutable size_t mbNulLen;
|
|
|
|
bool enableSubst;
|
2009-07-14 23:28:49 +02:00
|
|
|
|
2009-09-02 11:40:52 +02:00
|
|
|
size_t doConversion(iconv_t cd, char *dst, size_t dstSize, char *src, size_t srcSize) const;
|
|
|
|
size_t iconvWrapper(iconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft) const;
|
2009-07-14 23:28:49 +02:00
|
|
|
|
|
|
|
static void ucToMbFallback(
|
|
|
|
unsigned int code,
|
|
|
|
void (*callback) (const char *buf, size_t buflen, void* callback_arg),
|
|
|
|
void *callback_arg,
|
|
|
|
void *convPtr);
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2010-01-24 19:56:51 +01:00
|
|
|
/// Replacement character for characters which do not fit in the target
|
|
|
|
/// encoding and iconv does not have an appropriate substitute for
|
2009-07-14 23:28:49 +02:00
|
|
|
char invalidRep[8];
|
|
|
|
size_t invalidRepSize;
|
|
|
|
|
|
|
|
#ifndef ICONV_POSIX
|
2010-01-24 19:56:51 +01:00
|
|
|
mutable iconv_fallbacks fallbacks;
|
2009-07-14 23:28:49 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if wxUSE_THREADS
|
2010-01-24 19:56:51 +01:00
|
|
|
mutable wxMutex iconvMutex;
|
2009-07-14 23:28:49 +02:00
|
|
|
#endif
|
2010-01-24 19:56:51 +01:00
|
|
|
|
|
|
|
protected:
|
|
|
|
iconv_wrapper m2w, w2m;
|
2009-07-14 23:28:49 +02:00
|
|
|
};
|
|
|
|
|
2010-01-24 19:56:51 +01:00
|
|
|
// Predefined conversion for the current locale, intended to be a drop-in
|
|
|
|
// replacement for wxConvLocal
|
2009-07-14 23:28:49 +02:00
|
|
|
extern AegisubCSConv& csConvLocal;
|
|
|
|
|
|
|
|
#ifdef HAVE_BIG_ENDIAN
|
|
|
|
# if SIZEOF_WCHAR_T == 4
|
|
|
|
# define WCHAR_T_ENCODING "UTF-32BE"
|
|
|
|
# elif SIZEOF_WCHAR_T == 2
|
|
|
|
# define WCHAR_T_ENCODING "UTF-16BE"
|
|
|
|
# endif
|
|
|
|
#elif defined(HAVE_LITTLE_ENDIAN)
|
|
|
|
# if SIZEOF_WCHAR_T == 4
|
|
|
|
# define WCHAR_T_ENCODING "UTF-32LE"
|
|
|
|
# elif SIZEOF_WCHAR_T == 2
|
|
|
|
# define WCHAR_T_ENCODING "UTF-16LE"
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# if SIZEOF_WCHAR_T == 4
|
|
|
|
# define WCHAR_T_ENCODING ((Endian::MachineToBig((uint32_t)1) == 1) ? "UTF-32BE" : "UTF-32LE")
|
|
|
|
# elif SIZEOF_WCHAR_T == 2
|
|
|
|
# define WCHAR_T_ENCODING ((Endian::MachineToBig((uint32_t)1) == 1) ? "UTF-16BE" : "UTF-16LE")
|
|
|
|
# endif
|
|
|
|
#endif
|