wsstream.c
上传用户:gzpyjq
上传日期:2013-01-31
资源大小:1852k
文件大小:2k
源码类别:

手机WAP编程

开发平台:

WINDOWS

  1. /*
  2.  *
  3.  * wsstream.c
  4.  *
  5.  * Author: Markku Rossi <mtr@iki.fi>
  6.  *
  7.  * Copyright (c) 1999-2000 WAPIT OY LTD.
  8.  *  All rights reserved.
  9.  *
  10.  * Generic input / output stream.
  11.  *
  12.  */
  13. #include "wsint.h"
  14. /********************* Global functions *********************************/
  15. WsBool ws_stream_getc(WsStream *stream, WsUInt32 *ch_return)
  16. {
  17.     if (stream->ungetch_valid) {
  18.         *ch_return = stream->ungetch;
  19.         stream->ungetch_valid = WS_FALSE;
  20.         return WS_TRUE;
  21.     }
  22.     if (stream->buffer_pos >= stream->data_in_buffer) {
  23.         /* Read more data to the buffer. */
  24.         stream->buffer_pos = 0;
  25.         stream->data_in_buffer = (*stream->io)(stream->context,
  26.                                                stream->buffer,
  27.                                                WS_STREAM_BUFFER_SIZE);
  28.         if (stream->data_in_buffer == 0)
  29.             /* EOF reached. */
  30.             return WS_FALSE;
  31.     }
  32.     /* Return the next character. */
  33.     *ch_return = stream->buffer[stream->buffer_pos++];
  34.     return WS_TRUE;
  35. }
  36. void ws_stream_ungetc(WsStream *stream, WsUInt32 ch)
  37. {
  38.     stream->ungetch = ch;
  39.     stream->ungetch_valid = WS_TRUE;
  40. }
  41. WsBool ws_stream_flush(WsStream *stream)
  42. {
  43.     if (stream->flush)
  44.         return (*stream->flush)(stream->context);
  45.     return WS_TRUE;
  46. }
  47. void ws_stream_close(WsStream *stream)
  48. {
  49.     if (stream->close)
  50.         (*stream->close)(stream->context);
  51.     ws_free(stream);
  52. }
  53. WsStream *ws_stream_new(void *context, WsStreamIOProc io,
  54.                         WsStreamFlushProc flush, WsStreamCloseProc close)
  55. {
  56.     WsStream *stream = ws_calloc(1, sizeof(*stream));
  57.     if (stream == NULL)
  58.         return NULL;
  59.     stream->io = io;
  60.     stream->flush = flush;
  61.     stream->close = close;
  62.     stream->context = context;
  63.     return stream;
  64. }