[m-dev.] io streams

Michael Day mcda at cat.cs.mu.OZ.AU
Thu Dec 23 11:24:17 AEDT 1999


Hi,

below is the beginnings of a module for io streams, which will include
files, strings and anything else people feel like reading or writing. I'd
appreciate some comments, please be brutal :)

Michael

%-----------------------------------------------------------------------------%
% Copyright (C) 1993-1999 The University of Melbourne.
% This file may only be copied under the terms of the GNU Library General
% Public License - see the file COPYING.LIB in the Mercury distribution.
%-----------------------------------------------------------------------------%
%
% File: stream.m.
% Main author: mcda.
% Stability: exceptionally low.
%
% This file provides interfaces for stream based I/O.
%
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%

:- module stream.

:- interface.

:- import_module io, char, string, list.

:- type stream__result(T)
	--->	ok(T)
	;	eof
	;	error(string)
	.

%-----------------------------------------------------------------------------%

	% Interface for streams.

:- typeclass stream__stream(S) where
	[
		pred stream__get_name(string, S, S),
		mode stream__get_name(out, di, uo) is det
	].

%-----------------------------------------------------------------------------%

	% Interface for input streams.

:- typeclass stream__input(S) <= stream__stream(S) where
	[
		pred stream__read_char(stream__result(char), S, S),
		mode stream__read_char(out, di, uo) is det,

		pred stream__read_chars(stream__result(list(char)), int, S, S),
		mode stream__read_chars(out, in, di, uo) is det,

		pred stream__read_chars(stream__result(list(char)), S, S),
		mode stream__read_chars(out, di, uo) is det,

		pred stream__putback_char(char, S, S),
		mode stream__putback_char(in, di, uo) is det
	].

%-----------------------------------------------------------------------------%

	% Interface for output streams.

:- typeclass stream__output(S) <= stream__stream(S) where
	[
		pred stream__write_char(char, S, S),
		mode stream__write_char(in, di, uo) is det,

		pred stream__write_chars(list(char), S, S),
		mode stream__write_chars(in, di, uo) is det,

		pred stream__write_chars(list(char), list(char), S, S),
		mode stream__write_chars(in, out, di, uo) is det,
		mode stream__write_chars(di, uo, di, uo) is det,

		pred stream__flush_output(S, S),
		mode stream__flush_output(di, uo) is det
	].

%-----------------------------------------------------------------------------%

	% Interface for duplex streams.

:- typeclass stream__duplex(S) <= (stream__input(S), stream__output(S)) where
	[
	].

%-----------------------------------------------------------------------------%

	% Interface for seekable streams.

:- type stream__whence
	--->	start
	;	current
	;	end
	.

:- typeclass stream__seekable(S) where
	[
		pred stream__seek(stream__whence, int, S, S),
		mode stream__seek(in, in, di, uo) is det,

		pred stream__offset(int, S),
		mode stream__offset(out, ui) is det
	].

%-----------------------------------------------------------------------------%

	% Interface for things that can be read from an input stream.

:- typeclass stream__readable(T) where
	[
		pred stream__read(T, S, S) <= stream__input(S),
		mode stream__read(out, di, uo) is det
	].

%-----------------------------------------------------------------------------%

	% Interface for things that can be written to an output stream.

:- typeclass stream__writable(T) where
	[
		pred stream__write(T, S, S) <= stream__output(S),
		mode stream__write(in, di, uo) is det
	].

%-----------------------------------------------------------------------------%

	% Predicates for opening the standard streams.

:- some [S] pred stream__stdin(S, io__state, io__state) => stream__input(S).
:- mode stream__stdin(uo, di, uo) is det.

:- some [S] pred stream__stdout(S, io__state, io__state) => stream__output(S).
:- mode stream__stdout(uo, di, uo) is det.

:- some [S] pred stream__stderr(S, io__state, io__state) => stream__output(S).
:- mode stream__stderr(uo, di, uo) is det.

%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%

:- implementation.

...

:- end_module stream.



--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list