Mercurial > hg > th-libs
diff th_ioctx_stdio.c @ 678:7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 29 Feb 2020 12:19:02 +0200 |
parents | |
children | 49c818acbbbe |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/th_ioctx_stdio.c Sat Feb 29 12:19:02 2020 +0200 @@ -0,0 +1,170 @@ +/* + * Simple I/O abstraction and context handling layer + * Programmed and designed by Matti 'ccr' Hamalainen + * (C) Copyright 2012-2020 Tecnic Software productions (TNSP) + * + * Please read file 'COPYING' for information on license and distribution. + */ +#include "th_ioctx.h" +#include <stdio.h> + +#define CTX_FH ((FILE *) ctx->data) + + +static int th_stdio_fopen(th_ioctx *ctx) +{ + ctx->data = (void *) fopen(ctx->filename, ctx->mode); + ctx->status = th_get_error(); + return (ctx->data != NULL) ? THERR_OK : THERR_FOPEN; +} + + +static void th_stdio_fclose(th_ioctx *ctx) +{ + if (CTX_FH != NULL) + { + fclose(CTX_FH); + ctx->data = NULL; + } +} + + +static int th_stdio_ferror(th_ioctx *ctx) +{ + return ctx->status; +} + + +static off_t th_stdio_ftell(th_ioctx *ctx) +{ + return ftello(CTX_FH); +} + + +static int th_stdio_fseek(th_ioctx *ctx, const off_t pos, const int whence) +{ + int ret = fseeko(CTX_FH, pos, whence); + ctx->status = th_get_error(); + return ret; +} + + +static int th_stdio_freset(th_ioctx *ctx) +{ + if (CTX_FH != NULL) + return th_stdio_fseek(ctx, 0, SEEK_SET); + else + return THERR_OK; +} + + +static off_t th_stdio_fsize(th_ioctx *ctx) +{ + off_t savePos, fileSize; + + // Check if the size is cached + if (ctx->size != 0) + return ctx->size; + + // Get file size + if ((savePos = th_stdio_ftell(ctx)) < 0) + return -1; + + if (th_stdio_fseek(ctx, 0, SEEK_END) != 0) + return -1; + + if ((fileSize = th_stdio_ftell(ctx)) < 0) + return -1; + + if (th_stdio_fseek(ctx, savePos, SEEK_SET) != 0) + return -1; + + ctx->size = fileSize; + return fileSize; +} + + +static BOOL th_stdio_feof(th_ioctx *ctx) +{ + return feof(CTX_FH); +} + + +static int th_stdio_fgetc(th_ioctx *ctx) +{ + int ret = fgetc(CTX_FH); + ctx->status = th_get_error(); + return ret; +} + + +static int th_stdio_fputc(int v, th_ioctx *ctx) +{ + int ret = fputc(v, CTX_FH); + ctx->status = th_get_error(); + return ret; +} + + +static size_t th_stdio_fread(void *ptr, size_t size, size_t nmemb, th_ioctx *ctx) +{ + size_t ret = fread(ptr, size, nmemb, CTX_FH); + ctx->status = th_get_error(); + return ret; +} + + +static size_t th_stdio_fwrite(const void *ptr, size_t size, size_t nmemb, th_ioctx *ctx) +{ + size_t ret = fwrite(ptr, size, nmemb, CTX_FH); + ctx->status = th_get_error(); + return ret; +} + + +static char * th_stdio_fgets(char *str, int size, th_ioctx *ctx) +{ + char *ret = fgets(str, size, CTX_FH); + ctx->status = th_get_error(); + return ret; +} + + +static int th_stdio_fputs(const char *str, th_ioctx *ctx) +{ + int ret = fputs(str, CTX_FH); + ctx->status = th_get_error(); + return ret; +} + + +static int th_stdio_vfprintf(th_ioctx *ctx, const char *fmt, va_list ap) +{ + int ret = vfprintf(CTX_FH, fmt, ap); + ctx->status = th_get_error(); + return ret; +} + + +const th_ioctx_ops th_stdio_io_ops = +{ + "stdio", + + th_stdio_fopen, + th_stdio_fclose, + + th_stdio_freset, + th_stdio_ferror, + th_stdio_fseek, + th_stdio_fsize, + th_stdio_ftell, + th_stdio_feof, + th_stdio_fgetc, + th_stdio_fputc, + th_stdio_fread, + th_stdio_fwrite, + + th_stdio_fgets, + th_stdio_fputs, + th_stdio_vfprintf, +};