Kannel: Open Source WAP and SMS gateway  svn-r5335
wsstream.h File Reference

Go to the source code of this file.

Data Structures

struct  WsStreamRec
 

Macros

#define WS_STREAM_BUFFER_SIZE   1024
 

Typedefs

typedef size_t(* WsStreamIOProc) (void *context, WsUInt32 *buf, size_t buflen)
 
typedef WsBool(* WsStreamFlushProc) (void *context)
 
typedef void(* WsStreamCloseProc) (void *context)
 
typedef struct WsStreamRec WsStream
 

Functions

WsBool ws_stream_getc (WsStream *stream, WsUInt32 *ch_return)
 
void ws_stream_ungetc (WsStream *stream, WsUInt32 ch)
 
WsBool ws_stream_flush (WsStream *stream)
 
void ws_stream_close (WsStream *stream)
 
WsStreamws_stream_new (void *context, WsStreamIOProc io, WsStreamFlushProc flush, WsStreamCloseProc close)
 
WsStreamws_stream_new_file (FILE *fp, WsBool output, WsBool close)
 
WsStreamws_stream_new_data_input (const unsigned char *data, size_t data_len)
 

Macro Definition Documentation

◆ WS_STREAM_BUFFER_SIZE

#define WS_STREAM_BUFFER_SIZE   1024

Definition at line 78 of file wsstream.h.

Referenced by file_output(), and ws_stream_getc().

Typedef Documentation

◆ WsStream

typedef struct WsStreamRec WsStream

Definition at line 118 of file wsstream.h.

◆ WsStreamCloseProc

typedef void(* WsStreamCloseProc) (void *context)

Definition at line 95 of file wsstream.h.

◆ WsStreamFlushProc

typedef WsBool(* WsStreamFlushProc) (void *context)

Definition at line 92 of file wsstream.h.

◆ WsStreamIOProc

typedef size_t(* WsStreamIOProc) (void *context, WsUInt32 *buf, size_t buflen)

Definition at line 87 of file wsstream.h.

Function Documentation

◆ ws_stream_close()

void ws_stream_close ( WsStream stream)

Definition at line 117 of file wsstream.c.

References WsStreamRec::close, WsStreamRec::context, and ws_free().

Referenced by ws_compile_data(), and ws_compile_file().

118 {
119  if (stream->close)
120  (*stream->close)(stream->context);
121 
122  ws_free(stream);
123 }
void ws_free(void *ptr)
Definition: wsalloc.c:139
WsStreamCloseProc close
Definition: wsstream.h:103
void * context
Definition: wsstream.h:106

◆ ws_stream_flush()

WsBool ws_stream_flush ( WsStream stream)

Definition at line 108 of file wsstream.c.

References WsStreamRec::context, WsStreamRec::flush, and WS_TRUE.

109 {
110  if (stream->flush)
111  return (*stream->flush)(stream->context);
112 
113  return WS_TRUE;
114 }
Definition: wsint.h:131
void * context
Definition: wsstream.h:106
WsStreamFlushProc flush
Definition: wsstream.h:102

◆ ws_stream_getc()

WsBool ws_stream_getc ( WsStream stream,
WsUInt32 ch_return 
)

Definition at line 74 of file wsstream.c.

References WsStreamRec::buffer, WsStreamRec::buffer_pos, WsStreamRec::context, WsStreamRec::data_in_buffer, WsStreamRec::io, WsStreamRec::ungetch, WsStreamRec::ungetch_valid, WS_FALSE, WS_STREAM_BUFFER_SIZE, and WS_TRUE.

Referenced by read_float_from_exp(), read_float_from_point(), and ws_yy_lex().

75 {
76  if (stream->ungetch_valid) {
77  *ch_return = stream->ungetch;
78  stream->ungetch_valid = WS_FALSE;
79 
80  return WS_TRUE;
81  }
82 
83  if (stream->buffer_pos >= stream->data_in_buffer) {
84  /* Read more data to the buffer. */
85  stream->buffer_pos = 0;
86  stream->data_in_buffer = (*stream->io)(stream->context,
87  stream->buffer,
89  if (stream->data_in_buffer == 0)
90  /* EOF reached. */
91  return WS_FALSE;
92  }
93 
94  /* Return the next character. */
95  *ch_return = stream->buffer[stream->buffer_pos++];
96 
97  return WS_TRUE;
98 }
WsUInt32 buffer[WS_STREAM_BUFFER_SIZE]
Definition: wsstream.h:109
Definition: wsint.h:131
#define WS_STREAM_BUFFER_SIZE
Definition: wsstream.h:78
WsBool ungetch_valid
Definition: wsstream.h:114
size_t data_in_buffer
Definition: wsstream.h:111
WsStreamIOProc io
Definition: wsstream.h:101
size_t buffer_pos
Definition: wsstream.h:110
void * context
Definition: wsstream.h:106
WsUInt32 ungetch
Definition: wsstream.h:115

◆ ws_stream_new()

WsStream* ws_stream_new ( void *  context,
WsStreamIOProc  io,
WsStreamFlushProc  flush,
WsStreamCloseProc  close 
)

Definition at line 126 of file wsstream.c.

References WsStreamRec::close, WsStreamRec::context, WsStreamRec::flush, WsStreamRec::io, and ws_calloc().

Referenced by ws_stream_new_data_input(), and ws_stream_new_file().

128 {
129  WsStream *stream = ws_calloc(1, sizeof(*stream));
130 
131  if (stream == NULL)
132  return NULL;
133 
134  stream->io = io;
135  stream->flush = flush;
136  stream->close = close;
137  stream->context = context;
138 
139  return stream;
140 }
void * ws_calloc(size_t num, size_t size)
Definition: wsalloc.c:83
Definition: parse.c:65
WsStreamIOProc io
Definition: wsstream.h:101
WsStreamCloseProc close
Definition: wsstream.h:103
void * context
Definition: wsstream.h:106
WsStreamFlushProc flush
Definition: wsstream.h:102

◆ ws_stream_new_data_input()

WsStream* ws_stream_new_data_input ( const unsigned char *  data,
size_t  data_len 
)

Definition at line 108 of file wsstream_data.c.

References WsStreamDataInputCtxRec::data, data_close(), data_input(), WsStreamDataInputCtxRec::data_len, ws_calloc(), and ws_stream_new().

Referenced by ws_compile_data().

109 {
110  WsStreamDataInputCtx *ctx = ws_calloc(1, sizeof(*ctx));
111  WsStream *stream;
112 
113  if (ctx == NULL)
114  return NULL;
115 
116  ctx->data = data;
117  ctx->data_len = data_len;
118 
119  stream = ws_stream_new(ctx, data_input, NULL, data_close);
120 
121  if (stream == NULL)
122  /* The stream creation failed. Close the stream context. */
123  data_close(ctx);
124 
125  return stream;
126 }
void * ws_calloc(size_t num, size_t size)
Definition: wsalloc.c:83
WsStream * ws_stream_new(void *context, WsStreamIOProc io, WsStreamFlushProc flush, WsStreamCloseProc close)
Definition: wsstream.c:126
static size_t data_input(void *context, WsUInt32 *buf, size_t buflen)
Definition: wsstream_data.c:85
static void data_close(void *context)
Definition: wsstream_data.c:99
const unsigned char * data
Definition: wsstream_data.c:76

◆ ws_stream_new_file()

WsStream* ws_stream_new_file ( FILE *  fp,
WsBool  output,
WsBool  close 
)

Definition at line 206 of file wsstream_file.c.

References WsStreamFileCtxRec::close_fp, file_close(), file_flush(), file_input(), file_output(), WsStreamFileCtxRec::fp, ws_calloc(), and ws_stream_new().

Referenced by ws_compile_file().

207 {
208  WsStreamFileCtx *ctx = ws_calloc(1, sizeof(*ctx));
209  WsStream *stream;
210 
211  if (ctx == NULL)
212  return NULL;
213 
214  ctx->fp = fp;
215  ctx->close_fp = close;
216 
217  if (output)
219  else
221 
222  if (stream == NULL)
223  /* The stream creation failed. Close the stream context. */
224  file_close(ctx);
225 
226  return stream;
227 }
void * ws_calloc(size_t num, size_t size)
Definition: wsalloc.c:83
static size_t file_input(void *context, WsUInt32 *buf, size_t buflen)
Definition: wsstream_file.c:96
WsStream * ws_stream_new(void *context, WsStreamIOProc io, WsStreamFlushProc flush, WsStreamCloseProc close)
Definition: wsstream.c:126
static size_t file_output(void *context, WsUInt32 *buf, size_t buflen)
static void file_close(void *context)
static WsBool file_flush(void *context)

◆ ws_stream_ungetc()

void ws_stream_ungetc ( WsStream stream,
WsUInt32  ch 
)

Definition at line 101 of file wsstream.c.

References WsStreamRec::ungetch, WsStreamRec::ungetch_valid, and WS_TRUE.

Referenced by read_float_from_exp(), read_float_from_point(), and ws_yy_lex().

102 {
103  stream->ungetch = ch;
104  stream->ungetch_valid = WS_TRUE;
105 }
Definition: wsint.h:131
WsBool ungetch_valid
Definition: wsstream.h:114
WsUInt32 ungetch
Definition: wsstream.h:115
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.