-
Notifications
You must be signed in to change notification settings - Fork 1
/
SimpleFileIO.def
99 lines (70 loc) · 3.27 KB
/
SimpleFileIO.def
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
45
46
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
(*!m2r10*) (* Copyright (c) 2015 B.Kowarsch. All rights reserved. *)
DEFINITION MODULE SimpleFileIO;
(* Simple File IO library *)
TYPE File = OPAQUE;
TYPE Mode = ( Read, Write, Append );
TYPE Status = ( Success, Failure ); (* TO DO: refine *)
(* Operations *)
(* Support for an operation depends on the mode in which the file has
* been opened. Any attempt to carry out an unsupported operation will
* fail with status failure.
*
* operation supported in file mode
* Read Write Append
* ------------------------------------
* Open yes yes yes
* Close yes yes yes
* GetMode yes yes yes
* GetStatus yes yes yes
* GetPos yes yes no
* SetPos yes no no
* eof yes no no
* ReadOctets yes no no
* ReadChars yes no no
* WriteOctets no yes yes
* WriteChars no yes yes
* ------------------------------------
*)
(* Open and close *)
PROCEDURE Open
( VAR f : File; filename : ARRAY OF CHAR; mode : Mode; VAR s : Status );
(* Opens file filename in mode. Passes file handle in f and status in s.
If the file does not exist, it will be created when opened in write mode,
otherwise status failure is passed back in s. When opening an already
existing file in write mode, all of its current contents are replaced. *)
PROCEDURE Close ( VAR f : File; s : Status );
(* Closes file associated with file handle f. Passes status in s. *)
(* Introspection *)
PROCEDURE GetMode ( f : File; VAR m : Mode );
(* Passes the mode of file f in m. *)
PROCEDURE GetStatus ( f : File; VAR s : Status );
(* Passes the status of the last operation on file f in s. *)
(* Positioning *)
PROCEDURE GetPos ( f : File; VAR pos : LONGCARD );
(* Passes the current reading or writing position of file f in pos. *)
PROCEDURE SetPos ( f : File; pos : LONGCARD );
(* Sets the reading position of file f to pos. *)
PROCEDURE eof ( f : File ) : BOOLEAN;
(* Returns TRUE if the end of file f has been reached, otherwise FALSE. *)
(* IO operations *)
PROCEDURE ReadOctets
( f : File; VAR buffer : ARRAY OF OCTET; VAR bytesRead : LONGCARD );
(* Reads contents starting at the current reading position of file f into
buffer until either buffer is full or eof is reached. The number of octets
actually read is passed in bytesRead. *)
PROCEDURE ReadChars
( f : File; VAR buffer : ARRAY OF CHAR; VAR charsRead : LONGCARD );
(* Reads contents starting at the current reading position of file f into
buffer until either the pen-ultimate index of buffer is written or eof
is reached. The buffer is then terminated with ASCII NUL. The number of
characters actually read is passed in charsRead. *)
PROCEDURE WriteOctets
( f : File; buffer : ARRAY OF OCTET; VAR bytesWritten : LONGCARD );
(* Writes the contents of buffer at the current writing position to file f.
The number of octets actually written is passed in bytesWritten. *)
PROCEDURE WriteChars
( f : File; buffer : ARRAY OF CHAR; VAR charsWritten : LONGCARD );
(* Writes the contents of buffer up to and excluding the first ASCII NUL
character code at the current writing position to file f.
The number of characters actually written is passed in charsWritten. *)
END SimpleFileIO.