HTNetTxt.c
上传用户:zlh9724
上传日期:2007-01-04
资源大小:1991k
文件大小:5k
源码类别:

浏览器

开发平台:

Unix_Linux

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