1--  GHDL Run Time (GRT) -  VHDL files subprograms.
2--  Copyright (C) 2002 - 2014 Tristan Gingold
3--
4--  This program is free software: you can redistribute it and/or modify
5--  it under the terms of the GNU General Public License as published by
6--  the Free Software Foundation, either version 2 of the License, or
7--  (at your option) any later version.
8--
9--  This program is distributed in the hope that it will be useful,
10--  but WITHOUT ANY WARRANTY; without even the implied warranty of
11--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12--  GNU General Public License for more details.
13--
14--  You should have received a copy of the GNU General Public License
15--  along with this program.  If not, see <gnu.org/licenses>.
16--
17--  As a special exception, if other files instantiate generics from this
18--  unit, or you link this unit with other files to produce an executable,
19--  this unit does not by itself cause the resulting executable to be
20--  covered by the GNU General Public License. This exception does not
21--  however invalidate any other reasons why the executable file might be
22--  covered by the GNU Public License.
23
24with Interfaces;
25
26with Grt.Types; use Grt.Types;
27with Grt.Stdio;
28
29package Grt.Files_Operations is
30   type Ghdl_File_Index is new Interfaces.Integer_32;
31
32   --  File open mode.
33   Read_Mode   : constant Ghdl_I32 := 0;
34   Write_Mode  : constant Ghdl_I32 := 1;
35   Append_Mode : constant Ghdl_I32 := 2;
36
37   --  file_open_status.
38   Open_Ok      : constant Ghdl_I32 := 0;
39   Status_Error : constant Ghdl_I32 := 1;
40   Name_Error   : constant Ghdl_I32 := 2;
41   Mode_Error   : constant Ghdl_I32 := 3;
42
43   type Op_Status is
44     (
45      Op_Ok,
46
47      --  Correspond to file_open_status.
48      Op_Status_Error,
49      Op_Name_Error,
50      Op_Mode_Error,
51
52      --  For endfile: end of file reached (as if endfile returns True).
53      Op_End_Of_File,
54
55      --  Failed to call ungetc in endfile.
56      Op_Ungetc_Error,
57
58      --  Operation on a non-open file.
59      Op_Not_Open,
60
61      --  Try to read from a write-only file.
62      Op_Read_Write_File,
63
64      --  Try to write to a read-only file.
65      Op_Write_Read_File,
66
67      --  Internal error: incorrect file index.
68      Op_Bad_Index,
69
70      --  Internal error: binary operation on text file, or text operation
71      --  on binary file.
72      Op_Bad_Mode,
73
74      --  Internal error: destroy a file that is still open.
75      Op_Not_Closed,
76
77      --  System error during write.
78      Op_Write_Error,
79
80      --  System error during read.
81      Op_Read_Error,
82
83      --  System error during close.
84      Op_Close_Error,
85
86      --  Incorrect file name (too long).
87      Op_Filename_Error,
88
89      --  Incorrect file type.
90      Op_Signature_Error
91
92     );
93
94   --  General files.
95   procedure Ghdl_File_Endfile
96     (File : Ghdl_File_Index; Status : out Op_Status);
97
98   --  Elaboration.
99   function Ghdl_Text_File_Elaborate return Ghdl_File_Index;
100   function Ghdl_File_Elaborate (Sig : Ghdl_C_String) return Ghdl_File_Index;
101
102   --  Finalization.
103   procedure Ghdl_Text_File_Finalize
104     (File : Ghdl_File_Index; Status : out Op_Status);
105   procedure Ghdl_File_Finalize
106     (File : Ghdl_File_Index; Status : out Op_Status);
107
108   --  Subprograms.
109   procedure Ghdl_Text_File_Open (File : Ghdl_File_Index;
110                                  Mode : Ghdl_I32;
111                                  Name : Ghdl_C_String;
112                                  Status : out Op_Status);
113   procedure Ghdl_File_Open (File : Ghdl_File_Index;
114                             Mode : Ghdl_I32;
115                             Name : Ghdl_C_String;
116                             Status : out Op_Status);
117
118   procedure Ghdl_Text_Write (File : Ghdl_File_Index;
119                              Str : Std_String_Ptr;
120                              Status : out Op_Status);
121   procedure Ghdl_Write_Scalar (File : Ghdl_File_Index;
122                                Ptr : Ghdl_Ptr;
123                                Length : Ghdl_Index_Type;
124                                Status : out Op_Status);
125
126   procedure Ghdl_Read_Scalar (File : Ghdl_File_Index;
127                               Ptr : Ghdl_Ptr;
128                               Length : Ghdl_Index_Type;
129                               Status : out Op_Status);
130
131   procedure Ghdl_Text_Read_Length (File : Ghdl_File_Index;
132                                    Str : Std_String_Ptr;
133                                    Status : out Op_Status;
134                                    Length : out Std_Integer);
135
136   procedure Ghdl_Untruncated_Text_Read (File : Ghdl_File_Index;
137                                         Buf : Ghdl_C_String;
138                                         Len : in out Std_Integer;
139                                         Status : out Op_Status);
140
141   procedure Ghdl_Text_File_Close (File : Ghdl_File_Index;
142                                   Status : out Op_Status);
143   procedure Ghdl_File_Close (File : Ghdl_File_Index;
144                              Status : out Op_Status);
145
146   procedure Ghdl_File_Flush (File : Ghdl_File_Index; Status : out Op_Status);
147
148   type Open_Handler_Acc is access function
149     (Name : Ghdl_C_String; Mode : Ghdl_C_String) return Grt.Stdio.FILEs;
150
151   --  Like fopen(3)
152   function Simple_Open (Name : Ghdl_C_String; Mode : Ghdl_C_String)
153                        return Grt.Stdio.FILEs;
154
155   --  Function called to open a file.  This hook can be used to search a file
156   --  on a path.
157   Open_Handler : Open_Handler_Acc := Simple_Open'Access;
158end Grt.Files_Operations;
159