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-io_aux.adb < prev    next >
Text File  |  2000-07-19  |  5KB  |  109 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT RUNTIME COMPONENTS                          --
  4. --                                                                          --
  5. --                          G N A T . I O _ A U X                           --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.7 $
  10. --                                                                          --
  11. --           Copyright (C) 1995-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 Interfaces.C_Streams; use Interfaces.C_Streams;
  36.  
  37. package body GNAT.IO_Aux is
  38.  
  39.    Buflen : constant := 2000;
  40.    --  Buffer length. Works for any non-zero value, larger values take
  41.    --  more stack space, smaller values require more recursion.
  42.  
  43.    -----------------
  44.    -- File_Exists --
  45.    -----------------
  46.  
  47.    function File_Exists (Name : String) return Boolean
  48.    is
  49.       Namestr : aliased String (1 .. Name'Length + 1);
  50.       --  Name as given with ASCII.NUL appended
  51.  
  52.    begin
  53.       Namestr (1 .. Name'Length) := Name;
  54.       Namestr (Name'Length + 1)  := ASCII.NUL;
  55.       return file_exists (Namestr'Address) /= 0;
  56.    end File_Exists;
  57.  
  58.    --------------
  59.    -- Get_Line --
  60.    --------------
  61.  
  62.    --  Current_Input case
  63.  
  64.    function Get_Line return String is
  65.       Buffer : String (1 .. Buflen);
  66.       --  Buffer to read in chunks of remaining line. Will work with any
  67.       --  size buffer. We choose a length so that most of the time no
  68.       --  recursion will be required.
  69.  
  70.       Last : Natural;
  71.  
  72.    begin
  73.       Ada.Text_IO.Get_Line (Buffer, Last);
  74.  
  75.       --  If the buffer is not full, then we are all done
  76.  
  77.       if Last < Buffer'Last then
  78.          return Buffer (1 .. Last);
  79.  
  80.       --  Otherwise, we still have characters left on the line. Note that
  81.       --  as specified by (RM A.10.7(19)) the end of line is not skipped
  82.       --  in this case, even if we are right at it now.
  83.  
  84.       else
  85.          return Buffer & GNAT.IO_Aux.Get_Line;
  86.       end if;
  87.    end Get_Line;
  88.  
  89.    --  Case of reading from a specified file. Note that we could certainly
  90.    --  share code between these two versions, but these are very short
  91.    --  routines, and we may as well aim for maximum speed, cutting out an
  92.    --  intermediate call (calls returning string may be somewhat slow)
  93.  
  94.    function Get_Line (File : Ada.Text_IO.File_Type) return String is
  95.       Buffer : String (1 .. Buflen);
  96.       Last   : Natural;
  97.  
  98.    begin
  99.       Ada.Text_IO.Get_Line (File, Buffer, Last);
  100.  
  101.       if Last < Buffer'Last then
  102.          return Buffer (1 .. Last);
  103.       else
  104.          return Buffer & Get_Line (File);
  105.       end if;
  106.    end Get_Line;
  107.  
  108. end GNAT.IO_Aux;
  109.