diff options
author | simonmar <unknown> | 2001-05-18 16:54:11 +0000 |
---|---|---|
committer | simonmar <unknown> | 2001-05-18 16:54:11 +0000 |
commit | d9af408e5c512501cfa991f5e4a76c9154bca917 (patch) | |
tree | 627365b7dad9c2a5e1d892a1f631690b46e24a11 /ghc/lib/std/cbits/filePutc.c | |
parent | 5f6f90850d5c82dc56d13bbc035d635e1cb2106b (diff) | |
download | haskell-d9af408e5c512501cfa991f5e4a76c9154bca917.tar.gz |
[project @ 2001-05-18 16:54:04 by simonmar]
I/O library rewrite
-------------------
This commit replaces the old C/Haskell I/O implementation with a new
Haskell-only one using the new FFI & hsc2hs.
main points:
- lots of code deleted: we're about 3000 lines of C lighter,
but the amount of Haskell code is about the same.
- performance is ok: some operations are faster, others are
slower. There's still some tuning to do, though.
- the new library is designed to handle read/write streams
much better: a read/write stream gets a special kind of
handle internally called a "DuplexHandle", which actually
contains two separate handles, one for writing and one for
reading. The upshot is that you can do simultaneous reading
and writing to/from a socket or FIFO without any locking
problems. The effect is similar to calling socketToHandle
twice, except that finalization works properly (creating
two separate Handles could lead to the socket being closed
too early when one of the Handles is GC'd).
- hConnectTo and withHandleFor are gone (no one responded to
my mail on GHC users, but we can always bring 'em back if
necessary).
- I made a half-hearted attempt at keeping the system-specific
code in one place: see PrelPosix.hsc.
- I've rearranged the I/O tests and added lots more.
ghc/tests/lib/IO now contains Haskell 98-only IO tests,
ghc/test/lib/{IOExts, Directory, Time} now contain tests for
the relevant libraries. I haven't quite finished in here yet,
the IO tests work but the others don't yet.
- I haven't done anything about Unicode yet, but now we can
start to discuss what needs doing here. The new library
is using MutableByteArrays for its buffers because that
turned out to be a *lot* easier (and quicker) than malloc'd
buffers - I hope this won't cause trouble for unicode
translations though.
WARNING: Windows users refrain from updating until we've had a chance
to fix any issues that arise.
Testing: the basic H98 stuff has been pretty thoroughly tested, but
the new duplex handle stuff is still a little green.
Diffstat (limited to 'ghc/lib/std/cbits/filePutc.c')
-rw-r--r-- | ghc/lib/std/cbits/filePutc.c | 103 |
1 files changed, 0 insertions, 103 deletions
diff --git a/ghc/lib/std/cbits/filePutc.c b/ghc/lib/std/cbits/filePutc.c deleted file mode 100644 index 9ca8083247..0000000000 --- a/ghc/lib/std/cbits/filePutc.c +++ /dev/null @@ -1,103 +0,0 @@ -/* - * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998 - * - * $Id: filePutc.c,v 1.12 2000/08/07 23:37:23 qrczak Exp $ - * - * hPutChar Runtime Support - */ - -#include "Rts.h" -#include "stgio.h" - -#if defined(HAVE_WINSOCK_H) && !defined(__CYGWIN__) && !defined(__CYGWIN32__) -#define USE_WINSOCK -#endif - -#ifdef USE_WINSOCK -#include <winsock.h> -#endif - -#define TERMINATE_LINE(x) ((x) == '\n') - -StgInt -filePutc(StgForeignPtr ptr, StgChar c) -{ - IOFileObject* fo = (IOFileObject*)ptr; - int rc = 0; - unsigned char byte = (unsigned char) c; - - /* What filePutc needs to do: - - - if there's no buffering => write it out. - - if the buffer is line-buffered - write out buffer (+char), iff buffer would be full afterwards || - new char is the newline character - add to buffer , otherwise - - if the buffer is fully-buffered - write out buffer (+char), iff adding char fills up buffer. - add char to buffer, otherwise. - - In the cases where a file is buffered, the invariant is that operations - that fill up a buffer also flushes them. A consequence of this here, is - that we're guaranteed to be passed a buffer with space for (at least) - the one char we're adding. - - Supporting RW objects adds yet another twist, since we have to make - sure that if such objects have been read from just previously, we - flush(i.e., empty) the buffer first. (We could be smarter about this, - but aren't!) - - Only the lower 8 bits of a character are written. The data are supposed - to be already converted to the stream's 8-bit encoding. - - */ - - if ( FILEOBJ_READABLE(fo) && FILEOBJ_JUST_READ(fo) ) { - rc = flushReadBuffer(ptr); - if (rc<0) return rc; - } - - fo->flags = (fo->flags & ~FILEOBJ_RW_READ) | FILEOBJ_RW_WRITE; - - /* check whether we can just add it to the buffer.. */ - if ( FILEOBJ_UNBUFFERED(fo) ) { - ; - } else { - /* We're buffered, add it to the pack */ - ((unsigned char*)fo->buf)[fo->bufWPtr] = byte; - fo->bufWPtr++; - /* If the buffer filled up as a result, *or* - the added character terminated a line - => flush. - */ - if ( FILEOBJ_BUFFER_FULL(fo) || - (FILEOBJ_LINEBUFFERED(fo) && TERMINATE_LINE(c)) ) { - rc = writeBuffer(ptr, fo->bufWPtr); - /* Undo the write if we're blocking..*/ - if (rc == FILEOBJ_BLOCKED_WRITE ) fo->bufWPtr--; - } - return rc; - } - - /* Unbuffered, write the character directly. */ - while ((rc = ( -#ifdef USE_WINSOCK - fo->flags & FILEOBJ_WINSOCK ? - send(fo->fd, &byte, 1, 0) : - write(fo->fd, &byte, 1))) <= 0) { -#else - write(fo->fd, &byte, 1))) <= 0) { -#endif - - if ( rc == -1 && errno == EAGAIN) { - errno = 0; - return FILEOBJ_BLOCKED_WRITE; - } else if (rc == 0 || (rc == -1 && errno != EINTR)) { - cvtErrno(); - stdErrno(); - return -1; - } - } - - return 0; -} |