[m-dev.] .NET I/O handling in Mercury library

Fergus Henderson fjh at cs.mu.OZ.AU
Fri Jul 6 21:49:00 AEST 2001


[Summary: some technical questions about the .NET I/O handling in library/io.m.
This is mainly for Tyson.]

The code for io__write_char in the current Mercury library
seems to be a little bit baroque:

	:- pragma foreign_proc("MC++",
		io__write_char(Character::in, IO0::di, IO::uo),
			[may_call_mercury, thread_safe, tabled_for_io],
	"
		System::IO::StreamWriter *w = new System::IO::StreamWriter(
			mercury_current_text_output->stream);
		w->Write(Character);
		w->Flush();
		if (Character == '\\n') {
			mercury_current_text_output->line_number++;
		}
		update_io(IO0, IO);
	").

Why do we need to create a new StreamWriter each time, when the
MR_MercuryFile type already contains a TextWriter (which is a based class of
StreamWriter)?

Why do we need to call w->Flush() here?

The code for io__write_string also seems somewhat baroque:

	:- pragma foreign_proc("MC++",
		io__write_string(Message::in, IO0::di, IO::uo),
			[may_call_mercury, thread_safe, tabled_for_io],
	"
		mercury_print_string(mercury_current_text_output, Message);
		update_io(IO0, IO);
	").

	static void
	mercury_print_string(MR_MercuryFile mf, MR_String s)
	{
		unsigned char ByteArray __gc[] = ascii_encoder->GetBytes(s);
		mf->stream->Write(ByteArray, 0, ByteArray->get_Length());
		mf->stream->Flush();

		for (int i = 0; i < s->Length; i++) {
			if (s->Chars[i] == '\\n') {
				mf->line_number++;
			}
		}
	}

Why do we do the encoding manually here, using ascii_encoder,
rather than using a StreamWriter?

Also why do we use an System.Text.AsciiEncoder, which only supports 7-bit ASCII,
rather than using System.Text.Encoding.Default, which supports the
local 8-bit code page, or System.Text.Encoding.UTF8Encoding?
This is inconsistent with what is done for io__write_char,
where we use a StreamWriter constructed with the `StreamWriter(Stream)'
constructor, which constructs a StreamWriter that uses UTF8 encoding.

Why does ascii_encoder get set in both io__gc_init and io__stream_init?
Isn't it sufficient to set it just in io__stream_init?

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
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