home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / adav313.zip / gnat-3_13p-os2-bin-20010916.zip / emx / gnatlib / s-wchwts.adb < prev    next >
Text File  |  2000-07-19  |  7KB  |  166 lines

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