Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

wsstream_file.c File Reference

#include "wsint.h"

Include dependency graph for wsstream_file.c:

Include dependency graph

Go to the source code of this file.

Data Structures

struct  WsStreamFileCtxRec

Typedefs

typedef WsStreamFileCtxRec WsStreamFileCtx

Functions

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


Typedef Documentation

typedef struct WsStreamFileCtxRec WsStreamFileCtx
 

Definition at line 92 of file wsstream_file.c.

Referenced by file_close(), file_flush(), file_input(), file_output(), and ws_stream_new_file().


Function Documentation

void file_close void *  context  )  [static]
 

Definition at line 194 of file wsstream_file.c.

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

Referenced by ws_stream_new_file().

00195 {
00196     WsStreamFileCtx *ctx = (WsStreamFileCtx *) context;
00197 
00198     if (ctx->close_fp)
00199         fclose(ctx->fp);
00200 
00201     ws_free(ctx);
00202 }

Here is the call graph for this function:

WsBool file_flush void *  context  )  [static]
 

Definition at line 171 of file wsstream_file.c.

References WsStreamFileCtxRec::buf, WsStreamFileCtxRec::data_in_buf, WsStreamFileCtxRec::fp, WsBool, and WsStreamFileCtx.

Referenced by ws_stream_new_file().

00172 {
00173     WsStreamFileCtx *ctx = (WsStreamFileCtx *) context;
00174 
00175     /* If the internal buffer has any data, then this stream must be an
00176        output stream.  The variable `data_in_buf' is not updated on
00177        input streams. */
00178     if (ctx->data_in_buf) {
00179         if (fwrite(ctx->buf, 1, ctx->data_in_buf, ctx->fp) != ctx->data_in_buf) {
00180             /* The write failed. */
00181             ctx->data_in_buf = 0;
00182             return WS_FALSE;
00183         }
00184 
00185         /* The temporary buffer is not empty. */
00186         ctx->data_in_buf = 0;
00187     }
00188 
00189     /* Flush the underlying file stream. */
00190     return fflush(ctx->fp) == 0;
00191 }

size_t file_input void *  context,
WsUInt32 buf,
size_t  buflen
[static]
 

Definition at line 96 of file wsstream_file.c.

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

Referenced by ws_stream_new_file().

00097 {
00098     WsStreamFileCtx *ctx = (WsStreamFileCtx *) context;
00099     size_t read = 0;
00100 
00101     while (buflen > 0) {
00102         size_t toread = buflen < sizeof(ctx->buf) ? buflen : sizeof(ctx->buf);
00103         size_t got, i;
00104 
00105         got = fread(ctx->buf, 1, toread, ctx->fp);
00106 
00107         /* Convert the data to the stream's IO buffer. */
00108         for (i = 0; i < got; i++)
00109             buf[i] = ctx->buf[i];
00110 
00111         buflen -= got;
00112         buf += got;
00113         read += got;
00114 
00115         if (got < toread)
00116             /* EOF seen. */
00117             break;
00118     }
00119 
00120     return read;
00121 }

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 WsStreamFileCtx.

Referenced by ws_stream_new_file().

00125 {
00126     WsStreamFileCtx *ctx = (WsStreamFileCtx *) context;
00127     size_t wrote = 0;
00128     unsigned char ch;
00129 
00130     while (buflen) {
00131         /* Do we have any space in the stream's internal IO buffer? */
00132         if (ctx->data_in_buf >= WS_STREAM_BUFFER_SIZE) {
00133             size_t w;
00134 
00135             /* No, flush something to our file stream. */
00136             w = fwrite(ctx->buf, 1, ctx->data_in_buf, ctx->fp);
00137             if (w < ctx->data_in_buf) {
00138                 /* Write failed.  As a result code we return the number
00139                           of characters written from our current write
00140                           request. */
00141                 ctx->data_in_buf = 0;
00142                 return wrote;
00143             }
00144 
00145             ctx->data_in_buf = 0;
00146         }
00147         /* Now we have space in the internal buffer. */
00148 
00149         /* Here we could perform some sort of conversions from ISO 10646
00150            to the output character set.  Currently we just support
00151            ISO-8859/1 and all unknown characters are replaced with
00152            '?'. */
00153 
00154         if (*buf > 0xff)
00155             ch = '?';
00156         else
00157             ch = (unsigned char) * buf;
00158 
00159         ctx->buf[ctx->data_in_buf++] = ch;
00160 
00161         /* Move forward. */
00162         buf++;
00163         buflen--;
00164         wrote++;
00165     }
00166 
00167     return wrote;
00168 }

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(), ws_stream_new(), WsStream, and WsStreamFileCtx.

Referenced by ws_compile_file().

00207 {
00208     WsStreamFileCtx *ctx = ws_calloc(1, sizeof(*ctx));
00209     WsStream *stream;
00210 
00211     if (ctx == NULL)
00212         return NULL;
00213 
00214     ctx->fp = fp;
00215     ctx->close_fp = close;
00216 
00217     if (output)
00218         stream = ws_stream_new(ctx, file_output, file_flush, file_close);
00219     else
00220         stream = ws_stream_new(ctx, file_input, file_flush, file_close);
00221 
00222     if (stream == NULL)
00223         /* The stream creation failed.  Close the stream context. */
00224         file_close(ctx);
00225 
00226     return stream;
00227 }

Here is the call graph for this function:

See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.