Extend stream functionality

Writer and reader streams now come with methods to write and read
little-endian numbers. Whether that is the right place for such
methods is debatable, but for now, that's where they live.
This commit is contained in:
Marijn Haverbeke 2011-03-10 16:02:53 +01:00 committed by Graydon Hoare
parent c731d625fe
commit 441697ab35
8 changed files with 147 additions and 96 deletions

View File

@ -68,7 +68,7 @@ impure fn pretty_print_input(session.session sess,
auto def = tup(0, 0); auto def = tup(0, 0);
auto p = front.parser.new_parser(sess, env, def, input); auto p = front.parser.new_parser(sess, env, def, input);
auto crate = front.parser.parse_crate_from_source_file(p); auto crate = front.parser.parse_crate_from_source_file(p);
pretty.pprust.print_ast(crate.node.module, std.io.stdout_writer()); pretty.pprust.print_ast(crate.node.module, std.io.stdout());
} }
fn warn_wrong_compiler() { fn warn_wrong_compiler() {

View File

@ -1,4 +1,4 @@
import std.io.stdio_reader; import std.io;
import std._str; import std._str;
import std.map; import std.map;
import std.map.hashmap; import std.map.hashmap;
@ -18,9 +18,9 @@ state type reader = state obj {
fn get_reserved() -> hashmap[str,()]; fn get_reserved() -> hashmap[str,()];
}; };
fn new_reader(stdio_reader rdr, str filename) -> reader impure fn new_reader(io.reader rdr, str filename) -> reader
{ {
state obj reader(stdio_reader rdr, state obj reader(io.reader rdr,
str filename, str filename,
mutable char c, mutable char c,
mutable char n, mutable char n,
@ -72,7 +72,7 @@ fn new_reader(stdio_reader rdr, str filename) -> reader
col += 1u; col += 1u;
} }
n = rdr.getc() as char; n = rdr.read_char() as char;
} }
fn mark() { fn mark() {
@ -200,8 +200,8 @@ fn new_reader(stdio_reader rdr, str filename) -> reader
reserved.insert("m128", ()); // IEEE 754-2008 'decimal128' reserved.insert("m128", ()); // IEEE 754-2008 'decimal128'
reserved.insert("dec", ()); // One of m32, m64, m128 reserved.insert("dec", ()); // One of m32, m64, m128
ret reader(rdr, filename, rdr.getc() as char, rdr.getc() as char, ret reader(rdr, filename, rdr.read_char() as char,
1u, 0u, 1u, 0u, keywords, reserved); rdr.read_char() as char, 1u, 0u, 1u, 0u, keywords, reserved);
} }

View File

@ -115,7 +115,7 @@ impure fn new_parser(session.session sess,
if (_str.ends_with(path, ".rc")) { if (_str.ends_with(path, ".rc")) {
ftype = CRATE_FILE; ftype = CRATE_FILE;
} }
auto srdr = io.new_stdio_reader(path); auto srdr = io.file_reader(path);
auto rdr = lexer.new_reader(srdr, path); auto rdr = lexer.new_reader(srdr, path);
auto npos = rdr.get_curr_pos(); auto npos = rdr.get_curr_pos();
ret stdio_parser(sess, env, ftype, lexer.next_token(rdr), ret stdio_parser(sess, env, ftype, lexer.next_token(rdr),

View File

@ -1,87 +1,103 @@
import os.libc; import os.libc;
type stdio_reader = state obj { native "rust" mod rustrt {
fn getc() -> int; fn rust_get_stdin() -> os.libc.FILE;
fn ungetc(int i); fn rust_get_stdout() -> os.libc.FILE;
}; }
fn new_stdio_reader(str path) -> stdio_reader { // Reading
state obj stdio_FILE_reader(os.libc.FILE f) {
fn getc() -> int { // TODO This is all buffered. We might need an unbuffered variant as well
ret os.libc.fgetc(f);
} tag seek_style {seek_set; seek_end; seek_cur;}
fn ungetc(int i) {
os.libc.ungetc(i, f); type reader =
} state obj {
drop { impure fn read_byte() -> u8;
os.libc.fclose(f); impure fn read_bytes(uint len) -> vec[u8];
} impure fn read_char() -> int;
impure fn unread_char(int i);
impure fn read_c_str() -> str;
impure fn read_le_uint(uint size) -> uint;
impure fn read_le_int(uint size) -> int;
impure fn seek(int offset, seek_style whence);
};
state obj FILE_reader(os.libc.FILE f, bool must_close) {
impure fn read_byte() -> u8 {
ret os.libc.fgetc(f) as u8;
} }
auto FILE = os.libc.fopen(_str.buf(path), _str.buf("r")); impure fn read_bytes(uint len) -> vec[u8] {
check (FILE as uint != 0u); auto buf = _vec.alloc[u8](len);
ret stdio_FILE_reader(FILE); auto read = os.libc.fread(_vec.buf[u8](buf), 1u, len, f);
} check(read == len);
ret buf;
type buf_reader = state obj {
fn read() -> vec[u8];
};
type buf_writer = state obj {
fn write(vec[u8] v);
};
fn default_bufsz() -> uint {
ret 4096u;
}
fn new_buf() -> vec[u8] {
ret _vec.alloc[u8](default_bufsz());
}
fn new_buf_reader(str path) -> buf_reader {
state obj fd_buf_reader(int fd, mutable vec[u8] buf) {
fn read() -> vec[u8] {
// Ensure our buf is singly-referenced.
if (_vec.rustrt.refcount[u8](buf) != 1u) {
buf = new_buf();
}
auto len = default_bufsz();
auto vbuf = _vec.buf[u8](buf);
auto count = os.libc.read(fd, vbuf, len);
if (count < 0) {
log "error filling buffer";
log sys.rustrt.last_os_error();
fail;
}
_vec.len_set[u8](buf, count as uint);
ret buf;
}
drop {
os.libc.close(fd);
}
} }
impure fn read_char() -> int {
auto fd = os.libc.open(_str.buf(path), ret os.libc.fgetc(f);
os.libc_constants.O_RDONLY() | }
os.libc_constants.O_BINARY(), impure fn unread_char(int ch) {
0u); os.libc.ungetc(ch, f);
}
if (fd < 0) { impure fn read_c_str() -> str {
log "error opening file for reading"; auto buf = "";
log sys.rustrt.last_os_error(); while (true) {
fail; auto ch = os.libc.fgetc(f);
if (ch < 1) {break;}
buf += _str.unsafe_from_bytes(vec(ch as u8));
}
ret buf;
}
// TODO 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; // TODO does that work?
}
impure fn seek(int offset, seek_style whence) {
auto wh;
alt (whence) {
case (seek_set) {wh = 0;}
case (seek_cur) {wh = 1;}
case (seek_end) {wh = 2;}
}
check(os.libc.fseek(f, offset, wh) == 0);
}
drop {
if (must_close) {os.libc.fclose(f);}
} }
ret fd_buf_reader(fd, new_buf());
} }
fn stdin() -> reader {
ret FILE_reader(rustrt.rust_get_stdin(), false);
}
fn file_reader(str path) -> reader {
auto f = os.libc.fopen(_str.buf(path), _str.buf("r"));
check (f as uint != 0u);
ret FILE_reader(f, true);
}
// Writing
// TODO This is all unbuffered. We might need a buffered variant as well
tag fileflag { tag fileflag {
append; append;
create; create;
@ -89,6 +105,10 @@ tag fileflag {
none; none;
} }
type buf_writer = state obj {
fn write(vec[u8] v);
};
state obj fd_buf_writer(int fd, bool must_close) { state obj fd_buf_writer(int fd, bool must_close) {
fn write(vec[u8] v) { fn write(vec[u8] v) {
auto len = _vec.len[u8](v); auto len = _vec.len[u8](v);
@ -143,8 +163,21 @@ type writer =
impure fn write_str(str s); impure fn write_str(str s);
impure fn write_int(int n); impure fn write_int(int n);
impure fn write_uint(uint n); impure fn write_uint(uint n);
impure fn write_bytes(vec[u8] bytes);
impure fn write_le_uint(uint n, uint size);
impure fn write_le_int(int n, uint size);
}; };
fn uint_to_le_bytes(uint n, uint size) -> vec[u8] {
let vec[u8] bytes = vec();
while (size > 0u) {
bytes += vec((n & 255u) as u8);
n >>= 8u;
size -= 1u;
}
ret bytes;
}
state obj new_writer(buf_writer out) { state obj new_writer(buf_writer out) {
impure fn write_str(str s) { impure fn write_str(str s) {
out.write(_str.bytes(s)); out.write(_str.bytes(s));
@ -155,14 +188,23 @@ state obj new_writer(buf_writer out) {
impure fn write_uint(uint n) { impure fn write_uint(uint n) {
out.write(_str.bytes(_uint.to_str(n, 10u))); out.write(_str.bytes(_uint.to_str(n, 10u)));
} }
impure fn write_bytes(vec[u8] bytes) {
out.write(bytes);
}
impure fn write_le_uint(uint n, uint size) {
out.write(uint_to_le_bytes(n, size));
}
impure fn write_le_int(int n, uint size) {
out.write(uint_to_le_bytes(n as uint, size));
}
} }
fn file_writer(str path, vec[fileflag] flags) -> writer { fn file_writer(str path, vec[fileflag] flags) -> writer {
ret new_writer(file_buf_writer(path, flags)); ret new_writer(file_buf_writer(path, flags));
} }
// FIXME it would be great if this could be a const named stdout // FIXME it would be great if this could be a const
fn stdout_writer() -> writer { fn stdout() -> writer {
ret new_writer(fd_buf_writer(1, false)); ret new_writer(fd_buf_writer(1, false));
} }
@ -172,21 +214,21 @@ type str_writer =
fn get_str() -> str; fn get_str() -> str;
}; };
type str_buf = @rec(mutable str buf); type byte_buf = @rec(mutable vec[u8] buf);
state obj byte_buf_writer(byte_buf buf) {
fn write(vec[u8] v) {buf.buf += v;}
}
// TODO awkward! it's not possible to implement a writer with an extra method // TODO awkward! it's not possible to implement a writer with an extra method
fn string_writer() -> str_writer { fn string_writer() -> str_writer {
auto buf = @rec(mutable buf = ""); let vec[u8] b = vec();
state obj str_writer_writer(str_buf buf) { let byte_buf buf = @rec(mutable buf = b);
impure fn write_str(str s) { buf.buf += s; } state obj str_writer_wrap(writer wr, byte_buf buf) {
impure fn write_int(int n) { buf.buf += _int.to_str(n, 10u); }
impure fn write_uint(uint n) { buf.buf += _uint.to_str(n, 10u); }
}
state obj str_writer_wrap(writer wr, str_buf buf) {
fn get_writer() -> writer {ret wr;} fn get_writer() -> writer {ret wr;}
fn get_str() -> str {ret buf.buf;} fn get_str() -> str {ret _str.unsafe_from_bytes(buf.buf);}
} }
ret str_writer_wrap(str_writer_writer(buf), buf); ret str_writer_wrap(new_writer(byte_buf_writer(buf)), buf);
} }
// //

View File

@ -13,6 +13,8 @@ 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 fread(vbuf buf, uint size, uint n, FILE f) -> uint;
fn fseek(FILE f, int offset, int whence) -> int;
type dir; type dir;
fn opendir(sbuf d) -> dir; fn opendir(sbuf d) -> dir;

View File

@ -12,6 +12,8 @@ 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 fread(vbuf buf, uint size, uint n, FILE f) -> uint;
fn fseek(FILE f, int offset, int whence) -> int;
type dir; type dir;
fn opendir(sbuf d) -> dir; fn opendir(sbuf d) -> dir;

View File

@ -12,6 +12,8 @@ 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 fread(vbuf buf, uint size, uint n, FILE f) -> uint;
fn fseek(FILE f, int offset, int whence) -> int;
} }
mod libc_constants { mod libc_constants {

View File

@ -408,6 +408,9 @@ rust_file_is_dir(rust_task *task, rust_str *path) {
return S_ISDIR(buf.st_mode); return S_ISDIR(buf.st_mode);
} }
extern "C" CDECL FILE* rust_get_stdin() {return stdin;}
extern "C" CDECL FILE* rust_get_stdout() {return stdout;}
// //
// Local Variables: // Local Variables:
// mode: C++ // mode: C++