[m-dev.] output_string_with_context

Fergus Henderson fjh at cs.mu.oz.au
Wed Apr 23 19:34:54 AEST 1997


Christopher Rodd SPEIRS, you wrote:
> 
> Hi all,
> 	Seeing that i am about to have to write a number of predicates
> that output error messages, i was thinking of implementing a
> 'output_string_with_context' predicate.

The trouble with passing lists of strings is that it doesn't
help if you need to call e.g. hlds_out__write_pred_id,
since that is a predicate that does I/O rather than returning
a string.  Writing two versions of all such predicates isn't
an attractive approach either.

Here's my ideas about what I think the ideal design would be:

1.  Implement string streams and proc streams.

    To do this efficiently, you need to reimplement the Mercury
    I/O library using the low-level POSIX I/O (i.e. read() and write())
    rather than the high-level ANSI C I/O (printf(), fread(), etc.).
    But doing that would be a good idea anyway, for various reasons
    not relevant to this discussion.

    String streams let you collect output in a string.
    E.g. 
	    io__open_string_output_stream(StringStream),
	    io__write_float(StringStream, 1.0 / 3.0),
	    io__close_string_output_stream(StringStream, S)

    would bind `S' to "0.3333333333333333".
    Proc streams are similar, except that they let you provide
    hook procedures that are called when you read or write
    to the stream.

    For details, have a look at what the GNU C Library (glibc) does --
    it provides this sort of functionality as an extension.

2.  Use the proc stream functionality to implement a new sort of output
    stream.  Store the current context in the I/O state, and have
    this new sort of output stream do automatic wrapping at whitespace
    boundaries and automatic insertion of context prefixes.
    Then the code to output error messages would just need to call
    `start_error_message(Context)' before outputting the text
    of the error message.

All of this is quite a bit of work, which is why I haven't done it yet...

-- 
Fergus Henderson <fjh at cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3         |     -- the last words of T. S. Garp.



More information about the developers mailing list