for review: fseek/ftell

Thomas Charles CONWAY conway at cs.mu.oz.au
Mon May 19 08:33:50 AEST 1997


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.


cvs diff: Diffing .
Index: io.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/library/io.m,v
retrieving revision 1.120
diff -u -r1.120 io.m
--- io.m	1997/05/01 10:16:30	1.120
+++ io.m	1997/05/18 22:27:25
@@ -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,12 @@
 %	;	f(float).
 %
 
+:- type io__whence
+	--->	set
+	;	cur
+	;	end
+	.
+
 %-----------------------------------------------------------------------------%
 
 % Text input predicates.
@@ -571,6 +579,16 @@
 :- 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 (see man page for fseek)
+%	on a specified binary stream
+
+:- 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.
@@ -890,8 +908,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.
 
@@ -2148,6 +2165,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 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 */
 
-- 
ZZ:wq!
^X^C
Thomas Conway               				      conway at cs.mu.oz.au
AD DEUM ET VINUM	  			      Every sword has two edges.



More information about the developers mailing list