aboutsummaryrefslogtreecommitdiff
path: root/sys/libn/io.c
diff options
context:
space:
mode:
Diffstat (limited to 'sys/libn/io.c')
-rw-r--r--sys/libn/io.c91
1 files changed, 74 insertions, 17 deletions
diff --git a/sys/libn/io.c b/sys/libn/io.c
index 8aa4aa0..f0d270e 100644
--- a/sys/libn/io.c
+++ b/sys/libn/io.c
@@ -2,55 +2,112 @@
#include <libn.h>
// -----------------------------------------------------------------------
-// Open/Close
+// interface implementations
-Stream*
+/* casting functions */
+static
+int
+·read(void *rdr, int size, int n, void *buf)
+{
+ return io·read((io·Stream *)rdr, size, n, buf);
+}
+
+static
+byte
+·get(void *rdr)
+{
+ return io·getbyte((io·Stream *)rdr);
+}
+
+static
+error
+·unget(void *rdr, byte c)
+{
+ return io·ungetbyte((io·Stream *)rdr, c);
+}
+
+static
+int
+·write(void *wtr, int sz, int n, void *buf)
+{
+ return io·write((io·Stream *)wtr, sz, n, buf);
+}
+
+static
+error
+·put(void *wtr, byte c)
+{
+ return io·putbyte((io·Stream *)wtr, c);
+}
+
+static
+int
+·puts(void *wtr, string s)
+{
+ return io·putstring((io·Stream *)wtr, s);
+}
+
+/* actual interfaces */
+io·Reader sys·Reader = (io·Reader){ ·read };
+io·Peeker sys·Peeker = (io·Peeker){ ·get, ·unget };
+io·FullReader sys·FullReader = (io·FullReader){ ·read, ·get, ·unget };
+
+io·Writer sys·Writer = (io·Writer){ ·write };
+io·Putter sys·Putter = (io·Putter){ ·put, ·puts };
+io·FullWriter sys·FullWriter = (io·FullWriter){ ·write, ·put, ·puts };
+
+io·ReadWriter sys·ReadWriter = (io·ReadWriter){ ·read, ·write };
+
+// -----------------------------------------------------------------------
+// open/close
+
+io·Stream*
io·open(byte *name, byte *mode)
{
return fopen(name, mode);
}
int
-io·fd(Stream *s)
+io·fd(io·Stream *s)
{
return fileno(s);
}
error
-io·stat(Stream *s, io·Stat *buf)
+io·stat(io·Stream *s, io·Stat *buf)
{
return fstat(fileno(s), buf);
}
error
-io·close(Stream *s)
+io·close(io·Stream *s)
{
return fclose(s);
}
// -----------------------------------------------------------------------
-// Reading
+// reading
byte
-io·getbyte(Stream *s)
+io·getbyte(io·Stream *s)
{
return fgetc(s);
}
error
-io·ungetbyte(Stream *s, byte c)
+io·ungetbyte(io·Stream *s, byte c)
{
return ungetc(c, s);
}
int
-io·read(Stream *s, int sz, int n, void *buf)
+io·read(io·Stream *s, int sz, int n, void *buf)
{
return fread(buf, sz, n, s);
}
int
-io·readln(Stream *s, int n, byte* buf)
+io·readln(io·Stream *s, int n, byte* buf)
{
byte* b;
b = fgets(buf, n+1, s);
@@ -61,37 +118,37 @@ io·readln(Stream *s, int n, byte* buf)
}
// -----------------------------------------------------------------------
-// Writing
+// writing
error
-io·putbyte(Stream *s, byte c)
+io·putbyte(io·Stream *s, byte c)
{
return fputc(c, s);
}
int
-io·putstring(Stream *s, string str)
+io·putstring(io·Stream *s, string str)
{
return fputs(str, s);
}
int
-io·write(Stream *s, int sz, int n, void *buf)
+io·write(io·Stream *s, int sz, int n, void *buf)
{
return fwrite(buf, sz, n, s);
}
int
-io·flush(Stream *s)
+io·flush(io·Stream *s)
{
return fflush(s);
}
// -----------------------------------------------------------------------
-// Seek
+// seek
int
-io·seek(Stream *s, long off, enum SeekPos origin)
+io·seek(io·Stream *s, long off, enum SeekPos origin)
{
return fseek(s, off, origin);
}