diff options
author | Thomas E. Dickey <dickey@invisible-island.net> | 1996-09-02 19:39:24 -0400 |
---|---|---|
committer | Thomas E. Dickey <dickey@invisible-island.net> | 1996-09-02 19:39:24 -0400 |
commit | e087f6d44e87f489fcb3056e86319ebba4218156 (patch) | |
tree | d045b58011bfbbf5186d34c4fed9e0dedb363275 /WWW/Library/Implementation/HTWriter.c | |
download | lynx-snapshots-e087f6d44e87f489fcb3056e86319ebba4218156.tar.gz |
snapshot of project "lynx", label v2_6
Diffstat (limited to 'WWW/Library/Implementation/HTWriter.c')
-rw-r--r-- | WWW/Library/Implementation/HTWriter.c | 189 |
1 files changed, 189 insertions, 0 deletions
diff --git a/WWW/Library/Implementation/HTWriter.c b/WWW/Library/Implementation/HTWriter.c new file mode 100644 index 00000000..03a55648 --- /dev/null +++ b/WWW/Library/Implementation/HTWriter.c @@ -0,0 +1,189 @@ +/* FILE WRITER HTWrite.c +** =========== +** +*/ +#include "HTUtils.h" +#include "tcp.h" + +#include "HTWriter.h" + +#define BUFFER_SIZE 4096 /* Tradeoff */ + +/*#include <stdio.h> included by HTUtils.h -- FM */ + +#include "LYLeaks.h" + +#define FREE(x) if (x) {free(x); x = NULL;} + +/* HTML Object +** ----------- +*/ + +struct _HTStream { + CONST HTStreamClass * isa; + + int soc; + char *write_pointer; + char buffer[BUFFER_SIZE]; +#ifdef NOT_ASCII + BOOL make_ascii; /* Are we writing to the net? */ +#endif +}; + + +/* Write the buffer out to the socket +** ---------------------------------- +*/ + +PRIVATE void flush ARGS1(HTStream *, me) +{ + char *read_pointer = me->buffer; + char *write_pointer = me->write_pointer; + +#ifdef NOT_ASCCII + if (me->make_ascii) { + char * p; + for(p = me->buffer; p < me->write_pointer; p++) + *p = TOASCII(*p); + } +#endif + while (read_pointer < write_pointer) { + int status; + status = NETWRITE(me->soc, me->buffer, /* Put timeout? @@@ */ + write_pointer - read_pointer); + if (status<0) { + if(TRACE) fprintf(stderr, + "HTWrite: Error: write() on socket returns %d !!!\n", status); + return; + } + read_pointer = read_pointer + status; + } + me->write_pointer = me->buffer; +} + + +/*_________________________________________________________________________ +** +** A C T I O N R O U T I N E S +*/ + +/* Character handling +** ------------------ +*/ + +PRIVATE void HTWriter_put_character ARGS2(HTStream *, me, char, c) +{ + if (me->write_pointer == &me->buffer[BUFFER_SIZE]) flush(me); + *me->write_pointer++ = c; +} + + + +/* String handling +** --------------- +** +** Strings must be smaller than this buffer size. +*/ +PRIVATE void HTWriter_put_string ARGS2(HTStream *, me, CONST char*, s) +{ + int l = strlen(s); + if (me->write_pointer + l > &me->buffer[BUFFER_SIZE]) flush(me); + strcpy(me->write_pointer, s); + me->write_pointer = me->write_pointer + l; +} + + +/* Buffer write. Buffers can (and should!) be big. +** ------------ +*/ +PRIVATE void HTWriter_write ARGS3(HTStream *, me, CONST char*, s, int, l) +{ + + CONST char *read_pointer = s; + CONST char *write_pointer = s+l; + + flush(me); /* First get rid of our buffer */ + + while (read_pointer < write_pointer) { + int status = NETWRITE(me->soc, (char *)read_pointer, + write_pointer - read_pointer); + if (status<0) { + if(TRACE) fprintf(stderr, + "HTWriter_write: Error on socket output stream!!!\n"); + return; + } + read_pointer = read_pointer + status; + } +} + + + + +/* Free an HTML object +** ------------------- +** +** Note that the SGML parsing context is freed, but the created object is not, +** as it takes on an existence of its own unless explicitly freed. +*/ +PRIVATE void HTWriter_free ARGS1(HTStream *, me) +{ + flush(me); + NETCLOSE(me->soc); + FREE(me); +} + +PRIVATE void HTWriter_abort ARGS2(HTStream *, me, HTError, e) +{ + HTWriter_free(me); +} + + +/* Structured Object Class +** ----------------------- +*/ +PRIVATE CONST HTStreamClass HTWriter = /* As opposed to print etc */ +{ + "SocketWriter", + HTWriter_free, + HTWriter_abort, + HTWriter_put_character, HTWriter_put_string, + HTWriter_write +}; + + +/* Subclass-specific Methods +** ------------------------- +*/ + +PUBLIC HTStream* HTWriter_new ARGS1(int, soc) +{ + HTStream* me = (HTStream*)malloc(sizeof(*me)); + if (me == NULL) outofmem(__FILE__, "HTML_new"); + me->isa = &HTWriter; + +#ifdef NOT_ASCII + me->make_ascii = NO; +#endif + me->soc = soc; + me->write_pointer = me->buffer; + return me; +} + +/* Subclass-specific Methods +** ------------------------- +*/ + +PUBLIC HTStream* HTASCIIWriter ARGS1(int, soc) +{ + HTStream* me = (HTStream*)malloc(sizeof(*me)); + if (me == NULL) outofmem(__FILE__, "HTML_new"); + me->isa = &HTWriter; + +#ifdef NOT_ASCII + me->make_ascii = YES; +#endif + me->soc = soc; + me->write_pointer = me->buffer; + return me; +} + |