lib: Introduced a portable abstraction layer for custom stdio streams.
[ashd.git] / lib / utils.c
index 459afed..ed22b42 100644 (file)
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 #include <ctype.h>
+#include <errno.h>
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
 #include <utils.h>
 
 static char *base64set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
@@ -474,3 +475,127 @@ void *btreeget(struct btree *tree, void *key, int (*cmp)(void *, void *))
            return(tree->d);
     }
 }
+
+struct stdif {
+    ssize_t (*read)(void *pdata, void *buf, size_t len);
+    ssize_t (*write)(void *pdata, const void *buf, size_t len);
+    off_t (*seek)(void *pdata, off_t offset, int whence);
+    int (*close)(void *pdata);
+    void *pdata;
+};
+
+#if defined(HAVE_GLIBC_STDIO)
+static ssize_t wrapread(void *pdata, char *buf, size_t len)
+{
+    struct stdif *nf = pdata;
+    
+    return(nf->read(nf->pdata, buf, len));
+}
+
+static ssize_t wrapwrite(void *pdata, const char *buf, size_t len)
+{
+    struct stdif *nf = pdata;
+    
+    return(nf->write(nf->pdata, buf, len));
+}
+
+static int wrapseek(void *pdata, off_t *pos, int whence)
+{
+    struct stdif *nf = pdata;
+    off_t ret;
+    
+    ret = nf->seek(nf->pdata, *pos, whence);
+    if(ret < 0)
+       return(-1);
+    *pos = ret;
+    return(0);
+}
+
+static int wrapclose(void *pdata)
+{
+    struct stdif *nf = pdata;
+    int ret;
+    
+    if(nf->close != NULL)
+       ret = nf->close(nf->pdata);
+    free(nf);
+    return(ret);
+}
+
+FILE *funstdio(void *pdata,
+              ssize_t (*read)(void *pdata, void *buf, size_t len),
+              ssize_t (*write)(void *pdata, const void *buf, size_t len),
+              off_t (*seek)(void *pdata, off_t offset, int whence),
+              int (*close)(void *pdata))
+{
+    struct stdif *nf;
+    cookie_io_functions_t io;
+    char *mode;
+    
+    if(read && write) {
+       mode = "r+";
+    } else if(read) {
+       mode = "r";
+    } else if(write) {
+       mode = "w";
+    } else {
+       errno = EINVAL;
+       return(NULL);
+    }
+    omalloc(nf);
+    *nf = (struct stdif){.read = read, .write = write, .seek = seek, .close = close, .pdata = pdata};
+    io = (cookie_io_functions_t) {
+       .read = read?wrapread:NULL,
+       .write = write?wrapwrite:NULL,
+       .seek = seek?wrapseek:NULL,
+       .close = wrapclose,
+    };
+    return(fopencookie(nf, mode, io));
+}
+#elif defined(HAVE_BSD_STDIO)
+static int wrapread(void *pdata, char *buf, int len)
+{
+    struct stdif *nf = pdata;
+    
+    return(nf->read(nf->pdata, buf, len));
+}
+
+static int wrapwrite(void *pdata, const char *buf, int len)
+{
+    struct stdif *nf = pdata;
+    
+    return(nf->write(nf->pdata, buf, len));
+}
+
+static fpos_t wrapseek(void *pdata, fpos_t pos, int whence)
+{
+    struct stdif *nf = pdata;
+    
+    return(nf->seek(nf->pdata, pos, whence));
+}
+
+static int wrapclose(void *pdata)
+{
+    struct stdif *nf = pdata;
+    int ret;
+    
+    if(nf->close != NULL)
+       ret = nf->close(nf->pdata);
+    free(nf);
+    return(ret);
+}
+
+FILE *funstdio(void *pdata,
+              ssize_t (*read)(void *pdata, void *buf, size_t len),
+              ssize_t (*write)(void *pdata, const void *buf, size_t len),
+              off_t (*seek)(void *pdata, off_t offset, int whence),
+              int (*close)(void *pdata))
+{
+    struct stdif *nf;
+    
+    omalloc(nf);
+    return(funopen(pdata, read?wrapread:NULL, write:wrapwrite:NULL, seek:wrapseek:NULL, wrapclose));
+}
+#else
+#error "No stdio implementation for this system"
+#endif