]>
Commit | Line | Data |
---|---|---|
04be94a8 UD |
1 | /* Access functions for JISX0212 conversion. |
2 | Copyright (C) 1997 Free Software Foundation, Inc. | |
3 | This file is part of the GNU C Library. | |
4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
7 | modify it under the terms of the GNU Library General Public License as | |
8 | published by the Free Software Foundation; either version 2 of the | |
9 | License, or (at your option) any later version. | |
10 | ||
11 | The GNU C Library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Library General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Library General Public | |
17 | License along with the GNU C Library; see the file COPYING.LIB. If not, | |
18 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
19 | Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | #ifndef _JIS0212_H | |
22 | #define _JIS0212_H 1 | |
23 | ||
24 | #include <gconv.h> | |
25 | #include <inttypes.h> | |
26 | ||
27 | ||
28 | /* Struct for table with indeces in mapping table. */ | |
29 | struct jisx0212_idx | |
30 | { | |
31 | uint16_t start; | |
32 | uint16_t end; | |
33 | uint16_t idx; | |
34 | }; | |
35 | ||
36 | /* Conversion table. */ | |
37 | extern const struct jisx0212_idx jisx0212_to_ucs_idx[]; | |
38 | extern const uint16_t jisx0212_to_ucs[]; | |
39 | ||
40 | extern const struct jisx0212_idx jisx0212_from_ucs_idx[]; | |
41 | extern const char jisx0212_from_ucs[][2]; | |
42 | ||
43 | ||
44 | static inline wchar_t | |
45 | jisx0212_to_ucs4 (char **s, size_t avail) | |
46 | { | |
47 | const struct jisx0212_idx *rp = jisx0212_to_ucs_idx; | |
48 | unsigned char ch = *(*s); | |
49 | unsigned char ch2; | |
50 | wchar_t wch = L'\0'; | |
51 | int idx; | |
52 | ||
53 | if (ch <= 0x6d || ch > 0xea) | |
54 | return UNKNOWN_10646_CHAR; | |
55 | ||
56 | if (avail < 2) | |
57 | return 0; | |
58 | ||
59 | ch2 = (*s)[1]; | |
60 | if (ch2 <= 0x20 || ch2 >= 0x7f) | |
61 | return UNKNOWN_10646_CHAR; | |
62 | ||
63 | idx = (ch - 0x21) * 94 + (ch2 - 0x21); | |
64 | ||
65 | while (idx < rp->start) | |
66 | ++rp; | |
67 | if (idx <= rp->end) | |
68 | wch = jisx0212_to_ucs[rp->idx + idx - rp->start]; | |
69 | ||
70 | if (wch != L'\0') | |
71 | (*s) += 2; | |
72 | else | |
73 | wch = UNKNOWN_10646_CHAR; | |
74 | ||
75 | return wch; | |
76 | } | |
77 | ||
78 | ||
79 | static inline size_t | |
80 | ucs4_to_jisx0212 (wchar_t wch, char **s, size_t avail) | |
81 | { | |
82 | const struct jisx0212_idx *rp = jisx0212_from_ucs_idx; | |
83 | unsigned int ch = (unsigned int) wch; | |
84 | const char *cp = NULL; | |
85 | ||
86 | while (ch > rp->end) | |
87 | ++rp; | |
88 | if (ch >= rp->start) | |
89 | cp = jisx0212_from_ucs[rp->idx + ch - rp->start]; | |
90 | ||
91 | if (cp == NULL || cp[0] == '\0') | |
92 | return UNKNOWN_10646_CHAR; | |
93 | ||
94 | **s = cp[0]; | |
95 | if (cp[1] != '\0') | |
96 | { | |
97 | if (avail < 2) | |
98 | return 0; | |
99 | ||
100 | *++(*s) = cp[1]; | |
101 | } | |
102 | ++(*s); | |
103 | ||
104 | return 2; | |
105 | } | |
106 | ||
107 | #endif /* jis0212.h */ |