source: trunk/Lucide/SOURCE/plugins/ludoc/cpconv.cpp @ 35

Last change on this file since 35 was 35, checked in by Eugene Romanenko, 16 years ago

added licence block to source files

File size: 8.9 KB
Line 
1/* ***** BEGIN LICENSE BLOCK *****
2 * Version: CDDL 1.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the COMMON DEVELOPMENT AND
5 * DISTRIBUTION LICENSE (CDDL) Version 1.0 (the "License"); you may not use
6 * this file except in compliance with the License. You may obtain a copy of
7 * the License at http://www.sun.com/cddl/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Initial Developer of the Original Code is
15 * Eugene Romanenko, netlabs.org.
16 * Portions created by the Initial Developer are Copyright (C) 2006
17 * the Initial Developer. All Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 * Alternatively, the contents of this file may be used under the terms of
22 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
23 * in which case the provisions of the LGPL are applicable instead of those
24 * above. If you wish to allow use of your version of this file only under the
25 * terms of the LGPL, and not to allow others to use your version of this file
26 * under the terms of the CDDL, indicate your decision by deleting the
27 * provisions above and replace them with the notice and other provisions
28 * required by the LGPL. If you do not delete the provisions above, a recipient
29 * may use your version of this file under the terms of any one of the CDDL
30 * or the LGPL.
31 *
32 * ***** END LICENSE BLOCK ***** */
33
34
35#include <os2.h>
36
37#include <string.h>
38#include <malloc.h>
39#include <stdio.h>
40
41#include <uconv.h>
42
43
44class cpconv
45{
46    protected:
47        int  err;
48        void *objtoucs;
49        void *objfromucs;
50    public:
51        cpconv( int cpfrom, int cpto = 0 );
52        cpconv( UniChar *cpfrom, UniChar *cpto );
53        ~cpconv();
54        int conv( int chfrom );
55        int conv( const char **in, size_t *in_left, char **out, size_t *out_left );
56};
57
58cpconv::cpconv( UniChar *cpfrom, UniChar *cpto )
59{
60    err = 0;
61    int         rc = ULS_SUCCESS;
62
63    rc = UniCreateUconvObject( cpfrom, &objtoucs );
64    if (rc != ULS_SUCCESS)
65    {
66        err = 1;
67        return;
68    }
69
70    uconv_attribute_t attr;
71    UniQueryUconvObject(objtoucs, &attr, sizeof(attr), NULL, NULL, NULL);
72    attr.converttype &= ~(CVTTYPE_CTRL7F | CVTTYPE_PATH);
73    UniSetUconvObject(objtoucs, &attr);
74
75    rc = UniCreateUconvObject( cpto, &objfromucs );
76    if (rc != ULS_SUCCESS)
77    {
78        UniFreeUconvObject( objtoucs );
79        err = 1;
80        return;
81    }
82    UniQueryUconvObject(objfromucs, &attr, sizeof(uconv_attribute_t), NULL, NULL, NULL);
83    attr.options = UCONV_OPTION_SUBSTITUTE_BOTH;
84    UniSetUconvObject(objfromucs, &attr);
85}
86
87cpconv::cpconv( int cpfrom, int cpto )
88{
89    err = 0;
90
91    UniChar     ucs_code_page[12];
92    size_t      num_elems = 12;
93    int         rc = ULS_SUCCESS;
94
95    rc = UniMapCpToUcsCp(cpfrom, ucs_code_page, num_elems);
96    if (rc != ULS_SUCCESS)
97    {
98        err = 1;
99        return;
100    }
101
102    rc = UniCreateUconvObject(ucs_code_page, &objtoucs);
103    if (rc != ULS_SUCCESS)
104    {
105        err = 1;
106        return;
107    }
108
109    uconv_attribute_t attr;
110    UniQueryUconvObject(objtoucs, &attr, sizeof(attr), NULL, NULL, NULL);
111    attr.converttype &= ~(CVTTYPE_CTRL7F | CVTTYPE_PATH);
112    UniSetUconvObject(objtoucs, &attr);
113
114    rc = UniMapCpToUcsCp(cpto, ucs_code_page, num_elems);
115    if (rc != ULS_SUCCESS)
116    {
117        UniFreeUconvObject( objtoucs );
118        err = 1;
119        return;
120    }
121
122    rc = UniCreateUconvObject(ucs_code_page, &objfromucs);
123    if (rc != ULS_SUCCESS)
124    {
125        UniFreeUconvObject( objtoucs );
126        err = 1;
127        return;
128    }
129    UniQueryUconvObject(objfromucs, &attr, sizeof(uconv_attribute_t), NULL, NULL, NULL);
130    attr.options = UCONV_OPTION_SUBSTITUTE_BOTH;
131    UniSetUconvObject(objfromucs, &attr);
132}
133
134cpconv::~cpconv()
135{
136    if ( !err )
137    {
138        UniFreeUconvObject( objtoucs );
139        UniFreeUconvObject( objfromucs );
140    }
141}
142
143int cpconv::conv( int chfrom )
144{
145    int rc = ULS_SUCCESS;
146    size_t ns = 0;
147    int chto = 0;
148
149    if ( err )  return chfrom;
150
151    size_t len = 1;
152    UniChar unichar;
153    UniChar *punichar = &unichar;
154    void *pchfrom = &chfrom;
155    void *pchto = &chto;
156
157    rc = UniUconvToUcs( objtoucs, &pchfrom, &len, &punichar, &len, &ns);
158    if ( rc != ULS_SUCCESS )  return chfrom;
159
160    len = 1;
161    punichar = &unichar;
162    ns = 0;
163    rc = UniUconvFromUcs( objfromucs, &punichar, &len, &pchto, &len, &ns);
164    if ( rc != ULS_SUCCESS )  return chfrom;
165
166    return chto;
167}
168
169int cpconv::conv( const char **in, size_t *in_left, char **out, size_t *out_left )
170{
171    int       rc;
172    size_t    sl;
173    size_t    nonid;
174    UniChar  *ucs;
175    UniChar  *orig_ucs;
176    size_t    retval = 0;
177
178    if (!in || !*in) {
179        return 0;
180    }
181
182    sl =  *in_left;
183    ucs = (UniChar *) alloca(sl * sizeof(UniChar));
184    orig_ucs = ucs;
185
186    rc = UniUconvToUcs(objtoucs, (void **)in, in_left, &ucs, &sl, &retval);
187    if (rc) {
188        goto error;
189    }
190    sl = ucs - orig_ucs;
191    ucs = orig_ucs;
192    rc = UniUconvFromUcs(objfromucs, &ucs, &sl, (void **)out, out_left, &nonid);
193    if (rc) {
194        goto error;
195    }
196    retval += nonid;
197    return 0;
198
199error:
200    err = 1;
201    return -1;
202}
203
204extern "C" LONG APIENTRY cnvUniToUTF8( const char **in, unsigned *in_left,
205                                       char **out, unsigned *out_left )
206{
207    cpconv c( 1200, 1208 );
208    return c.conv( in, in_left, out, out_left );
209}
210
211extern "C" LONG APIENTRY cnvUniBEToUTF8( const char **in, unsigned *in_left,
212                                         char **out, unsigned *out_left )
213{
214    cpconv c( (UniChar *)(L"UCS-2@endian=big"), (UniChar *)(L"UTF-8") );
215    return c.conv( in, in_left, out, out_left );
216}
217
218extern "C" LONG APIENTRY cnvUTF8ToUni( const char **in, unsigned *in_left,
219                                       char **out, unsigned *out_left )
220{
221    cpconv c( 1208, 1200 );
222    return c.conv( in, in_left, out, out_left );
223}
224
225
226static void convSpchars( UniChar *uni )
227{
228    while ( *uni )
229    {
230//printf( ":%x:", *uni );
231        switch ( *uni )
232        {
233            case 0x2018:
234            case 0x2019:
235            case 0x2032:
236                *uni = 0x0027;
237                break;
238            case 0x201C:
239            case 0x201D:
240            case 0x00AB:
241            case 0x00BB:
242                *uni = 0x0022;
243                break;
244            case 0x2014:
245                *uni = 0x002D;
246                break;
247        }
248        *uni++;
249    }
250//printf( "\n" );
251}
252
253
254extern "C" LONG APIENTRY cnvUTF8ToSys( const char **in, unsigned *in_left,
255                                       char **out, unsigned *out_left )
256{
257    unsigned ulen = ( (*in_left) * 2 ) + 2;
258    char *uni = new char[ ulen ];
259    memset( uni, 0, ulen );
260    char *savuni = uni;
261    unsigned savulen = ulen;
262    cnvUTF8ToUni( in, in_left, &uni, &ulen );
263    uni = savuni;
264    ulen = savulen;
265    convSpchars( (UniChar *)uni );
266    cpconv c( 1200 );
267    LONG rc = c.conv( (const char **)&uni, &ulen, out, out_left );
268    uni = savuni;
269    delete uni;
270    return rc;
271}
272
273extern "C" LONG APIENTRY cnvUniBEToSys( const char **in, unsigned *in_left,
274                                         char **out, unsigned *out_left )
275{
276    unsigned ulen = ( (*in_left) * 2 ) + 2;
277    char *uni = new char[ ulen ];
278    memset( uni, 0, ulen );
279    char *savuni = uni;
280    unsigned savulen = ulen;
281    cpconv c1( (UniChar *)(L"UCS-2@endian=big"), (UniChar *)(L"UCS-2") );
282    c1.conv( in, in_left, &uni, &ulen );
283    uni = savuni;
284    ulen = savulen;
285    convSpchars( (UniChar *)uni );
286    cpconv c2( 1200 );
287    LONG rc = c2.conv( (const char **)&uni, &ulen, out, out_left );
288    uni = savuni;
289    delete uni;
290    return rc;
291}
292
293extern "C" LONG APIENTRY cnvUniToSys( const char **in, unsigned *in_left,
294                                      char **out, unsigned *out_left )
295{
296    convSpchars( (UniChar *)in );
297    cpconv c( 1200 );
298    return c.conv( (const char **)&in, in_left, out, out_left );
299}
300
301extern "C" LONG APIENTRY cnvSysToUCS2( const char **in, unsigned *in_left,
302                                       char **out, unsigned *out_left )
303{
304    cpconv c( (UniChar *)(L""), (UniChar *)(L"UCS-2") );
305    return c.conv( in, in_left, out, out_left );
306}
307
308
309// test
310/*void main()
311{
312    const char *testutf8 = "test UTF-8  ’¥áâ! à®¢¥àª !";
313    char buf[ 100 ];
314    memset( buf, 0, sizeof( buf ) );
315    char *bufsav = buf;
316    char *buf1 = buf;
317    unsigned in_len = strlen( testutf8 );
318    unsigned out_len = sizeof( buf );
319   
320    cnvUTF8ToUCS4( &testutf8, &in_len, &buf1, &out_len );
321   
322    for ( int i = 0; i<100; i++ )
323    {
324        printf( ":%d:", (int)bufsav[i] );
325    }
326    printf( "\n" );
327}
328*/
Note: See TracBrowser for help on using the repository browser.