1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- S Y S T E M . W C H _ S T W --
11 -- Copyright (C) 1992-2000 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 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
34 ------------------------------------------------------------------------------
36 with Interfaces
; use Interfaces
;
37 with System
.WCh_Con
; use System
.WCh_Con
;
38 with System
.WCh_JIS
; use System
.WCh_JIS
;
40 package body System
.WCh_StW
is
42 ---------------------------
43 -- String_To_Wide_String --
44 ---------------------------
46 function String_To_Wide_String
48 EM
: WC_Encoding_Method
)
51 R
: Wide_String (1 .. S
'Length);
59 Last
: constant Natural := S
'Last;
61 function Get_Hex
(C
: Character) return Unsigned_16
;
62 -- Converts character from hex digit to value in range 0-15. The
63 -- input must be in 0-9, A-F, or a-f, and no check is needed.
66 -- Translates four hex characters starting at S (SP) to a single
67 -- wide character. Used in WCEM_Hex and WCEM_Brackets mode. SP
68 -- is not modified by the call. The resulting wide character value
69 -- is stored in R (RP). RP is not modified by the call.
71 function Get_Hex
(C
: Character) return Unsigned_16
is
73 if C
in '0' .. '9' then
74 return Character'Pos (C
) - Character'Pos ('0');
75 elsif C
in 'A' .. 'F' then
76 return Character'Pos (C
) - Character'Pos ('A') + 10;
78 return Character'Pos (C
) - Character'Pos ('a') + 10;
82 procedure Get_Hex_4
is
84 R
(RP
) := Wide_Character'Val (
85 Get_Hex
(S
(SP
+ 3)) + 16 *
86 (Get_Hex
(S
(SP
+ 2)) + 16 *
87 (Get_Hex
(S
(SP
+ 1)) + 16 *
88 (Get_Hex
(S
(SP
+ 0))))));
91 -- Start of processing for String_To_Wide_String
99 -- ESC-Hex representation
102 while SP
<= Last
- 4 loop
105 if S
(SP
) = ASCII
.ESC
then
110 R
(RP
) := Wide_Character'Val (Character'Pos (S
(SP
)));
115 -- Upper bit shift, internal code = external code
121 if S
(SP
) >= Character'Val (16#
80#
) then
122 U1
:= Character'Pos (S
(SP
));
123 U2
:= Character'Pos (S
(SP
+ 1));
124 R
(RP
) := Wide_Character'Val (256 * U1
+ U2
);
127 R
(RP
) := Wide_Character'Val (Character'Pos (S
(SP
)));
132 -- Upper bit shift, shift-JIS
134 when WCEM_Shift_JIS
=>
138 if S
(SP
) >= Character'Val (16#
80#
) then
139 R
(RP
) := Shift_JIS_To_JIS
(S
(SP
), S
(SP
+ 1));
142 R
(RP
) := Wide_Character'Val (Character'Pos (S
(SP
)));
147 -- Upper bit shift, EUC
153 if S
(SP
) >= Character'Val (16#
80#
) then
154 R
(RP
) := EUC_To_JIS
(S
(SP
), S
(SP
+ 1));
157 R
(RP
) := Wide_Character'Val (Character'Pos (S
(SP
)));
162 -- Upper bit shift, UTF-8
168 if S
(SP
) >= Character'Val (16#
80#
) then
169 U1
:= Character'Pos (S
(SP
));
170 U2
:= Character'Pos (S
(SP
+ 1));
172 U
:= Shift_Left
(U1
and 2#
00011111#
, 6) +
173 (U2
and 2#
00111111#
);
176 if U1
>= 2#
11100000#
then
177 U3
:= Character'Pos (S
(SP
));
178 U
:= Shift_Left
(U
, 6) + (U3
and 2#
00111111#
);
182 R
(RP
) := Wide_Character'Val (U
);
185 R
(RP
) := Wide_Character'Val (Character'Pos (S
(SP
)));
190 -- Brackets representation
192 when WCEM_Brackets
=>
193 while SP
<= Last
- 7 loop
197 and then S
(SP
+ 1) = '"'
198 and then S
(SP
+ 2) /= '"'
205 R
(RP
) := Wide_Character'Val (Character'Pos (S
(SP
)));
212 while SP
<= Last
loop
214 R
(RP
) := Wide_Character'Val (Character'Pos (S
(SP
)));
219 end String_To_Wide_String
;