OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-wchstw.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUNTIME COMPONENTS                          --
4 --                                                                          --
5 --                       S Y S T E M . W C H _ S T W                        --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --                                                                          --
10 --          Copyright (C) 1992-2000 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 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
32 --                                                                          --
33 ------------------------------------------------------------------------------
34
35 with Interfaces;     use Interfaces;
36 with System.WCh_Con; use System.WCh_Con;
37 with System.WCh_JIS; use System.WCh_JIS;
38
39 package body System.WCh_StW is
40
41    ---------------------------
42    -- String_To_Wide_String --
43    ---------------------------
44
45    function String_To_Wide_String
46      (S    : String;
47       EM   : WC_Encoding_Method)
48       return Wide_String
49    is
50       R  : Wide_String (1 .. S'Length);
51       RP : Natural;
52       SP : Natural;
53       U1 : Unsigned_16;
54       U2 : Unsigned_16;
55       U3 : Unsigned_16;
56       U  : Unsigned_16;
57
58       Last : constant Natural := S'Last;
59
60       function Get_Hex (C : Character) return Unsigned_16;
61       --  Converts character from hex digit to value in range 0-15. The
62       --  input must be in 0-9, A-F, or a-f, and no check is needed.
63
64       procedure Get_Hex_4;
65       --  Translates four hex characters starting at S (SP) to a single
66       --  wide character. Used in WCEM_Hex and WCEM_Brackets mode. SP
67       --  is not modified by the call. The resulting wide character value
68       --  is stored in R (RP). RP is not modified by the call.
69
70       function Get_Hex (C : Character) return Unsigned_16 is
71       begin
72          if C in '0' .. '9' then
73             return Character'Pos (C) - Character'Pos ('0');
74          elsif C in 'A' .. 'F' then
75             return Character'Pos (C) - Character'Pos ('A') + 10;
76          else
77             return Character'Pos (C) - Character'Pos ('a') + 10;
78          end if;
79       end Get_Hex;
80
81       procedure Get_Hex_4 is
82       begin
83          R (RP) := Wide_Character'Val (
84             Get_Hex (S (SP + 3)) + 16 *
85               (Get_Hex (S (SP + 2)) + 16 *
86                 (Get_Hex (S (SP + 1)) + 16 *
87                   (Get_Hex (S (SP + 0))))));
88       end Get_Hex_4;
89
90    --  Start of processing for String_To_Wide_String
91
92    begin
93       SP := S'First;
94       RP := 0;
95
96       case EM is
97
98          --  ESC-Hex representation
99
100          when WCEM_Hex =>
101             while SP <= Last - 4 loop
102                RP := RP + 1;
103
104                if S (SP) = ASCII.ESC then
105                   SP := SP + 1;
106                   Get_Hex_4;
107                   SP := SP + 4;
108                else
109                   R (RP) := Wide_Character'Val (Character'Pos (S (SP)));
110                   SP := SP + 1;
111                end if;
112             end loop;
113
114          --  Upper bit shift, internal code = external code
115
116          when WCEM_Upper =>
117             while SP < Last loop
118                RP := RP + 1;
119
120                if S (SP) >= Character'Val (16#80#) then
121                   U1 := Character'Pos (S (SP));
122                   U2 := Character'Pos (S (SP + 1));
123                   R (RP) := Wide_Character'Val (256 * U1 + U2);
124                   SP := SP + 2;
125                else
126                   R (RP) := Wide_Character'Val (Character'Pos (S (SP)));
127                   SP := SP + 1;
128                end if;
129             end loop;
130
131          --  Upper bit shift, shift-JIS
132
133          when WCEM_Shift_JIS =>
134             while SP < Last loop
135                RP := RP + 1;
136
137                if S (SP) >= Character'Val (16#80#) then
138                   R (RP) := Shift_JIS_To_JIS (S (SP), S (SP + 1));
139                   SP := SP + 2;
140                else
141                   R (RP) := Wide_Character'Val (Character'Pos (S (SP)));
142                   SP := SP + 1;
143                end if;
144             end loop;
145
146          --  Upper bit shift, EUC
147
148          when WCEM_EUC =>
149             while SP < Last loop
150                RP := RP + 1;
151
152                if S (SP) >= Character'Val (16#80#) then
153                   R (RP) := EUC_To_JIS (S (SP), S (SP + 1));
154                   SP := SP + 2;
155                else
156                   R (RP) := Wide_Character'Val (Character'Pos (S (SP)));
157                   SP := SP + 1;
158                end if;
159             end loop;
160
161          --  Upper bit shift, UTF-8
162
163          when WCEM_UTF8 =>
164             while SP < Last loop
165                RP := RP + 1;
166
167                if S (SP) >= Character'Val (16#80#) then
168                   U1 := Character'Pos (S (SP));
169                   U2 := Character'Pos (S (SP + 1));
170
171                   U := Shift_Left (U1 and 2#00011111#, 6) +
172                          (U2 and 2#00111111#);
173                   SP := SP + 2;
174
175                   if U1 >= 2#11100000# then
176                      U3 := Character'Pos (S (SP));
177                      U := Shift_Left (U, 6) + (U3 and 2#00111111#);
178                      SP := SP + 1;
179                   end if;
180
181                   R (RP) := Wide_Character'Val (U);
182
183                else
184                   R (RP) := Wide_Character'Val (Character'Pos (S (SP)));
185                   SP := SP + 1;
186                end if;
187             end loop;
188
189          --  Brackets representation
190
191          when WCEM_Brackets =>
192             while SP <= Last - 7 loop
193                RP := RP + 1;
194
195                if S (SP) = '['
196                  and then S (SP + 1) = '"'
197                  and then S (SP + 2) /= '"'
198                then
199                   SP := SP + 2;
200                   Get_Hex_4;
201                   SP := SP + 6;
202
203                else
204                   R (RP) := Wide_Character'Val (Character'Pos (S (SP)));
205                   SP := SP + 1;
206                end if;
207             end loop;
208
209       end case;
210
211       while SP <= Last loop
212          RP := RP + 1;
213          R (RP) := Wide_Character'Val (Character'Pos (S (SP)));
214          SP := SP + 1;
215       end loop;
216
217       return R (1 .. RP);
218    end String_To_Wide_String;
219
220 end System.WCh_StW;