Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
wsstream_file.c File Reference
#include "wsint.h"

Go to the source code of this file.

Data Structures

struct  WsStreamFileCtxRec
 

Typedefs

typedef struct WsStreamFileCtxRec WsStreamFileCtx
 

Functions

static size_t file_input (void *context, WsUInt32 *buf, size_t buflen)
 
static size_t file_output (void *context, WsUInt32 *buf, size_t buflen)
 
static WsBool file_flush (void *context)
 
static void file_close (void *context)
 
WsStreamws_stream_new_file (FILE *fp, WsBool output, WsBool close)
 

Typedef Documentation

Definition at line 92 of file wsstream_file.c.

Function Documentation

static void file_close ( void *  context)
static

Definition at line 194 of file wsstream_file.c.

References WsStreamFileCtxRec::close_fp, WsStreamFileCtxRec::fp, and ws_free().

Referenced by ws_stream_new_file().

195 {
197 
198  if (ctx->close_fp)
199  fclose(ctx->fp);
200 
201  ws_free(ctx);
202 }
Definition: parse.c:65
void ws_free(void *ptr)
Definition: wsalloc.c:139
static WsBool file_flush ( void *  context)
static

Definition at line 171 of file wsstream_file.c.

References WsStreamFileCtxRec::buf, WsStreamFileCtxRec::data_in_buf, WsStreamFileCtxRec::fp, and WS_FALSE.

Referenced by ws_stream_new_file().

172 {
174 
175  /* If the internal buffer has any data, then this stream must be an
176  output stream. The variable `data_in_buf' is not updated on
177  input streams. */
178  if (ctx->data_in_buf) {
179  if (fwrite(ctx->buf, 1, ctx->data_in_buf, ctx->fp) != ctx->data_in_buf) {
180  /* The write failed. */
181  ctx->data_in_buf = 0;
182  return WS_FALSE;
183  }
184 
185  /* The temporary buffer is not empty. */
186  ctx->data_in_buf = 0;
187  }
188 
189  /* Flush the underlying file stream. */
190  return fflush(ctx->fp) == 0;
191 }
Definition: parse.c:65
unsigned char buf[WS_STREAM_BUFFER_SIZE]
Definition: wsstream_file.c:82
static size_t file_input ( void *  context,
WsUInt32 buf,
size_t  buflen 
)
static

Definition at line 96 of file wsstream_file.c.

References WsStreamFileCtxRec::buf, and WsStreamFileCtxRec::fp.

Referenced by ws_stream_new_file().

97 {
99  size_t read = 0;
100 
101  while (buflen > 0) {
102  size_t toread = buflen < sizeof(ctx->buf) ? buflen : sizeof(ctx->buf);
103  size_t got, i;
104 
105  got = fread(ctx->buf, 1, toread, ctx->fp);
106 
107  /* Convert the data to the stream's IO buffer. */
108  for (i = 0; i < got; i++)
109  buf[i] = ctx->buf[i];
110 
111  buflen -= got;
112  buf += got;
113  read += got;
114 
115  if (got < toread)
116  /* EOF seen. */
117  break;
118  }
119 
120  return read;
121 }
Definition: parse.c:65
unsigned char buf[WS_STREAM_BUFFER_SIZE]
Definition: wsstream_file.c:82
static size_t file_output ( void *  context,
WsUInt32 buf,
size_t  buflen 
)
static

Definition at line 124 of file wsstream_file.c.

References WsStreamFileCtxRec::buf, WsStreamFileCtxRec::data_in_buf, WsStreamFileCtxRec::fp, and WS_STREAM_BUFFER_SIZE.

Referenced by ws_stream_new_file().

125 {
127  size_t wrote = 0;
128  unsigned char ch;
129 
130  while (buflen) {
131  /* Do we have any space in the stream's internal IO buffer? */
132  if (ctx->data_in_buf >= WS_STREAM_BUFFER_SIZE) {
133  size_t w;
134 
135  /* No, flush something to our file stream. */
136  w = fwrite(ctx->buf, 1, ctx->data_in_buf, ctx->fp);
137  if (w < ctx->data_in_buf) {
138  /* Write failed. As a result code we return the number
139  of characters written from our current write
140  request. */
141  ctx->data_in_buf = 0;
142  return wrote;
143  }
144 
145  ctx->data_in_buf = 0;
146  }
147  /* Now we have space in the internal buffer. */
148 
149  /* Here we could perform some sort of conversions from ISO 10646
150  to the output character set. Currently we just support
151  ISO-8859/1 and all unknown characters are replaced with
152  '?'. */
153 
154  if (*buf > 0xff)
155  ch = '?';
156  else
157  ch = (unsigned char) * buf;
158 
159  ctx->buf[ctx->data_in_buf++] = ch;
160 
161  /* Move forward. */
162  buf++;
163  buflen--;
164  wrote++;
165  }
166 
167  return wrote;
168 }
Definition: parse.c:65
#define WS_STREAM_BUFFER_SIZE
Definition: wsstream.h:78
unsigned char buf[WS_STREAM_BUFFER_SIZE]
Definition: wsstream_file.c:82
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)
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.