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 / i-c.adb < prev    next >
Text File  |  2000-07-19  |  12KB  |  440 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT COMPILER COMPONENTS                         --
  4. --                                                                          --
  5. --                         I N T E R F A C E S . C                          --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.13 $                             --
  10. --                                                                          --
  11. --          Copyright (C) 1992-1997 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 System;
  37.  
  38. package body Interfaces.C is
  39.  
  40.    -----------------------
  41.    -- Is_Nul_Terminated --
  42.    -----------------------
  43.  
  44.    --  Case of char_array
  45.  
  46.    function Is_Nul_Terminated (Item : in char_array) return Boolean is
  47.    begin
  48.       for J in Item'Range loop
  49.          if Item (J) = nul then
  50.             return True;
  51.          end if;
  52.       end loop;
  53.  
  54.       return False;
  55.    end Is_Nul_Terminated;
  56.  
  57.    --  Case of wchar_array
  58.  
  59.    function Is_Nul_Terminated (Item : in wchar_array) return Boolean is
  60.    begin
  61.       for J in Item'Range loop
  62.          if Item (J) = wide_nul then
  63.             return True;
  64.          end if;
  65.       end loop;
  66.  
  67.       return False;
  68.    end Is_Nul_Terminated;
  69.  
  70.    ------------
  71.    -- To_Ada --
  72.    ------------
  73.  
  74.    --  Convert char to Character
  75.  
  76.    function To_Ada (Item : char) return Character is
  77.    begin
  78.       return Character'Val (char'Pos (Item));
  79.    end To_Ada;
  80.  
  81.    --  Convert char_array to String (function form)
  82.  
  83.    function To_Ada
  84.      (Item     : in char_array;
  85.       Trim_Nul : in Boolean := True)
  86.       return     String
  87.    is
  88.       Count : Natural;
  89.       From  : size_t;
  90.  
  91.    begin
  92.       if Trim_Nul then
  93.          From := Item'First;
  94.  
  95.          loop
  96.             if From > Item'Last then
  97.                raise Terminator_Error;
  98.             elsif Item (From) = nul then
  99.                exit;
  100.             else
  101.                From := From + 1;
  102.             end if;
  103.          end loop;
  104.  
  105.          Count := Natural (From - Item'First);
  106.  
  107.       else
  108.          Count := Item'Length;
  109.       end if;
  110.  
  111.       declare
  112.          R : String (1 .. Count);
  113.  
  114.       begin
  115.          for J in R'Range loop
  116.             R (J) := To_Ada (Item (size_t (J) + (Item'First - 1)));
  117.          end loop;
  118.  
  119.          return R;
  120.       end;
  121.    end To_Ada;
  122.  
  123.    --  Convert char_array to String (procedure form)
  124.  
  125.    procedure To_Ada
  126.      (Item       : in char_array;
  127.       Target     : out String;
  128.       Count      : out Natural;
  129.       Trim_Nul   : in Boolean := True)
  130.    is
  131.       From : size_t;
  132.       To   : Positive;
  133.  
  134.    begin
  135.       if Trim_Nul then
  136.          From := Item'First;
  137.          loop
  138.             if From > Item'Last then
  139.                raise Terminator_Error;
  140.             elsif Item (From) = nul then
  141.                exit;
  142.             else
  143.                From := From + 1;
  144.             end if;
  145.          end loop;
  146.  
  147.          Count := Natural (From - Item'First);
  148.  
  149.       else
  150.          Count := Item'Length;
  151.       end if;
  152.  
  153.       if Count > Target'Length then
  154.          raise Constraint_Error;
  155.  
  156.       else
  157.          From := Item'First;
  158.          To   := Target'First;
  159.  
  160.          for J in 1 .. Count loop
  161.             Target (To) := Character (Item (From));
  162.             From := From + 1;
  163.             To   := To + 1;
  164.          end loop;
  165.       end if;
  166.  
  167.    end To_Ada;
  168.  
  169.    --  Convert wchar_t to Wide_Character
  170.  
  171.    function To_Ada (Item : in wchar_t) return Wide_Character is
  172.    begin
  173.       return Wide_Character (Item);
  174.    end To_Ada;
  175.  
  176.    --  Convert wchar_array to Wide_String (function form)
  177.  
  178.    function To_Ada
  179.      (Item     : in wchar_array;
  180.       Trim_Nul : in Boolean := True)
  181.       return     Wide_String
  182.    is
  183.       Count : Natural;
  184.       From  : size_t;
  185.  
  186.    begin
  187.       if Trim_Nul then
  188.          From := Item'First;
  189.  
  190.          loop
  191.             if From > Item'Last then
  192.                raise Terminator_Error;
  193.             elsif Item (From) = wide_nul then
  194.                exit;
  195.             else
  196.                From := From + 1;
  197.             end if;
  198.          end loop;
  199.  
  200.          Count := Natural (From - Item'First);
  201.  
  202.       else
  203.          Count := Item'Length;
  204.       end if;
  205.  
  206.       declare
  207.          R : Wide_String (1 .. Count);
  208.  
  209.       begin
  210.          for J in R'Range loop
  211.             R (J) := To_Ada (Item (size_t (J) + (Item'First - 1)));
  212.          end loop;
  213.  
  214.          return R;
  215.       end;
  216.    end To_Ada;
  217.  
  218.    --  Convert wchar_array to Wide_String (procedure form)
  219.  
  220.    procedure To_Ada
  221.      (Item       : in wchar_array;
  222.       Target     : out Wide_String;
  223.       Count      : out Natural;
  224.       Trim_Nul   : in Boolean := True)
  225.    is
  226.       From   : size_t;
  227.       To     : Positive;
  228.  
  229.    begin
  230.       if Trim_Nul then
  231.          From := Item'First;
  232.          loop
  233.             if From > Item'Last then
  234.                raise Terminator_Error;
  235.             elsif Item (From) = wide_nul then
  236.                exit;
  237.             else
  238.                From := From + 1;
  239.             end if;
  240.          end loop;
  241.  
  242.          Count := Natural (From - Item'First);
  243.  
  244.       else
  245.          Count := Item'Length;
  246.       end if;
  247.  
  248.       if Count > Target'Length then
  249.          raise Constraint_Error;
  250.  
  251.       else
  252.          From := Item'First;
  253.          To   := Target'First;
  254.  
  255.          for J in 1 .. Count loop
  256.             Target (To) := To_Ada (Item (From));
  257.             From := From + 1;
  258.             To   := To + 1;
  259.          end loop;
  260.       end if;
  261.  
  262.    end To_Ada;
  263.  
  264.    ----------
  265.    -- To_C --
  266.    ----------
  267.  
  268.    --  Convert Character to char
  269.  
  270.    function To_C (Item : Character) return char is
  271.    begin
  272.       return char'Val (Character'Pos (Item));
  273.    end To_C;
  274.  
  275.    --  Convert String to char_array (function form)
  276.  
  277.    function To_C
  278.      (Item       : in String;
  279.       Append_Nul : in Boolean := True)
  280.       return       char_array
  281.    is
  282.    begin
  283.       if Append_Nul then
  284.          declare
  285.             R : char_array (0 .. Item'Length);
  286.  
  287.          begin
  288.             for J in Item'Range loop
  289.                R (size_t (J - Item'First)) := To_C (Item (J));
  290.             end loop;
  291.  
  292.             R (R'Last) := nul;
  293.             return R;
  294.          end;
  295.  
  296.       else -- Append_Nul is False
  297.  
  298.          --  A nasty case, if the string is null, we must return
  299.          --  a null char_array. The lower bound of this array is
  300.          --  required to be zero (RM B.3(50)) but that is of course
  301.          --  impossible given that size_t is unsigned. This needs
  302.          --  ARG resolution, but for now GNAT returns bounds 1 .. 0
  303.  
  304.          if Item'Length = 0 then
  305.             declare
  306.                R : char_array (1 .. 0);
  307.  
  308.             begin
  309.                return R;
  310.             end;
  311.  
  312.          else
  313.             declare
  314.                R : char_array (0 .. Item'Length - 1);
  315.  
  316.             begin
  317.                for J in Item'Range loop
  318.                   R (size_t (J - Item'First)) := To_C (Item (J));
  319.                end loop;
  320.  
  321.                return R;
  322.             end;
  323.          end if;
  324.       end if;
  325.    end To_C;
  326.  
  327.    --  Convert String to char_array (procedure form)
  328.  
  329.    procedure To_C
  330.      (Item       : in String;
  331.       Target     : out char_array;
  332.       Count      : out size_t;
  333.       Append_Nul : in  Boolean := True)
  334.    is
  335.       To : size_t;
  336.  
  337.    begin
  338.       if Target'Length < Item'Length then
  339.          raise Constraint_Error;
  340.  
  341.       else
  342.          To := Target'First;
  343.          for From in Item'Range loop
  344.             Target (To) := char (Item (From));
  345.             To := To + 1;
  346.          end loop;
  347.  
  348.          if Append_Nul then
  349.             if To > Target'Last then
  350.                raise Constraint_Error;
  351.             else
  352.                Target (To) := nul;
  353.                Count := Item'Length + 1;
  354.             end if;
  355.  
  356.          else
  357.             Count := Item'Length;
  358.          end if;
  359.       end if;
  360.    end To_C;
  361.  
  362.    --  Convert Wide_Character to wchar_t
  363.  
  364.    function To_C (Item : in Wide_Character) return wchar_t is
  365.    begin
  366.       return wchar_t (Item);
  367.    end To_C;
  368.  
  369.    --  Convert Wide_String to wchar_array (function form)
  370.  
  371.    function To_C
  372.      (Item       : in Wide_String;
  373.       Append_Nul : in Boolean := True)
  374.       return       wchar_array
  375.    is
  376.    begin
  377.       if Append_Nul then
  378.          declare
  379.             R : wchar_array (0 .. Item'Length);
  380.  
  381.          begin
  382.             for J in size_t range 0 .. Item'Length - 1 loop
  383.                R (J) := To_C (Item (Integer (J) + Item'First));
  384.             end loop;
  385.  
  386.             R (R'Last) := wide_nul;
  387.             return R;
  388.          end;
  389.  
  390.       else
  391.          declare
  392.             R : wchar_array (0 .. Item'Length - 1);
  393.  
  394.          begin
  395.             for J in size_t range 0 .. Item'Length - 1 loop
  396.                R (J) := To_C (Item (Integer (J) + Item'First));
  397.             end loop;
  398.  
  399.             return R;
  400.          end;
  401.       end if;
  402.    end To_C;
  403.  
  404.    --  Convert Wide_String to wchar_array (procedure form)
  405.  
  406.    procedure To_C
  407.      (Item       : in Wide_String;
  408.       Target     : out wchar_array;
  409.       Count      : out size_t;
  410.       Append_Nul : in  Boolean := True)
  411.    is
  412.       To : size_t;
  413.  
  414.    begin
  415.       if Target'Length < Item'Length then
  416.          raise Constraint_Error;
  417.  
  418.       else
  419.          To := Target'First;
  420.          for From in Item'Range loop
  421.             Target (To) := To_C (Item (From));
  422.             To := To + 1;
  423.          end loop;
  424.  
  425.          if Append_Nul then
  426.             if To > Target'Last then
  427.                raise Constraint_Error;
  428.             else
  429.                Target (To) := wide_nul;
  430.                Count := Item'Length + 1;
  431.             end if;
  432.  
  433.          else
  434.             Count := Item'Length;
  435.          end if;
  436.       end if;
  437.    end To_C;
  438.  
  439. end Interfaces.C;
  440.