1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- S Y S T E M . W C H _ C N V --
9 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
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. --
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. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 -- This package contains generic subprograms used for converting between
35 -- sequences of Character and Wide_Character. All access to wide character
36 -- sequences is isolated in this unit.
38 with Interfaces; use Interfaces;
39 with System.WCh_Con; use System.WCh_Con;
40 with System.WCh_JIS; use System.WCh_JIS;
42 package body System.WCh_Cnv is
44 --------------------------------
45 -- Char_Sequence_To_Wide_Char --
46 --------------------------------
48 function Char_Sequence_To_Wide_Char
50 EM : WC_Encoding_Method)
58 procedure Get_Hex (N : Character);
59 -- If N is a hex character, then set B1 to 16 * B1 + character N.
60 -- Raise Constraint_Error if character N is not a hex character.
66 procedure Get_Hex (N : Character) is
67 B2 : constant Integer := Character'Pos (N);
70 if B2 in Character'Pos ('0') .. Character'Pos ('9') then
71 B1 := B1 * 16 + B2 - Character'Pos ('0');
73 elsif B2 in Character'Pos ('A') .. Character'Pos ('F') then
74 B1 := B1 * 16 + B2 - (Character'Pos ('A') - 10);
76 elsif B2 in Character'Pos ('a') .. Character'Pos ('f') then
77 B1 := B1 * 16 + B2 - (Character'Pos ('a') - 10);
80 raise Constraint_Error;
84 -- Start of processing for Char_Sequence_To_Wide_Char
90 if C /= ASCII.ESC then
91 return Wide_Character'Val (Character'Pos (C));
100 return Wide_Character'Val (B1);
104 if C > ASCII.DEL then
107 (Integer (256 * Character'Pos (C)) +
108 Character'Pos (In_Char));
110 return Wide_Character'Val (Character'Pos (C));
113 when WCEM_Shift_JIS =>
114 if C > ASCII.DEL then
115 return Shift_JIS_To_JIS (C, In_Char);
117 return Wide_Character'Val (Character'Pos (C));
121 if C > ASCII.DEL then
122 return EUC_To_JIS (C, In_Char);
124 return Wide_Character'Val (Character'Pos (C));
128 if C > ASCII.DEL then
130 -- 16#0080#-16#07ff#: 2#110xxxxx# 2#10xxxxxx#
131 -- 16#0800#-16#ffff#: 2#1110xxxx# 2#10xxxxxx# 2#10xxxxxx#
133 U := Unsigned_16 (Character'Pos (C));
135 if (U and 2#11100000#) = 2#11000000# then
136 W := Shift_Left (U and 2#00011111#, 6);
137 U := Unsigned_16 (Character'Pos (In_Char));
139 if (U and 2#11000000#) /= 2#10000000# then
140 raise Constraint_Error;
143 W := W or (U and 2#00111111#);
145 elsif (U and 2#11110000#) = 2#11100000# then
146 W := Shift_Left (U and 2#00001111#, 12);
147 U := Unsigned_16 (Character'Pos (In_Char));
149 if (U and 2#11000000#) /= 2#10000000# then
150 raise Constraint_Error;
153 W := W or Shift_Left (U and 2#00111111#, 6);
154 U := Unsigned_16 (Character'Pos (In_Char));
156 if (U and 2#11000000#) /= 2#10000000# then
157 raise Constraint_Error;
160 W := W or (U and 2#00111111#);
163 raise Constraint_Error;
166 return Wide_Character'Val (W);
169 return Wide_Character'Val (Character'Pos (C));
172 when WCEM_Brackets =>
175 return Wide_Character'Val (Character'Pos (C));
178 if In_Char /= '"' then
179 raise Constraint_Error;
193 raise Constraint_Error;
197 if In_Char /= ']' then
198 raise Constraint_Error;
201 return Wide_Character'Val (B1);
204 end Char_Sequence_To_Wide_Char;
206 --------------------------------
207 -- Wide_Char_To_Char_Sequence --
208 --------------------------------
210 procedure Wide_Char_To_Char_Sequence
211 (WC : Wide_Character;
212 EM : WC_Encoding_Method)
214 Val : constant Natural := Wide_Character'Pos (WC);
215 Hexc : constant array (0 .. 15) of Character := "0123456789ABCDEF";
224 Out_Char (Character'Val (Val));
227 Out_Char (ASCII.ESC);
228 Out_Char (Hexc (Val / (16**3)));
229 Out_Char (Hexc ((Val / (16**2)) mod 16));
230 Out_Char (Hexc ((Val / 16) mod 16));
231 Out_Char (Hexc (Val mod 16));
236 Out_Char (Character'Val (Val));
238 elsif Val < 16#8000# then
239 raise Constraint_Error;
242 Out_Char (Character'Val (Val / 256));
243 Out_Char (Character'Val (Val mod 256));
246 when WCEM_Shift_JIS =>
248 Out_Char (Character'Val (Val));
250 JIS_To_Shift_JIS (WC, C1, C2);
257 Out_Char (Character'Val (Val));
259 JIS_To_EUC (WC, C1, C2);
265 U := Unsigned_16 (Val);
267 -- 16#0000#-16#007f#: 2#0xxxxxxx#
268 -- 16#0080#-16#07ff#: 2#110xxxxx# 2#10xxxxxx#
269 -- 16#0800#-16#ffff#: 2#1110xxxx# 2#10xxxxxx# 2#10xxxxxx#
272 Out_Char (Character'Val (U));
274 elsif U < 16#0800# then
275 Out_Char (Character'Val (2#11000000# or Shift_Right (U, 6)));
276 Out_Char (Character'Val (2#10000000# or (U and 2#00111111#)));
279 Out_Char (Character'Val (2#11100000# or Shift_Right (U, 12)));
280 Out_Char (Character'Val (2#10000000# or (Shift_Right (U, 6)
282 Out_Char (Character'Val (2#10000000# or (U and 2#00111111#)));
285 when WCEM_Brackets =>
288 Out_Char (Character'Val (Val));
293 Out_Char (Hexc (Val / (16**3)));
294 Out_Char (Hexc ((Val / (16**2)) mod 16));
295 Out_Char (Hexc ((Val / 16) mod 16));
296 Out_Char (Hexc (Val mod 16));
301 end Wide_Char_To_Char_Sequence;