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 / g-dirope.adb < prev    next >
Text File  |  2000-07-19  |  8KB  |  257 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT COMPILER COMPONENTS                         --
  4. --                                                                          --
  5. --            G N A T . D I R E C T O R Y _ O P E R A T I O N S             --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.9 $
  10. --                                                                          --
  11. --            Copyright (C) 1998-2000 Ada Core Technologies, 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 is maintained by Ada Core Technologies Inc (http://www.gnat.com).   --
  32. --                                                                          --
  33. ------------------------------------------------------------------------------
  34.  
  35. with Unchecked_Deallocation;
  36. with Unchecked_Conversion;
  37. with System;  use System;
  38.  
  39. package body GNAT.Directory_Operations is
  40.  
  41.    type Dir_Type_Value is new System.Address;
  42.  
  43.    procedure Free is new
  44.      Unchecked_Deallocation (Dir_Type_Value, Dir_Type);
  45.  
  46.    ----------------
  47.    -- Change_Dir --
  48.    ----------------
  49.  
  50.    procedure Change_Dir (Dir_Name : in Dir_Name_Str) is
  51.       C_Dir_Name : String := Dir_Name & ASCII.NUL;
  52.  
  53.       function chdir (Dir_Name : String) return Integer;
  54.       pragma Import (C, chdir, "chdir");
  55.  
  56.    begin
  57.       if chdir (C_Dir_Name) /= 0 then
  58.          raise Directory_Error;
  59.       end if;
  60.    end Change_Dir;
  61.  
  62.    ----------------
  63.    -- Create_Dir --
  64.    ----------------
  65.  
  66.    procedure Make_Dir (Dir_Name : in Dir_Name_Str) is
  67.       C_Dir_Name : String := Dir_Name & ASCII.NUL;
  68.  
  69.       function mkdir (Dir_Name : String) return Integer;
  70.       pragma Import (C, mkdir, "__gnat_mkdir");
  71.  
  72.    begin
  73.       if mkdir (C_Dir_Name) /= 0 then
  74.          raise Directory_Error;
  75.       end if;
  76.    end Make_Dir;
  77.  
  78.    -----------
  79.    -- Close --
  80.    -----------
  81.  
  82.    procedure Close (Dir : in out Dir_Type) is
  83.  
  84.       function closedir (Directory : System.Address) return Integer;
  85.       pragma Import (C, closedir, "closedir");
  86.  
  87.       Discard : Integer;
  88.  
  89.    begin
  90.       if not Is_Open (Dir) then
  91.          raise Directory_Error;
  92.       end if;
  93.  
  94.       Discard := closedir (System.Address (Dir.all));
  95.       Free (Dir);
  96.    end Close;
  97.  
  98.    ---------------------
  99.    -- Get_Current_Dir --
  100.    ---------------------
  101.  
  102.    function Get_Current_Dir return Dir_Name_Str is
  103.       Max_Path : Integer;
  104.       pragma Import (C, Max_Path, "max_path_len");
  105.  
  106.       Current_Dir : String (1 .. Max_Path + 1);
  107.       Last        : Natural;
  108.  
  109.    begin
  110.       Get_Current_Dir (Current_Dir, Last);
  111.       return Current_Dir (1 .. Last);
  112.    end Get_Current_Dir;
  113.  
  114.    ---------------------
  115.    -- Get_Current_Dir --
  116.    ---------------------
  117.  
  118.    procedure Get_Current_Dir (Dir : out Dir_Name_Str; Last : out Natural) is
  119.       Max_Path : Integer;
  120.       pragma Import (C, Max_Path, "max_path_len");
  121.  
  122.       Path_Len : Natural := Max_Path;
  123.  
  124.       Buffer   : String (Dir'First .. Dir'First + Max_Path + 1);
  125.  
  126.       procedure Local_Get_Current_Dir
  127.         (Dir    : System.Address;
  128.          Length : System.Address);
  129.       pragma Import (C, Local_Get_Current_Dir, "Get_Current_Dir");
  130.  
  131.    begin
  132.       Local_Get_Current_Dir (Buffer'Address, Path_Len'Address);
  133.  
  134.       if Dir'Length > Path_Len then
  135.          Last := Dir'First + Path_Len - 1;
  136.       else
  137.          Last := Dir'Last;
  138.       end if;
  139.  
  140.       Dir (Buffer'First .. Last) := Buffer (Buffer'First .. Last);
  141.    end Get_Current_Dir;
  142.  
  143.    -------------
  144.    -- Is_Open --
  145.    -------------
  146.  
  147.    function Is_Open (Dir : in Dir_Type) return Boolean is
  148.    begin
  149.       return Dir /= Null_Dir
  150.         and then System.Address (Dir.all) /= System.Null_Address;
  151.    end Is_Open;
  152.  
  153.    ----------
  154.    -- Open --
  155.    ----------
  156.  
  157.    procedure Open
  158.      (Dir      : out Dir_Type;
  159.       Dir_Name : in  Dir_Name_Str)
  160.    is
  161.       C_File_Name : String := Dir_Name & ASCII.NUL;
  162.  
  163.       function opendir
  164.         (File_Name : String)
  165.          return      Dir_Type_Value;
  166.       pragma Import (C, opendir, "opendir");
  167.  
  168.    begin
  169.       Dir := new Dir_Type_Value'(opendir (C_File_Name));
  170.  
  171.       if not Is_Open (Dir) then
  172.          Free (Dir);
  173.          Dir := Null_Dir;
  174.          raise Directory_Error;
  175.       end if;
  176.    end Open;
  177.  
  178.    ----------
  179.    -- Read --
  180.    ----------
  181.  
  182.    procedure Read
  183.      (Dir  : in out Dir_Type;
  184.       Str  : out String;
  185.       Last : out Natural)
  186.    is
  187.       Filename_Addr : Address;
  188.       Filename_Len  : Integer;
  189.  
  190.       Buffer : array (0 .. 1024) of Character;
  191.       --  1024 is the value of FILENAME_MAX in stdio.h
  192.  
  193.       function readdir_gnat
  194.          (Directory : System.Address;
  195.           Buffer    : System.Address)
  196.           return      System.Address;
  197.       pragma Import (C, readdir_gnat, "readdir_gnat");
  198.  
  199.       function strlen (S : Address) return Integer;
  200.       pragma Import (C, strlen, "strlen");
  201.  
  202.    begin
  203.       if not Is_Open (Dir) then
  204.          raise Directory_Error;
  205.       end if;
  206.  
  207.       Filename_Addr :=
  208.         readdir_gnat (System.Address (Dir.all), Buffer'Address);
  209.  
  210.       if Filename_Addr = System.Null_Address then
  211.          Last := 0;
  212.          return;
  213.       end if;
  214.  
  215.       Filename_Len  := strlen (Filename_Addr);
  216.  
  217.       if Str'Length > Filename_Len then
  218.          Last := Str'First + Filename_Len - 1;
  219.       else
  220.          Last := Str'Last;
  221.       end if;
  222.  
  223.       declare
  224.          subtype Path_String is String (1 .. Filename_Len);
  225.          type    Path_String_Access is access Path_String;
  226.  
  227.          function Address_To_Access is new
  228.            Unchecked_Conversion
  229.              (Source => Address,
  230.               Target => Path_String_Access);
  231.  
  232.          Path_Access : Path_String_Access :=
  233.                          Address_To_Access (Filename_Addr);
  234.  
  235.       begin
  236.          for J in Str'First .. Last loop
  237.             Str (J) := Path_Access (J - Str'First + 1);
  238.          end loop;
  239.       end;
  240.    end Read;
  241.  
  242.    -------------------------
  243.    -- Read_Is_Thread_Sage --
  244.    -------------------------
  245.  
  246.    function Read_Is_Thread_Safe return Boolean is
  247.  
  248.       function readdir_is_thread_safe return Integer;
  249.       pragma Import (C, readdir_is_thread_safe, "readdir_is_thread_safe");
  250.  
  251.    begin
  252.       return (readdir_is_thread_safe /= 0);
  253.    end Read_Is_Thread_Safe;
  254.  
  255.  
  256. end GNAT.Directory_Operations;
  257.