2011-07-10 14:47:51 -05:00
|
|
|
import os::libc;
|
|
|
|
|
2011-10-20 09:04:20 -05:00
|
|
|
native "c-stack-cdecl" mod rustrt {
|
2011-07-10 14:47:51 -05:00
|
|
|
fn rust_get_stdin() -> os::libc::FILE;
|
|
|
|
fn rust_get_stdout() -> os::libc::FILE;
|
2011-08-24 23:19:56 -05:00
|
|
|
fn rust_get_stderr() -> os::libc::FILE;
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Reading
|
|
|
|
|
|
|
|
// FIXME This is all buffered. We might need an unbuffered variant as well
|
|
|
|
tag seek_style { seek_set; seek_end; seek_cur; }
|
|
|
|
|
|
|
|
|
|
|
|
// The raw underlying reader class. All readers must implement this.
|
|
|
|
type buf_reader =
|
2011-07-27 07:19:39 -05:00
|
|
|
// FIXME: Seekable really should be orthogonal. We will need
|
|
|
|
// inheritance.
|
2011-07-10 14:47:51 -05:00
|
|
|
obj {
|
2011-08-19 17:16:48 -05:00
|
|
|
fn read(uint) -> [u8];
|
|
|
|
fn read_byte() -> int;
|
|
|
|
fn unread_byte(int);
|
|
|
|
fn eof() -> bool;
|
|
|
|
fn seek(int, seek_style);
|
|
|
|
fn tell() -> uint;
|
2011-07-10 14:47:51 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Convenience methods for reading.
|
|
|
|
type reader =
|
2011-07-27 07:19:39 -05:00
|
|
|
// FIXME: This should inherit from buf_reader.
|
2011-08-19 17:16:48 -05:00
|
|
|
// FIXME: eventually u64
|
2011-07-27 07:19:39 -05:00
|
|
|
|
|
|
|
obj {
|
2011-08-19 17:16:48 -05:00
|
|
|
fn get_buf_reader() -> buf_reader;
|
|
|
|
fn read_byte() -> int;
|
|
|
|
fn unread_byte(int);
|
|
|
|
fn read_bytes(uint) -> [u8];
|
|
|
|
fn read_char() -> char;
|
|
|
|
fn eof() -> bool;
|
2011-09-02 17:34:58 -05:00
|
|
|
fn read_line() -> str;
|
|
|
|
fn read_c_str() -> str;
|
2011-08-19 17:16:48 -05:00
|
|
|
fn read_le_uint(uint) -> uint;
|
|
|
|
fn read_le_int(uint) -> int;
|
|
|
|
fn read_be_uint(uint) -> uint;
|
|
|
|
fn read_whole_stream() -> [u8];
|
|
|
|
fn seek(int, seek_style);
|
|
|
|
fn tell() -> uint;
|
2011-07-10 14:47:51 -05:00
|
|
|
};
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn convert_whence(whence: seek_style) -> int {
|
|
|
|
ret alt whence { seek_set. { 0 } seek_cur. { 1 } seek_end. { 2 } };
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
|
|
|
|
2011-08-19 17:16:48 -05:00
|
|
|
resource FILE_res(f: os::libc::FILE) { os::libc::fclose(f); }
|
2011-07-29 06:31:44 -05:00
|
|
|
|
2011-08-10 11:27:11 -05:00
|
|
|
obj FILE_buf_reader(f: os::libc::FILE, res: option::t<@FILE_res>) {
|
2011-10-12 15:31:41 -05:00
|
|
|
fn read(len: uint) -> [u8] unsafe {
|
2011-08-19 17:16:48 -05:00
|
|
|
let buf = [];
|
2011-08-12 12:56:57 -05:00
|
|
|
vec::reserve::<u8>(buf, len);
|
2011-09-02 17:34:58 -05:00
|
|
|
let read =
|
|
|
|
os::libc::fread(vec::unsafe::to_ptr::<u8>(buf), 1u, len, f);
|
2011-08-12 12:56:57 -05:00
|
|
|
vec::unsafe::set_len::<u8>(buf, read);
|
2011-07-10 14:47:51 -05:00
|
|
|
ret buf;
|
|
|
|
}
|
|
|
|
fn read_byte() -> int { ret os::libc::fgetc(f); }
|
2011-07-27 07:19:39 -05:00
|
|
|
fn unread_byte(byte: int) { os::libc::ungetc(byte, f); }
|
2011-07-10 14:47:51 -05:00
|
|
|
fn eof() -> bool { ret os::libc::feof(f) != 0; }
|
2011-07-27 07:19:39 -05:00
|
|
|
fn seek(offset: int, whence: seek_style) {
|
2011-07-10 14:47:51 -05:00
|
|
|
assert (os::libc::fseek(f, offset, convert_whence(whence)) == 0);
|
|
|
|
}
|
2011-08-19 17:16:48 -05:00
|
|
|
fn tell() -> uint { ret os::libc::ftell(f) as uint; }
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// FIXME: Convert this into pseudomethods on buf_reader.
|
2011-07-27 07:19:39 -05:00
|
|
|
obj new_reader(rdr: buf_reader) {
|
2011-07-10 14:47:51 -05:00
|
|
|
fn get_buf_reader() -> buf_reader { ret rdr; }
|
|
|
|
fn read_byte() -> int { ret rdr.read_byte(); }
|
2011-07-27 07:19:39 -05:00
|
|
|
fn unread_byte(byte: int) { ret rdr.unread_byte(byte); }
|
2011-08-04 19:24:54 -05:00
|
|
|
fn read_bytes(len: uint) -> [u8] { ret rdr.read(len); }
|
2011-07-10 14:47:51 -05:00
|
|
|
fn read_char() -> char {
|
2011-07-27 07:19:39 -05:00
|
|
|
let c0 = rdr.read_byte();
|
|
|
|
if c0 == -1 {
|
2011-07-10 14:47:51 -05:00
|
|
|
ret -1 as char; // FIXME will this stay valid?
|
|
|
|
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
let b0 = c0 as u8;
|
2011-09-01 19:27:58 -05:00
|
|
|
let w = str::utf8_char_width(b0);
|
2011-07-10 14:47:51 -05:00
|
|
|
assert (w > 0u);
|
2011-07-27 07:19:39 -05:00
|
|
|
if w == 1u { ret b0 as char; }
|
|
|
|
let val = 0u;
|
|
|
|
while w > 1u {
|
2011-07-10 14:47:51 -05:00
|
|
|
w -= 1u;
|
2011-07-27 07:19:39 -05:00
|
|
|
let next = rdr.read_byte();
|
2011-07-10 14:47:51 -05:00
|
|
|
assert (next > -1);
|
|
|
|
assert (next & 192 == 128);
|
|
|
|
val <<= 6u;
|
|
|
|
val += next & 63 as uint;
|
|
|
|
}
|
|
|
|
// See str::char_at
|
|
|
|
|
|
|
|
val += (b0 << (w + 1u as u8) as uint) << (w - 1u) * 6u - w - 1u;
|
|
|
|
ret val as char;
|
|
|
|
}
|
|
|
|
fn eof() -> bool { ret rdr.eof(); }
|
2011-09-02 17:34:58 -05:00
|
|
|
fn read_line() -> str {
|
2011-08-19 17:16:48 -05:00
|
|
|
let buf: [u8] = [];
|
2011-07-10 14:47:51 -05:00
|
|
|
// No break yet in rustc
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
let go_on = true;
|
|
|
|
while go_on {
|
|
|
|
let ch = rdr.read_byte();
|
|
|
|
if ch == -1 || ch == 10 {
|
2011-07-10 14:47:51 -05:00
|
|
|
go_on = false;
|
2011-08-19 17:16:48 -05:00
|
|
|
} else { buf += [ch as u8]; }
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
2011-09-01 19:27:58 -05:00
|
|
|
ret str::unsafe_from_bytes(buf);
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
2011-09-02 17:34:58 -05:00
|
|
|
fn read_c_str() -> str {
|
2011-08-19 17:16:48 -05:00
|
|
|
let buf: [u8] = [];
|
2011-07-27 07:19:39 -05:00
|
|
|
let go_on = true;
|
|
|
|
while go_on {
|
|
|
|
let ch = rdr.read_byte();
|
2011-08-19 17:16:48 -05:00
|
|
|
if ch < 1 { go_on = false; } else { buf += [ch as u8]; }
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
2011-09-01 19:27:58 -05:00
|
|
|
ret str::unsafe_from_bytes(buf);
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME deal with eof?
|
2011-07-27 07:19:39 -05:00
|
|
|
fn read_le_uint(size: uint) -> uint {
|
|
|
|
let val = 0u;
|
|
|
|
let pos = 0u;
|
|
|
|
while size > 0u {
|
2011-07-10 14:47:51 -05:00
|
|
|
val += (rdr.read_byte() as uint) << pos;
|
|
|
|
pos += 8u;
|
|
|
|
size -= 1u;
|
|
|
|
}
|
|
|
|
ret val;
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
fn read_le_int(size: uint) -> int {
|
|
|
|
let val = 0u;
|
|
|
|
let pos = 0u;
|
|
|
|
while size > 0u {
|
2011-07-10 14:47:51 -05:00
|
|
|
val += (rdr.read_byte() as uint) << pos;
|
|
|
|
pos += 8u;
|
|
|
|
size -= 1u;
|
|
|
|
}
|
|
|
|
ret val as int;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME deal with eof?
|
2011-07-27 07:19:39 -05:00
|
|
|
fn read_be_uint(sz: uint) -> uint {
|
|
|
|
let val = 0u;
|
2011-07-10 14:47:51 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
while sz > 0u {
|
2011-07-10 14:47:51 -05:00
|
|
|
sz -= 1u;
|
|
|
|
val += (rdr.read_byte() as uint) << sz * 8u;
|
|
|
|
}
|
|
|
|
ret val;
|
|
|
|
}
|
2011-08-04 19:24:54 -05:00
|
|
|
fn read_whole_stream() -> [u8] {
|
2011-08-19 17:16:48 -05:00
|
|
|
let buf: [u8] = [];
|
2011-07-27 07:19:39 -05:00
|
|
|
while !rdr.eof() { buf += rdr.read(2048u); }
|
2011-07-10 14:47:51 -05:00
|
|
|
ret buf;
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
fn seek(offset: int, whence: seek_style) { ret rdr.seek(offset, whence); }
|
2011-07-10 14:47:51 -05:00
|
|
|
fn tell() -> uint { ret rdr.tell(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stdin() -> reader {
|
2011-07-29 06:31:44 -05:00
|
|
|
ret new_reader(FILE_buf_reader(rustrt::rust_get_stdin(), option::none));
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn file_reader(path: str) -> reader {
|
2011-10-06 05:26:12 -05:00
|
|
|
let f = str::as_buf(path, {|pathbuf|
|
|
|
|
str::as_buf("r", {|modebuf|
|
|
|
|
os::libc::fopen(pathbuf, modebuf)
|
|
|
|
})
|
|
|
|
});
|
2011-09-02 17:34:58 -05:00
|
|
|
if f as uint == 0u { log_err "error opening " + path; fail; }
|
2011-07-29 06:31:44 -05:00
|
|
|
ret new_reader(FILE_buf_reader(f, option::some(@FILE_res(f))));
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Byte buffer readers
|
|
|
|
|
|
|
|
// TODO: mutable? u8, but this fails with rustboot.
|
2011-08-04 19:24:54 -05:00
|
|
|
type byte_buf = @{buf: [u8], mutable pos: uint};
|
2011-07-27 07:19:39 -05:00
|
|
|
|
|
|
|
obj byte_buf_reader(bbuf: byte_buf) {
|
2011-08-04 19:24:54 -05:00
|
|
|
fn read(len: uint) -> [u8] {
|
2011-08-12 12:56:57 -05:00
|
|
|
let rest = vec::len::<u8>(bbuf.buf) - bbuf.pos;
|
2011-07-27 07:19:39 -05:00
|
|
|
let to_read = len;
|
|
|
|
if rest < to_read { to_read = rest; }
|
2011-08-12 12:56:57 -05:00
|
|
|
let range = vec::slice::<u8>(bbuf.buf, bbuf.pos, bbuf.pos + to_read);
|
2011-07-10 14:47:51 -05:00
|
|
|
bbuf.pos += to_read;
|
|
|
|
ret range;
|
|
|
|
}
|
|
|
|
fn read_byte() -> int {
|
2011-08-12 12:56:57 -05:00
|
|
|
if bbuf.pos == vec::len::<u8>(bbuf.buf) { ret -1; }
|
2011-08-19 17:16:48 -05:00
|
|
|
let b = bbuf.buf[bbuf.pos];
|
2011-07-10 14:47:51 -05:00
|
|
|
bbuf.pos += 1u;
|
|
|
|
ret b as int;
|
|
|
|
}
|
2011-08-18 03:01:39 -05:00
|
|
|
fn unread_byte(_byte: int) { log_err "TODO: unread_byte"; fail; }
|
2011-08-12 12:56:57 -05:00
|
|
|
fn eof() -> bool { ret bbuf.pos == vec::len::<u8>(bbuf.buf); }
|
2011-07-27 07:19:39 -05:00
|
|
|
fn seek(offset: int, whence: seek_style) {
|
|
|
|
let pos = bbuf.pos;
|
2011-08-12 12:56:57 -05:00
|
|
|
let len = vec::len::<u8>(bbuf.buf);
|
2011-07-10 14:47:51 -05:00
|
|
|
bbuf.pos = seek_in_buf(offset, pos, len, whence);
|
|
|
|
}
|
|
|
|
fn tell() -> uint { ret bbuf.pos; }
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn new_byte_buf_reader(buf: [u8]) -> buf_reader {
|
2011-07-27 07:19:39 -05:00
|
|
|
ret byte_buf_reader(@{buf: buf, mutable pos: 0u});
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn string_reader(s: str) -> reader {
|
2011-09-01 19:27:58 -05:00
|
|
|
ret new_reader(new_byte_buf_reader(str::bytes(s)));
|
2011-07-25 16:04:01 -05:00
|
|
|
}
|
|
|
|
|
2011-07-10 14:47:51 -05:00
|
|
|
|
|
|
|
// Writing
|
|
|
|
tag fileflag { append; create; truncate; none; }
|
|
|
|
|
|
|
|
type buf_writer =
|
2011-07-27 07:19:39 -05:00
|
|
|
// FIXME: Seekable really should be orthogonal. We will need
|
|
|
|
// inheritance.
|
2011-08-19 17:16:48 -05:00
|
|
|
// FIXME: eventually u64
|
2011-07-10 14:47:51 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
obj {
|
2011-09-12 04:27:30 -05:00
|
|
|
fn write([u8]);
|
2011-08-19 17:16:48 -05:00
|
|
|
fn seek(int, seek_style);
|
|
|
|
fn tell() -> uint;
|
2011-07-10 14:47:51 -05:00
|
|
|
};
|
|
|
|
|
2011-08-10 11:27:11 -05:00
|
|
|
obj FILE_writer(f: os::libc::FILE, res: option::t<@FILE_res>) {
|
2011-10-12 15:31:41 -05:00
|
|
|
fn write(v: [u8]) unsafe {
|
2011-08-12 12:56:57 -05:00
|
|
|
let len = vec::len::<u8>(v);
|
2011-08-25 03:18:02 -05:00
|
|
|
let vbuf = vec::unsafe::to_ptr::<u8>(v);
|
2011-08-12 12:40:25 -05:00
|
|
|
let nout = os::libc::fwrite(vbuf, len, 1u, f);
|
2011-07-27 07:19:39 -05:00
|
|
|
if nout < 1u { log_err "error dumping buffer"; }
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
fn seek(offset: int, whence: seek_style) {
|
2011-07-10 14:47:51 -05:00
|
|
|
assert (os::libc::fseek(f, offset, convert_whence(whence)) == 0);
|
|
|
|
}
|
2011-08-19 17:16:48 -05:00
|
|
|
fn tell() -> uint { ret os::libc::ftell(f) as uint; }
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
|
|
|
|
2011-08-19 17:16:48 -05:00
|
|
|
resource fd_res(fd: int) { os::libc::close(fd); }
|
2011-07-29 06:31:44 -05:00
|
|
|
|
2011-08-10 11:27:11 -05:00
|
|
|
obj fd_buf_writer(fd: int, res: option::t<@fd_res>) {
|
2011-10-12 15:31:41 -05:00
|
|
|
fn write(v: [u8]) unsafe {
|
2011-08-12 12:56:57 -05:00
|
|
|
let len = vec::len::<u8>(v);
|
2011-07-27 07:19:39 -05:00
|
|
|
let count = 0u;
|
|
|
|
let vbuf;
|
|
|
|
while count < len {
|
2011-08-25 03:18:02 -05:00
|
|
|
vbuf = ptr::offset(vec::unsafe::to_ptr::<u8>(v), count);
|
2011-08-12 12:40:25 -05:00
|
|
|
let nout = os::libc::write(fd, vbuf, len);
|
2011-07-27 07:19:39 -05:00
|
|
|
if nout < 0 {
|
2011-07-10 14:47:51 -05:00
|
|
|
log_err "error dumping buffer";
|
2011-10-06 18:42:27 -05:00
|
|
|
log_err sys::last_os_error();
|
2011-07-10 14:47:51 -05:00
|
|
|
fail;
|
|
|
|
}
|
|
|
|
count += nout as uint;
|
|
|
|
}
|
|
|
|
}
|
2011-08-18 03:01:39 -05:00
|
|
|
fn seek(_offset: int, _whence: seek_style) {
|
2011-07-10 14:47:51 -05:00
|
|
|
log_err "need 64-bit native calls for seek, sorry";
|
|
|
|
fail;
|
|
|
|
}
|
|
|
|
fn tell() -> uint {
|
|
|
|
log_err "need 64-bit native calls for tell, sorry";
|
|
|
|
fail;
|
2011-07-27 10:05:34 -05:00
|
|
|
}
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn file_buf_writer(path: str, flags: [fileflag]) -> buf_writer {
|
2011-07-27 07:19:39 -05:00
|
|
|
let fflags: int =
|
2011-07-10 14:47:51 -05:00
|
|
|
os::libc_constants::O_WRONLY() | os::libc_constants::O_BINARY();
|
2011-08-15 23:54:52 -05:00
|
|
|
for f: fileflag in flags {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt f {
|
|
|
|
append. { fflags |= os::libc_constants::O_APPEND(); }
|
|
|
|
create. { fflags |= os::libc_constants::O_CREAT(); }
|
|
|
|
truncate. { fflags |= os::libc_constants::O_TRUNC(); }
|
|
|
|
none. { }
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
|
|
|
}
|
2011-09-02 17:34:58 -05:00
|
|
|
let fd =
|
|
|
|
str::as_buf(path,
|
|
|
|
{|pathbuf|
|
|
|
|
os::libc::open(pathbuf, fflags,
|
|
|
|
os::libc_constants::S_IRUSR() |
|
|
|
|
os::libc_constants::S_IWUSR())
|
|
|
|
});
|
2011-07-27 07:19:39 -05:00
|
|
|
if fd < 0 {
|
2011-07-10 14:47:51 -05:00
|
|
|
log_err "error opening file for writing";
|
2011-10-06 18:42:27 -05:00
|
|
|
log_err sys::last_os_error();
|
2011-07-10 14:47:51 -05:00
|
|
|
fail;
|
|
|
|
}
|
2011-07-29 06:31:44 -05:00
|
|
|
ret fd_buf_writer(fd, option::some(@fd_res(fd)));
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type writer =
|
2011-07-27 07:19:39 -05:00
|
|
|
// write_str will continue to do utf-8 output only. an alternative
|
|
|
|
// function will be provided for general encoded string output
|
2011-07-10 14:47:51 -05:00
|
|
|
obj {
|
2011-08-19 17:16:48 -05:00
|
|
|
fn get_buf_writer() -> buf_writer;
|
2011-09-12 04:27:30 -05:00
|
|
|
fn write_str(str);
|
|
|
|
fn write_line(str);
|
2011-08-19 17:16:48 -05:00
|
|
|
fn write_char(char);
|
|
|
|
fn write_int(int);
|
|
|
|
fn write_uint(uint);
|
2011-09-12 04:27:30 -05:00
|
|
|
fn write_bytes([u8]);
|
2011-08-19 17:16:48 -05:00
|
|
|
fn write_le_uint(uint, uint);
|
|
|
|
fn write_le_int(int, uint);
|
|
|
|
fn write_be_uint(uint, uint);
|
2011-07-10 14:47:51 -05:00
|
|
|
};
|
|
|
|
|
2011-08-04 19:24:54 -05:00
|
|
|
fn uint_to_le_bytes(n: uint, size: uint) -> [u8] {
|
2011-08-19 17:16:48 -05:00
|
|
|
let bytes: [u8] = [];
|
|
|
|
while size > 0u { bytes += [n & 255u as u8]; n >>= 8u; size -= 1u; }
|
2011-07-10 14:47:51 -05:00
|
|
|
ret bytes;
|
|
|
|
}
|
|
|
|
|
2011-08-04 19:24:54 -05:00
|
|
|
fn uint_to_be_bytes(n: uint, size: uint) -> [u8] {
|
2011-08-19 17:16:48 -05:00
|
|
|
let bytes: [u8] = [];
|
2011-07-27 07:19:39 -05:00
|
|
|
let i = size - 1u as int;
|
2011-08-19 17:16:48 -05:00
|
|
|
while i >= 0 { bytes += [n >> (i * 8 as uint) & 255u as u8]; i -= 1; }
|
2011-07-10 14:47:51 -05:00
|
|
|
ret bytes;
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
obj new_writer(out: buf_writer) {
|
2011-07-10 14:47:51 -05:00
|
|
|
fn get_buf_writer() -> buf_writer { ret out; }
|
2011-09-12 04:27:30 -05:00
|
|
|
fn write_str(s: str) { out.write(str::bytes(s)); }
|
|
|
|
fn write_line(s: str) {
|
2011-09-01 19:27:58 -05:00
|
|
|
out.write(str::bytes(s));
|
2011-09-02 17:34:58 -05:00
|
|
|
out.write(str::bytes("\n"));
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
fn write_char(ch: char) {
|
2011-07-10 14:47:51 -05:00
|
|
|
// FIXME needlessly consy
|
|
|
|
|
2011-09-01 19:27:58 -05:00
|
|
|
out.write(str::bytes(str::from_char(ch)));
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
2011-09-02 17:34:58 -05:00
|
|
|
fn write_int(n: int) { out.write(str::bytes(int::to_str(n, 10u))); }
|
|
|
|
fn write_uint(n: uint) { out.write(str::bytes(uint::to_str(n, 10u))); }
|
2011-09-12 04:27:30 -05:00
|
|
|
fn write_bytes(bytes: [u8]) { out.write(bytes); }
|
2011-07-27 07:19:39 -05:00
|
|
|
fn write_le_uint(n: uint, size: uint) {
|
2011-07-10 14:47:51 -05:00
|
|
|
out.write(uint_to_le_bytes(n, size));
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
fn write_le_int(n: int, size: uint) {
|
2011-07-10 14:47:51 -05:00
|
|
|
out.write(uint_to_le_bytes(n as uint, size));
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
fn write_be_uint(n: uint, size: uint) {
|
2011-07-10 14:47:51 -05:00
|
|
|
out.write(uint_to_be_bytes(n, size));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn file_writer(path: str, flags: [fileflag]) -> writer {
|
2011-07-10 14:47:51 -05:00
|
|
|
ret new_writer(file_buf_writer(path, flags));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// FIXME: fileflags
|
2011-09-12 04:27:30 -05:00
|
|
|
fn buffered_file_buf_writer(path: str) -> buf_writer {
|
2011-09-02 17:34:58 -05:00
|
|
|
let f =
|
|
|
|
str::as_buf(path,
|
|
|
|
{|pathbuf|
|
|
|
|
str::as_buf("w",
|
|
|
|
{|modebuf|
|
|
|
|
os::libc::fopen(pathbuf, modebuf)
|
|
|
|
})
|
|
|
|
});
|
|
|
|
if f as uint == 0u { log_err "error opening " + path; fail; }
|
2011-07-29 06:31:44 -05:00
|
|
|
ret FILE_writer(f, option::some(@FILE_res(f)));
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// FIXME it would be great if this could be a const
|
2011-07-29 06:31:44 -05:00
|
|
|
fn stdout() -> writer { ret new_writer(fd_buf_writer(1, option::none)); }
|
2011-08-24 23:19:56 -05:00
|
|
|
fn stderr() -> writer { ret new_writer(fd_buf_writer(2, option::none)); }
|
2011-07-10 14:47:51 -05:00
|
|
|
|
2011-10-14 05:44:40 -05:00
|
|
|
fn print(s: str) { stdout().write_str(s); }
|
|
|
|
fn println(s: str) { stdout().write_str(s + "\n"); }
|
|
|
|
|
2011-07-10 14:47:51 -05:00
|
|
|
type str_writer =
|
|
|
|
obj {
|
2011-08-19 17:16:48 -05:00
|
|
|
fn get_writer() -> writer;
|
2011-09-02 17:34:58 -05:00
|
|
|
fn get_str() -> str;
|
2011-07-10 14:47:51 -05:00
|
|
|
};
|
|
|
|
|
2011-08-04 19:24:54 -05:00
|
|
|
type mutable_byte_buf = @{mutable buf: [mutable u8], mutable pos: uint};
|
2011-07-10 14:47:51 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
obj byte_buf_writer(buf: mutable_byte_buf) {
|
2011-09-12 04:27:30 -05:00
|
|
|
fn write(v: [u8]) {
|
2011-07-10 14:47:51 -05:00
|
|
|
// Fast path.
|
|
|
|
|
2011-08-15 18:38:23 -05:00
|
|
|
if buf.pos == vec::len(buf.buf) {
|
2011-08-19 17:16:48 -05:00
|
|
|
for b: u8 in v { buf.buf += [mutable b]; }
|
2011-08-12 12:56:57 -05:00
|
|
|
buf.pos += vec::len::<u8>(v);
|
2011-07-10 14:47:51 -05:00
|
|
|
ret;
|
|
|
|
}
|
|
|
|
// FIXME: Optimize: These should be unique pointers.
|
|
|
|
|
2011-08-12 12:56:57 -05:00
|
|
|
let vlen = vec::len::<u8>(v);
|
2011-07-27 07:19:39 -05:00
|
|
|
let vpos = 0u;
|
|
|
|
while vpos < vlen {
|
2011-08-19 17:16:48 -05:00
|
|
|
let b = v[vpos];
|
2011-08-15 18:38:23 -05:00
|
|
|
if buf.pos == vec::len(buf.buf) {
|
2011-08-19 17:16:48 -05:00
|
|
|
buf.buf += [mutable b];
|
|
|
|
} else { buf.buf[buf.pos] = b; }
|
2011-07-10 14:47:51 -05:00
|
|
|
buf.pos += 1u;
|
|
|
|
vpos += 1u;
|
|
|
|
}
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
fn seek(offset: int, whence: seek_style) {
|
|
|
|
let pos = buf.pos;
|
2011-08-15 18:38:23 -05:00
|
|
|
let len = vec::len(buf.buf);
|
2011-07-10 14:47:51 -05:00
|
|
|
buf.pos = seek_in_buf(offset, pos, len, whence);
|
|
|
|
}
|
|
|
|
fn tell() -> uint { ret buf.pos; }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn string_writer() -> str_writer {
|
|
|
|
// FIXME: yikes, this is bad. Needs fixing of mutable syntax.
|
|
|
|
|
2011-08-19 17:16:48 -05:00
|
|
|
let b: [mutable u8] = [mutable 0u8];
|
2011-08-15 18:38:23 -05:00
|
|
|
vec::pop(b);
|
2011-07-27 07:19:39 -05:00
|
|
|
let buf: mutable_byte_buf = @{mutable buf: b, mutable pos: 0u};
|
|
|
|
obj str_writer_wrap(wr: writer, buf: mutable_byte_buf) {
|
2011-07-10 14:47:51 -05:00
|
|
|
fn get_writer() -> writer { ret wr; }
|
2011-09-02 17:34:58 -05:00
|
|
|
fn get_str() -> str { ret str::unsafe_from_bytes(buf.buf); }
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
|
|
|
ret str_writer_wrap(new_writer(byte_buf_writer(buf)), buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Utility functions
|
2011-07-27 07:19:39 -05:00
|
|
|
fn seek_in_buf(offset: int, pos: uint, len: uint, whence: seek_style) ->
|
|
|
|
uint {
|
|
|
|
let bpos = pos as int;
|
|
|
|
let blen = len as int;
|
|
|
|
alt whence {
|
|
|
|
seek_set. { bpos = offset; }
|
|
|
|
seek_cur. { bpos += offset; }
|
|
|
|
seek_end. { bpos = blen + offset; }
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
2011-08-19 17:16:48 -05:00
|
|
|
if bpos < 0 { bpos = 0; } else if bpos > blen { bpos = blen; }
|
2011-07-10 14:47:51 -05:00
|
|
|
ret bpos as uint;
|
|
|
|
}
|
2011-08-01 17:53:14 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn read_whole_file_str(file: str) -> str {
|
2011-09-01 19:27:58 -05:00
|
|
|
str::unsafe_from_bytes(read_whole_file(file))
|
2011-08-01 17:53:14 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn read_whole_file(file: str) -> [u8] {
|
2011-08-19 17:16:48 -05:00
|
|
|
|
2011-08-01 17:53:14 -05:00
|
|
|
// FIXME: There's a lot of copying here
|
|
|
|
file_reader(file).read_whole_stream()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-07-10 14:47:51 -05:00
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
|
|
|
// End:
|
|
|
|
//
|