1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- S Y S T E M . W C H _ J I S --
10 -- Copyright (C) 1992,1993,1994 Free Software Foundation, Inc. --
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. --
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. --
30 -- GNAT was originally developed by the GNAT team at New York University. --
31 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
33 ------------------------------------------------------------------------------
35 package body System
.WCh_JIS
is
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
);
50 if EUC2B
not in 16#A0#
.. 16#FE#
then
51 raise Constraint_Error
;
54 if EUC1B
= EUC_Hankaku_Kana
then
55 return Wide_Character'Val (EUC2B
);
58 if EUC1B
not in 16#A0#
.. 16#FE#
then
59 raise Constraint_Error
;
61 return Wide_Character'Val
62 (256 * Natural (EUC1B
and 16#
7F#
) + Natural (EUC2B
and 16#
7F#
));
72 (J
: in Wide_Character;
76 JIS1
: constant Natural := Wide_Character'Pos (J
) / 256;
77 JIS2
: constant Natural := Wide_Character'Pos (J
) rem 256;
81 EUC1
:= Character'Val (EUC_Hankaku_Kana
);
82 EUC2
:= Character'Val (JIS2
);
85 EUC1
:= Character'Val (JIS1
+ 16#
80#
);
86 EUC2
:= Character'Val (JIS2
+ 16#
80#
);
90 ----------------------
91 -- JIS_To_Shift_JIS --
92 ----------------------
94 procedure JIS_To_Shift_JIS
95 (J
: in Wide_Character;
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#
;
114 if (JIS1
mod 2) = 0 then
115 SJ1
:= Character'Val ((JIS1
- 16#
30#
) / 2 + 16#
88#
);
116 SJ2
:= Character'Val (JIS2
+ 16#
7E#
);
119 if JIS2
>= 16#
60#
then
120 JIS2
:= JIS2
+ 16#
01#
;
123 SJ1
:= Character'Val ((JIS1
- 16#
31#
) / 2 + 16#
89#
);
124 SJ2
:= Character'Val (JIS2
+ 16#
1F#
);
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
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#
;
150 if SJIS2
>= 16#
9F#
then
151 JIS1
:= (SJIS1
- 16#
88#
) * 2 + 16#
30#
;
152 JIS2
:= SJIS2
- 16#
7E#
;
155 if SJIS2
>= 16#
7F#
then
156 SJIS2
:= SJIS2
- 16#
01#
;
159 JIS1
:= (SJIS1
- 16#
89#
) * 2 + 16#
31#
;
160 JIS2
:= SJIS2
- 16#
1F#
;
163 if JIS1
not in 16#
20#
.. 16#
7E#
164 or else JIS2
not in 16#
20#
.. 16#
7E#
166 raise Constraint_Error
;
168 return Wide_Character'Val (256 * Natural (JIS1
) + Natural (JIS2
));
170 end Shift_JIS_To_JIS
;