OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-wchwts.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUNTIME COMPONENTS                          --
4 --                                                                          --
5 --                       S Y S T E M . W C H _ W T S                        --
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_WtS is
40
41    ---------------------------
42    -- Wide_String_To_String --
43    ---------------------------
44
45    function Wide_String_To_String
46      (S    : Wide_String;
47       EM   : WC_Encoding_Method)
48       return String
49    is
50       R  : String (1 .. 5 * S'Length); -- worst case length!
51       RP : Natural;
52       C1 : Character;
53       C2 : Character;
54
55    begin
56       RP := 0;
57
58       for SP in S'Range loop
59          declare
60             C   : constant Wide_Character := S (SP);
61             CV  : constant Unsigned_16    := Wide_Character'Pos (C);
62             Hex : constant array (Unsigned_16 range 0 .. 15) of Character :=
63                     "0123456789ABCDEF";
64
65          begin
66             if CV <= 127 then
67                RP := RP + 1;
68                R (RP) := Character'Val (CV);
69
70             else
71                case EM is
72
73                   --  Hex ESC sequence encoding
74
75                   when WCEM_Hex =>
76                      if CV <= 16#FF# then
77                         RP := RP + 1;
78                         R (RP) := Character'Val (CV);
79
80                      else
81                         R (RP + 1) := ASCII.ESC;
82                         R (RP + 2) := Hex (Shift_Right (CV, 12));
83                         R (RP + 3) := Hex (Shift_Right (CV, 8)  and 16#000F#);
84                         R (RP + 4) := Hex (Shift_Right (CV, 4)  and 16#000F#);
85                         R (RP + 5) := Hex (CV                   and 16#000F#);
86                         RP := RP + 5;
87                      end if;
88
89                   --  Upper bit shift (internal code = external code)
90
91                   when WCEM_Upper =>
92                      R (RP + 1) := Character'Val (Shift_Right (CV, 8));
93                      R (RP + 2) := Character'Val (CV and 16#FF#);
94                      RP := RP + 2;
95
96                   --  Upper bit shift (EUC)
97
98                   when WCEM_EUC =>
99                      JIS_To_EUC (C, C1, C2);
100                      R (RP + 1) := C1;
101                      R (RP + 2) := C2;
102                      RP := RP + 2;
103
104                   --  Upper bit shift (Shift-JIS)
105
106                   when WCEM_Shift_JIS =>
107                      JIS_To_Shift_JIS (C, C1, C2);
108                      R (RP + 1) := C1;
109                      R (RP + 2) := C2;
110                      RP := RP + 2;
111
112                   --  Upper bit shift (UTF-8)
113
114                   --    16#0080#-16#07ff#: 2#110xxxxx# 2#10xxxxxx#
115                   --    16#0800#-16#ffff#: 2#1110xxxx# 2#10xxxxxx# 2#10xxxxxx#
116
117                   when WCEM_UTF8 =>
118                      if CV < 16#0800# then
119                         R (RP + 1) :=
120                           Character'Val (2#11000000# or Shift_Right (CV, 6));
121                         R (RP + 2) :=
122                           Character'Val (2#10000000# or (CV and 2#00111111#));
123                         RP := RP + 2;
124
125                      else
126                         R (RP + 1) :=
127                           Character'Val (2#11100000# or Shift_Right (CV, 12));
128                         R (RP + 2) :=
129                           Character'Val (2#10000000# or
130                                           (Shift_Right (CV, 6) and
131                                                                2#00111111#));
132                         R (RP + 3) :=
133                           Character'Val (2#10000000# or (CV and 2#00111111#));
134                         RP := RP + 3;
135                      end if;
136
137                   --  Brackets encoding
138
139                   when WCEM_Brackets =>
140                      if CV <= 16#FF# then
141                         RP := RP + 1;
142                         R (RP) := Character'Val (CV);
143
144                      else
145                         R (RP + 1) := '[';
146                         R (RP + 2) := '"';
147                         R (RP + 3) := Hex (Shift_Right (CV, 12));
148                         R (RP + 4) := Hex (Shift_Right (CV, 8)  and 16#000F#);
149                         R (RP + 5) := Hex (Shift_Right (CV, 4)  and 16#000F#);
150                         R (RP + 6) := Hex (CV                   and 16#000F#);
151                         R (RP + 7) := '"';
152                         R (RP + 8) := ']';
153                         RP := RP + 8;
154                      end if;
155
156                end case;
157             end if;
158          end;
159       end loop;
160
161       return R (1 .. RP);
162    end Wide_String_To_String;
163
164 end System.WCh_WtS;