Mercurial > hg > th-libs
view th_ioctx_stdio.c @ 708:a34715d51ea4
Add support for deallocator function in th_cfg_free() for freeing the values.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 27 Apr 2020 00:44:15 +0300 |
parents | 49c818acbbbe |
children | 4ca6a3b30fe8 |
line wrap: on
line source
/* * 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) { return th_stdio_fseek(ctx, 0, SEEK_SET); } 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, };