aboutsummaryrefslogtreecommitdiff
path: root/sys/libbio/io/newick.c
diff options
context:
space:
mode:
Diffstat (limited to 'sys/libbio/io/newick.c')
-rw-r--r--sys/libbio/io/newick.c47
1 files changed, 23 insertions, 24 deletions
diff --git a/sys/libbio/io/newick.c b/sys/libbio/io/newick.c
index 0004fac..82ea0d7 100644
--- a/sys/libbio/io/newick.c
+++ b/sys/libbio/io/newick.c
@@ -65,20 +65,20 @@ tokstr(struct Token tok)
// TODO: Bounds checking on buffer
static
struct Token
-lex(Stream *s)
+lex(io·Peeker s)
{
byte *c;
struct Token tok;
static byte b[1024];
c = b;
- *c = io·getbyte(s);
+ *c = s.get();
if (isspace(*c)) {
while (isspace(*c)) {
- *(++c) = io·getbyte(s);
+ *(++c) = s.get();
}
- io·ungetbyte(s, *c);
+ s.unget(*c);
Assert(c - b < 1024);
*c = 0;
@@ -101,11 +101,11 @@ lex(Stream *s)
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
while (isdigit(*c)) {
- NUM: *(++c) = io·getbyte(s);
+ NUM: *(++c) = s.get();
}
if (*c == '.') goto NUM;
- io·ungetbyte(s, *c);
+ s.unget(*c);
Assert(c - b < 1024);
*c = 0;
@@ -115,10 +115,10 @@ lex(Stream *s)
default:
while (isalnum(*c)) {
- *(++c) = io·getbyte(s);
+ *(++c) = s.get();
}
- io·ungetbyte(s, *c);
+ s.unget(*c);
Assert(c - b < 1024);
*c = '\0';
@@ -130,7 +130,7 @@ lex(Stream *s)
static
struct Token
-lex_nospace(Stream *s)
+lex_nospace(io·Peeker s)
{
struct Token tok;
tok = lex(s);
@@ -147,7 +147,7 @@ struct Parser
bio·Node *root;
struct Token tok;
- Stream *file;
+ io·Peeker file;
mem·Allocator heap;
};
@@ -280,7 +280,7 @@ parse(struct Parser *p)
break;
case tok·semi:
- io·ungetbyte(p->file, ';');
+ p->file.unget(';');
if (p->lev) {
errorf("format error: uneven number of parentheses found at ';'");
goto ERROR;
@@ -307,7 +307,7 @@ ERROR:
}
bio·Tree
-bio·readnewick(Stream *file, mem·Allocator heap)
+bio·readnewick(io·Peeker stream, mem·Allocator heap)
{
error err;
struct Parser p;
@@ -317,7 +317,7 @@ bio·readnewick(Stream *file, mem·Allocator heap)
.lev = 0,
.root = nil,
.tok = (struct Token){ 0 },
- .file = file,
+ .file = stream,
.heap = heap,
};
err = parse(&p);
@@ -334,7 +334,7 @@ bio·readnewick(Stream *file, mem·Allocator heap)
// Write
error
-dump(bio·Node *node, Stream *out)
+dump(bio·Node *node, io·Putter out)
{
byte b[24];
@@ -343,36 +343,35 @@ dump(bio·Node *node, Stream *out)
}
bio·Node *child;
if (node->nchild) {
- io·putbyte(out, '(');
+ out.put('(');
dump(node->child[0], out);
for (child = node->child[1]; child != nil; child = child->sibling) {
- io·putbyte(out, ',');
+ out.put(',');
dump(child, out);
}
- io·putbyte(out, ')');
+ out.put(')');
}
if (node->name) {
- io·putstring(out, node->name);
+ out.putstr(node->name);
}
if (node->parent) {
- io·putbyte(out, ':');
+ out.put(':');
snprintf(b, arrlen(b), "%f", node->dist);
- io·putstring(out, b);
+ out.putstr(b);
}
return 0;
}
error
-bio·writenewick(bio·Tree tree, Stream *out)
+bio·writenewick(bio·Tree tree, io·Putter out)
{
dump(tree.root, out);
- io·putbyte(out, ';');
- io·putbyte(out, '\n');
- io·flush(out);
+ out.put(';');
+ out.put('\n');
return 0;
}