for review: enhancement to io.m

Thomas Charles CONWAY conway at cs.mu.oz.au
Thu Jun 19 10:55:37 AEST 1997


Hi

Here is a couple of new preds for io.m. I should have added them long ago -
I've ended up writing them several times in difference circumstances....

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


library/io.m:
	add io__read_file/3, io__read_file/4 and
	add io__read_binary_file/3, io__read_binary_file/4.

NEWS:
	mention io__read_file/{3,4} and io__read_binary_file/{3,4}


cvs diff: Diffing .
Index: NEWS
===================================================================
RCS file: /home/staff/zs/imp/mercury/NEWS,v
retrieving revision 1.52
diff -u -r1.52 NEWS
--- NEWS	1997/06/12 04:58:29	1.52
+++ NEWS	1997/06/19 00:50:51
@@ -184,10 +184,11 @@
     lists using a user-specified procedure to write the elements and separating
     the elements with a user-specified separator string.
 
+  - We've add io__read_file/{3,4} and io__read_binary_file/{3,4} which read
+    whole files (until error or eof).
+
   - We've added a double accumulator version of list__foldl/4 called
     list__foldl2/6, which is a convenient generalisation for accumulators
     that also do I/O. Also, we've added have list__map_foldl/5, which is an
     amalgam of list__map/3 and list__foldl/4.
-
-
 
cvs diff: Diffing bindist
cvs diff: Diffing boehm_gc
cvs diff: Diffing boehm_gc/Mac_files
cvs diff: Diffing boehm_gc/cord
cvs diff: Diffing boehm_gc/cord/private
cvs diff: Diffing boehm_gc/include
cvs diff: Diffing boehm_gc/include/private
cvs diff: Diffing bytecode
cvs diff: Diffing compiler
cvs diff: Diffing compiler/notes
cvs diff: Diffing doc
cvs diff: Diffing library
Index: library/io.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/library/io.m,v
retrieving revision 1.124
diff -u -r1.124 io.m
--- io.m	1997/06/09 23:20:21	1.124
+++ io.m	1997/06/19 00:44:03
@@ -115,6 +115,11 @@
 :- mode io__read_line(out, di, uo) is det.
 %		Reads a line from the current input stream.
 
+:- pred io__read_file(io__result(list(char)), io__state, io__state).
+:- mode io__read_file(out, di, uo) is det.
+%		Reads all the characters from the current input stream until
+%		eof or error.
+
 :- pred io__putback_char(char, io__state, io__state).
 :- mode io__putback_char(in, di, uo) is det.
 %		Un-reads a character from the current input stream.
@@ -136,6 +141,12 @@
 :- mode io__read_line(in, out, di, uo) is det.
 %		Reads a line from specified stream.
 
+:- pred io__read_file(io__input_stream, io__result(list(char)),
+							io__state, io__state).
+:- mode io__read_file(in, out, di, uo) is det.
+%		Reads all the characters from the given input stream until
+%		eof or error.
+
 :- pred io__putback_char(io__input_stream, char, io__state, io__state).
 :- mode io__putback_char(in, in, di, uo) is det.
 %		Un-reads a character from specified stream.
@@ -524,6 +535,17 @@
 %		Reads a single 8-bit byte from the specified binary input
 %		stream.
 
+:- pred io__read_binary_file(io__result(list(int)), io__state, io__state).
+:- mode io__read_binary_file(out, di, uo) is det.
+%		Reads all the bytes from the current binary input stream
+%		until eof or error.
+
+:- pred io__read_binary_file(io__input_stream, io__result(list(int)),
+							io__state, io__state).
+:- mode io__read_binary_file(in, out, di, uo) is det.
+%		Reads all the bytes from the given binary input stream until
+%		eof or error.
+
 :- pred io__putback_byte(int, io__state, io__state).
 :- mode io__putback_byte(in, di, uo) is det.
 %		Un-reads a byte from the current binary input stream.
@@ -1101,6 +1123,56 @@
 				{ Result = ok([Char]) }
 			)
 		)
+	).
+
+io__read_file(Result) -->
+	io__input_stream(Stream),
+	io__read_file(Stream, Result).
+
+io__read_file(Stream, Result) -->
+	io__read_file_2(Stream, [], Result).
+
+:- pred io__read_file_2(io__input_stream, list(char), io__result(list(char)),
+		io__state, io__state).
+:- mode io__read_file_2(in, in, out, di, uo) is det.
+
+io__read_file_2(Stream, Chars0, Result) -->
+	io__read_char(Stream, Result0),
+	(
+		{ Result0 = eof },
+		{ list__reverse(Chars0, Chars) },
+		{ Result = ok(Chars) }
+	;
+		{ Result0 = error(Err) },
+		{ Result = error(Err) }
+	;
+		{ Result0 = ok(Char) },
+		io__read_file_2(Stream, [Char|Chars0], Result)
+	).
+
+io__read_binary_file(Result) -->
+	io__binary_input_stream(Stream),
+	io__read_binary_file(Stream, Result).
+
+io__read_binary_file(Stream, Result) -->
+	io__read_binary_file_2(Stream, [], Result).
+
+:- pred io__read_binary_file_2(io__input_stream, list(int),
+		io__result(list(int)), io__state, io__state).
+:- mode io__read_binary_file_2(in, in, out, di, uo) is det.
+
+io__read_binary_file_2(Stream, Bytes0, Result) -->
+	io__read_byte(Stream, Result0),
+	(
+		{ Result0 = eof },
+		{ list__reverse(Bytes0, Bytes) },
+		{ Result = ok(Bytes) }
+	;
+		{ Result0 = error(Err) },
+		{ Result = error(Err) }
+	;
+		{ Result0 = ok(Byte) },
+		io__read_binary_file_2(Stream, [Byte|Bytes0], Result)
 	).
 
 io__putback_char(Char) -->
cvs diff: Diffing lp_solve
cvs diff: Diffing lp_solve/lp_examples
cvs diff: Diffing profiler
cvs diff: Diffing runtime
cvs diff: Diffing runtime/machdeps
cvs diff: Diffing samples
cvs diff: Diffing samples/c_interface
cvs diff: Diffing samples/c_interface/c_calls_mercury
cvs diff: Diffing samples/c_interface/cplusplus_calls_mercury
cvs diff: Diffing samples/c_interface/mercury_calls_c
cvs diff: Diffing samples/c_interface/mercury_calls_cplusplus
cvs diff: Diffing samples/diff
cvs diff: Diffing scripts
cvs diff: Diffing tools
cvs diff: Diffing trial
cvs diff: Diffing util



More information about the developers mailing list