Commit | Line | Data |
---|---|---|
8d138742 CE |
1 | /* |
2 | ** Copyright 2000-2003 Double Precision, Inc. | |
3 | ** See COPYING for distribution information. | |
4 | ** | |
5 | ** $Id: ibm864convert.c,v 1.2 2003/03/07 00:47:31 mrsam Exp $ | |
6 | */ | |
7 | ||
8 | #include "unicode_config.h" | |
9 | #include "unicode.h" | |
10 | #include <string.h> | |
11 | #include <stdlib.h> | |
12 | ||
13 | /* IBM864 has this pesky 0x25 mapping charsets all share the same functions */ | |
14 | ||
15 | unicode_char *unicode_ibm864_c2u(const char *str, int *err, | |
16 | const unicode_char *table) | |
17 | { | |
18 | size_t l=strlen(str); | |
19 | unicode_char *p=(unicode_char *)malloc((l+1) * sizeof(unicode_char)); | |
20 | ||
21 | if (err) | |
22 | *err= -1; | |
23 | ||
24 | if (!p) | |
25 | return (0); | |
26 | ||
27 | for (l=0; str[l]; l++) | |
28 | { | |
29 | unicode_char c=table[(unsigned char)str[l]]; | |
30 | ||
31 | if (!c) | |
32 | { | |
33 | if (err) | |
34 | { | |
35 | *err=l; | |
36 | free(p); | |
37 | return (0); | |
38 | } | |
39 | c=(int)(unsigned char)str[l]; | |
40 | } | |
41 | p[l]=c; | |
42 | } | |
43 | p[l]=0; | |
44 | return (p); | |
45 | } | |
46 | ||
47 | ||
48 | char *unicode_ibm864_u2c(const unicode_char *uc, int *errflag, | |
49 | const unicode_char *tab) | |
50 | { | |
51 | size_t l; | |
52 | char *p; | |
53 | ||
54 | for (l=0; uc[l]; l++) | |
55 | ; | |
56 | ||
57 | if (errflag) *errflag= -1; | |
58 | p=malloc(l+1); | |
59 | if (!p) | |
60 | return (0); | |
61 | ||
62 | for (l=0; uc[l]; l++) | |
63 | { | |
64 | int c; | |
65 | unicode_char ucc=uc[l]; | |
66 | ||
67 | /* First, guess */ | |
68 | ||
69 | if (tab[ ucc & 0xFF ] == uc[l]) | |
70 | c=(int)(ucc & 0xFF); | |
71 | else | |
72 | { | |
73 | for (c=0; c<256; c++) | |
74 | if (tab[c] == uc[l]) | |
75 | break; | |
76 | if (c >= 256) | |
77 | { | |
78 | if (errflag) | |
79 | { | |
80 | *errflag=l; | |
81 | free(p); | |
82 | return (0); | |
83 | } | |
84 | c=uc[l]; | |
85 | } | |
86 | } | |
87 | if (c == 0) | |
88 | c=255; | |
89 | p[l]=(char)c; | |
90 | } | |
91 | p[l]=0; | |
92 | return (p); | |
93 | } |