ocarina-files.adb 7.78 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--                        O C A R I N A . F I L E S                         --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--               Copyright (C) 2008-2009, GET-Telecom Paris.                --
--                                                                          --
-- Ocarina  is free software;  you  can  redistribute  it and/or  modify    --
-- it under terms of the GNU General Public License as published by the     --
-- Free Software Foundation; either version 2, or (at your option) any      --
-- later version. Ocarina is distributed  in  the  hope  that it will be    --
-- useful, but WITHOUT ANY WARRANTY;  without even the implied warranty of  --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General --
-- Public License for more details. You should have received  a copy of the --
-- GNU General Public License distributed with Ocarina; see file COPYING.   --
-- If not, write to the Free Software Foundation, 51 Franklin Street, Fifth --
-- Floor, Boston, MA 02111-1301, USA.                                       --
--                                                                          --
-- As a special exception,  if other files  instantiate  generics from this --
-- unit, or you link  this unit with other files  to produce an executable, --
-- this  unit  does not  by itself cause  the resulting  executable to be   --
-- covered  by the  GNU  General  Public  License. This exception does not  --
-- however invalidate  any other reasons why the executable file might be   --
-- covered by the GNU Public License.                                       --
--                                                                          --
--                 Ocarina is maintained by the Ocarina team                --
--                       (ocarina-users@listes.enst.fr)                     --
--                                                                          --
------------------------------------------------------------------------------

with GNAT.OS_Lib;

with Errors;
with Namet;

with Ocarina.Options;       use Ocarina.Options;

package body Ocarina.Files is

   use ASCII;
   use GNAT.OS_Lib;
45

46
   use Errors;
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
   use Namet;

   ----------------------------
   -- Add_File_To_Parse_List --
   ----------------------------

   procedure Add_File_To_Parse_List (File_Name : Name_Id) is
      File_Name_With_Extension : Name_Id;
      --  Full_Name : Name_Id;
      Do_Add : Boolean := True;
   begin
      Get_Name_String (File_Name);
      if Name_Buffer (Name_Len - 4 .. Name_Len) /= ".aadl" then
         Add_Str_To_Name_Buffer (".aadl");
      end if;
      File_Name_With_Extension := Name_Find;

      --  Full_Name := Search_File (File_Name_With_Extension);

      --  Exit_On_Error (Full_Name = No_Name,
      --                 "Cannot find file "
      --                   & Get_Name_String (File_Name_With_Extension));

      for J in 1 .. Sources.Last loop
         if File_Name_With_Extension = Sources.Table (J) then
            Do_Add := False;
         end if;
      end loop;

      if Do_Add then
         Sources.Append (File_Name_With_Extension);
      end if;
   end Add_File_To_Parse_List;
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199

   -----------------
   -- End_Of_File --
   -----------------

   function End_Of_File return Boolean is
   begin
      Skip_Spaces;
      return Buffer_Location.Scan > Buffer_Location.EOF;
   end End_Of_File;

   --------------
   -- New_Line --
   --------------

   procedure New_Line is
   begin
      if Buffer (Buffer_Location.Scan) = CR
        and then Buffer (Buffer_Location.Scan + 1) = LF
      then
         Buffer_Location.Scan  := Buffer_Location.Scan + 2;

         --  Microsoft OS's
      else
         Buffer_Location.Scan  := Buffer_Location.Scan + 1;

         --  Other OS's
      end if;

      Buffer_Location.First_Pos := Buffer_Location.Scan;
      Buffer_Location.Last_Pos  := Buffer_Location.Scan;
      Buffer_Location.Line      := Buffer_Location.Line + 1;
   end New_Line;

   ---------------
   -- Load_File --
   ---------------

   function Load_File (File_Name : Name_Id) return Location is
      File_Desc : File_Descriptor;
      Result    : Integer;
      Length    : Integer;
   begin
      --  Open the file

      Get_Name_String (File_Name);
      Name_Buffer (Name_Len + 1) := NUL;

      File_Desc := Open_Read (Name_Buffer'Address, Binary);
      if File_Desc = Invalid_FD then
         DE ("cannot open file " & Get_Name_String (File_Name));
         return No_Location;
      end if;

      --  Load file in a buffer

      Length := Integer (File_Length (File_Desc));
      Buffer := new Text_Buffer (1 .. Text_Ptr (Length + 1));

      Buffer_Location.Scan := 1;
      loop
         Result := Read
           (File_Desc, Buffer (Buffer_Location.Scan)'Address, Length);

         exit when Result = Length;

         if Result <= 0 then
            DE ("cannot read file " & Get_Name_String (File_Name));
            return No_Location;
         end if;

         Buffer_Location.Scan := Buffer_Location.Scan + Text_Ptr (Result);
         Length  := Length - Result;
      end loop;

      Close (File_Desc);
      --  Source file is totally loaded

      Initialize (Buffer_Location, File_Name, Int (Length), Buffer);
      return Buffer_Location;
   end Load_File;

   ----------------------
   -- Restore_Location --
   ----------------------

   procedure Restore_Location (State : Location) is
   begin
      Buffer_Location := State;
      Buffer          := State.Buffer;
   end Restore_Location;

   -------------------
   -- Save_Location --
   -------------------

   procedure Save_Location (State : out Location) is
   begin
      State := Buffer_Location;
   end Save_Location;

   -----------------
   -- Search_File --
   -----------------

   function Search_File (File_Name : Name_Id) return Name_Id is
   begin
      --  Check in the current directory first since -I- is not allowed

      Get_Name_String (File_Name);
      if Is_Regular_File (Name_Buffer (1 .. Name_Len)) then
         return File_Name;
      end if;

      --  Check in the library paths

      for Path in Library_Paths.First .. Library_Paths.Last loop
         Get_Name_String (Library_Paths.Table (Path));
         Add_Char_To_Name_Buffer (Directory_Separator);
         Get_Name_String_And_Append (File_Name);
200

201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
         if Is_Regular_File (Name_Buffer (1 .. Name_Len)) then
            return Name_Find;
         end if;
      end loop;

      return No_Name;
   end Search_File;

   ---------------
   -- Skip_Line --
   ---------------

   procedure Skip_Line is
   begin
      while Buffer_Location.Scan <= Buffer_Location.EOF loop
         case Buffer (Buffer_Location.Scan) is
            when LF | FF | CR | VT =>
               New_Line;
               exit;
            when others =>
               null;
         end case;

         Buffer_Location.Scan := Buffer_Location.Scan + 1;
      end loop;
   end Skip_Line;

   -----------------
   -- Skip_Spaces --
   -----------------

   procedure Skip_Spaces is
   begin
      while Buffer_Location.Scan <= Buffer_Location.EOF loop
         case Buffer (Buffer_Location.Scan) is
            when ' ' | HT =>
               Buffer_Location.Scan := Buffer_Location.Scan + 1;
            when LF | FF | CR | VT =>
               New_Line;
            when others =>
               exit;
         end case;
      end loop;
   end Skip_Spaces;

end Ocarina.Files;