-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiconv.hh
67 lines (49 loc) · 2.24 KB
/
iconv.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#ifndef __ICONV_HH_INCLUDED__
#define __ICONV_HH_INCLUDED__
#include <iconv.h>
#include "wstring.hh"
#include "ex.hh"
/// A wrapper for the iconv() character set conversion functions
class Iconv
{
iconv_t state;
public:
DEF_EX( Ex, "Iconv exception", std::exception )
DEF_EX_STR( exCantInit, "Can't initialize iconv conversion:", Ex )
DEF_EX( exIncorrectSeq, "Invalid character sequence encountered during character conversion", Ex )
DEF_EX( exPrematureEnd, "Character sequence ended prematurely during character conversion", Ex )
DEF_EX_STR( exOther, "An error has occured during character conversion:", Ex )
// Some predefined character sets' names
static char const * const GdWchar;
static char const * const Utf16Le;
static char const * const Utf8;
Iconv( char const * to, char const * from ) throw( exCantInit );
// Changes to another pair of encodings. All the internal state is reset.
void reinit( char const * to, char const * from ) throw( exCantInit );
~Iconv() throw();
enum Result
{
Success, // All the data was successfully converted
NeedMoreIn, // Input has an incomplete multibyte character at its end
NeedMoreOut // The output buffer can't hold the result
};
Result convert( void const * & inBuf, size_t & inBytesLeft,
void * & outBuf, size_t & outBytesLeft ) throw( exIncorrectSeq,
exOther );
// Converts a given block of data from the given encoding to a wide string.
static gd::wstring toWstring( char const * fromEncoding, void const * fromData,
size_t dataSize )
throw( exCantInit, exIncorrectSeq, exPrematureEnd, exOther );
// Converts a given block of data from the given encoding to an utf8-encoded
// string.
static std::string toUtf8( char const * fromEncoding, void const * fromData,
size_t dataSize )
throw( exCantInit, exIncorrectSeq, exPrematureEnd, exOther );
private:
// Copying/assigning not supported
Iconv( Iconv const & );
Iconv & operator = ( Iconv const & );
};
#endif