[m-rev.] for review: implement io__read_line_as_string_2 in mercury

Peter Ross pro at missioncriticalit.com
Wed Dec 4 02:08:40 AEDT 2002


Hi,

For anyone to review.


===================================================================


Estimated hours taken: 0.5
Branches: main

library/io.m:
	Implement io__read_line_as_string_2 in Mercury.

Index: io.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/io.m,v
retrieving revision 1.280
diff -u -r1.280 io.m
--- io.m	2 Dec 2002 11:25:37 -0000	1.280
+++ io.m	3 Dec 2002 15:06:37 -0000
@@ -1637,7 +1637,7 @@
 	io__read_line_as_string(Stream, Result).
 
 io__read_line_as_string(Stream, Result, IO0, IO) :-
-	io__read_line_as_string_2(Stream, Res, String, IO0, IO1),
+	io__read_line_as_string_2(Stream, yes, Res, String, IO0, IO1),
 	( Res < 0 ->
 		( Res = -1 ->
 			Result = eof,
@@ -1651,11 +1651,12 @@
 		IO = IO1
 	).
 
-:- pred io__read_line_as_string_2(io__input_stream, int, string,
+:- pred io__read_line_as_string_2(io__input_stream, bool, int, string,
 		io__state, io__state).
-:- mode io__read_line_as_string_2(in, out, out, di, uo) is det.
+:- mode io__read_line_as_string_2(in, in, out, out, di, uo) is det.
 
-:- pragma foreign_proc("C", io__read_line_as_string_2(File::in, Res :: out,
+:- pragma foreign_proc("C", io__read_line_as_string_2(File::in,
+			_Bool::in, Res :: out,
 			RetString::out, IO0::di, IO::uo),
 		[will_not_call_mercury, promise_pure,
 			tabled_for_io, thread_safe],
@@ -1714,10 +1715,30 @@
 	MR_update_io(IO0, IO);
 ").
 
-io__read_line_as_string_2(_, _, _) -->
-	% This version is only used for back-ends for which there is no
-	% matching foreign_proc version.
-	{ private_builtin__sorry("io__read_line_as_string_2") }.
+	% XXX This is terribly inefficient, a better approach would be to
+	% use a buffer like what is done for io__read_file_as_string.
+io__read_line_as_string_2(Stream, FirstCall, Res, String) -->
+	io__read_char(Stream, Result),
+	( { Result = ok(Char) },
+		( { Char = '\n' } ->
+			{ Res = 0 },
+			{ String = "\n" }
+		;
+			io__read_line_as_string_2(Stream, no, Res, String0),
+			{ string__first_char(String, Char, String0) } 
+		)
+	; { Result = eof },
+		{ FirstCall = yes ->
+			String = "",
+			Res = -1
+		;
+			String = "",
+			Res = 0
+		}
+	; { Result = error(_) },
+		{ String = "" },
+		{ Res = -2 }
+	).
 
 io__read_file(Result) -->
 	io__input_stream(Stream),
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list