[mercury-users] Convert posix__fd to io__stream

Fergus Henderson fjh at cs.mu.OZ.AU
Sat Feb 18 19:55:33 AEDT 2006


On 08-Feb-2006, Ondrej Bojar <obo at cuni.cz> wrote:
> What is the correct way of redirecting io__write to a file descriptor 
> and read_term to read from a file descriptor?

In library/io.m, there is a C function mercury_open():

	MercuryFilePtr
	mercury_open(const char *filename, const char *openmode)
	{
	    MercuryFilePtr  mf;
	    FILE            *f;

	    f = fopen(filename, openmode);
	    if (f == NULL) {
		return NULL;
	    }
	    mf = MR_GC_NEW(MercuryFile);
	    MR_mercuryfile_init(f, 1, mf);
	    return mf;
	}

You need something very similar, but using fdopen() instead of fopen().
Call this say mercury_open_fd().

	MercuryFilePtr
	mercury_open_fd(int fd, const char *openmode)
	{
	    MercuryFilePtr  mf;
	    FILE            *f;

	    f = fdopen(fd, openmode);
	    if (f == NULL) {
		return NULL;
	    }
	    mf = MR_GC_NEW(MercuryFile);
	    MR_mercuryfile_init(f, 1, mf);
	    return mf;
	}

Then you need to make copies of all the callers (io__open_input,
io__open_output, and io__do_open_text, or the binary versions)
but modified to call mercury_open_fd() instead of mercury_open().

-- 
Fergus Henderson                    |  "I have always known that the pursuit
                                    |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list