stdlib: Separate basic I/O reading functionality into a separate buf_reader object, like writing

This commit is contained in:
Patrick Walton
2011-03-25 13:54:54 -07:00
parent 91c2b82b09
commit 739c4aedf1
5 changed files with 86 additions and 26 deletions

View File

@@ -11,8 +11,25 @@ native "rust" mod rustrt {
tag seek_style {seek_set; seek_end; seek_cur;} tag seek_style {seek_set; seek_end; seek_cur;}
// The raw underlying reader class. All readers must implement this.
type buf_reader =
state obj {
impure fn read(uint len) -> vec[u8];
impure fn unread_byte(int byte);
impure fn eof() -> bool;
// FIXME: Seekable really should be orthogonal. We will need
// inheritance.
impure fn seek(int offset, seek_style whence);
impure fn tell() -> uint;
};
// Convenience methods for reading.
type reader = type reader =
state obj { state obj {
// FIXME: This should inherit from buf_reader.
impure fn get_buf_reader() -> buf_reader;
impure fn read_byte() -> int; impure fn read_byte() -> int;
impure fn unread_byte(int byte); impure fn unread_byte(int byte);
impure fn read_bytes(uint len) -> vec[u8]; impure fn read_bytes(uint len) -> vec[u8];
@@ -35,86 +52,18 @@ fn convert_whence(seek_style whence) -> int {
} }
} }
state obj FILE_reader(os.libc.FILE f, bool must_close) { state obj FILE_buf_reader(os.libc.FILE f, bool must_close) {
impure fn read_byte() -> int { impure fn read(uint len) -> vec[u8] {
ret os.libc.fgetc(f);
}
impure fn unread_byte(int byte) {
os.libc.ungetc(byte, f);
}
impure fn read_bytes(uint len) -> vec[u8] {
auto buf = _vec.alloc[u8](len); auto buf = _vec.alloc[u8](len);
auto read = os.libc.fread(_vec.buf[u8](buf), 1u, len, f); auto read = os.libc.fread(_vec.buf[u8](buf), 1u, len, f);
_vec.len_set[u8](buf, read); _vec.len_set[u8](buf, read);
ret buf; ret buf;
} }
impure fn read_char() -> char { impure fn unread_byte(int byte) {
auto c0 = os.libc.fgetc(f); os.libc.ungetc(byte, f);
if (c0 == -1) {ret -1 as char;} // FIXME will this stay valid?
auto b0 = c0 as u8;
auto w = _str.utf8_char_width(b0);
check(w > 0u);
if (w == 1u) {ret b0 as char;}
auto val = 0u;
while (w > 1u) {
w -= 1u;
auto next = os.libc.fgetc(f);
check(next > -1);
check(next & 0xc0 == 0x80);
val <<= 6u;
val += (next & 0x3f) as uint;
}
// See _str.char_at
val += ((b0 << ((w + 1u) as u8)) as uint) << ((w - 1u) * 6u - w - 1u);
ret val as char;
} }
impure fn eof() -> bool { impure fn eof() -> bool {
auto ch = os.libc.fgetc(f); ret os.libc.feof(f) != 0;
if (ch == -1) {ret true;}
os.libc.ungetc(ch, f);
ret false;
}
impure fn read_line() -> str {
let vec[u8] buf = vec();
// No break yet in rustc
auto go_on = true;
while (go_on) {
auto ch = os.libc.fgetc(f);
if (ch == -1 || ch == 10) {go_on = false;}
else {_vec.push[u8](buf, ch as u8);}
}
ret _str.unsafe_from_bytes(buf);
}
impure fn read_c_str() -> str {
let vec[u8] buf = vec();
auto go_on = true;
while (go_on) {
auto ch = os.libc.fgetc(f);
if (ch < 1) {go_on = false;}
else {_vec.push[u8](buf, ch as u8);}
}
ret _str.unsafe_from_bytes(buf);
}
// FIXME deal with eof?
impure fn read_le_uint(uint size) -> uint {
auto val = 0u;
auto pos = 0u;
while (size > 0u) {
val += (os.libc.fgetc(f) as uint) << pos;
pos += 8u;
size -= 1u;
}
ret val;
}
impure fn read_le_int(uint size) -> int {
auto val = 0u;
auto pos = 0u;
while (size > 0u) {
val += (os.libc.fgetc(f) as uint) << pos;
pos += 8u;
size -= 1u;
}
ret val as int;
} }
impure fn seek(int offset, seek_style whence) { impure fn seek(int offset, seek_style whence) {
check (os.libc.fseek(f, offset, convert_whence(whence)) == 0); check (os.libc.fseek(f, offset, convert_whence(whence)) == 0);
@@ -127,8 +76,107 @@ state obj FILE_reader(os.libc.FILE f, bool must_close) {
} }
} }
// FIXME: When we have a "self" keyword, move this into read_byte(). This is
// only here so that multiple method implementations below can use it.
//
// FIXME: Return value should be option[u8], not int.
impure fn read_byte_from_buf_reader(buf_reader rdr) -> int {
auto buf = rdr.read(1u);
if (_vec.len[u8](buf) == 0u) {
ret -1;
}
ret buf.(0) as int;
}
// FIXME: Convert this into pseudomethods on buf_reader.
state obj new_reader(buf_reader rdr) {
impure fn get_buf_reader() -> buf_reader {
ret rdr;
}
impure fn read_byte() -> int {
ret read_byte_from_buf_reader(rdr);
}
impure fn unread_byte(int byte) {
ret rdr.unread_byte(byte);
}
impure fn read_bytes(uint len) -> vec[u8] {
ret rdr.read(len);
}
impure fn read_char() -> char {
auto c0 = read_byte_from_buf_reader(rdr);
if (c0 == -1) {ret -1 as char;} // FIXME will this stay valid?
auto b0 = c0 as u8;
auto w = _str.utf8_char_width(b0);
check(w > 0u);
if (w == 1u) {ret b0 as char;}
auto val = 0u;
while (w > 1u) {
w -= 1u;
auto next = read_byte_from_buf_reader(rdr);
check(next > -1);
check(next & 0xc0 == 0x80);
val <<= 6u;
val += (next & 0x3f) as uint;
}
// See _str.char_at
val += ((b0 << ((w + 1u) as u8)) as uint) << ((w - 1u) * 6u - w - 1u);
ret val as char;
}
impure fn eof() -> bool {
ret rdr.eof();
}
impure fn read_line() -> str {
let vec[u8] buf = vec();
// No break yet in rustc
auto go_on = true;
while (go_on) {
auto ch = read_byte_from_buf_reader(rdr);
if (ch == -1 || ch == 10) {go_on = false;}
else {_vec.push[u8](buf, ch as u8);}
}
ret _str.unsafe_from_bytes(buf);
}
impure fn read_c_str() -> str {
let vec[u8] buf = vec();
auto go_on = true;
while (go_on) {
auto ch = read_byte_from_buf_reader(rdr);
if (ch < 1) {go_on = false;}
else {_vec.push[u8](buf, ch as u8);}
}
ret _str.unsafe_from_bytes(buf);
}
// FIXME deal with eof?
impure fn read_le_uint(uint size) -> uint {
auto val = 0u;
auto pos = 0u;
while (size > 0u) {
val += (read_byte_from_buf_reader(rdr) as uint) << pos;
pos += 8u;
size -= 1u;
}
ret val;
}
impure fn read_le_int(uint size) -> int {
auto val = 0u;
auto pos = 0u;
while (size > 0u) {
val += (read_byte_from_buf_reader(rdr) as uint) << pos;
pos += 8u;
size -= 1u;
}
ret val as int;
}
impure fn seek(int offset, seek_style whence) {
ret rdr.seek(offset, whence);
}
impure fn tell() -> uint {
ret rdr.tell();
}
}
fn stdin() -> reader { fn stdin() -> reader {
ret FILE_reader(rustrt.rust_get_stdin(), false); ret new_reader(FILE_buf_reader(rustrt.rust_get_stdin(), false));
} }
fn file_reader(str path) -> reader { fn file_reader(str path) -> reader {
@@ -137,9 +185,17 @@ fn file_reader(str path) -> reader {
log "error opening " + path; log "error opening " + path;
fail; fail;
} }
ret FILE_reader(f, true); ret new_reader(FILE_buf_reader(f, true));
} }
// Byte buffer readers
//state obj byte_buf_reader(vec[mutable? u8] buf) {
// fn read(
//}
// Writing // Writing
tag fileflag { tag fileflag {
@@ -152,6 +208,7 @@ tag fileflag {
type buf_writer = state obj { type buf_writer = state obj {
fn write(vec[u8] v); fn write(vec[u8] v);
// FIXME: Seekable really should be orthogonal. We will need inheritance.
fn seek(int offset, seek_style whence); fn seek(int offset, seek_style whence);
fn tell() -> uint; // FIXME: eventually u64 fn tell() -> uint; // FIXME: eventually u64
}; };

View File

@@ -17,6 +17,7 @@ native mod libc = "libc.so.6" {
fn fclose(FILE f); fn fclose(FILE f);
fn fgetc(FILE f) -> int; fn fgetc(FILE f) -> int;
fn ungetc(int c, FILE f); fn ungetc(int c, FILE f);
fn feof(FILE f) -> int;
fn fread(vbuf buf, uint size, uint n, FILE f) -> uint; fn fread(vbuf buf, uint size, uint n, FILE f) -> uint;
fn fwrite(vbuf buf, uint size, uint n, FILE f) -> uint; fn fwrite(vbuf buf, uint size, uint n, FILE f) -> uint;
fn fseek(FILE f, int offset, int whence) -> int; fn fseek(FILE f, int offset, int whence) -> int;

View File

@@ -14,6 +14,7 @@ native mod libc = "libc.dylib" {
fn fclose(FILE f); fn fclose(FILE f);
fn fgetc(FILE f) -> int; fn fgetc(FILE f) -> int;
fn ungetc(int c, FILE f); fn ungetc(int c, FILE f);
fn feof(FILE f) -> int;
fn fread(vbuf buf, uint size, uint n, FILE f) -> uint; fn fread(vbuf buf, uint size, uint n, FILE f) -> uint;
fn fwrite(vbuf buf, uint size, uint n, FILE f) -> uint; fn fwrite(vbuf buf, uint size, uint n, FILE f) -> uint;
fn fseek(FILE f, int offset, int whence) -> int; fn fseek(FILE f, int offset, int whence) -> int;

View File

@@ -48,7 +48,7 @@ impure fn start_program(str prog, vec[str] args) -> @program {
ret io.new_writer(io.fd_buf_writer(in_fd, false)); ret io.new_writer(io.fd_buf_writer(in_fd, false));
} }
fn output() -> io.reader { fn output() -> io.reader {
ret io.FILE_reader(out_file, false); ret io.new_reader(io.FILE_buf_reader(out_file, false));
} }
impure fn close_input() { impure fn close_input() {
os.libc.close(in_fd); os.libc.close(in_fd);

View File

@@ -13,6 +13,7 @@ native mod libc = "msvcrt.dll" {
fn fclose(FILE f); fn fclose(FILE f);
fn fgetc(FILE f) -> int; fn fgetc(FILE f) -> int;
fn ungetc(int c, FILE f); fn ungetc(int c, FILE f);
fn feof(FILE f) -> int;
fn fread(vbuf buf, uint size, uint n, FILE f) -> uint; fn fread(vbuf buf, uint size, uint n, FILE f) -> uint;
fn fwrite(vbuf buf, uint size, uint n, FILE f) -> uint; fn fwrite(vbuf buf, uint size, uint n, FILE f) -> uint;
fn fseek(FILE f, int offset, int whence) -> int; fn fseek(FILE f, int offset, int whence) -> int;