OSDN Git Service

2007-09-12 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-wchjis.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUN-TIME COMPONENTS                         --
4 --                                                                          --
5 --                       S Y S T E M . W C H _ J I S                        --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2007, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- As a special exception,  if other files  instantiate  generics from this --
23 -- unit, or you link  this unit with other files  to produce an executable, --
24 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
25 -- covered  by the  GNU  General  Public  License.  This exception does not --
26 -- however invalidate  any other reasons why  the executable file  might be --
27 -- covered by the  GNU Public License.                                      --
28 --                                                                          --
29 -- GNAT was originally developed  by the GNAT team at  New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 pragma Warnings (Off);
35 pragma Compiler_Unit;
36 pragma Warnings (On);
37
38 package body System.WCh_JIS is
39
40    type Byte is mod 256;
41
42    EUC_Hankaku_Kana : constant Byte := 16#8E#;
43    --  Prefix byte in EUC for Hankaku Kana (small Katakana). Such characters
44    --  in EUC are represented by a prefix byte followed by the code, which
45    --  is in the upper half (the corresponding JIS internal code is in the
46    --  range 16#0080# - 16#00FF#).
47
48    function EUC_To_JIS (EUC1, EUC2 : Character) return Wide_Character is
49       EUC1B  : constant Byte := Character'Pos (EUC1);
50       EUC2B  : constant Byte := Character'Pos (EUC2);
51
52    begin
53       if EUC2B not in 16#A0# .. 16#FE# then
54          raise Constraint_Error;
55       end if;
56
57       if EUC1B = EUC_Hankaku_Kana then
58          return Wide_Character'Val (EUC2B);
59
60       else
61          if EUC1B not in 16#A0# .. 16#FE# then
62             raise Constraint_Error;
63          else
64             return Wide_Character'Val
65               (256 * Natural (EUC1B and 16#7F#) + Natural (EUC2B and 16#7F#));
66          end if;
67       end if;
68    end EUC_To_JIS;
69
70    ----------------
71    -- JIS_To_EUC --
72    ----------------
73
74    procedure JIS_To_EUC
75      (J    : Wide_Character;
76       EUC1 : out Character;
77       EUC2 : out Character)
78    is
79       JIS1 : constant Natural := Wide_Character'Pos (J) / 256;
80       JIS2 : constant Natural := Wide_Character'Pos (J) rem 256;
81
82    begin
83       --  Special case of small Katakana
84
85       if JIS1 = 0 then
86
87          --  The value must be in the range 16#80# to 16#FF# so that the upper
88          --  bit is set in both bytes.
89
90          if JIS2 < 16#80# then
91             raise Constraint_Error;
92          end if;
93
94          EUC1 := Character'Val (EUC_Hankaku_Kana);
95          EUC2 := Character'Val (JIS2);
96
97       --  The upper bit of both characters must be clear, or this is not
98       --  a valid character for representation in EUC form.
99
100       elsif JIS1 > 16#7F# or else JIS2 > 16#7F# then
101          raise Constraint_Error;
102
103       --  Result is just the two characters with upper bits set
104
105       else
106          EUC1 := Character'Val (JIS1 + 16#80#);
107          EUC2 := Character'Val (JIS2 + 16#80#);
108       end if;
109    end JIS_To_EUC;
110
111    ----------------------
112    -- JIS_To_Shift_JIS --
113    ----------------------
114
115    procedure JIS_To_Shift_JIS
116      (J   : Wide_Character;
117       SJ1 : out Character;
118       SJ2 : out Character)
119    is
120       JIS1 : Byte;
121       JIS2 : Byte;
122
123    begin
124       --  The following is the required algorithm, it's hard to make any
125       --  more intelligent comments! This was copied from a public domain
126       --  C program called etos.c (author unknown).
127
128       JIS1 := Byte (Natural (Wide_Character'Pos (J) / 256));
129       JIS2 := Byte (Natural (Wide_Character'Pos (J) rem 256));
130
131       if JIS1 > 16#5F# then
132          JIS1 := JIS1 + 16#80#;
133       end if;
134
135       if (JIS1 mod 2) = 0 then
136          SJ1 := Character'Val ((JIS1 - 16#30#) / 2 + 16#88#);
137          SJ2 := Character'Val (JIS2 + 16#7E#);
138
139       else
140          if JIS2 >= 16#60# then
141             JIS2 := JIS2 + 16#01#;
142          end if;
143
144          SJ1 := Character'Val ((JIS1 - 16#31#) / 2 + 16#89#);
145          SJ2 := Character'Val (JIS2 + 16#1F#);
146       end if;
147    end JIS_To_Shift_JIS;
148
149    ----------------------
150    -- Shift_JIS_To_JIS --
151    ----------------------
152
153    function Shift_JIS_To_JIS (SJ1, SJ2 : Character) return Wide_Character is
154       SJIS1 : Byte;
155       SJIS2 : Byte;
156       JIS1  : Byte;
157       JIS2  : Byte;
158
159    begin
160       --  The following is the required algorithm, it's hard to make any
161       --  more intelligent comments! This was copied from a public domain
162       --  C program called stoj.c written by shige@csk.JUNET.
163
164       SJIS1 := Character'Pos (SJ1);
165       SJIS2 := Character'Pos (SJ2);
166
167       if SJIS1 >= 16#E0# then
168          SJIS1 := SJIS1 - 16#40#;
169       end if;
170
171       if SJIS2 >= 16#9F# then
172          JIS1 := (SJIS1 - 16#88#) * 2 + 16#30#;
173          JIS2 := SJIS2 - 16#7E#;
174
175       else
176          if SJIS2 >= 16#7F# then
177             SJIS2 := SJIS2 - 16#01#;
178          end if;
179
180          JIS1 := (SJIS1 - 16#89#) * 2 + 16#31#;
181          JIS2 := SJIS2 - 16#1F#;
182       end if;
183
184       if JIS1 not in 16#20# .. 16#7E#
185         or else JIS2 not in 16#20# .. 16#7E#
186       then
187          raise Constraint_Error;
188       else
189          return Wide_Character'Val (256 * Natural (JIS1) + Natural (JIS2));
190       end if;
191    end Shift_JIS_To_JIS;
192
193 end System.WCh_JIS;