config/sparc/sol2-bi.h: Revert previous delta.
[official-gcc.git] / gcc / ada / s-wchjis.adb
blob59987e03f0543ad8d656bcc3410c3f86f93a402d
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 -- --
10 -- Copyright (C) 1992,1993,1994 Free Software Foundation, Inc. --
11 -- --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
22 -- --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
29 -- --
30 -- GNAT was originally developed by the GNAT team at New York University. --
31 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 -- --
33 ------------------------------------------------------------------------------
35 package body System.WCh_JIS is
37 type Byte is mod 256;
39 EUC_Hankaku_Kana : constant Byte := 16#8E#;
40 -- Prefix byte in EUC for Hankaku Kana (small Katakana). Such characters
41 -- in EUC are represented by a prefix byte followed by the code, which
42 -- is in the upper half (the corresponding JIS internal code is in the
43 -- range 16#0080# - 16#00FF#).
45 function EUC_To_JIS (EUC1, EUC2 : Character) return Wide_Character is
46 EUC1B : constant Byte := Character'Pos (EUC1);
47 EUC2B : constant Byte := Character'Pos (EUC2);
49 begin
50 if EUC2B not in 16#A0# .. 16#FE# then
51 raise Constraint_Error;
52 end if;
54 if EUC1B = EUC_Hankaku_Kana then
55 return Wide_Character'Val (EUC2B);
57 else
58 if EUC1B not in 16#A0# .. 16#FE# then
59 raise Constraint_Error;
60 else
61 return Wide_Character'Val
62 (256 * Natural (EUC1B and 16#7F#) + Natural (EUC2B and 16#7F#));
63 end if;
64 end if;
65 end EUC_To_JIS;
67 ----------------
68 -- JIS_To_EUC --
69 ----------------
71 procedure JIS_To_EUC
72 (J : in Wide_Character;
73 EUC1 : out Character;
74 EUC2 : out Character)
76 JIS1 : constant Natural := Wide_Character'Pos (J) / 256;
77 JIS2 : constant Natural := Wide_Character'Pos (J) rem 256;
79 begin
80 if JIS1 = 0 then
81 EUC1 := Character'Val (EUC_Hankaku_Kana);
82 EUC2 := Character'Val (JIS2);
84 else
85 EUC1 := Character'Val (JIS1 + 16#80#);
86 EUC2 := Character'Val (JIS2 + 16#80#);
87 end if;
88 end JIS_To_EUC;
90 ----------------------
91 -- JIS_To_Shift_JIS --
92 ----------------------
94 procedure JIS_To_Shift_JIS
95 (J : in Wide_Character;
96 SJ1 : out Character;
97 SJ2 : out Character)
99 JIS1 : Byte;
100 JIS2 : Byte;
102 begin
103 -- The following is the required algorithm, it's hard to make any
104 -- more intelligent comments! This was copied from a public domain
105 -- C program called etos.c (author unknown).
107 JIS1 := Byte (Natural (Wide_Character'Pos (J) / 256));
108 JIS2 := Byte (Natural (Wide_Character'Pos (J) rem 256));
110 if JIS1 > 16#5F# then
111 JIS1 := JIS1 + 16#80#;
112 end if;
114 if (JIS1 mod 2) = 0 then
115 SJ1 := Character'Val ((JIS1 - 16#30#) / 2 + 16#88#);
116 SJ2 := Character'Val (JIS2 + 16#7E#);
118 else
119 if JIS2 >= 16#60# then
120 JIS2 := JIS2 + 16#01#;
121 end if;
123 SJ1 := Character'Val ((JIS1 - 16#31#) / 2 + 16#89#);
124 SJ2 := Character'Val (JIS2 + 16#1F#);
125 end if;
126 end JIS_To_Shift_JIS;
128 ----------------------
129 -- Shift_JIS_To_JIS --
130 ----------------------
132 function Shift_JIS_To_JIS (SJ1, SJ2 : Character) return Wide_Character is
133 SJIS1 : Byte;
134 SJIS2 : Byte;
135 JIS1 : Byte;
136 JIS2 : Byte;
138 begin
139 -- The following is the required algorithm, it's hard to make any
140 -- more intelligent comments! This was copied from a public domain
141 -- C program called stoj.c written by shige@csk.JUNET.
143 SJIS1 := Character'Pos (SJ1);
144 SJIS2 := Character'Pos (SJ2);
146 if SJIS1 >= 16#E0# then
147 SJIS1 := SJIS1 - 16#40#;
148 end if;
150 if SJIS2 >= 16#9F# then
151 JIS1 := (SJIS1 - 16#88#) * 2 + 16#30#;
152 JIS2 := SJIS2 - 16#7E#;
154 else
155 if SJIS2 >= 16#7F# then
156 SJIS2 := SJIS2 - 16#01#;
157 end if;
159 JIS1 := (SJIS1 - 16#89#) * 2 + 16#31#;
160 JIS2 := SJIS2 - 16#1F#;
161 end if;
163 if JIS1 not in 16#20# .. 16#7E#
164 or else JIS2 not in 16#20# .. 16#7E#
165 then
166 raise Constraint_Error;
167 else
168 return Wide_Character'Val (256 * Natural (JIS1) + Natural (JIS2));
169 end if;
170 end Shift_JIS_To_JIS;
172 end System.WCh_JIS;