2010-12-09 12:31:50 +00:00
|
|
|
/* This file is (c) 2008-2011 Konstantin Isakov <ikm@goldendict.org>
|
2009-01-28 20:55:45 +00:00
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <string>
|
|
|
|
#include "ex.hh"
|
2009-04-18 17:20:12 +00:00
|
|
|
#include "wstring.hh"
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
/// A simple UTF-8 encoder/decoder. Some dictionary backends only require
|
|
|
|
/// utf8, so we have this separately, removing the iconv dependency for them.
|
|
|
|
/// Besides, utf8 is quite ubiquitous now, and its use is spreaded over many
|
|
|
|
/// places.
|
|
|
|
namespace Utf8 {
|
|
|
|
|
|
|
|
using std::string;
|
2009-04-18 17:20:12 +00:00
|
|
|
using gd::wstring;
|
|
|
|
using gd::wchar;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
DEF_EX_STR( exCantDecode, "Can't decode the given string from Utf8:", std::exception )
|
|
|
|
|
|
|
|
/// Encodes the given UCS-4 into UTF-8. The inSize specifies the number
|
|
|
|
/// of wide characters the 'in' pointer points to. The 'out' buffer must be
|
|
|
|
/// at least inSize * 4 bytes long. The function returns the number of chars
|
|
|
|
/// stored in the 'out' buffer. The result is not 0-terminated.
|
2009-04-18 17:20:12 +00:00
|
|
|
size_t encode( wchar const * in, size_t inSize, char * out );
|
2009-01-28 20:55:45 +00:00
|
|
|
/// Decodes the given UTF-8 into UCS-32. The inSize specifies the number
|
|
|
|
/// of bytes the 'in' pointer points to. The 'out' buffer must be at least
|
|
|
|
/// inSize wide characters long. If the given UTF-8 is invalid, the decode
|
|
|
|
/// function returns -1, otherwise it returns the number of wide characters
|
|
|
|
/// stored in the 'out' buffer. The result is not 0-terminated.
|
2009-04-18 17:20:12 +00:00
|
|
|
long decode( char const * in, size_t inSize, wchar * out );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
/// Versions for non time-critical code.
|
|
|
|
string encode( wstring const & ) throw();
|
|
|
|
wstring decode( string const & ) throw( exCantDecode );
|
|
|
|
|
|
|
|
}
|