Annotation of libwww/Library/src/HTNetTxt.c, revision 2.4
2.1 frystyk 1: /* HTNetTxt.c
2: ** NETWORK TELNET TO INTERNAL CHARACTER TEXT AND VISE VERSA
3: **
2.2 frystyk 4: ** (c) COPYRIGHT MIT 1995.
2.1 frystyk 5: ** Please first read the full copyright statement in the file COPYRIGH.
6: **
2.3 frystyk 7: ** This module provides two basic streams that converts from CRLF to
8: ** an internal C representation (\n) and from the C representation to
9: ** CRLF.
2.1 frystyk 10: **
11: ** HISTORY:
12: ** 27 Mar 95 HFN Spawned off from HTFormat.c
13: */
14:
15: /* Library Include files */
16: #include "tcp.h"
17: #include "HTUtils.h"
18: #include "HTString.h"
19: #include "HTStream.h"
20: #include "HTNetTxt.h" /* Implemented here */
21:
22: /* Typedefs and global variable local to this module */
23: struct _HTStream {
24: CONST HTStreamClass * isa;
25: HTStream * target;
26: CONST char * start;
27: BOOL had_cr;
28: };
29:
30: #define PUTC(c) (*me->target->isa->put_character)(me->target, c)
31: #define PUTBLOCK(b, l) (*me->target->isa->put_block)(me->target, b, l)
32:
33: /* ------------------------------------------------------------------------- */
34:
2.4 ! frystyk 35: /* Converter from CRLF to \n
! 36: ** -------------------------
! 37: ** The input is assumed to be in local representation with lines
! 38: ** delimited by (CR,LF) pairs in the local representation.
! 39: ** The conversion to local representation is always done in HTSocket.c
! 40: ** The (CR,LF) sequence when found is changed to a '\n' character,
! 41: ** the internal C representation of a new line.
2.3 frystyk 42: */
2.4 ! frystyk 43: PRIVATE int NetToText_put_block (HTStream * me, CONST char * s, int l)
2.1 frystyk 44: {
45: int status;
46: if (!me->start)
47: me->start = s;
48: else {
49: l -= (me->start - s);
50: s = me->start;
51: }
52: while (l-- > 0) {
53: if (me->had_cr && *s == LF) {
54: if (s > me->start+1) {
55: if ((status = PUTBLOCK(me->start, s - me->start-1)) != HT_OK)
56: return status;
57: }
58: me->start = s+1;
59: if ((status = PUTC('\n')) != HT_OK)
60: return status;
61: }
62: me->had_cr = (*s++ == CR);
63: }
64: if (me->start < s && (status = PUTBLOCK(me->start, s-me->start)) != HT_OK)
65: return status;
66: me->start = NULL; /* Whole buffer is written :-) */
67: return HT_OK;
68: }
69:
2.4 ! frystyk 70: PRIVATE int NetToText_put_character (HTStream * me, char c)
2.3 frystyk 71: {
72: return NetToText_put_block(me, &c, 1);
73: }
74:
2.4 ! frystyk 75: PRIVATE int NetToText_put_string (HTStream * me, CONST char * s)
2.3 frystyk 76: {
77: return NetToText_put_block(me, s, (int) strlen(s));
78: }
79:
2.4 ! frystyk 80: PRIVATE int Net_flush (HTStream * me)
2.1 frystyk 81: {
2.4 ! frystyk 82: return me->target ? (*me->target->isa->flush)(me->target) : HT_OK;
2.1 frystyk 83: }
84:
2.4 ! frystyk 85: PRIVATE int Net_free (HTStream * me)
2.1 frystyk 86: {
2.4 ! frystyk 87: int status = HT_OK;
! 88: if (me->target) {
! 89: if ((status = (*me->target->isa->_free)(me->target)) == HT_WOULD_BLOCK)
! 90: return HT_WOULD_BLOCK;
! 91: }
2.1 frystyk 92: free(me);
2.4 ! frystyk 93: return status;
2.1 frystyk 94: }
95:
2.4 ! frystyk 96: PRIVATE int Net_abort (HTStream * me, HTError e)
2.1 frystyk 97: {
2.4 ! frystyk 98: if (me->target)
! 99: (*me->target->isa->abort)(me->target, e);
2.1 frystyk 100: free(me);
101: return HT_ERROR;
102: }
103:
104: PRIVATE HTStreamClass NetToTextClass = {
105: "NetToText",
2.4 ! frystyk 106: Net_flush,
! 107: Net_free,
! 108: Net_abort,
2.1 frystyk 109: NetToText_put_character,
110: NetToText_put_string,
111: NetToText_put_block
112: };
113:
2.4 ! frystyk 114: PUBLIC HTStream * HTNetToText (HTStream * target)
2.1 frystyk 115: {
2.4 ! frystyk 116: HTStream* me = (HTStream *) calloc(1, sizeof(HTStream));
2.1 frystyk 117: if (me == NULL) outofmem(__FILE__, "NetToText");
118: me->isa = &NetToTextClass;
2.3 frystyk 119: me->had_cr = NO;
120: me->target = target;
121: return me;
122: }
123:
2.4 ! frystyk 124: /* Converter from \n to CRLF
! 125: ** -------------------------
! 126: ** The input is assumed to be in local representation with lines
! 127: ** delimited by \n. The \n when found is changed to a CRLF sequence,
! 128: ** the network representation of a new line.
! 129: ** Conversion: '\r' is stripped and \n => CRLF
2.3 frystyk 130: */
2.4 ! frystyk 131: PRIVATE int TextToNet_put_block (HTStream * me, CONST char* b, int len)
2.3 frystyk 132: {
133: int status;
134: CONST char *limit = b+len;
135:
136: if (!me->start)
137: me->start = b;
138: else {
139: len -= (me->start - b);
140: b = me->start;
141: }
142: while (len-- > 0) {
143: if (me->had_cr && *b == LF) {
144: if (b > me->start+1) {
145: if ((status = PUTBLOCK(me->start, b - me->start-1)) != HT_OK)
146: return status;
147: }
148: me->start = b+1;
149: if ((status = PUTC('\n')) != HT_OK)
150: return status;
151: }
152: me->had_cr = (*b++ == CR);
153: }
154: if (me->start < b && (status = PUTBLOCK(me->start, b-me->start)) != HT_OK)
155: return status;
156: me->start = NULL; /* Whole buffer is written :-) */
157: return HT_OK;
158: }
159:
2.4 ! frystyk 160: PRIVATE int TextToNet_put_character (HTStream * me, char c)
2.3 frystyk 161: {
162: return TextToNet_put_block(me, &c, 1);
163: }
164:
2.4 ! frystyk 165: PRIVATE int TextToNet_put_string (HTStream * me, CONST char * s)
2.3 frystyk 166: {
167: return TextToNet_put_block(me, s, (int) strlen(s));
168: }
169:
170: PRIVATE HTStreamClass TextToNetClass = {
171: "TextToNet",
2.4 ! frystyk 172: Net_flush,
! 173: Net_free,
! 174: Net_abort,
2.3 frystyk 175: TextToNet_put_character,
176: TextToNet_put_string,
177: TextToNet_put_block
178: };
179:
2.4 ! frystyk 180: PUBLIC HTStream * HTTextToNet (HTStream * target)
2.3 frystyk 181: {
2.4 ! frystyk 182: HTStream* me = (HTStream *) calloc(1, sizeof(HTStream));
2.3 frystyk 183: if (me == NULL) outofmem(__FILE__, "TextToNet");
184: me->isa = &TextToNetClass;
2.1 frystyk 185: me->had_cr = NO;
186: me->target = target;
187: return me;
188: }
2.4 ! frystyk 189:
! 190:
! 191:
2.1 frystyk 192:
Webmaster