1 ------------------------------------------------------------------------------
4 -- Copyright (C) 2006-2008, Pascal Obry --
6 -- This library is free software; you can redistribute it and/or modify --
7 -- it under the terms of the GNU General Public License as published by --
8 -- the Free Software Foundation; either version 2 of the License, or (at --
9 -- your option) any later version. --
11 -- This library is distributed in the hope that it will be useful, but --
12 -- WITHOUT ANY WARRANTY; without even the implied warranty of --
13 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU --
14 -- General Public License for more details. --
16 -- You should have received a copy of the GNU General Public License --
17 -- along with this library; if not, write to the Free Software Foundation, --
18 -- Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. --
20 ------------------------------------------------------------------------------
23 with Ada
.Unchecked_Conversion
;
25 package body File_Reader
is
29 function Read_Char
(File
: in File_Type
) return Character;
30 -- Returns one character, ASCII.EOT at end of file
32 function Peek_Char
(File
: in File_Type
) return Character;
38 procedure Close
(File
: in out File_Type
) is
40 Stream_IO
.Close
(File
.File
);
47 function End_Of_File
(File
: in File_Type
) return Boolean is
49 return File
.Index
> File
.Size
and then Stream_IO
.End_Of_File
(File
.File
);
60 Ending
: out Checks
.Line_Ending_Style
)
62 C
: Character := ASCII
.NUL
;
65 Last
:= Buffer
'First - 1;
67 while Last
< Buffer
'Last
68 and then C
/= ASCII
.LF
69 and then C
/= ASCII
.CR
70 and then C
/= ASCII
.EOT
72 C
:= Read_Char
(File
);
80 elsif Last
= Buffer
'First - 1 then
85 elsif Buffer
(Last
) = ASCII
.LF
then
87 if Peek_Char
(File
) = ASCII
.CR
then
89 C
:= Read_Char
(File
);
92 Ending
:= Checks
.UNIX
;
95 elsif Buffer
(Last
) = ASCII
.CR
then
97 if Peek_Char
(File
) = ASCII
.LF
then
99 C
:= Read_Char
(File
);
102 Ending
:= Checks
.MAC
;
107 File
.Self
.Line
:= File
.Self
.Line
+ 1;
114 function Line
(File
: in File_Type
) return Natural is
124 (File
: in File_Type
;
125 Absolute
: in Boolean) return String is
128 return Directories
.Full_Name
(Stream_IO
.Name
(File
.File
));
130 return To_String
(File
.Relative_Name
);
138 procedure Open
(File
: in out File_Type
; Name
: in String) is
140 Stream_IO
.Open
(File
.File
, Stream_IO
.In_File
, Name
);
141 File
.Relative_Name
:= To_Unbounded_String
(Name
);
148 function Peek_Char
(File
: in File_Type
) return Character is
150 function To_String
(Data
: in Stream_Element_Array
) return String;
151 -- Convert a Stream_Element_Array to a String. Fast version working on
152 -- any computer where a Character is equal to a Byte.
159 (Data
: in Stream_Element_Array
)
164 is String (Integer (Data
'First) .. Integer (Data
'Last));
166 subtype Fixed_Array
is Stream_Element_Array
167 (Data
'First .. Data
'Last);
169 function To_Characters
is
170 new Ada
.Unchecked_Conversion
(Fixed_Array
, Fixed_String
);
173 return To_Characters
(Data
);
176 BC
: Stream_Element_Array
(1 .. Cache_Size
);
177 Size
: Stream_Element_Count
;
180 if File
.Size
= 0 or else File
.Index
= File
.Size
+ 1 then
181 if Stream_IO
.End_Of_File
(File
.File
) then
185 Stream_IO
.Read
(File
.File
, BC
, Size
);
186 File
.Self
.Cache
(1 .. Integer (Size
)) := To_String
(BC
(1 .. Size
));
187 File
.Self
.Index
:= 1;
188 File
.Self
.Size
:= Integer (Size
);
191 return File
.Cache
(File
.Index
);
198 function Read_Char
(File
: in File_Type
) return Character is
199 C
: constant Character := Peek_Char
(File
);
201 File
.Self
.Index
:= File
.Index
+ 1;