OSDN Git Service

2012-01-10 Richard Guenther <rguenther@suse.de>
[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-2010, 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 3,  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.                                     --
17 --                                                                          --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception,   --
20 -- version 3.1, as published by the Free Software Foundation.               --
21 --                                                                          --
22 -- You should have received a copy of the GNU General Public License and    --
23 -- a copy of the GCC Runtime Library Exception along with this program;     --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
25 -- <http://www.gnu.org/licenses/>.                                          --
26 --                                                                          --
27 -- GNAT was originally developed  by the GNAT team at  New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
29 --                                                                          --
30 ------------------------------------------------------------------------------
31
32 with Interfaces.C_Streams; use Interfaces.C_Streams;
33
34 package body GNAT.IO_Aux is
35
36    Buflen : constant := 2000;
37    --  Buffer length. Works for any non-zero value, larger values take
38    --  more stack space, smaller values require more recursion.
39
40    -----------------
41    -- File_Exists --
42    -----------------
43
44    function File_Exists (Name : String) return Boolean
45    is
46       Namestr : aliased String (1 .. Name'Length + 1);
47       --  Name as given with ASCII.NUL appended
48
49    begin
50       Namestr (1 .. Name'Length) := Name;
51       Namestr (Name'Length + 1)  := ASCII.NUL;
52       return file_exists (Namestr'Address) /= 0;
53    end File_Exists;
54
55    --------------
56    -- Get_Line --
57    --------------
58
59    --  Current_Input case
60
61    function Get_Line return String is
62       Buffer : String (1 .. Buflen);
63       --  Buffer to read in chunks of remaining line. Will work with any
64       --  size buffer. We choose a length so that most of the time no
65       --  recursion will be required.
66
67       Last : Natural;
68
69    begin
70       Ada.Text_IO.Get_Line (Buffer, Last);
71
72       --  If the buffer is not full, then we are all done
73
74       if Last < Buffer'Last then
75          return Buffer (1 .. Last);
76
77       --  Otherwise, we still have characters left on the line. Note that
78       --  as specified by (RM A.10.7(19)) the end of line is not skipped
79       --  in this case, even if we are right at it now.
80
81       else
82          return Buffer & GNAT.IO_Aux.Get_Line;
83       end if;
84    end Get_Line;
85
86    --  Case of reading from a specified file. Note that we could certainly
87    --  share code between these two versions, but these are very short
88    --  routines, and we may as well aim for maximum speed, cutting out an
89    --  intermediate call (calls returning string may be somewhat slow)
90
91    function Get_Line (File : Ada.Text_IO.File_Type) return String is
92       Buffer : String (1 .. Buflen);
93       Last   : Natural;
94
95    begin
96       Ada.Text_IO.Get_Line (File, Buffer, Last);
97
98       if Last < Buffer'Last then
99          return Buffer (1 .. Last);
100       else
101          return Buffer & Get_Line (File);
102       end if;
103    end Get_Line;
104
105 end GNAT.IO_Aux;