Add support for checking operators at end-of-line
[style_checker.git] / src / file_reader.adb
blob43dd75a74c2e7c1f4490ea249e541ce915ca69a4
1 ------------------------------------------------------------------------------
2 -- Style Checker --
3 -- --
4 -- Copyright (C) 2006-2008, Pascal Obry --
5 -- --
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. --
10 -- --
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. --
15 -- --
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. --
19 -- --
20 ------------------------------------------------------------------------------
22 with Ada.Directories;
23 with Ada.Unchecked_Conversion;
25 package body File_Reader is
27 use Ada;
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;
34 -----------
35 -- Close --
36 -----------
38 procedure Close (File : in out File_Type) is
39 begin
40 Stream_IO.Close (File.File);
41 end Close;
43 -----------------
44 -- End_Of_File --
45 -----------------
47 function End_Of_File (File : in File_Type) return Boolean is
48 begin
49 return File.Index > File.Size and then Stream_IO.End_Of_File (File.File);
50 end End_Of_File;
52 --------------
53 -- Get_Line --
54 --------------
56 procedure Get_Line
57 (File : in File_Type;
58 Buffer : out String;
59 Last : out Natural;
60 Ending : out Checks.Line_Ending_Style)
62 C : Character := ASCII.NUL;
63 begin
64 Ending := Checks.Any;
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
71 loop
72 C := Read_Char (File);
73 Last := Last + 1;
74 Buffer (Last) := C;
75 end loop;
77 if C = ASCII.EOT then
78 Ending := Checks.No;
80 elsif Last = Buffer'First - 1 then
81 Ending := Checks.Any;
82 Last := 0;
83 return;
85 elsif Buffer (Last) = ASCII.LF then
87 if Peek_Char (File) = ASCII.CR then
88 Ending := Checks.MAC;
89 C := Read_Char (File);
91 else
92 Ending := Checks.UNIX;
93 end if;
95 elsif Buffer (Last) = ASCII.CR then
97 if Peek_Char (File) = ASCII.LF then
98 Ending := Checks.DOS;
99 C := Read_Char (File);
101 else
102 Ending := Checks.MAC;
103 end if;
104 end if;
106 Last := Last - 1;
107 File.Self.Line := File.Self.Line + 1;
108 end Get_Line;
110 ----------
111 -- Line --
112 ----------
114 function Line (File : in File_Type) return Natural is
115 begin
116 return File.Line;
117 end Line;
119 ----------
120 -- Name --
121 ----------
123 function Name
124 (File : in File_Type;
125 Absolute : in Boolean) return String is
126 begin
127 if Absolute then
128 return Directories.Full_Name (Stream_IO.Name (File.File));
129 else
130 return To_String (File.Relative_Name);
131 end if;
132 end Name;
134 ----------
135 -- Open --
136 ----------
138 procedure Open (File : in out File_Type; Name : in String) is
139 begin
140 Stream_IO.Open (File.File, Stream_IO.In_File, Name);
141 File.Relative_Name := To_Unbounded_String (Name);
142 end Open;
144 ---------------
145 -- Peek_Char --
146 ---------------
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.
154 --------------
155 -- To_String --
156 ---------------
158 function To_String
159 (Data : in Stream_Element_Array)
160 return String
163 subtype Fixed_String
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);
172 begin
173 return To_Characters (Data);
174 end To_String;
176 BC : Stream_Element_Array (1 .. Cache_Size);
177 Size : Stream_Element_Count;
179 begin
180 if File.Size = 0 or else File.Index = File.Size + 1 then
181 if Stream_IO.End_Of_File (File.File) then
182 return ASCII.EOT;
183 end if;
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);
189 end if;
191 return File.Cache (File.Index);
192 end Peek_Char;
194 ---------------
195 -- Read_Char --
196 ---------------
198 function Read_Char (File : in File_Type) return Character is
199 C : constant Character := Peek_Char (File);
200 begin
201 File.Self.Index := File.Index + 1;
202 return C;
203 end Read_Char;
205 end File_Reader;