diff th_ioctx.h @ 202:b392293047da

Refactor I/O contexts. Breaks API and all that.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 11 Feb 2016 22:27:56 +0200
parents aa2d608fb3f3
children 55f429dff750
line wrap: on
line diff
--- a/th_ioctx.h	Thu Feb 11 21:38:52 2016 +0200
+++ b/th_ioctx.h	Thu Feb 11 22:27:56 2016 +0200
@@ -1,7 +1,7 @@
 /*
- * Standard I/O context helpers
+ * Simple I/O abstraction and context handling layer
  * Programmed and designed by Matti 'ccr' Hamalainen
- * (C) Copyright 2012 Tecnic Software productions (TNSP)
+ * (C) Copyright 2012,2016 Tecnic Software productions (TNSP)
  *
  * Please read file 'COPYING' for information on license and distribution.
  */
@@ -9,7 +9,7 @@
 #define TH_IOCTX_H
 
 #include "th_util.h"
-#include <stdio.h>
+#include <time.h>
 
 
 #ifdef __cplusplus
@@ -17,35 +17,117 @@
 #endif
 
 
-/* Simple STD I/O contexts
- */
-typedef struct _th_ioctx_t
+// Typedefs and structures
+//
+struct th_ioctx;
+struct th_ioctx_ops;
+
+
+typedef struct th_ioctx
 {
-    BOOL allocated;
     char *filename;
-    FILE *fp;
+    char *mode;
+    void *data;
+    time_t atime;
+    int64_t size;
+    int errno;
     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;
+    void (*error)(struct th_ioctx *, const int err, const char *msg);
+    void (*msg)(struct th_ioctx *, const int level, const char *msg);
+
+    const struct th_ioctx_ops *fops;
+} th_ioctx;
+
+
+typedef struct th_ioctx_ops
+{
+    char    *name;
+
+    int     (*fopen)(th_ioctx *ctx);
+    void    (*fclose)(th_ioctx *ctx);
+
+    int     (*freset)(th_ioctx *ctx);
+    int     (*ferror)(th_ioctx *ctx);
+    int     (*fseek)(th_ioctx *ctx, const off_t, const int whence);
+    off_t   (*fsize)(th_ioctx *ctx);
+    off_t   (*ftell)(th_ioctx *ctx);
+    BOOL    (*feof)(th_ioctx *ctx);
+    int     (*fgetc)(th_ioctx *ctx);
+    int     (*fputc)(int, th_ioctx *ctx);
+    size_t  (*fread)(void *ptr, const size_t, const size_t, th_ioctx *ctx);
+    size_t  (*fwrite)(const void *ptr, const size_t, const size_t, th_ioctx *ctx);
+    int     (*vfprintf)(th_ioctx *ctx, const char *format, va_list ap);
+
+} th_ioctx_ops;
+
+
+//
+// Some basic iops
+//
+extern const th_ioctx_ops th_stdio_io_ops;
+
 
 
-BOOL th_ioctx_init(th_ioctx_t *ctx, const char *filename,
-    void (*error)(struct _th_ioctx_t *, const int, const char *msg),
-    void (*msg)(struct _th_ioctx_t *, const char *msg));
+//
+// I/O context management functions
+//
+th_ioctx *   th_io_new(const th_ioctx_ops *fops);
+int          th_io_open(th_ioctx *ctx, const char *filename, const char *mode);
+void         th_io_close(th_ioctx *ctx);
+void         th_io_free(th_ioctx *ctx);
 
-BOOL th_ioctx_open(th_ioctx_t *ctx, const char *filename, const char *mode,
-    void (*error)(struct _th_ioctx_t *, const int, const char *msg),
-    void (*msg)(struct _th_ioctx_t *, const char *msg));
+BOOL         th_io_set_handlers(th_ioctx *ctx,
+             void (*error)(th_ioctx *, const int, const char *msg),
+             void (*msg)(th_ioctx *, const int, const char *msg));
+
+void         th_io_error_v(th_ioctx *ctx, const int err, const char *fmt, va_list ap);
+void         th_io_msg_v(th_ioctx *ctx, const int level, const char *fmt, va_list ap);
+void         th_io_error(th_ioctx *ctx, const int err, const char *fmt, ...);
+void         th_io_msg(th_ioctx *ctx, const int level, const char *fmt, ...);
+
 
-void th_ioctx_close(th_ioctx_t *ctx);
-void th_ioctx_error(th_ioctx_t *ctx, const int err, const char *fmt, ...);
+//
+// Basic I/O operations
+//
+int          thfreset(th_ioctx *ctx);
+int          thferror(th_ioctx *ctx);
+int          thfseek(th_ioctx *ctx, const off_t, const int whence);
+off_t        thfsize(th_ioctx *ctx);
+off_t        thftell(th_ioctx *ctx);
+BOOL         thfeof(th_ioctx *ctx);
+int          thfgetc(th_ioctx *ctx);
+int          thfputc(int ch, th_ioctx *ctx);
+size_t       thfread(void *ptr, const size_t, const size_t, th_ioctx *ctx);
+size_t       thfwrite(const void *, const size_t, const size_t, th_ioctx *ctx);
+char *       thfgets(char *ptr, int size, th_ioctx *ctx);
+
+int          thvfprintf(th_ioctx *ctx, const char *fmt, va_list ap);
+int          thfprintf(th_ioctx *ctx, const char *fmt, ...);
 
-th_ioctx_t * th_ioctx_new(const char *filename,
-    void (*error)(struct _th_ioctx_t *, const int, const char *msg),
-    void (*msg)(struct _th_ioctx_t *, const char *msg));
-void th_ioctx_free(th_ioctx_t *ctx);
+int          thfread_str(th_ioctx *ctx, void *ptr, const size_t len);
+BOOL         thfread_byte(th_ioctx *ctx, uint8_t *);
+int          thfwrite_str(th_ioctx *ctx, const void *ptr, const size_t len);
+BOOL         thfwrite_byte(th_ioctx *ctx, const uint8_t);
+
+
+//
+// Endian-handling file read/write routines
+//
+#define TH_DEFINE_HEADER(xname, xtype)                       \
+BOOL    thfread_ ## xname (th_ioctx *ctx, xtype *v);        \
+BOOL    thfwrite_ ## xname (th_ioctx *ctx, const xtype v);
+
+TH_DEFINE_HEADER(le16, uint16_t)
+TH_DEFINE_HEADER(le32, uint32_t)
+
+TH_DEFINE_HEADER(be16, uint16_t)
+TH_DEFINE_HEADER(be32, uint32_t)
+
+TH_DEFINE_HEADER(be64, uint64_t)
+TH_DEFINE_HEADER(le64, uint64_t)
+
+#undef TH_DEFINE_HEADER
 
 
 #ifdef __cplusplus