OSDN Git Service

* gimplify.c (gimplify_type_sizes) [POINTER_TYPE, REFERENCE_TYPE]:
[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-2006, 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 with System.Pure_Exceptions; use System.Pure_Exceptions;
35
36 package body System.WCh_JIS is
37
38    type Byte is mod 256;
39
40    EUC_Hankaku_Kana : constant Byte := 16#8E#;
41    --  Prefix byte in EUC for Hankaku Kana (small Katakana). Such characters
42    --  in EUC are represented by a prefix byte followed by the code, which
43    --  is in the upper half (the corresponding JIS internal code is in the
44    --  range 16#0080# - 16#00FF#).
45
46    function EUC_To_JIS (EUC1, EUC2 : Character) return Wide_Character is
47       EUC1B  : constant Byte := Character'Pos (EUC1);
48       EUC2B  : constant Byte := Character'Pos (EUC2);
49
50    begin
51       if EUC2B not in 16#A0# .. 16#FE# then
52          raise Constraint_Error;
53       end if;
54
55       if EUC1B = EUC_Hankaku_Kana then
56          return Wide_Character'Val (EUC2B);
57
58       else
59          if EUC1B not in 16#A0# .. 16#FE# then
60             raise Constraint_Error;
61          else
62             return Wide_Character'Val
63               (256 * Natural (EUC1B and 16#7F#) + Natural (EUC2B and 16#7F#));
64          end if;
65       end if;
66    end EUC_To_JIS;
67
68    ----------------
69    -- JIS_To_EUC --
70    ----------------
71
72    procedure JIS_To_EUC
73      (J    : Wide_Character;
74       EUC1 : out Character;
75       EUC2 : out Character)
76    is
77       JIS1 : constant Natural := Wide_Character'Pos (J) / 256;
78       JIS2 : constant Natural := Wide_Character'Pos (J) rem 256;
79
80    begin
81       --  Special case of small Katakana
82
83       if JIS1 = 0 then
84
85          --  The value must be in the range 16#80# to 16#FF# so that the upper
86          --  bit is set in both bytes.
87
88          if JIS2 < 16#80# then
89             Raise_Exception (CE, "invalid small Katakana character");
90          end if;
91
92          EUC1 := Character'Val (EUC_Hankaku_Kana);
93          EUC2 := Character'Val (JIS2);
94
95       --  The upper bit of both characters must be clear, or this is not
96       --  a valid character for representation in EUC form.
97
98       elsif JIS1 > 16#7F# or else JIS2 > 16#7F# then
99          Raise_Exception (CE, "wide character value out of EUC range");
100
101       --  Result is just the two characters with upper bits set
102
103       else
104          EUC1 := Character'Val (JIS1 + 16#80#);
105          EUC2 := Character'Val (JIS2 + 16#80#);
106       end if;
107    end JIS_To_EUC;
108
109    ----------------------
110    -- JIS_To_Shift_JIS --
111    ----------------------
112
113    procedure JIS_To_Shift_JIS
114      (J   : Wide_Character;
115       SJ1 : out Character;
116       SJ2 : out Character)
117    is
118       JIS1 : Byte;
119       JIS2 : Byte;
120
121    begin
122       --  The following is the required algorithm, it's hard to make any
123       --  more intelligent comments! This was copied from a public domain
124       --  C program called etos.c (author unknown).
125
126       JIS1 := Byte (Natural (Wide_Character'Pos (J) / 256));
127       JIS2 := Byte (Natural (Wide_Character'Pos (J) rem 256));
128
129       if JIS1 > 16#5F# then
130          JIS1 := JIS1 + 16#80#;
131       end if;
132
133       if (JIS1 mod 2) = 0 then
134          SJ1 := Character'Val ((JIS1 - 16#30#) / 2 + 16#88#);
135          SJ2 := Character'Val (JIS2 + 16#7E#);
136
137       else
138          if JIS2 >= 16#60# then
139             JIS2 := JIS2 + 16#01#;
140          end if;
141
142          SJ1 := Character'Val ((JIS1 - 16#31#) / 2 + 16#89#);
143          SJ2 := Character'Val (JIS2 + 16#1F#);
144       end if;
145    end JIS_To_Shift_JIS;
146
147    ----------------------
148    -- Shift_JIS_To_JIS --
149    ----------------------
150
151    function Shift_JIS_To_JIS (SJ1, SJ2 : Character) return Wide_Character is
152       SJIS1 : Byte;
153       SJIS2 : Byte;
154       JIS1  : Byte;
155       JIS2  : Byte;
156
157    begin
158       --  The following is the required algorithm, it's hard to make any
159       --  more intelligent comments! This was copied from a public domain
160       --  C program called stoj.c written by shige@csk.JUNET.
161
162       SJIS1 := Character'Pos (SJ1);
163       SJIS2 := Character'Pos (SJ2);
164
165       if SJIS1 >= 16#E0# then
166          SJIS1 := SJIS1 - 16#40#;
167       end if;
168
169       if SJIS2 >= 16#9F# then
170          JIS1 := (SJIS1 - 16#88#) * 2 + 16#30#;
171          JIS2 := SJIS2 - 16#7E#;
172
173       else
174          if SJIS2 >= 16#7F# then
175             SJIS2 := SJIS2 - 16#01#;
176          end if;
177
178          JIS1 := (SJIS1 - 16#89#) * 2 + 16#31#;
179          JIS2 := SJIS2 - 16#1F#;
180       end if;
181
182       if JIS1 not in 16#20# .. 16#7E#
183         or else JIS2 not in 16#20# .. 16#7E#
184       then
185          raise Constraint_Error;
186       else
187          return Wide_Character'Val (256 * Natural (JIS1) + Natural (JIS2));
188       end if;
189    end Shift_JIS_To_JIS;
190
191 end System.WCh_JIS;