1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT RUNTIME 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,1993,1994 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,  59 Temple Place - Suite 330,  Boston, --
20-- MA 02111-1307, 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
34package body System.WCh_JIS is
35
36   type Byte is mod 256;
37
38   EUC_Hankaku_Kana : constant Byte := 16#8E#;
39   --  Prefix byte in EUC for Hankaku Kana (small Katakana). Such characters
40   --  in EUC are represented by a prefix byte followed by the code, which
41   --  is in the upper half (the corresponding JIS internal code is in the
42   --  range 16#0080# - 16#00FF#).
43
44   function EUC_To_JIS (EUC1, EUC2 : Character) return Wide_Character is
45      EUC1B  : constant Byte := Character'Pos (EUC1);
46      EUC2B  : constant Byte := Character'Pos (EUC2);
47
48   begin
49      if EUC2B not in 16#A0# .. 16#FE# then
50         raise Constraint_Error;
51      end if;
52
53      if EUC1B = EUC_Hankaku_Kana then
54         return Wide_Character'Val (EUC2B);
55
56      else
57         if EUC1B not in 16#A0# .. 16#FE# then
58            raise Constraint_Error;
59         else
60            return Wide_Character'Val
61              (256 * Natural (EUC1B and 16#7F#) + Natural (EUC2B and 16#7F#));
62         end if;
63      end if;
64   end EUC_To_JIS;
65
66   ----------------
67   -- JIS_To_EUC --
68   ----------------
69
70   procedure JIS_To_EUC
71     (J    : in Wide_Character;
72      EUC1 : out Character;
73      EUC2 : out Character)
74   is
75      JIS1 : constant Natural := Wide_Character'Pos (J) / 256;
76      JIS2 : constant Natural := Wide_Character'Pos (J) rem 256;
77
78   begin
79      if JIS1 = 0 then
80         EUC1 := Character'Val (EUC_Hankaku_Kana);
81         EUC2 := Character'Val (JIS2);
82
83      else
84         EUC1 := Character'Val (JIS1 + 16#80#);
85         EUC2 := Character'Val (JIS2 + 16#80#);
86      end if;
87   end JIS_To_EUC;
88
89   ----------------------
90   -- JIS_To_Shift_JIS --
91   ----------------------
92
93   procedure JIS_To_Shift_JIS
94     (J   : in Wide_Character;
95      SJ1 : out Character;
96      SJ2 : out Character)
97   is
98      JIS1 : Byte;
99      JIS2 : Byte;
100
101   begin
102      --  The following is the required algorithm, it's hard to make any
103      --  more intelligent comments! This was copied from a public domain
104      --  C program called etos.c (author unknown).
105
106      JIS1 := Byte (Natural (Wide_Character'Pos (J) / 256));
107      JIS2 := Byte (Natural (Wide_Character'Pos (J) rem 256));
108
109      if JIS1 > 16#5F# then
110         JIS1 := JIS1 + 16#80#;
111      end if;
112
113      if (JIS1 mod 2) = 0 then
114         SJ1 := Character'Val ((JIS1 - 16#30#) / 2 + 16#88#);
115         SJ2 := Character'Val (JIS2 + 16#7E#);
116
117      else
118         if JIS2 >= 16#60# then
119            JIS2 := JIS2 + 16#01#;
120         end if;
121
122         SJ1 := Character'Val ((JIS1 - 16#31#) / 2 + 16#89#);
123         SJ2 := Character'Val (JIS2 + 16#1F#);
124      end if;
125   end JIS_To_Shift_JIS;
126
127   ----------------------
128   -- Shift_JIS_To_JIS --
129   ----------------------
130
131   function Shift_JIS_To_JIS (SJ1, SJ2 : Character) return Wide_Character is
132      SJIS1 : Byte;
133      SJIS2 : Byte;
134      JIS1  : Byte;
135      JIS2  : Byte;
136
137   begin
138      --  The following is the required algorithm, it's hard to make any
139      --  more intelligent comments! This was copied from a public domain
140      --  C program called stoj.c written by shige@csk.JUNET.
141
142      SJIS1 := Character'Pos (SJ1);
143      SJIS2 := Character'Pos (SJ2);
144
145      if SJIS1 >= 16#E0# then
146         SJIS1 := SJIS1 - 16#40#;
147      end if;
148
149      if SJIS2 >= 16#9F# then
150         JIS1 := (SJIS1 - 16#88#) * 2 + 16#30#;
151         JIS2 := SJIS2 - 16#7E#;
152
153      else
154         if SJIS2 >= 16#7F# then
155            SJIS2 := SJIS2 - 16#01#;
156         end if;
157
158         JIS1 := (SJIS1 - 16#89#) * 2 + 16#31#;
159         JIS2 := SJIS2 - 16#1F#;
160      end if;
161
162      if JIS1 not in 16#20# .. 16#7E#
163        or else JIS2 not in 16#20# .. 16#7E#
164      then
165         raise Constraint_Error;
166      else
167         return Wide_Character'Val (256 * Natural (JIS1) + Natural (JIS2));
168      end if;
169   end Shift_JIS_To_JIS;
170
171end System.WCh_JIS;
172