Skip to content

Commit

Permalink
implement rb_io_getbyte()
Browse files Browse the repository at this point in the history
  • Loading branch information
lrz committed Mar 12, 2009
1 parent eaabdfb commit 294eba3
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions io.c
Expand Up @@ -815,6 +815,7 @@ io_read(VALUE io, SEL sel, int argc, VALUE *argv)
CFDataIncreaseLength(data, FIX2LONG(len)); // sentinel byte?
CFReadStreamRead(io_struct->readStream, CFDataGetMutableBytePtr(data),
FIX2LONG(len));
// TODO check the return value of CFReadStreamRead() for errors/eof
return outbuf;
}

Expand Down Expand Up @@ -1116,7 +1117,20 @@ rb_io_readchar(VALUE io, SEL sel)
VALUE
rb_io_getbyte(VALUE io, SEL sel)
{
rb_notimplement();
rb_io_t *io_struct = ExtractIOStruct(io);

UInt8 byte;
int code = CFReadStreamRead(io_struct->readStream, &byte, 1);
if (code != 1) {
if (code == 0) {
// EOF
return Qnil;
}
// TODO raise an exception
abort();
}

return INT2FIX(byte);
}

/*
Expand All @@ -1130,7 +1144,12 @@ rb_io_getbyte(VALUE io, SEL sel)
static VALUE
rb_io_readbyte(VALUE io, SEL sel)
{
rb_notimplement();
VALUE c = rb_io_getbyte(io, 0);

if (NIL_P(c)) {
rb_eof_error();
}
return c;
}

/*
Expand Down

0 comments on commit 294eba3

Please sign in to comment.