OSDN Git Service

2009-04-08 Thomas Quinot <quinot@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-io_aux.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUN-TIME COMPONENTS                         --
4 --                                                                          --
5 --                          G N A T . I O _ A U X                           --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --                     Copyright (C) 1995-2005, AdaCore                     --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- As a special exception,  if other files  instantiate  generics from this --
23 -- unit, or you link  this unit with other files  to produce an executable, --
24 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
25 -- covered  by the  GNU  General  Public  License.  This exception does not --
26 -- however invalidate  any other reasons why  the executable file  might be --
27 -- covered by the  GNU Public License.                                      --
28 --                                                                          --
29 -- GNAT was originally developed  by the GNAT team at  New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 with Interfaces.C_Streams; use Interfaces.C_Streams;
35
36 package body GNAT.IO_Aux is
37
38    Buflen : constant := 2000;
39    --  Buffer length. Works for any non-zero value, larger values take
40    --  more stack space, smaller values require more recursion.
41
42    -----------------
43    -- File_Exists --
44    -----------------
45
46    function File_Exists (Name : String) return Boolean
47    is
48       Namestr : aliased String (1 .. Name'Length + 1);
49       --  Name as given with ASCII.NUL appended
50
51    begin
52       Namestr (1 .. Name'Length) := Name;
53       Namestr (Name'Length + 1)  := ASCII.NUL;
54       return file_exists (Namestr'Address) /= 0;
55    end File_Exists;
56
57    --------------
58    -- Get_Line --
59    --------------
60
61    --  Current_Input case
62
63    function Get_Line return String is
64       Buffer : String (1 .. Buflen);
65       --  Buffer to read in chunks of remaining line. Will work with any
66       --  size buffer. We choose a length so that most of the time no
67       --  recursion will be required.
68
69       Last : Natural;
70
71    begin
72       Ada.Text_IO.Get_Line (Buffer, Last);
73
74       --  If the buffer is not full, then we are all done
75
76       if Last < Buffer'Last then
77          return Buffer (1 .. Last);
78
79       --  Otherwise, we still have characters left on the line. Note that
80       --  as specified by (RM A.10.7(19)) the end of line is not skipped
81       --  in this case, even if we are right at it now.
82
83       else
84          return Buffer & GNAT.IO_Aux.Get_Line;
85       end if;
86    end Get_Line;
87
88    --  Case of reading from a specified file. Note that we could certainly
89    --  share code between these two versions, but these are very short
90    --  routines, and we may as well aim for maximum speed, cutting out an
91    --  intermediate call (calls returning string may be somewhat slow)
92
93    function Get_Line (File : Ada.Text_IO.File_Type) return String is
94       Buffer : String (1 .. Buflen);
95       Last   : Natural;
96
97    begin
98       Ada.Text_IO.Get_Line (File, Buffer, Last);
99
100       if Last < Buffer'Last then
101          return Buffer (1 .. Last);
102       else
103          return Buffer & Get_Line (File);
104       end if;
105    end Get_Line;
106
107 end GNAT.IO_Aux;