bugfix and enhancement to io.m

Thomas Charles CONWAY conway at cs.mu.oz.au
Mon May 26 12:38:43 AEST 1997


Can someone please review this?

-- 
ZZ:wq!
^X^C
Thomas Conway               				      conway at cs.mu.oz.au
AD DEUM ET VINUM	  			      Every sword has two edges.


library/io.m:
        added two new predicates:
                io__seek_binary/5 which interfaces to fseek
                io__binary_stream_offset/4 which interfaces to ftell
        Make io__binary_*_stream concrete type equivalences to
        io__binary_stream. This is to overcome the problem that operations
        like the two new ones above are used on both input and output
        streams, and this seems to be the best way to overcome the
        subtyping problem at this stage.

	Make io__read_binary/4 set the io__stream correctly.

Index: io.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/library/io.m,v
retrieving revision 1.121
diff -u -r1.121 io.m
--- io.m	1997/05/21 02:16:11	1.121
+++ io.m	1997/05/26 02:06:22
@@ -48,9 +48,11 @@
 
 	% Opaque handles for binary I/O streams.
 
-:- type io__binary_input_stream.
+:- type io__binary_input_stream		==	io__binary_stream.
 
-:- type io__binary_output_stream.
+:- type io__binary_output_stream	==	io__binary_stream.
+
+:- type io__binary_stream.
 
 	% Various types used for the result from the access predicates
 
@@ -86,6 +88,17 @@
 %	;	f(float).
 %
 
+	% io__whence denotes the base for a seek operation.
+	% 	set	- seek relative to the start of the file
+	%	cur	- seek relative to the current position in the file
+	%	end	- seek relative to the end of the file.
+
+:- type io__whence
+	--->	set
+	;	cur
+	;	end
+	.
+
 %-----------------------------------------------------------------------------%
 
 % Text input predicates.
@@ -571,6 +584,17 @@
 :- mode io__flush_binary_output(in, di, uo) is det.
 %	Flush the output buffer of the specified binary output stream.
 
+:- pred io__seek_binary(io__binary_stream, io__whence, int,
+		io__state, io__state).
+:- mode io__seek_binary(in, in, in, di, uo) is det.
+%	Seek to an offset relative to Whence (documented above)
+%	on a specified binary stream. Attempting to seek on a
+%	pipe or tty results in implementation defined behaviour.
+
+:- pred io__binary_stream_offset(io__binary_stream, int, io__state, io__state).
+:- mode io__binary_stream_offset(in, out, di, uo) is det.
+%	Returns the offset (in bytes) into the specified binary stream.
+
 %-----------------------------------------------------------------------------%
 
 % Binary input stream predicates.
@@ -891,8 +915,7 @@
 :- type io__input_stream ==	io__stream.
 :- type io__output_stream ==	io__stream.
 
-:- type io__binary_input_stream ==	io__stream.
-:- type io__binary_output_stream ==	io__stream.
+:- type io__binary_stream ==	io__stream.
 
 :- type io__stream == c_pointer.
 
@@ -1480,9 +1503,9 @@
 	io__set_binary_output_stream(OrigStream, _Stream).
 
 io__read_binary(Stream, Result) -->
-	io__set_output_stream(Stream, OrigStream),
+	io__set_binary_input_stream(Stream, OrigStream),
 	io__read_binary(Result),
-	io__set_output_stream(OrigStream, _Stream).
+	io__set_binary_input_stream(OrigStream, _Stream).
 
 io__write_binary(Term) -->
 	% a quick-and-dirty implementation... not very space-efficient
@@ -2149,6 +2172,39 @@
 	}
 	update_io(IO0, IO);
 ").
+
+/* moving about binary streams */
+
+:- pred whence_to_int(io__whence::in, int::out) is det.
+
+whence_to_int(set, 0).
+whence_to_int(cur, 1).
+whence_to_int(end, 2).
+
+io__seek_binary(Stream, Whence, Offset, IO0, IO) :-
+	whence_to_int(Whence, Flag),
+	io__seek_binary_2(Stream, Flag, Offset, IO0, IO).
+
+:- pred io__seek_binary_2(io__stream, int, int, io__state, io__state).
+:- mode io__seek_binary_2(in, in, in, di, uo) is det.
+
+:- pragma c_code(io__seek_binary_2(Stream::in, Flag::in, Off::in,
+	IO0::di, IO::uo),
+"{
+	static const int seek_flags[] = { SEEK_SET, SEEK_CUR, SEEK_END };
+	MercuryFile *stream = (MercuryFile *) Stream;
+	fseek(stream->file, Off, seek_flags[Flag]);
+	IO = IO0;
+}").
+
+:- pragma c_code(io__binary_stream_offset(Stream::in, Offset::out,
+		IO0::di, IO::uo),
+"{
+	MercuryFile *stream = (MercuryFile *) Stream;
+	Offset = ftell(stream->file);
+	IO = IO0;
+}").
+
 
 /* output predicates - with output to the specified stream */
 



More information about the developers mailing list