summaryrefslogtreecommitdiff
path: root/Build/source/libs/teckit/TECkit-2.5.1/source/Perl/TECkit.xs
blob: 6517e41d7815b343418c0671900793c203e07679 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#include "TECkit_Engine.h"

#define STYLE_InUni     1
#define STYLE_OutUni    2

typedef TECkit_Converter Encode__TECkit;

MODULE = Encode::TECkit     PACKAGE = Encode::TECkit

Encode::TECkit
new_conv(mapping, mapForward, style)
        char    *mapping
        Byte    mapForward
        Byte    style
    PREINIT:
        UInt16  sourceForm;
        UInt16  targetForm;
        TECkit_Status   hr;
        PerlIO *infile;
        UInt32  filelen;
        Byte    *fBuff;
        SV      *mySv;
    PPCODE:
        if (style & 1)
            sourceForm = kForm_UTF8;
        else
            sourceForm = kForm_Bytes;
        if (style & 2)
            targetForm = kForm_UTF8;
        else
            targetForm = kForm_Bytes;
        
        infile = PerlIO_open(mapping, "rb");
        if (!infile)
            XSRETURN_UNDEF;
        PerlIO_seek(infile, 0, 2);
        filelen = PerlIO_tell(infile);
        New(1, fBuff, filelen, Byte);
        PerlIO_seek(infile, 0, 0);
        PerlIO_read(infile, fBuff, filelen);
        PerlIO_close(infile);
        
        hr = TECkit_CreateConverter(fBuff, filelen, mapForward, sourceForm, targetForm, (TECkit_Converter *)&RETVAL);
        EXTEND(SP, 2);
        XSprePUSH;
        if (hr)
        {
            hr = -hr;
            PUSHs(&PL_sv_undef);
            PUSHs(sv_2mortal(newSViv(hr)));
        }
        else
        {
            mapForward = 0;
        	mySv = sv_newmortal();
    	    sv_setref_pv(mySv, "Encode::TECkit", (void*)RETVAL);
            PUSHs(mySv);
            PUSHs(sv_2mortal(newSViv(0)));
        }
        XSRETURN(2);
        
Encode::TECkit
new_conv_scalar(mapping, mapForward, style)
        SV     *mapping
        Byte    mapForward
        Byte    style
    PREINIT:
        UInt16  sourceForm;
        UInt16  targetForm;
        TECkit_Status   hr;
        Byte   *fBuff;
        STRLEN  filelen;
        SV     *mySv;
    PPCODE:
        if (style & 1)
            sourceForm = kForm_UTF8;
        else
            sourceForm = kForm_Bytes;
        if (style & 2)
            targetForm = kForm_UTF8;
        else
            targetForm = kForm_Bytes;
        
        fBuff = (Byte *)SvPV(mapping, filelen);
        
        hr = TECkit_CreateConverter(fBuff, (long)filelen, mapForward, sourceForm, targetForm, (TECkit_Converter *)&RETVAL);
        EXTEND(SP, 2);
        XSprePUSH;
        if (hr)
        {
            hr = -hr;
            PUSHs(&PL_sv_undef);
            PUSHs(sv_2mortal(newSViv(hr)));
        }
        else
        {
            mapForward = 0;
        	mySv = sv_newmortal();
    	    sv_setref_pv(mySv, "Encode::TECkit", (void*)RETVAL);
            PUSHs(mySv);
            PUSHs(sv_2mortal(newSViv(0)));
        }
        XSRETURN(2);
        
SV *
convert(converter, input, style, isComplete)
        Encode::TECkit converter
        SV      *input
        Byte    style
        Byte    isComplete
    PREINIT:
        Byte    *inBuff;
        Byte    *outBuff;
        STRLEN  pvLen;
        UInt32  inLen;
        UInt32  outLen;
        TECkit_Status   hr;
    CODE:
        inBuff = (Byte *)SvPV(input, pvLen);
        inLen = (UInt32)pvLen;
        New(0, outBuff, inLen * 4, Byte);
        outLen = inLen * 4;
        while ((hr = TECkit_ConvertBuffer((TECkit_Converter)converter, inBuff, inLen, 0, outBuff, 
                        outLen, &outLen, isComplete)) == kStatus_OutputBufferFull)
        {
            Safefree(outBuff);
            New(0, outBuff, outLen * 2, Byte);
            outLen = outLen * 2;
            if (isComplete)
                TECkit_ResetConverter((TECkit_Converter)converter);
        }
        hr = -hr;
        if (isComplete)
            TECkit_ResetConverter((TECkit_Converter)converter);
        isComplete = (Byte)hr;
        RETVAL = newSVpv((char *)outBuff, (STRLEN)outLen);
        if (style & 2)
            SvUTF8_on(RETVAL);
        else
            SvUTF8_off(RETVAL);
    OUTPUT:
        RETVAL
        isComplete

SV *
flush(converter, style, hr)
        Encode::TECkit converter
        Byte    style
        Byte    hr = NO_INIT
    PREINIT:
        Byte    *outBuff;
        UInt32  outLen;
    CODE:
        outLen = 128;
        New(0, outBuff, outLen, Byte);
        hr = (Byte)TECkit_Flush((TECkit_Converter)converter, outBuff, outLen, &outLen);
        RETVAL = newSVpv((char *)outBuff, (STRLEN)outLen);
        if (style & 2)
            SvUTF8_on(RETVAL);
        else
            SvUTF8_off(RETVAL);
        TECkit_ResetConverter((TECkit_Converter)converter);
    OUTPUT:
        RETVAL

void
DESTROY(converter)
        Encode::TECkit converter
    CODE:
        TECkit_DisposeConverter((TECkit_Converter)converter);