remove the read and write modules from libgu. this simplifies the i/o layer

This commit is contained in:
kr.angelov
2013-09-05 11:20:39 +00:00
parent d0f527d0dd
commit 83ca6c4f9f
32 changed files with 418 additions and 670 deletions

View File

@@ -255,66 +255,23 @@ gu_in_fini(GuFinalizer* fin)
gu_pool_free(pool);
}
GuIn
gu_init_in(GuInStream* stream)
{
return (GuIn) {
.buf_end = NULL,
.buf_curr = 0,
.buf_size = 0,
.stream = stream,
.fini.fn = gu_in_fini
};
}
GuIn*
gu_new_in(GuInStream* stream, GuPool* pool)
{
gu_require(stream != NULL);
GuIn* in = gu_new(GuIn, pool);
*in = gu_init_in(stream);
in->buf_end = NULL;
in->buf_curr = 0;
in->buf_size = 0;
in->stream = stream;
in->fini.fn = gu_in_fini;
return in;
}
typedef struct GuProxyInStream GuProxyInStream;
struct GuProxyInStream {
GuInStream stream;
GuIn* real_in;
};
static const uint8_t*
gu_proxy_in_begin_buffer(GuInStream* self, size_t* sz_out, GuExn* err)
{
GuProxyInStream* pis = gu_container(self, GuProxyInStream, stream);
return gu_in_begin_span(pis->real_in, sz_out, err);
}
static void
gu_proxy_in_end_buffer(GuInStream* self, size_t sz, GuExn* err)
{
GuProxyInStream* pis = gu_container(self, GuProxyInStream, stream);
gu_in_end_span(pis->real_in, sz);
}
static size_t
gu_proxy_in_input(GuInStream* self, uint8_t* dst, size_t sz, GuExn* err)
{
GuProxyInStream* pis = gu_container(self, GuProxyInStream, stream);
return gu_in_some(pis->real_in, dst, sz, err);
}
GuInStream*
gu_in_proxy_stream(GuIn* in, GuPool* pool)
{
GuProxyInStream* ins = gu_new(GuProxyInStream, pool);
ins->stream.begin_buffer = gu_proxy_in_begin_buffer;
ins->stream.end_buffer = gu_proxy_in_end_buffer;
ins->stream.input = gu_proxy_in_input;
ins->real_in = in;
return &ins->stream;
}
enum {
GU_BUFFERED_IN_BUF_SIZE = 4096
};

View File

@@ -25,16 +25,9 @@ struct GuIn {
GuFinalizer fini;
};
GuIn
gu_init_in(GuInStream* stream);
GuIn*
gu_new_in(GuInStream* stream, GuPool* pool);
GuInStream*
gu_in_proxy_stream(GuIn* in, GuPool* pool);
const uint8_t*
gu_in_begin_span(GuIn* in, size_t *sz_out, GuExn* err);

View File

@@ -1,20 +1,6 @@
#include <gu/seq.h>
#include <gu/out.h>
GuOut
gu_init_out(GuOutStream* stream)
{
gu_require(stream != NULL);
GuOut out = {
.buf_end = NULL,
.buf_curr = 0,
.stream = stream,
.fini.fn = NULL
};
return out;
}
#include <gu/utf8.h>
static bool
gu_out_is_buffering(GuOut* out)
@@ -81,8 +67,12 @@ gu_out_fini(GuFinalizer* self)
GuOut*
gu_new_out(GuOutStream* stream, GuPool* pool)
{
gu_require(stream != NULL);
GuOut* out = gu_new(GuOut, pool);
*out = gu_init_out(stream);
out->buf_end = NULL,
out->buf_curr = 0,
out->stream = stream,
out->fini.fn = gu_out_fini;
gu_pool_finally(pool, &out->fini);
return out;
@@ -185,72 +175,11 @@ gu_out_try_u8_(GuOut* restrict out, uint8_t u);
typedef struct GuProxyOutStream GuProxyOutStream;
struct GuProxyOutStream {
GuOutStream stream;
GuOut* real_out;
};
static uint8_t*
gu_proxy_out_buf_begin(GuOutStream* self, size_t req, size_t* sz_out,
GuExn* err)
{
GuProxyOutStream* pos =
gu_container(self, GuProxyOutStream, stream);
return gu_out_begin_span(pos->real_out, req, sz_out, err);
}
static void
gu_proxy_out_buf_end(GuOutStream* self, size_t sz, GuExn* err)
{
GuProxyOutStream* pos =
gu_container(self, GuProxyOutStream, stream);
gu_out_end_span(pos->real_out, sz);
}
static size_t
gu_proxy_out_output(GuOutStream* self, const uint8_t* src, size_t sz,
GuExn* err)
{
GuProxyOutStream* pos =
gu_container(self, GuProxyOutStream, stream);
return gu_out_bytes(pos->real_out, src, sz, err);
}
static void
gu_proxy_out_flush(GuOutStream* self, GuExn* err)
{
GuProxyOutStream* pos =
gu_container(self, GuProxyOutStream, stream);
gu_out_flush(pos->real_out, err);
}
GuOutStream*
gu_out_proxy_stream(GuOut* out, GuPool* pool)
{
GuProxyOutStream* pos = gu_new(GuProxyOutStream, pool);
pos->stream.begin_buf = gu_proxy_out_buf_begin;
pos->stream.end_buf = gu_proxy_out_buf_end;
pos->stream.output = gu_proxy_out_output;
pos->stream.flush = gu_proxy_out_flush;
pos->real_out = out;
return &pos->stream;
}
typedef struct GuBufferedOutStream GuBufferedOutStream;
struct GuBufferedOutStream {
GuProxyOutStream pstream;
GuOutStream stream;
GuOut* real_out;
size_t sz;
uint8_t buf[];
};
@@ -261,7 +190,7 @@ gu_buffered_out_buf_begin(GuOutStream* self, size_t req, size_t* sz_out,
{
(void) (req && err);
GuBufferedOutStream* b =
gu_container(self, GuBufferedOutStream, pstream.stream);
gu_container(self, GuBufferedOutStream, stream);
*sz_out = b->sz;
return b->buf;
}
@@ -270,9 +199,26 @@ static void
gu_buffered_out_buf_end(GuOutStream* self, size_t sz, GuExn* err)
{
GuBufferedOutStream* b =
gu_container(self, GuBufferedOutStream, pstream.stream);
gu_container(self, GuBufferedOutStream, stream);
gu_require(sz <= b->sz);
gu_out_bytes(b->pstream.real_out, b->buf, sz, err);
gu_out_bytes(b->real_out, b->buf, sz, err);
}
static size_t
gu_buffered_out_output(GuOutStream* self, const uint8_t* src, size_t sz,
GuExn* err)
{
GuBufferedOutStream* bos =
gu_container(self, GuBufferedOutStream, stream);
return gu_out_bytes(bos->real_out, src, sz, err);
}
static void
gu_buffered_out_flush(GuOutStream* self, GuExn* err)
{
GuBufferedOutStream* bos =
gu_container(self, GuBufferedOutStream, stream);
gu_out_flush(bos->real_out, err);
}
GuOut*
@@ -280,15 +226,15 @@ gu_new_buffered_out(GuOut* out, size_t sz, GuPool* pool)
{
GuBufferedOutStream* b =
gu_new_flex(pool, GuBufferedOutStream, buf, sz);
b->pstream.stream = (GuOutStream) {
b->stream = (GuOutStream) {
.begin_buf = gu_buffered_out_buf_begin,
.end_buf = gu_buffered_out_buf_end,
.output = gu_proxy_out_output,
.flush = gu_proxy_out_flush
.output = gu_buffered_out_output,
.flush = gu_buffered_out_flush
};
b->pstream.real_out = out;
b->real_out = out;
b->sz = sz;
return gu_new_out(&b->pstream.stream, pool);
return gu_new_out(&b->stream, pool);
}
GuOut*
@@ -301,3 +247,29 @@ gu_out_buffered(GuOut* out, GuPool* pool)
}
extern inline void
gu_putc(char c, GuOut* out, GuExn* err);
void
gu_puts(const char* str, GuOut* out, GuExn* err)
{
gu_str_out_utf8(str, out, err);
}
void
gu_vprintf(const char* fmt, va_list args, GuOut* out, GuExn* err)
{
GuPool* tmp_pool = gu_local_pool();
char* str = gu_vasprintf(fmt, args, tmp_pool);
gu_str_out_utf8(str, out, err);
gu_pool_free(tmp_pool);
}
void
gu_printf(GuOut* out, GuExn* err, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
gu_vprintf(fmt, args, out, err);
va_end(args);
}

View File

@@ -4,6 +4,7 @@
#include <gu/defs.h>
#include <gu/assert.h>
#include <gu/exn.h>
#include <gu/ucs.h>
typedef struct GuOut GuOut;
@@ -27,10 +28,6 @@ struct GuOut {
GuFinalizer fini;
};
GuOut
gu_init_out(GuOutStream* stream);
GuOut*
gu_new_out(GuOutStream* stream, GuPool* pool);
@@ -40,9 +37,6 @@ gu_out_is_buffered(GuOut* out)
return !!out->stream->begin_buf;
}
GuOutStream*
gu_out_proxy_stream(GuOut* out, GuPool* pool);
GuOut*
gu_new_buffered_out(GuOut* out, size_t buf_sz, GuPool* pool);
@@ -163,4 +157,20 @@ gu_out_f64le(GuOut* out, double d, GuExn* err);
void
gu_out_f64be(GuOut* out, double d, GuExn* err);
inline void
gu_putc(char c, GuOut* out, GuExn* err)
{
GuUCS ucs = gu_char_ucs(c);
gu_out_u8(out, (uint8_t) ucs, err);
}
void
gu_puts(const char* str, GuOut* out, GuExn* err);
void
gu_vprintf(const char* fmt, va_list args, GuOut* out, GuExn* err);
void
gu_printf(GuOut* out, GuExn* err, const char* fmt, ...);
#endif // GU_OUT_H_

View File

@@ -1,15 +0,0 @@
#include <gu/read.h>
extern inline GuUCS
gu_read_ucs(GuReader* rdr, GuExn* err);
extern inline char
gu_getc(GuReader* rdr, GuExn* err);
GuReader*
gu_new_utf8_reader(GuIn* utf8_in, GuPool* pool)
{
GuReader* rdr = gu_new(GuReader, pool);
rdr->in_ = gu_init_in(gu_in_proxy_stream(utf8_in, pool));
return rdr;
}

View File

@@ -1,31 +0,0 @@
#ifndef GU_READ_H_
#define GU_READ_H_
#include <gu/in.h>
#include <gu/ucs.h>
#include <gu/utf8.h>
typedef struct GuReader GuReader;
struct GuReader {
GuIn in_;
};
inline GuUCS
gu_read_ucs(GuReader* rdr, GuExn* err)
{
return gu_in_utf8(&rdr->in_, err);
}
inline char
gu_getc(GuReader* rdr, GuExn* err)
{
return gu_in_utf8_char(&rdr->in_, err);
}
GuReader*
gu_new_utf8_reader(GuIn* utf8_in, GuPool* pool);
/**< @todo Implement. */
#endif // GU_READ_H_

View File

@@ -10,7 +10,7 @@ const GuString gu_empty_string = { 1 };
struct GuStringBuf {
GuByteBuf* bbuf;
GuWriter* wtr;
GuOut* out;
};
GuStringBuf*
@@ -18,17 +18,16 @@ gu_string_buf(GuPool* pool)
{
GuBuf* buf = gu_new_buf(uint8_t, pool);
GuOut* out = gu_buf_out(buf, pool);
GuWriter* wtr = gu_new_utf8_writer(out, pool);
GuStringBuf* sbuf = gu_new(GuStringBuf, pool);
sbuf->bbuf = buf;
sbuf->wtr = wtr;
sbuf->out = out;
return sbuf;
}
GuWriter*
gu_string_buf_writer(GuStringBuf* sb)
GuOut*
gu_string_buf_out(GuStringBuf* sb)
{
return sb->wtr;
return sb->out;
}
static GuString
@@ -61,14 +60,14 @@ gu_utf8_string(const uint8_t* buf, size_t sz, GuPool* pool)
GuString
gu_string_buf_freeze(GuStringBuf* sb, GuPool* pool)
{
gu_writer_flush(sb->wtr, NULL);
gu_out_flush(sb->out, NULL);
uint8_t* data = gu_buf_data(sb->bbuf);
size_t len = gu_buf_length(sb->bbuf);
return gu_utf8_string(data, len, pool);
}
GuReader*
gu_string_reader(GuString s, GuPool* pool)
GuIn*
gu_string_in(GuString s, GuPool* pool)
{
GuWord w = s.w_;
uint8_t* buf = NULL;
@@ -85,9 +84,7 @@ gu_string_reader(GuString s, GuPool* pool)
len = (p[0] == 0) ? ((size_t*) p)[-1] : p[0];
buf = &p[1];
}
GuIn* in = gu_data_in(buf, len, pool);
GuReader* rdr = gu_new_utf8_reader(in, pool);
return rdr;
return gu_data_in(buf, len, pool);
}
static bool
@@ -145,7 +142,7 @@ gu_string_copy(GuString string, GuPool* pool)
void
gu_string_write(GuString s, GuWriter* wtr, GuExn* err)
gu_string_write(GuString s, GuOut* out, GuExn* err)
{
GuWord w = s.w_;
uint8_t buf[sizeof(GuWord)];
@@ -165,7 +162,7 @@ gu_string_write(GuString s, GuWriter* wtr, GuExn* err)
sz = (p[0] == 0) ? ((size_t*) p)[-1] : p[0];
src = &p[1];
}
gu_utf8_write(src, sz, wtr, err);
gu_out_bytes(out, src, sz, err);
}
GuString
@@ -173,9 +170,9 @@ gu_format_string_v(const char* fmt, va_list args, GuPool* pool)
{
GuPool* tmp_pool = gu_local_pool();
GuStringBuf* sb = gu_string_buf(tmp_pool);
GuWriter* wtr = gu_string_buf_writer(sb);
gu_vprintf(fmt, args, wtr, NULL);
gu_writer_flush(wtr, NULL);
GuOut* out = gu_string_buf_out(sb);
gu_vprintf(fmt, args, out, NULL);
gu_out_flush(out, NULL);
GuString s = gu_string_buf_freeze(sb, pool);
gu_pool_free(tmp_pool);
return s;

View File

@@ -21,8 +21,8 @@
#define GU_STRING_H_
#include <gu/bits.h>
#include <gu/read.h>
#include <gu/write.h>
#include <gu/in.h>
#include <gu/out.h>
typedef GuOpaque() GuString;
@@ -32,10 +32,10 @@ GuString
gu_string_copy(GuString string, GuPool* pool);
void
gu_string_write(GuString string, GuWriter* wtr, GuExn* err);
gu_string_write(GuString string, GuOut* out, GuExn* err);
GuReader*
gu_string_reader(GuString string, GuPool* pool);
GuIn*
gu_string_in(GuString string, GuPool* pool);
bool
gu_string_is_stable(GuString string);
@@ -48,8 +48,8 @@ typedef struct GuStringBuf GuStringBuf;
GuStringBuf*
gu_string_buf(GuPool* pool);
GuWriter*
gu_string_buf_writer(GuStringBuf* sb);
GuOut*
gu_string_buf_out(GuStringBuf* sb);
GuString
gu_string_buf_freeze(GuStringBuf* sb, GuPool* pool);

View File

@@ -1,53 +0,0 @@
#include <gu/write.h>
size_t
gu_utf32_write(const GuUCS* src, size_t len, GuWriter* wtr, GuExn* err)
{
return gu_utf32_out_utf8(src, len, &wtr->out_, err);
}
void
gu_vprintf(const char* fmt, va_list args, GuWriter* wtr, GuExn* err)
{
GuPool* tmp_pool = gu_local_pool();
char* str = gu_vasprintf(fmt, args, tmp_pool);
gu_puts(str, wtr, err);
gu_pool_free(tmp_pool);
}
void
gu_printf(GuWriter* wtr, GuExn* err, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
gu_vprintf(fmt, args, wtr, err);
va_end(args);
}
GuWriter*
gu_new_utf8_writer(GuOut* utf8_out, GuPool* pool)
{
GuOutStream* stream = gu_out_proxy_stream(utf8_out, pool);
GuWriter* wtr = gu_new(GuWriter, pool);
wtr->out_ = gu_init_out(stream);
return wtr;
}
extern inline void
gu_ucs_write(GuUCS ucs, GuWriter* wtr, GuExn* err);
extern inline void
gu_writer_flush(GuWriter* wtr, GuExn* err);
extern inline void
gu_putc(char c, GuWriter* wtr, GuExn* err);
extern inline void
gu_puts(const char* str, GuWriter* wtr, GuExn* err);
extern inline size_t
gu_utf8_write(const uint8_t* src, size_t sz, GuWriter* wtr, GuExn* err);

View File

@@ -1,64 +0,0 @@
#ifndef GU_WRITE_H_
#define GU_WRITE_H_
#include <gu/exn.h>
#include <gu/ucs.h>
#include <gu/out.h>
#include <gu/utf8.h>
typedef struct GuWriter GuWriter;
struct GuWriter {
GuOut out_;
};
size_t
gu_utf32_write(const GuUCS* buf, size_t size, GuWriter* wtr, GuExn* err);
inline void
gu_writer_flush(GuWriter* wtr, GuExn* err)
{
gu_out_flush(&wtr->out_, err);
}
inline void
gu_ucs_write(GuUCS ucs, GuWriter* wtr, GuExn* err)
{
gu_out_utf8(ucs, &wtr->out_, err);
}
inline void
gu_putc(char c, GuWriter* wtr, GuExn* err)
{
GuUCS ucs = gu_char_ucs(c);
gu_out_u8(&wtr->out_, (uint8_t) ucs, err);
}
inline void
gu_puts(const char* str, GuWriter* wtr, GuExn* err)
{
gu_str_out_utf8(str, &wtr->out_, err);
}
inline size_t
gu_utf8_write(const uint8_t* src, size_t sz, GuWriter* wtr, GuExn* err)
{
return gu_out_bytes(&wtr->out_, src, sz, err);
}
void
gu_vprintf(const char* fmt, va_list args, GuWriter* wtr, GuExn* err);
void
gu_printf(GuWriter* wtr, GuExn* err, const char* fmt, ...);
//GuWriter
//gu_init_utf8_writer(GuOut* utf8_out);
GuWriter*
gu_new_utf8_writer(GuOut* utf8_out, GuPool* pool);
GuWriter*
gu_make_locale_writer(GuOut* locale_out, GuPool* pool);
#endif // GU_WRITE_H_