home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / gnat-2.06-src.tgz / tar.out / fsf / gnat / ada / s-wchwts.adb < prev    next >
Text File  |  1996-09-28  |  4KB  |  105 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.7 $                              --
  10. --                                                                          --
  11. --        Copyright (c) 1992,1993,1994,1995 NYU, All Rights Reserved        --
  12. --                                                                          --
  13. -- The GNAT library is free software; you can redistribute it and/or modify --
  14. -- it under terms of the GNU Library General Public License as published by --
  15. -- the Free Software  Foundation; either version 2, or (at your option) any --
  16. -- later version.  The GNAT library is distributed in the hope that it will --
  17. -- be useful, but WITHOUT ANY WARRANTY;  without even  the implied warranty --
  18. -- of MERCHANTABILITY  or  FITNESS FOR  A PARTICULAR PURPOSE.  See the  GNU --
  19. -- Library  General  Public  License for  more  details.  You  should  have --
  20. -- received  a copy of the GNU  Library  General Public License  along with --
  21. -- the GNAT library;  see the file  COPYING.LIB.  If not, write to the Free --
  22. -- Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.        --
  23. --                                                                          --
  24. ------------------------------------------------------------------------------
  25.  
  26. with System.WCh_Con; use System.WCh_Con;
  27. with System.WCh_JIS; use System.WCh_JIS;
  28.  
  29. package body System.WCh_WtS is
  30.  
  31.    ---------------------------
  32.    -- Wide_String_To_String --
  33.    ---------------------------
  34.  
  35.    function Wide_String_To_String
  36.      (S    : Wide_String;
  37.       EM   : WC_Encoding_Method)
  38.       return String
  39.    is
  40.       R  : String (1 .. 5 * S'Length); -- worst case length!
  41.       RP : Natural;
  42.       B1 : Natural;
  43.       B2 : Natural;
  44.       C1 : Character;
  45.       C2 : Character;
  46.  
  47.    begin
  48.       RP := 0;
  49.  
  50.       for SP in S'Range loop
  51.          declare
  52.             C   : constant Wide_Character := S (SP);
  53.             CV  : constant Natural        := Wide_Character'Pos (C);
  54.             Hex : constant array (0 .. 15) of Character := "0123456789ABCDEF";
  55.  
  56.          begin
  57.             if CV <= 127
  58.               or else (CV <= 255 and then EM = WCEM_Hex)
  59.             then
  60.                RP := RP + 1;
  61.                R (RP) := Character'Val (CV);
  62.  
  63.             else
  64.                B1 := CV / 256;
  65.                B2 := CV mod 256;
  66.  
  67.                --  Hex ESC sequence encoding
  68.  
  69.                if EM = WCEM_Hex or else EM = WCEM_None then
  70.                   R (RP + 1) := Ascii.ESC;
  71.                   R (RP + 2) := Hex (B1 / 16);
  72.                   R (RP + 3) := Hex (B1 rem 16);
  73.                   R (RP + 4) := Hex (B2 / 16);
  74.                   R (RP + 5) := Hex (B2 rem 16);
  75.                   RP := RP + 5;
  76.  
  77.                --  Upper bit shift (internal code = external code)
  78.  
  79.                elsif EM = WCEM_Upper then
  80.                   C1 := Character'Val (B1);
  81.                   C2 := Character'Val (B2);
  82.  
  83.                --  Upper bit shift (EUC)
  84.  
  85.                elsif EM = WCEM_EUC then
  86.                   JIS_To_EUC (C, C1, C2);
  87.  
  88.                --  Upper bit shift (Shift-JIS)
  89.  
  90.                else -- EM = WCEM_Shift_JIS
  91.                   JIS_To_Shift_JIS (C, C1, C2);
  92.                end if;
  93.  
  94.                R (RP + 1) := C1;
  95.                R (RP + 2) := C2;
  96.                RP := RP + 2;
  97.             end if;
  98.          end;
  99.       end loop;
  100.  
  101.       return R (1 .. RP);
  102.    end Wide_String_To_String;
  103.  
  104. end System.WCh_WtS;
  105.