summaryrefslogtreecommitdiff
path: root/converters.h
blob: d2df61efd733ae14afb37a8c173c5c293394c560 (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright (C) 2003 Mooffie <mooffie@typo.co.il>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.

#ifndef BDE_CONVERTERS_H
#define BDE_CONVERTERS_H

#include <config.h>

#include <errno.h>
#ifndef EILSEQ
# define EILSEQ 2000
#endif

#ifdef USE_ICONV
# include <iconv.h>
#endif

#include "types.h"

// A Converter object converts between encodings.
//
//  Converter (an abstract class)
//  |
//  +-- IconvConverter	    (used when the system has ICONV)
//  |
//  +-- ISO88598Converter   (used when no ICONV presents)
//  +-- Latin1Converter	    ditto
//  +-- UTF8Converter	    ditto

const char *guess_encoding(const char *buf, int len);

class Converter {
protected:
    bool ilseq_repr;
public:
    // The Converter interface is based on iconv()'s interface.
    // if a conversion function fails, it returns -1 and updates errno.
    virtual int convert(unichar **dest, char **src, int len) = 0;
    virtual int convert(char **dest, unichar **src, int len) = 0;
    Converter() {
	ilseq_repr = false;
    }
    virtual ~Converter() { }
    void enable_ilseq_repr(bool val = true) {
	ilseq_repr = val;
    }
};

#ifdef USE_ICONV
class IconvConverter : public Converter {
    iconv_t cd;
public:
    IconvConverter();
    virtual ~IconvConverter();

    int set_source_encoding(const char *encoding);
    int set_target_encoding(const char *encoding);
    virtual int convert(unichar **dest, char **src, int len);
    virtual int convert(char **dest, unichar **src, int len);
};
#endif

class ISO88598Converter : public Converter {
public:
    virtual int convert(unichar **dest, char **src, int len);
    virtual int convert(char **dest, unichar **src, int len);
};

class Latin1Converter : public Converter {
public:
    virtual int convert(unichar **dest, char **src, int len);
    virtual int convert(char **dest, unichar **src, int len);
};

class UTF8Converter : public Converter {
public:
    virtual int convert(unichar **dest, char **src, int len);
    virtual int convert(char **dest, unichar **src, int len);
};

class ConverterFactory {
    static Converter *get_internal_converter(const char *encoding);
public:
    static Converter *get_converter_from(const char *encoding);
    static Converter *get_converter_to(const char *encoding);
};

#endif