From 7d9770fc12d1570ecd19255240501c696722f5e1 Mon Sep 17 00:00:00 2001 From: Jordan Bancino Date: Thu, 16 Mar 2023 12:28:55 +0000 Subject: [PATCH] Add some convenience functions for working with Io and Stream. Also broke out IoFd into it's own file, and did the same with IoFile. --- src/Io.c | 59 +----------------------------------- src/Io/IoFd.c | 72 ++++++++++++++++++++++++++++++++++++++++++++ src/Io/IoFile.c | 54 +++++++++++++++++++++++++++++++++ src/Stream.c | 67 ++++++++++++++++++++++++++++++----------- src/include/Io.h | 19 +++++++++--- src/include/Stream.h | 8 ++++- 6 files changed, 198 insertions(+), 81 deletions(-) create mode 100644 src/Io/IoFd.c create mode 100644 src/Io/IoFile.c diff --git a/src/Io.c b/src/Io.c index 7f1038a..37c3e6a 100644 --- a/src/Io.c +++ b/src/Io.c @@ -4,10 +4,7 @@ #include #include - -#ifndef IO_BUFFER -#define IO_BUFFER 4096 -#endif +#include struct Io { @@ -110,60 +107,6 @@ IoClose(Io *io) return ret; } -static ssize_t -IoReadFd(void *cookie, void *buf, size_t nBytes) -{ - int fd = *((int *) cookie); - - return read(fd, buf, nBytes); -} - -static ssize_t -IoWriteFd(void *cookie, void *buf, size_t nBytes) -{ - int fd = *((int *) cookie); - - return write(fd, buf, nBytes); -} - -static off_t -IoSeekFd(void *cookie, off_t offset, int whence) -{ - int fd = *((int *) cookie); - - return lseek(fd, offset, whence); -} - -static int -IoCloseFd(void *cookie) -{ - int fd = *((int *) cookie); - - Free(cookie); - return close(fd); -} - -Io * -IoOpen(int fd) -{ - int *cookie = Malloc(sizeof(int)); - IoFunctions f; - - if (!cookie) - { - return NULL; - } - - *cookie = fd; - - f.read = IoReadFd; - f.write = IoWriteFd; - f.seek = IoSeekFd; - f.close = IoCloseFd; - - return IoCreate(cookie, f); -} - int IoVprintf(Io *io, const char *fmt, va_list ap) { diff --git a/src/Io/IoFd.c b/src/Io/IoFd.c new file mode 100644 index 0000000..e381587 --- /dev/null +++ b/src/Io/IoFd.c @@ -0,0 +1,72 @@ +#include + +#include + +#include + +static ssize_t +IoReadFd(void *cookie, void *buf, size_t nBytes) +{ + int fd = *((int *) cookie); + + return read(fd, buf, nBytes); +} + +static ssize_t +IoWriteFd(void *cookie, void *buf, size_t nBytes) +{ + int fd = *((int *) cookie); + + return write(fd, buf, nBytes); +} + +static off_t +IoSeekFd(void *cookie, off_t offset, int whence) +{ + int fd = *((int *) cookie); + + return lseek(fd, offset, whence); +} + +static int +IoCloseFd(void *cookie) +{ + int fd = *((int *) cookie); + + Free(cookie); + return close(fd); +} + +Io * +IoFd(int fd) +{ + int *cookie = Malloc(sizeof(int)); + IoFunctions f; + + if (!cookie) + { + return NULL; + } + + *cookie = fd; + + f.read = IoReadFd; + f.write = IoWriteFd; + f.seek = IoSeekFd; + f.close = IoCloseFd; + + return IoCreate(cookie, f); +} + +Io * +IoOpen(const char *path, int flags, mode_t mode) +{ + int fd = open(path, flags, mode); + + if (fd == -1) + { + return NULL; + } + + return IoFd(fd); +} diff --git a/src/Io/IoFile.c b/src/Io/IoFile.c new file mode 100644 index 0000000..63d7dde --- /dev/null +++ b/src/Io/IoFile.c @@ -0,0 +1,54 @@ +#include + +#include + +static ssize_t +IoReadFile(void *cookie, void *buf, size_t nBytes) +{ + FILE *fp = cookie; + + return fread(buf, 1, nBytes, fp); +} + +static ssize_t +IoWriteFile(void *cookie, void *buf, size_t nBytes) +{ + FILE *fp = cookie; + + return fwrite(buf, 1, nBytes, fp); +} + +static off_t +IoSeekFile(void *cookie, off_t offset, int whence) +{ + FILE *fp = cookie; + + return fseeko(fp, offset, whence); +} + +static int +IoCloseFile(void *cookie) +{ + FILE *fp = cookie; + + return fclose(fp); +} + +Io * +IoFile(FILE *fp) +{ + IoFunctions f; + + if (!fp) + { + return NULL; + } + + f.read = IoReadFile; + f.write = IoWriteFile; + f.seek = IoSeekFile; + f.close = IoCloseFile; + + return IoCreate(fp, f); +} + diff --git a/src/Stream.c b/src/Stream.c index 19801b9..6a405df 100644 --- a/src/Stream.c +++ b/src/Stream.c @@ -1,8 +1,13 @@ #include -#ifndef STREAM_BUFFER -#define STREAM_BUFFER 4096 -#endif +#include +#include +#include + +#include +#include +#include +#include #ifndef STREAM_RETRIES #define STREAM_RETRIES 10 @@ -15,14 +20,6 @@ #define STREAM_EOF (1 << 0) #define STREAM_ERR (1 << 1) -#include -#include -#include - -#include -#include -#include - struct Stream { Io *io; @@ -42,7 +39,7 @@ struct Stream }; Stream * -StreamOpen(Io * io) +StreamIo(Io * io) { Stream *stream; @@ -63,6 +60,40 @@ StreamOpen(Io * io) return stream; } +Stream * +StreamFd(int fd) +{ + Io *io = IoFd(fd); + + if (!io) + { + return NULL; + } + + return StreamIo(io); +} + +Stream * +StreamOpen(const char *path, const char *mode) +{ + FILE *fp = fopen(path, mode); + Io *io; + + if (!fp) + { + return NULL; + } + + io = IoFile(fp); + + if (!io) + { + return NULL; + } + + return StreamIo(io); +} + int StreamClose(Stream * stream) { @@ -159,7 +190,7 @@ StreamGetc(Stream * stream) if (!stream->rBuf) { /* No buffer allocated yet */ - stream->rBuf = Malloc(STREAM_BUFFER * sizeof(int)); + stream->rBuf = Malloc(IO_BUFFER * sizeof(int)); if (!stream->rBuf) { stream->flags |= STREAM_ERR; @@ -173,7 +204,7 @@ StreamGetc(Stream * stream) if (stream->rOff >= stream->rLen) { /* We read through the entire buffer; get a new one */ - ssize_t readRes = IoRead(stream->io, stream->rBuf, STREAM_BUFFER); + ssize_t readRes = IoRead(stream->io, stream->rBuf, IO_BUFFER); if (readRes == 0) { @@ -209,7 +240,7 @@ StreamUngetc(Stream * stream, int c) if (!stream->ugBuf) { - stream->ugSize = STREAM_BUFFER; + stream->ugSize = IO_BUFFER; stream->ugBuf = Malloc(stream->ugSize); if (!stream->ugBuf) @@ -223,7 +254,7 @@ StreamUngetc(Stream * stream, int c) { int *new; - stream->ugSize += STREAM_BUFFER; + stream->ugSize += IO_BUFFER; new = Realloc(stream->ugBuf, stream->ugSize); if (!new) { @@ -254,7 +285,7 @@ StreamPutc(Stream * stream, int c) if (!stream->wBuf) { - stream->wBuf = Malloc(STREAM_BUFFER * sizeof(int)); + stream->wBuf = Malloc(IO_BUFFER * sizeof(int)); if (!stream->wBuf) { stream->flags |= STREAM_ERR; @@ -262,7 +293,7 @@ StreamPutc(Stream * stream, int c) } } - if (stream->wLen == STREAM_BUFFER) + if (stream->wLen == IO_BUFFER) { /* Buffer full; write it */ ssize_t writeRes = IoWrite(stream->io, stream->wBuf, stream->wLen); diff --git a/src/include/Io.h b/src/include/Io.h index ff86675..87c6d6a 100644 --- a/src/include/Io.h +++ b/src/include/Io.h @@ -1,8 +1,13 @@ #ifndef TELODENDRIA_IO_H #define TELODENDRIA_IO_H -#include +#include #include +#include + +#ifndef IO_BUFFER +#define IO_BUFFER 4096 +#endif typedef struct Io Io; @@ -34,9 +39,6 @@ IoSeek(Io *, off_t, int); extern int IoClose(Io *); -extern Io * -IoOpen(int); - extern int IoVprintf(Io *, const char *, va_list); @@ -46,4 +48,13 @@ IoPrintf(Io *, const char *, ...); extern ssize_t IoCopy(Io *, Io *); +extern Io * +IoFd(int); + +extern Io * +IoOpen(const char *, int, mode_t); + +extern Io * +IoFile(FILE *); + #endif /* TELODENDRIA_IO_H */ diff --git a/src/include/Stream.h b/src/include/Stream.h index ef94f52..0c37d23 100644 --- a/src/include/Stream.h +++ b/src/include/Stream.h @@ -8,7 +8,13 @@ typedef struct Stream Stream; extern Stream * -StreamOpen(Io *io); +StreamIo(Io *io); + +extern Stream * +StreamFd(int); + +extern Stream * +StreamOpen(const char *, const char *); extern int StreamClose(Stream *);