Parsee/src/Streams/Writer.c

71 lines
1.3 KiB
C

#include <StringStream.h>
#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Stream.h>
#include <Cytoplasm/Str.h>
#include <Cytoplasm/Io.h>
#include <string.h>
static ssize_t
ReadStreamWriter(void *coop, void *to, size_t n)
{
/* Reading from a stream writer is silly. */
(void) coop;
(void) to;
(void) n;
return 0;
}
static ssize_t
WriteStreamWriter(void *coop, void *from, size_t n)
{
char **cook = coop;
char *tmp = NULL;
char *str = Malloc(n + 1);
memcpy(str, from, n);
str[n] = '\0';
tmp = *cook;
*cook = StrConcat(2, *cook, str);
Free(tmp);
Free(str);
return 0;
}
static off_t
SeekStreamWriter(void *coop, off_t mag, int sgn)
{
/* TODO: Seeking would be useful, though not supported yet. */
(void) coop;
(void) mag;
(void) sgn;
return 0;
}
static int
CloseStreamWriter(void *coop)
{
/* Nothing to free as of now. */
(void) coop;
return 0;
}
static const IoFunctions Functions = {
.read = ReadStreamWriter,
.seek = SeekStreamWriter,
.write = WriteStreamWriter,
.close = CloseStreamWriter,
};
Stream *
StrStreamWriter(char **buffer)
{
Io *raw_io;
if (!buffer)
{
return NULL;
}
raw_io = IoCreate(buffer, Functions);
return StreamIo(raw_io);
}