2014-11-18 Christophe Lyon <christophe.lyon@linaro.org>
[official-gcc.git] / gcc / ada / g-io_aux.adb
blob2e0b0caa27969403243c6bb56e773151e901ee4e
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 ------------------------------------------------------------------------------
32 with Interfaces.C_Streams; use Interfaces.C_Streams;
34 package body GNAT.IO_Aux is
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.
40 -----------------
41 -- File_Exists --
42 -----------------
44 function File_Exists (Name : String) return Boolean
46 Namestr : aliased String (1 .. Name'Length + 1);
47 -- Name as given with ASCII.NUL appended
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;
55 --------------
56 -- Get_Line --
57 --------------
59 -- Current_Input case
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.
67 Last : Natural;
69 begin
70 Ada.Text_IO.Get_Line (Buffer, Last);
72 -- If the buffer is not full, then we are all done
74 if Last < Buffer'Last then
75 return Buffer (1 .. Last);
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.
81 else
82 return Buffer & GNAT.IO_Aux.Get_Line;
83 end if;
84 end Get_Line;
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)
91 function Get_Line (File : Ada.Text_IO.File_Type) return String is
92 Buffer : String (1 .. Buflen);
93 Last : Natural;
95 begin
96 Ada.Text_IO.Get_Line (File, Buffer, Last);
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;
105 end GNAT.IO_Aux;