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-wchjis.adb < prev    next >
Text File  |  2000-07-19  |  6KB  |  174 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT RUNTIME COMPONENTS                          --
  4. --                                                                          --
  5. --                       S Y S T E M . W C H _ J I S                        --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.5 $                              --
  10. --                                                                          --
  11. --        Copyright (C) 1992,1993,1994 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. package body System.WCh_JIS is
  37.  
  38.    type Byte is mod 256;
  39.  
  40.    EUC_Hankaku_Kana : constant Byte := 16#8E#;
  41.    --  Prefix byte in EUC for Hankaku Kana (small Katakana). Such characters
  42.    --  in EUC are represented by a prefix byte followed by the code, which
  43.    --  is in the upper half (the corresponding JIS internal code is in the
  44.    --  range 16#0080# - 16#00FF#).
  45.  
  46.    function EUC_To_JIS (EUC1, EUC2 : Character) return Wide_Character is
  47.       EUC1B  : constant Byte := Character'Pos (EUC1);
  48.       EUC2B  : constant Byte := Character'Pos (EUC2);
  49.  
  50.    begin
  51.       if EUC2B not in 16#A0# .. 16#FE# then
  52.          raise Constraint_Error;
  53.       end if;
  54.  
  55.       if EUC1B = EUC_Hankaku_Kana then
  56.          return Wide_Character'Val (EUC2B);
  57.  
  58.       else
  59.          if EUC1B not in 16#A0# .. 16#FE# then
  60.             raise Constraint_Error;
  61.          else
  62.             return Wide_Character'Val
  63.               (256 * Natural (EUC1B and 16#7F#) + Natural (EUC2B and 16#7F#));
  64.          end if;
  65.       end if;
  66.    end EUC_To_JIS;
  67.  
  68.    ----------------
  69.    -- JIS_To_EUC --
  70.    ----------------
  71.  
  72.    procedure JIS_To_EUC
  73.      (J    : in Wide_Character;
  74.       EUC1 : out Character;
  75.       EUC2 : out Character)
  76.    is
  77.       JIS1 : constant Natural := Wide_Character'Pos (J) / 256;
  78.       JIS2 : constant Natural := Wide_Character'Pos (J) rem 256;
  79.  
  80.    begin
  81.       if JIS1 = 0 then
  82.          EUC1 := Character'Val (EUC_Hankaku_Kana);
  83.          EUC2 := Character'Val (JIS2);
  84.  
  85.       else
  86.          EUC1 := Character'Val (JIS1 + 16#80#);
  87.          EUC2 := Character'Val (JIS2 + 16#80#);
  88.       end if;
  89.    end JIS_To_EUC;
  90.  
  91.    ----------------------
  92.    -- JIS_To_Shift_JIS --
  93.    ----------------------
  94.  
  95.    procedure JIS_To_Shift_JIS
  96.      (J   : in Wide_Character;
  97.       SJ1 : out Character;
  98.       SJ2 : out Character)
  99.    is
  100.       JIS1 : Byte;
  101.       JIS2 : Byte;
  102.  
  103.    begin
  104.       --  The following is the required algorithm, it's hard to make any
  105.       --  more intelligent comments! This was copied from a public domain
  106.       --  C program called etos.c (author unknown).
  107.  
  108.       JIS1 := Byte (Natural (Wide_Character'Pos (J) / 256));
  109.       JIS2 := Byte (Natural (Wide_Character'Pos (J) rem 256));
  110.  
  111.       if JIS1 > 16#5F# then
  112.          JIS1 := JIS1 + 16#80#;
  113.       end if;
  114.  
  115.       if (JIS1 mod 2) = 0 then
  116.          SJ1 := Character'Val ((JIS1 - 16#30#) / 2 + 16#88#);
  117.          SJ2 := Character'Val (JIS2 + 16#7E#);
  118.  
  119.       else
  120.          if JIS2 >= 16#60# then
  121.             JIS2 := JIS2 + 16#01#;
  122.          end if;
  123.  
  124.          SJ1 := Character'Val ((JIS1 - 16#31#) / 2 + 16#89#);
  125.          SJ2 := Character'Val (JIS2 + 16#1F#);
  126.       end if;
  127.    end JIS_To_Shift_JIS;
  128.  
  129.    ----------------------
  130.    -- Shift_JIS_To_JIS --
  131.    ----------------------
  132.  
  133.    function Shift_JIS_To_JIS (SJ1, SJ2 : Character) return Wide_Character is
  134.       SJIS1 : Byte;
  135.       SJIS2 : Byte;
  136.       JIS1  : Byte;
  137.       JIS2  : Byte;
  138.  
  139.    begin
  140.       --  The following is the required algorithm, it's hard to make any
  141.       --  more intelligent comments! This was copied from a public domain
  142.       --  C program called stoj.c written by shige@csk.JUNET.
  143.  
  144.       SJIS1 := Character'Pos (SJ1);
  145.       SJIS2 := Character'Pos (SJ2);
  146.  
  147.       if SJIS1 >= 16#E0# then
  148.          SJIS1 := SJIS1 - 16#40#;
  149.       end if;
  150.  
  151.       if SJIS2 >= 16#9F# then
  152.          JIS1 := (SJIS1 - 16#88#) * 2 + 16#30#;
  153.          JIS2 := SJIS2 - 16#7E#;
  154.  
  155.       else
  156.          if SJIS2 >= 16#7F# then
  157.             SJIS2 := SJIS2 - 16#01#;
  158.          end if;
  159.  
  160.          JIS1 := (SJIS1 - 16#89#) * 2 + 16#31#;
  161.          JIS2 := SJIS2 - 16#1F#;
  162.       end if;
  163.  
  164.       if JIS1 not in 16#20# .. 16#7E#
  165.         or else JIS2 not in 16#20# .. 16#7E#
  166.       then
  167.          raise Constraint_Error;
  168.       else
  169.          return Wide_Character'Val (256 * Natural (JIS1) + Natural (JIS2));
  170.       end if;
  171.    end Shift_JIS_To_JIS;
  172.  
  173. end System.WCh_JIS;
  174.