Annotation of libwww/Library/src/HTNetTxt.c, revision 2.1
2.1 ! frystyk 1: /* HTNetTxt.c
! 2: ** NETWORK TELNET TO INTERNAL CHARACTER TEXT AND VISE VERSA
! 3: **
! 4: ** (c) COPYRIGHT CERN 1994.
! 5: ** Please first read the full copyright statement in the file COPYRIGH.
! 6: **
! 7: ** The input is assumed to be in local representation with lines
! 8: ** delimited by (CR,LF) pairs in the local representation.
! 9: ** The (CR,LF) sequenc when found is changed to a '\n' character,
! 10: ** the internal C representation of a new line.
! 11: **
! 12: ** HISTORY:
! 13: ** 27 Mar 95 HFN Spawned off from HTFormat.c
! 14: */
! 15:
! 16: /* Library Include files */
! 17: #include "tcp.h"
! 18: #include "HTUtils.h"
! 19: #include "HTString.h"
! 20: #include "HTStream.h"
! 21: #include "HTNetTxt.h" /* Implemented here */
! 22:
! 23: /* Typedefs and global variable local to this module */
! 24: struct _HTStream {
! 25: CONST HTStreamClass * isa;
! 26: HTStream * target;
! 27: CONST char * start;
! 28: BOOL had_cr;
! 29: };
! 30:
! 31: #define PUTC(c) (*me->target->isa->put_character)(me->target, c)
! 32: #define PUTBLOCK(b, l) (*me->target->isa->put_block)(me->target, b, l)
! 33:
! 34: /* ------------------------------------------------------------------------- */
! 35:
! 36: PRIVATE int NetToText_put_character ARGS2(HTStream *, me, char, c)
! 37: {
! 38: return PUTBLOCK(&c, 1);
! 39: }
! 40:
! 41: PRIVATE int NetToText_put_string ARGS2(HTStream *, me, CONST char *, s)
! 42: {
! 43: return PUTBLOCK(s, (int) strlen(s));
! 44: }
! 45:
! 46: PRIVATE int NetToText_put_block ARGS3(HTStream *, me, CONST char*, s, int, l)
! 47: {
! 48: int status;
! 49: if (!me->start)
! 50: me->start = s;
! 51: else {
! 52: l -= (me->start - s);
! 53: s = me->start;
! 54: }
! 55: while (l-- > 0) {
! 56: if (me->had_cr && *s == LF) {
! 57: if (s > me->start+1) {
! 58: if ((status = PUTBLOCK(me->start, s - me->start-1)) != HT_OK)
! 59: return status;
! 60: }
! 61: me->start = s+1;
! 62: if ((status = PUTC('\n')) != HT_OK)
! 63: return status;
! 64: }
! 65: me->had_cr = (*s++ == CR);
! 66: }
! 67: if (me->start < s && (status = PUTBLOCK(me->start, s-me->start)) != HT_OK)
! 68: return status;
! 69: me->start = NULL; /* Whole buffer is written :-) */
! 70: return HT_OK;
! 71: }
! 72:
! 73: PRIVATE int NetToText_flush ARGS1(HTStream *, me)
! 74: {
! 75: return me->target->isa->flush(me->target);
! 76: }
! 77:
! 78: PRIVATE int NetToText_free ARGS1(HTStream *, me)
! 79: {
! 80: me->target->isa->_free(me->target); /* Close rest of pipe */
! 81: free(me);
! 82: return HT_OK;
! 83: }
! 84:
! 85: PRIVATE int NetToText_abort ARGS2(HTStream *, me, HTError, e)
! 86: {
! 87: me->target->isa->abort(me->target,e); /* Abort rest of pipe */
! 88: free(me);
! 89: return HT_ERROR;
! 90: }
! 91:
! 92: PRIVATE HTStreamClass NetToTextClass = {
! 93: "NetToText",
! 94: NetToText_flush,
! 95: NetToText_free,
! 96: NetToText_abort,
! 97: NetToText_put_character,
! 98: NetToText_put_string,
! 99: NetToText_put_block
! 100: };
! 101:
! 102: PUBLIC HTStream * HTNetToText ARGS1(HTStream *, target)
! 103: {
! 104: HTStream* me = (HTStream *) calloc(1, sizeof(*me));
! 105: if (me == NULL) outofmem(__FILE__, "NetToText");
! 106: me->isa = &NetToTextClass;
! 107:
! 108: me->had_cr = NO;
! 109: me->target = target;
! 110: return me;
! 111: }
! 112:
Webmaster