1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- S Y S T E M . W C H _ C N V --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNAT was originally developed by the GNAT team at New York University. --
32 -- Extensive contributions were provided by Ada Core Technologies Inc. --
34 ------------------------------------------------------------------------------
36 -- This package contains generic subprograms used for converting between
37 -- sequences of Character and Wide_Character. All access to wide character
38 -- sequences is isolated in this unit.
40 with Interfaces
; use Interfaces
;
41 with System
.WCh_Con
; use System
.WCh_Con
;
42 with System
.WCh_JIS
; use System
.WCh_JIS
;
44 package body System
.WCh_Cnv
is
46 --------------------------------
47 -- Char_Sequence_To_Wide_Char --
48 --------------------------------
50 function Char_Sequence_To_Wide_Char
52 EM
: WC_Encoding_Method
)
60 procedure Get_Hex
(N
: Character);
61 -- If N is a hex character, then set B1 to 16 * B1 + character N.
62 -- Raise Constraint_Error if character N is not a hex character.
68 procedure Get_Hex
(N
: Character) is
69 B2
: constant Integer := Character'Pos (N
);
72 if B2
in Character'Pos ('0') .. Character'Pos ('9') then
73 B1
:= B1
* 16 + B2
- Character'Pos ('0');
75 elsif B2
in Character'Pos ('A') .. Character'Pos ('F') then
76 B1
:= B1
* 16 + B2
- (Character'Pos ('A') - 10);
78 elsif B2
in Character'Pos ('a') .. Character'Pos ('f') then
79 B1
:= B1
* 16 + B2
- (Character'Pos ('a') - 10);
82 raise Constraint_Error
;
86 -- Start of processing for Char_Sequence_To_Wide_Char
92 if C
/= ASCII
.ESC
then
93 return Wide_Character'Val (Character'Pos (C
));
102 return Wide_Character'Val (B1
);
106 if C
> ASCII
.DEL
then
109 (Integer (256 * Character'Pos (C
)) +
110 Character'Pos (In_Char
));
112 return Wide_Character'Val (Character'Pos (C
));
115 when WCEM_Shift_JIS
=>
116 if C
> ASCII
.DEL
then
117 return Shift_JIS_To_JIS
(C
, In_Char
);
119 return Wide_Character'Val (Character'Pos (C
));
123 if C
> ASCII
.DEL
then
124 return EUC_To_JIS
(C
, In_Char
);
126 return Wide_Character'Val (Character'Pos (C
));
130 if C
> ASCII
.DEL
then
132 -- 16#0080#-16#07ff#: 2#110xxxxx# 2#10xxxxxx#
133 -- 16#0800#-16#ffff#: 2#1110xxxx# 2#10xxxxxx# 2#10xxxxxx#
135 U
:= Unsigned_16
(Character'Pos (C
));
137 if (U
and 2#
11100000#
) = 2#
11000000#
then
138 W
:= Shift_Left
(U
and 2#
00011111#
, 6);
139 U
:= Unsigned_16
(Character'Pos (In_Char
));
141 if (U
and 2#
11000000#
) /= 2#
10000000#
then
142 raise Constraint_Error
;
145 W
:= W
or (U
and 2#
00111111#
);
147 elsif (U
and 2#
11110000#
) = 2#
11100000#
then
148 W
:= Shift_Left
(U
and 2#
00001111#
, 12);
149 U
:= Unsigned_16
(Character'Pos (In_Char
));
151 if (U
and 2#
11000000#
) /= 2#
10000000#
then
152 raise Constraint_Error
;
155 W
:= W
or Shift_Left
(U
and 2#
00111111#
, 6);
156 U
:= Unsigned_16
(Character'Pos (In_Char
));
158 if (U
and 2#
11000000#
) /= 2#
10000000#
then
159 raise Constraint_Error
;
162 W
:= W
or (U
and 2#
00111111#
);
165 raise Constraint_Error
;
168 return Wide_Character'Val (W
);
171 return Wide_Character'Val (Character'Pos (C
));
174 when WCEM_Brackets
=>
177 return Wide_Character'Val (Character'Pos (C
));
180 if In_Char
/= '"' then
181 raise Constraint_Error
;
195 raise Constraint_Error
;
199 if In_Char
/= ']' then
200 raise Constraint_Error
;
203 return Wide_Character'Val (B1
);
206 end Char_Sequence_To_Wide_Char
;
208 --------------------------------
209 -- Wide_Char_To_Char_Sequence --
210 --------------------------------
212 procedure Wide_Char_To_Char_Sequence
213 (WC
: Wide_Character;
214 EM
: WC_Encoding_Method
)
216 Val
: constant Natural := Wide_Character'Pos (WC
);
217 Hexc
: constant array (0 .. 15) of Character := "0123456789ABCDEF";
226 Out_Char
(Character'Val (Val
));
229 Out_Char
(ASCII
.ESC
);
230 Out_Char
(Hexc
(Val
/ (16**3)));
231 Out_Char
(Hexc
((Val
/ (16**2)) mod 16));
232 Out_Char
(Hexc
((Val
/ 16) mod 16));
233 Out_Char
(Hexc
(Val
mod 16));
238 Out_Char
(Character'Val (Val
));
240 elsif Val
< 16#
8000#
then
241 raise Constraint_Error
;
244 Out_Char
(Character'Val (Val
/ 256));
245 Out_Char
(Character'Val (Val
mod 256));
248 when WCEM_Shift_JIS
=>
250 Out_Char
(Character'Val (Val
));
252 JIS_To_Shift_JIS
(WC
, C1
, C2
);
259 Out_Char
(Character'Val (Val
));
261 JIS_To_EUC
(WC
, C1
, C2
);
267 U
:= Unsigned_16
(Val
);
269 -- 16#0000#-16#007f#: 2#0xxxxxxx#
270 -- 16#0080#-16#07ff#: 2#110xxxxx# 2#10xxxxxx#
271 -- 16#0800#-16#ffff#: 2#1110xxxx# 2#10xxxxxx# 2#10xxxxxx#
274 Out_Char
(Character'Val (U
));
276 elsif U
< 16#
0800#
then
277 Out_Char
(Character'Val (2#
11000000#
or Shift_Right
(U
, 6)));
278 Out_Char
(Character'Val (2#
10000000#
or (U
and 2#
00111111#
)));
281 Out_Char
(Character'Val (2#
11100000#
or Shift_Right
(U
, 12)));
282 Out_Char
(Character'Val (2#
10000000#
or (Shift_Right
(U
, 6)
284 Out_Char
(Character'Val (2#
10000000#
or (U
and 2#
00111111#
)));
287 when WCEM_Brackets
=>
290 Out_Char
(Character'Val (Val
));
295 Out_Char
(Hexc
(Val
/ (16**3)));
296 Out_Char
(Hexc
((Val
/ (16**2)) mod 16));
297 Out_Char
(Hexc
((Val
/ 16) mod 16));
298 Out_Char
(Hexc
(Val
mod 16));
303 end Wide_Char_To_Char_Sequence
;