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.
This commit is contained in:
Jordan Bancino 2023-03-16 12:28:55 +00:00
parent 65f4c90df3
commit 7d9770fc12
6 changed files with 198 additions and 81 deletions

View file

@ -4,10 +4,7 @@
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <fcntl.h>
#ifndef IO_BUFFER
#define IO_BUFFER 4096
#endif
struct Io struct Io
{ {
@ -110,60 +107,6 @@ IoClose(Io *io)
return ret; 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 int
IoVprintf(Io *io, const char *fmt, va_list ap) IoVprintf(Io *io, const char *fmt, va_list ap)
{ {

72
src/Io/IoFd.c Normal file
View file

@ -0,0 +1,72 @@
#include <Io.h>
#include <Memory.h>
#include <fcntl.h>
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);
}

54
src/Io/IoFile.c Normal file
View file

@ -0,0 +1,54 @@
#include <Io.h>
#include <stdio.h>
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);
}

View file

@ -1,8 +1,13 @@
#include <Stream.h> #include <Stream.h>
#ifndef STREAM_BUFFER #include <Io.h>
#define STREAM_BUFFER 4096 #include <Memory.h>
#endif #include <Util.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#ifndef STREAM_RETRIES #ifndef STREAM_RETRIES
#define STREAM_RETRIES 10 #define STREAM_RETRIES 10
@ -15,14 +20,6 @@
#define STREAM_EOF (1 << 0) #define STREAM_EOF (1 << 0)
#define STREAM_ERR (1 << 1) #define STREAM_ERR (1 << 1)
#include <Io.h>
#include <Memory.h>
#include <Util.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
struct Stream struct Stream
{ {
Io *io; Io *io;
@ -42,7 +39,7 @@ struct Stream
}; };
Stream * Stream *
StreamOpen(Io * io) StreamIo(Io * io)
{ {
Stream *stream; Stream *stream;
@ -63,6 +60,40 @@ StreamOpen(Io * io)
return stream; 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 int
StreamClose(Stream * stream) StreamClose(Stream * stream)
{ {
@ -159,7 +190,7 @@ StreamGetc(Stream * stream)
if (!stream->rBuf) if (!stream->rBuf)
{ {
/* No buffer allocated yet */ /* No buffer allocated yet */
stream->rBuf = Malloc(STREAM_BUFFER * sizeof(int)); stream->rBuf = Malloc(IO_BUFFER * sizeof(int));
if (!stream->rBuf) if (!stream->rBuf)
{ {
stream->flags |= STREAM_ERR; stream->flags |= STREAM_ERR;
@ -173,7 +204,7 @@ StreamGetc(Stream * stream)
if (stream->rOff >= stream->rLen) if (stream->rOff >= stream->rLen)
{ {
/* We read through the entire buffer; get a new one */ /* 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) if (readRes == 0)
{ {
@ -209,7 +240,7 @@ StreamUngetc(Stream * stream, int c)
if (!stream->ugBuf) if (!stream->ugBuf)
{ {
stream->ugSize = STREAM_BUFFER; stream->ugSize = IO_BUFFER;
stream->ugBuf = Malloc(stream->ugSize); stream->ugBuf = Malloc(stream->ugSize);
if (!stream->ugBuf) if (!stream->ugBuf)
@ -223,7 +254,7 @@ StreamUngetc(Stream * stream, int c)
{ {
int *new; int *new;
stream->ugSize += STREAM_BUFFER; stream->ugSize += IO_BUFFER;
new = Realloc(stream->ugBuf, stream->ugSize); new = Realloc(stream->ugBuf, stream->ugSize);
if (!new) if (!new)
{ {
@ -254,7 +285,7 @@ StreamPutc(Stream * stream, int c)
if (!stream->wBuf) if (!stream->wBuf)
{ {
stream->wBuf = Malloc(STREAM_BUFFER * sizeof(int)); stream->wBuf = Malloc(IO_BUFFER * sizeof(int));
if (!stream->wBuf) if (!stream->wBuf)
{ {
stream->flags |= STREAM_ERR; 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 */ /* Buffer full; write it */
ssize_t writeRes = IoWrite(stream->io, stream->wBuf, stream->wLen); ssize_t writeRes = IoWrite(stream->io, stream->wBuf, stream->wLen);

View file

@ -1,8 +1,13 @@
#ifndef TELODENDRIA_IO_H #ifndef TELODENDRIA_IO_H
#define TELODENDRIA_IO_H #define TELODENDRIA_IO_H
#include <unistd.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include <unistd.h>
#ifndef IO_BUFFER
#define IO_BUFFER 4096
#endif
typedef struct Io Io; typedef struct Io Io;
@ -34,9 +39,6 @@ IoSeek(Io *, off_t, int);
extern int extern int
IoClose(Io *); IoClose(Io *);
extern Io *
IoOpen(int);
extern int extern int
IoVprintf(Io *, const char *, va_list); IoVprintf(Io *, const char *, va_list);
@ -46,4 +48,13 @@ IoPrintf(Io *, const char *, ...);
extern ssize_t extern ssize_t
IoCopy(Io *, Io *); IoCopy(Io *, Io *);
extern Io *
IoFd(int);
extern Io *
IoOpen(const char *, int, mode_t);
extern Io *
IoFile(FILE *);
#endif /* TELODENDRIA_IO_H */ #endif /* TELODENDRIA_IO_H */

View file

@ -8,7 +8,13 @@
typedef struct Stream Stream; typedef struct Stream Stream;
extern Stream * extern Stream *
StreamOpen(Io *io); StreamIo(Io *io);
extern Stream *
StreamFd(int);
extern Stream *
StreamOpen(const char *, const char *);
extern int extern int
StreamClose(Stream *); StreamClose(Stream *);