Mercurial > hg > th-libs
changeset 67:d94af48cef7b
Added new module, a simple I/O context helper.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 15 Nov 2012 19:21:14 +0200 |
parents | 34d58b0f2d52 |
children | 29f9651465c6 |
files | th_ioctx.c th_ioctx.h |
diffstat | 2 files changed, 133 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/th_ioctx.c Thu Nov 15 19:21:14 2012 +0200 @@ -0,0 +1,88 @@ +/* + * Standard I/O context helpers + * Programmed and designed by Matti 'ccr' Hamalainen + * (C) Copyright 2012 Tecnic Software productions (TNSP) + * + * Please read file 'COPYING' for information on license and distribution. + */ +#include "th_ioctx.h" +#include "th_string.h" + +/* Simple STD I/O contexts + */ +BOOL th_ioctx_init(th_ioctx_t *ctx, const char *filename) +{ + if (ctx == NULL || filename == NULL) + return FALSE; + + th_memset(&ctx, 0, sizeof(ctx)); + if ((ctx->filename = th_strdup(filename)) == NULL) + return FALSE; + + return TRUE; +} + + +BOOL th_ioctx_open(th_ioctx_t *ctx, const char *filename, const char *mode) +{ + if (!th_ioctx_init(ctx, filename) || mode == NULL) + return FALSE; + + if ((ctx->fp = fopen(filename, mode)) == NULL) + return FALSE; + + return TRUE; +} + + +void th_ioctx_close(th_ioctx_t *ctx) +{ + if (ctx != NULL) + { + th_free(ctx->filename); + ctx->filename = NULL; + + if (ctx->fp != NULL) + fclose(ctx->fp); + ctx->fp = NULL; + } +} + + +void th_ioctx_error(th_ioctx_t *ctx, const int err, const char *fmt, ...) +{ + char *msg; + va_list ap; + + va_start(ap, fmt); + msg = th_strdup_vprintf(fmt, ap); + va_end(ap); + + if (ctx->error != NULL) + ctx->error(ctx, err, msg); + else + THERR("'%s' #%d: %s\n", ctx->filename, (unsigned int) ctx->line, msg); + + th_free(msg); +} + + +th_ioctx_t *th_ioctx_new(const char *filename) +{ + th_ioctx_t *ctx = th_malloc0(sizeof(th_ioctx_t)); + if (ctx == NULL) + return NULL; + + th_ioctx_init(ctx, filename); + ctx->allocated = TRUE; + return ctx; +} + + +void th_ioctx_free(th_ioctx_t *ctx) +{ + th_ioctx_close(ctx); + + if (ctx->allocated) + th_free(ctx); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/th_ioctx.h Thu Nov 15 19:21:14 2012 +0200 @@ -0,0 +1,45 @@ +/* + * Standard I/O context helpers + * Programmed and designed by Matti 'ccr' Hamalainen + * (C) Copyright 2012 Tecnic Software productions (TNSP) + * + * Please read file 'COPYING' for information on license and distribution. + */ +#ifndef TH_IOCTX_H +#define TH_IOCTX_H + +#include "th_util.h" +#include <stdio.h> + +#ifdef __cplusplus +extern "C" { +#endif + + +/* Simple STD I/O contexts + */ +typedef struct _th_ioctx_t +{ + BOOL allocated; + char *filename; + FILE *fp; + size_t line; + + void (*error)(struct _th_ioctx_t *, const int, const char *msg); + void (*msg)(struct _th_ioctx_t *, const char *msg); +} th_ioctx_t; + + +BOOL th_ioctx_init(th_ioctx_t *ctx, const char *filename); +BOOL th_ioctx_open(th_ioctx_t *ctx, const char *filename, const char *mode); +void th_ioctx_close(th_ioctx_t *ctx); +void th_ioctx_error(th_ioctx_t *ctx, const int err, const char *fmt, ...); + +th_ioctx_t * th_ioctx_new(const char *filename); +void th_ioctx_free(th_ioctx_t *ctx); + + +#ifdef __cplusplus +} +#endif +#endif /* TH_IOCTX_H */