2011-12-06 15:58:45 -06:00
|
|
|
/*
|
|
|
|
Module: io
|
|
|
|
|
|
|
|
Basic input/output
|
|
|
|
*/
|
|
|
|
|
2012-03-16 17:14:37 -05:00
|
|
|
import result::result;
|
|
|
|
|
2012-03-12 22:04:27 -05:00
|
|
|
import libc::{c_int, c_uint, c_void, size_t, ssize_t};
|
|
|
|
import libc::consts::os::posix88::*;
|
|
|
|
import libc::consts::os::extra::*;
|
|
|
|
|
|
|
|
type fd_t = c_int;
|
2011-07-10 14:47:51 -05:00
|
|
|
|
2011-11-16 22:49:38 -06:00
|
|
|
#[abi = "cdecl"]
|
|
|
|
native mod rustrt {
|
2012-03-12 22:04:27 -05:00
|
|
|
fn rust_get_stdin() -> *libc::FILE;
|
|
|
|
fn rust_get_stdout() -> *libc::FILE;
|
|
|
|
fn rust_get_stderr() -> *libc::FILE;
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reading
|
|
|
|
|
|
|
|
// FIXME This is all buffered. We might need an unbuffered variant as well
|
2012-03-15 23:04:17 -05:00
|
|
|
// #2004
|
2012-01-19 19:55:34 -06:00
|
|
|
enum seek_style { seek_set, seek_end, seek_cur, }
|
2011-07-10 14:47:51 -05:00
|
|
|
|
|
|
|
|
2012-01-11 08:15:54 -06:00
|
|
|
// The raw underlying reader iface. All readers must implement this.
|
|
|
|
iface reader {
|
2012-03-15 23:04:17 -05:00
|
|
|
// FIXME: Seekable really should be orthogonal. // #2004
|
2012-01-11 08:15:54 -06:00
|
|
|
fn read_bytes(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
|
|
|
}
|
|
|
|
|
2012-01-11 08:15:54 -06:00
|
|
|
// Generic utility functions defined on readers
|
2011-07-29 06:31:44 -05:00
|
|
|
|
2012-01-11 08:15:54 -06:00
|
|
|
impl reader_util for reader {
|
2012-01-08 10:37:03 -06:00
|
|
|
fn read_chars(n: uint) -> [char] {
|
|
|
|
// returns the (consumed offset, n_req), appends characters to &chars
|
|
|
|
fn chars_from_buf(buf: [u8], &chars: [char]) -> (uint, uint) {
|
2012-03-12 22:04:27 -05:00
|
|
|
let mut i = 0u;
|
2012-01-08 10:37:03 -06:00
|
|
|
while i < vec::len(buf) {
|
|
|
|
let b0 = buf[i];
|
|
|
|
let w = str::utf8_char_width(b0);
|
|
|
|
let end = i + w;
|
|
|
|
i += 1u;
|
|
|
|
assert (w > 0u);
|
|
|
|
if w == 1u {
|
|
|
|
chars += [ b0 as char ];
|
|
|
|
cont;
|
|
|
|
}
|
|
|
|
// can't satisfy this char with the existing data
|
|
|
|
if end > vec::len(buf) {
|
|
|
|
ret (i - 1u, end - vec::len(buf));
|
|
|
|
}
|
2012-03-12 22:04:27 -05:00
|
|
|
let mut val = 0u;
|
2012-01-08 10:37:03 -06:00
|
|
|
while i < end {
|
|
|
|
let next = buf[i] as int;
|
|
|
|
i += 1u;
|
|
|
|
assert (next > -1);
|
|
|
|
assert (next & 192 == 128);
|
|
|
|
val <<= 6u;
|
2012-02-09 04:50:54 -06:00
|
|
|
val += (next & 63) as uint;
|
2012-01-08 10:37:03 -06:00
|
|
|
}
|
|
|
|
// See str::char_at
|
2012-02-09 04:50:54 -06:00
|
|
|
val += ((b0 << ((w + 1u) as u8)) as uint)
|
2012-01-09 06:49:17 -06:00
|
|
|
<< (w - 1u) * 6u - w - 1u;
|
2012-01-08 10:37:03 -06:00
|
|
|
chars += [ val as char ];
|
|
|
|
}
|
|
|
|
ret (i, 0u);
|
|
|
|
}
|
2012-03-12 22:04:27 -05:00
|
|
|
let mut buf: [u8] = [];
|
|
|
|
let mut chars: [char] = [];
|
2012-01-09 06:49:17 -06:00
|
|
|
// might need more bytes, but reading n will never over-read
|
2012-03-12 22:04:27 -05:00
|
|
|
let mut nbread = n;
|
2012-01-08 10:37:03 -06:00
|
|
|
while nbread > 0u {
|
2012-01-09 06:49:17 -06:00
|
|
|
let data = self.read_bytes(nbread);
|
2012-01-08 10:37:03 -06:00
|
|
|
if vec::len(data) == 0u {
|
2012-01-09 06:49:17 -06:00
|
|
|
// eof - FIXME should we do something if
|
2012-03-15 23:04:17 -05:00
|
|
|
// we're split in a unicode char? // #2004
|
2012-01-08 10:37:03 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf += data;
|
|
|
|
let (offset, nbreq) = chars_from_buf(buf, chars);
|
|
|
|
let ncreq = n - vec::len(chars);
|
2012-01-09 06:49:17 -06:00
|
|
|
// again we either know we need a certain number of bytes
|
|
|
|
// to complete a character, or we make sure we don't
|
|
|
|
// over-read by reading 1-byte per char needed
|
2012-01-08 10:37:03 -06:00
|
|
|
nbread = if ncreq > nbreq { ncreq } else { nbreq };
|
|
|
|
if nbread > 0u {
|
|
|
|
buf = vec::slice(buf, offset, vec::len(buf));
|
|
|
|
}
|
|
|
|
}
|
2012-01-11 08:15:54 -06:00
|
|
|
chars
|
2012-01-08 10:37:03 -06:00
|
|
|
}
|
2012-01-11 08:15:54 -06:00
|
|
|
|
2011-07-10 14:47:51 -05:00
|
|
|
fn read_char() -> char {
|
2012-01-08 10:37:03 -06:00
|
|
|
let c = self.read_chars(1u);
|
|
|
|
if vec::len(c) == 0u {
|
2012-03-15 23:04:17 -05:00
|
|
|
ret -1 as char; // FIXME will this stay valid? // #2004
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
2012-01-08 10:37:03 -06:00
|
|
|
assert(vec::len(c) == 1u);
|
|
|
|
ret c[0];
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
2012-01-11 08:15:54 -06:00
|
|
|
|
2011-09-02 17:34:58 -05:00
|
|
|
fn read_line() -> str {
|
2012-03-12 22:04:27 -05:00
|
|
|
let mut buf: [u8] = [];
|
|
|
|
while true {
|
2012-01-11 08:15:54 -06:00
|
|
|
let ch = self.read_byte();
|
|
|
|
if ch == -1 || ch == 10 { break; }
|
|
|
|
buf += [ch as u8];
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
2012-01-25 03:29:44 -06:00
|
|
|
str::from_bytes(buf)
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
2012-01-11 08:15:54 -06:00
|
|
|
|
2011-09-02 17:34:58 -05:00
|
|
|
fn read_c_str() -> str {
|
2012-03-12 22:04:27 -05:00
|
|
|
let mut buf: [u8] = [];
|
|
|
|
while true {
|
2012-01-11 08:15:54 -06:00
|
|
|
let ch = self.read_byte();
|
|
|
|
if ch < 1 { break; } else { buf += [ch as u8]; }
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
2012-01-25 03:29:44 -06:00
|
|
|
str::from_bytes(buf)
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
|
|
|
|
2012-03-15 23:04:17 -05:00
|
|
|
// FIXME deal with eof? // #2004
|
2011-07-27 07:19:39 -05:00
|
|
|
fn read_le_uint(size: uint) -> uint {
|
2012-03-12 22:04:27 -05:00
|
|
|
let mut val = 0u, pos = 0u, i = size;
|
2011-11-03 04:57:54 -05:00
|
|
|
while i > 0u {
|
2012-01-11 08:15:54 -06:00
|
|
|
val += (self.read_byte() as uint) << pos;
|
2011-07-10 14:47:51 -05:00
|
|
|
pos += 8u;
|
2011-11-03 04:57:54 -05:00
|
|
|
i -= 1u;
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
2012-01-11 08:15:54 -06:00
|
|
|
val
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
fn read_le_int(size: uint) -> int {
|
2012-03-12 22:04:27 -05:00
|
|
|
let mut val = 0u, pos = 0u, i = size;
|
2011-11-03 04:57:54 -05:00
|
|
|
while i > 0u {
|
2012-01-11 08:15:54 -06:00
|
|
|
val += (self.read_byte() as uint) << pos;
|
2011-07-10 14:47:51 -05:00
|
|
|
pos += 8u;
|
2011-11-03 04:57:54 -05:00
|
|
|
i -= 1u;
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
2012-01-11 08:15:54 -06:00
|
|
|
val as int
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
2012-01-11 08:15:54 -06:00
|
|
|
fn read_be_uint(size: uint) -> uint {
|
2012-03-12 22:04:27 -05:00
|
|
|
let mut val = 0u, i = size;
|
2011-11-03 04:57:54 -05:00
|
|
|
while i > 0u {
|
|
|
|
i -= 1u;
|
2012-01-11 08:15:54 -06:00
|
|
|
val += (self.read_byte() as uint) << i * 8u;
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
2012-01-11 08:15:54 -06:00
|
|
|
val
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
2012-01-11 08:15:54 -06:00
|
|
|
|
2011-08-04 19:24:54 -05:00
|
|
|
fn read_whole_stream() -> [u8] {
|
2012-03-12 22:04:27 -05:00
|
|
|
let mut buf: [u8] = [];
|
2012-01-11 08:15:54 -06:00
|
|
|
while !self.eof() { buf += self.read_bytes(2048u); }
|
|
|
|
buf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reader implementations
|
|
|
|
|
|
|
|
fn convert_whence(whence: seek_style) -> i32 {
|
|
|
|
ret alt whence {
|
2012-01-19 00:37:22 -06:00
|
|
|
seek_set { 0i32 }
|
|
|
|
seek_cur { 1i32 }
|
|
|
|
seek_end { 2i32 }
|
2012-01-11 08:15:54 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-03-12 22:04:27 -05:00
|
|
|
impl of reader for *libc::FILE {
|
2012-01-11 08:15:54 -06:00
|
|
|
fn read_bytes(len: uint) -> [u8] unsafe {
|
2012-03-26 20:35:18 -05:00
|
|
|
let mut buf : [mut u8] = [mut];
|
2012-01-11 08:15:54 -06:00
|
|
|
vec::reserve(buf, len);
|
2012-03-12 22:04:27 -05:00
|
|
|
vec::as_mut_buf(buf) {|b|
|
2012-03-26 20:35:18 -05:00
|
|
|
let read = libc::fread(b as *mut c_void, 1u,
|
2012-03-12 22:04:27 -05:00
|
|
|
len, self);
|
|
|
|
vec::unsafe::set_len(buf, read);
|
|
|
|
}
|
|
|
|
ret vec::from_mut(buf);
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
2012-03-12 22:04:27 -05:00
|
|
|
fn read_byte() -> int { ret libc::fgetc(self) as int; }
|
|
|
|
fn unread_byte(byte: int) { libc::ungetc(byte as c_int, self); }
|
|
|
|
fn eof() -> bool { ret libc::feof(self) != 0 as c_int; }
|
2012-01-11 08:15:54 -06:00
|
|
|
fn seek(offset: int, whence: seek_style) {
|
2012-03-12 22:04:27 -05:00
|
|
|
assert libc::fseek(self, offset, convert_whence(whence))
|
|
|
|
== 0 as c_int;
|
2012-01-11 08:15:54 -06:00
|
|
|
}
|
2012-03-12 22:04:27 -05:00
|
|
|
fn tell() -> uint { ret libc::ftell(self) as uint; }
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
|
|
|
|
2012-01-11 08:15:54 -06:00
|
|
|
// A forwarding impl of reader that also holds on to a resource for the
|
|
|
|
// duration of its lifetime.
|
2012-03-15 23:04:17 -05:00
|
|
|
// FIXME there really should be a better way to do this // #2004
|
2012-01-11 08:15:54 -06:00
|
|
|
impl <T: reader, C> of reader for {base: T, cleanup: C} {
|
|
|
|
fn read_bytes(len: uint) -> [u8] { self.base.read_bytes(len) }
|
|
|
|
fn read_byte() -> int { self.base.read_byte() }
|
|
|
|
fn unread_byte(byte: int) { self.base.unread_byte(byte); }
|
|
|
|
fn eof() -> bool { self.base.eof() }
|
|
|
|
fn seek(off: int, whence: seek_style) { self.base.seek(off, whence) }
|
|
|
|
fn tell() -> uint { self.base.tell() }
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
|
|
|
|
2012-03-12 22:04:27 -05:00
|
|
|
resource FILE_res(f: *libc::FILE) { libc::fclose(f); }
|
2012-01-11 08:15:54 -06:00
|
|
|
|
2012-03-12 22:04:27 -05:00
|
|
|
fn FILE_reader(f: *libc::FILE, cleanup: bool) -> reader {
|
2012-01-11 08:15:54 -06:00
|
|
|
if cleanup {
|
|
|
|
{base: f, cleanup: FILE_res(f)} as reader
|
|
|
|
} else {
|
|
|
|
f as reader
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: this should either be an iface-less impl, a set of top-level
|
|
|
|
// functions that take a reader, or a set of default methods on reader
|
2012-03-15 23:04:17 -05:00
|
|
|
// (which can then be called reader) // #2004
|
2012-01-11 08:15:54 -06:00
|
|
|
|
|
|
|
fn stdin() -> reader { rustrt::rust_get_stdin() as reader }
|
|
|
|
|
2012-03-13 16:39:28 -05:00
|
|
|
fn file_reader(path: str) -> result<reader, str> {
|
2012-03-12 22:04:27 -05:00
|
|
|
let f = os::as_c_charp(path, {|pathbuf|
|
|
|
|
os::as_c_charp("r", {|modebuf|
|
|
|
|
libc::fopen(pathbuf, modebuf)
|
2011-10-06 05:26:12 -05:00
|
|
|
})
|
|
|
|
});
|
2011-10-28 23:19:59 -05:00
|
|
|
ret if f as uint == 0u { result::err("error opening " + path) }
|
|
|
|
else {
|
2012-01-11 08:15:54 -06:00
|
|
|
result::ok(FILE_reader(f, true))
|
2011-10-28 23:19:59 -05:00
|
|
|
}
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Byte buffer readers
|
|
|
|
|
2011-11-16 16:41:32 -06:00
|
|
|
// TODO: const u8, but this fails with rustboot.
|
2012-03-26 20:35:18 -05:00
|
|
|
type byte_buf = {buf: [u8], mut pos: uint, len: uint};
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2012-01-11 08:15:54 -06:00
|
|
|
impl of reader for byte_buf {
|
|
|
|
fn read_bytes(len: uint) -> [u8] {
|
2012-02-29 12:48:57 -06:00
|
|
|
let rest = self.len - self.pos;
|
2012-03-12 22:04:27 -05:00
|
|
|
let mut to_read = len;
|
2011-07-27 07:19:39 -05:00
|
|
|
if rest < to_read { to_read = rest; }
|
2012-01-11 08:15:54 -06:00
|
|
|
let range = vec::slice(self.buf, self.pos, self.pos + to_read);
|
|
|
|
self.pos += to_read;
|
2011-07-10 14:47:51 -05:00
|
|
|
ret range;
|
|
|
|
}
|
|
|
|
fn read_byte() -> int {
|
2012-02-29 12:48:57 -06:00
|
|
|
if self.pos == self.len { ret -1; }
|
2012-01-11 08:15:54 -06:00
|
|
|
let b = self.buf[self.pos];
|
|
|
|
self.pos += 1u;
|
2011-07-10 14:47:51 -05:00
|
|
|
ret b as int;
|
|
|
|
}
|
2011-12-22 16:42:52 -06:00
|
|
|
fn unread_byte(_byte: int) { #error("TODO: unread_byte"); fail; }
|
2012-02-29 12:48:57 -06:00
|
|
|
fn eof() -> bool { self.pos == self.len }
|
2011-07-27 07:19:39 -05:00
|
|
|
fn seek(offset: int, whence: seek_style) {
|
2012-01-11 08:15:54 -06:00
|
|
|
let pos = self.pos;
|
2012-02-29 12:48:57 -06:00
|
|
|
self.pos = seek_in_buf(offset, pos, self.len, whence);
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
2012-01-11 08:15:54 -06:00
|
|
|
fn tell() -> uint { self.pos }
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
|
|
|
|
2011-12-28 02:30:04 -06:00
|
|
|
fn bytes_reader(bytes: [u8]) -> reader {
|
2012-02-29 12:48:57 -06:00
|
|
|
bytes_reader_between(bytes, 0u, vec::len(bytes))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bytes_reader_between(bytes: [u8], start: uint, end: uint) -> reader {
|
2012-03-26 20:35:18 -05:00
|
|
|
{buf: bytes, mut pos: start, len: end} as reader
|
2012-02-29 12:48:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn with_bytes_reader<t>(bytes: [u8], f: fn(reader) -> t) -> t {
|
|
|
|
f(bytes_reader(bytes))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn with_bytes_reader_between<t>(bytes: [u8], start: uint, end: uint,
|
|
|
|
f: fn(reader) -> t) -> t {
|
|
|
|
f(bytes_reader_between(bytes, start, end))
|
2011-12-28 02:30:04 -06:00
|
|
|
}
|
|
|
|
|
2012-02-29 23:14:04 -06:00
|
|
|
fn str_reader(s: str) -> reader {
|
2012-01-11 08:15:54 -06:00
|
|
|
bytes_reader(str::bytes(s))
|
2011-07-25 16:04:01 -05:00
|
|
|
}
|
|
|
|
|
2012-02-29 12:48:57 -06:00
|
|
|
fn with_str_reader<T>(s: str, f: fn(reader) -> T) -> T {
|
|
|
|
str::as_bytes(s) { |bytes|
|
|
|
|
with_bytes_reader_between(bytes, 0u, str::len(s), f)
|
|
|
|
}
|
|
|
|
}
|
2011-07-10 14:47:51 -05:00
|
|
|
|
|
|
|
// Writing
|
2012-02-09 08:30:19 -06:00
|
|
|
enum fileflag { append, create, truncate, no_flag, }
|
2011-07-10 14:47:51 -05:00
|
|
|
|
2012-01-11 08:15:54 -06:00
|
|
|
// FIXME: Seekable really should be orthogonal.
|
|
|
|
// FIXME: eventually u64
|
2012-03-15 23:04:17 -05:00
|
|
|
// #2004
|
2012-01-11 08:15:54 -06:00
|
|
|
iface writer {
|
|
|
|
fn write([const u8]);
|
|
|
|
fn seek(int, seek_style);
|
|
|
|
fn tell() -> uint;
|
|
|
|
fn flush() -> int;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl <T: writer, C> of writer for {base: T, cleanup: C} {
|
|
|
|
fn write(bs: [const u8]) { self.base.write(bs); }
|
|
|
|
fn seek(off: int, style: seek_style) { self.base.seek(off, style); }
|
|
|
|
fn tell() -> uint { self.base.tell() }
|
|
|
|
fn flush() -> int { self.base.flush() }
|
|
|
|
}
|
2011-07-10 14:47:51 -05:00
|
|
|
|
2012-03-12 22:04:27 -05:00
|
|
|
impl of writer for *libc::FILE {
|
2011-12-27 18:38:48 -06:00
|
|
|
fn write(v: [const u8]) unsafe {
|
2012-01-11 08:15:54 -06:00
|
|
|
let len = vec::len(v);
|
2012-03-12 22:04:27 -05:00
|
|
|
vec::as_buf(v) {|vbuf|
|
|
|
|
let nout = libc::fwrite(vbuf as *c_void, len, 1u, self);
|
|
|
|
if nout < 1 as size_t {
|
|
|
|
#error("error writing buffer");
|
|
|
|
log(error, sys::last_os_error());
|
|
|
|
fail;
|
|
|
|
}
|
|
|
|
}
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
fn seek(offset: int, whence: seek_style) {
|
2012-03-12 22:04:27 -05:00
|
|
|
assert libc::fseek(self, offset, convert_whence(whence))
|
|
|
|
== 0 as c_int;
|
2011-11-16 16:14:18 -06:00
|
|
|
}
|
2012-03-12 22:04:27 -05:00
|
|
|
fn tell() -> uint { libc::ftell(self) as uint }
|
|
|
|
fn flush() -> int { libc::fflush(self) as int }
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
|
|
|
|
2012-03-12 22:04:27 -05:00
|
|
|
fn FILE_writer(f: *libc::FILE, cleanup: bool) -> writer {
|
2012-01-11 08:15:54 -06:00
|
|
|
if cleanup {
|
|
|
|
{base: f, cleanup: FILE_res(f)} as writer
|
|
|
|
} else {
|
|
|
|
f as writer
|
|
|
|
}
|
|
|
|
}
|
2011-07-29 06:31:44 -05:00
|
|
|
|
2012-01-11 08:15:54 -06:00
|
|
|
impl of writer for fd_t {
|
2011-12-27 18:38:48 -06:00
|
|
|
fn write(v: [const u8]) unsafe {
|
2012-01-11 08:15:54 -06:00
|
|
|
let len = vec::len(v);
|
2012-03-12 22:04:27 -05:00
|
|
|
let mut count = 0u;
|
|
|
|
vec::as_buf(v) {|vbuf|
|
|
|
|
while count < len {
|
|
|
|
let vb = ptr::offset(vbuf, count) as *c_void;
|
|
|
|
let nout = libc::write(self, vb, len);
|
|
|
|
if nout < 0 as ssize_t {
|
|
|
|
#error("error writing buffer");
|
|
|
|
log(error, sys::last_os_error());
|
|
|
|
fail;
|
|
|
|
}
|
|
|
|
count += nout as uint;
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-08-18 03:01:39 -05:00
|
|
|
fn seek(_offset: int, _whence: seek_style) {
|
2011-12-22 16:42:52 -06:00
|
|
|
#error("need 64-bit native calls for seek, sorry");
|
2011-07-10 14:47:51 -05:00
|
|
|
fail;
|
|
|
|
}
|
|
|
|
fn tell() -> uint {
|
2011-12-22 16:42:52 -06:00
|
|
|
#error("need 64-bit native calls for tell, sorry");
|
2011-07-10 14:47:51 -05:00
|
|
|
fail;
|
2011-07-27 10:05:34 -05:00
|
|
|
}
|
2012-01-11 08:15:54 -06:00
|
|
|
fn flush() -> int { 0 }
|
|
|
|
}
|
2011-11-16 16:14:18 -06:00
|
|
|
|
2012-03-12 22:04:27 -05:00
|
|
|
resource fd_res(fd: fd_t) { libc::close(fd); }
|
2011-11-16 16:14:18 -06:00
|
|
|
|
2012-01-11 08:15:54 -06:00
|
|
|
fn fd_writer(fd: fd_t, cleanup: bool) -> writer {
|
|
|
|
if cleanup {
|
|
|
|
{base: fd, cleanup: fd_res(fd)} as writer
|
|
|
|
} else {
|
|
|
|
fd as writer
|
2011-11-16 16:14:18 -06:00
|
|
|
}
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
|
|
|
|
2012-03-12 22:04:27 -05:00
|
|
|
|
2012-01-11 08:15:54 -06:00
|
|
|
fn mk_file_writer(path: str, flags: [fileflag])
|
2012-03-13 16:39:28 -05:00
|
|
|
-> result<writer, str> {
|
2012-03-12 22:04:27 -05:00
|
|
|
|
|
|
|
#[cfg(target_os = "win32")]
|
|
|
|
fn wb() -> c_int { (O_WRONLY | O_BINARY) as c_int }
|
|
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
#[cfg(target_os = "freebsd")]
|
|
|
|
fn wb() -> c_int { O_WRONLY as c_int }
|
|
|
|
|
|
|
|
let mut fflags: c_int = wb();
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(flags) {|f|
|
2011-07-27 07:19:39 -05:00
|
|
|
alt f {
|
2012-03-12 22:04:27 -05:00
|
|
|
append { fflags |= O_APPEND as c_int; }
|
|
|
|
create { fflags |= O_CREAT as c_int; }
|
|
|
|
truncate { fflags |= O_TRUNC as c_int; }
|
2012-02-09 08:30:19 -06:00
|
|
|
no_flag { }
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
|
|
|
}
|
2012-03-12 22:04:27 -05:00
|
|
|
let fd = os::as_c_charp(path) {|pathbuf|
|
|
|
|
libc::open(pathbuf, fflags,
|
|
|
|
(S_IRUSR | S_IWUSR) as c_int)
|
|
|
|
};
|
|
|
|
if fd < (0 as c_int) {
|
2012-03-15 22:44:47 -05:00
|
|
|
result::err(#fmt("error opening %s: %s", path, sys::last_os_error()))
|
2011-10-28 23:19:59 -05:00
|
|
|
} else {
|
2012-01-11 08:15:54 -06:00
|
|
|
result::ok(fd_writer(fd, true))
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-14 17:21:53 -06:00
|
|
|
fn u64_to_le_bytes(n: u64, size: uint) -> [u8] {
|
2012-03-12 22:04:27 -05:00
|
|
|
let mut bytes: [u8] = [], i = size, n = n;
|
2012-02-14 17:21:53 -06:00
|
|
|
while i > 0u {
|
|
|
|
bytes += [(n & 255_u64) as u8];
|
|
|
|
n >>= 8_u64;
|
|
|
|
i -= 1u;
|
|
|
|
}
|
2011-07-10 14:47:51 -05:00
|
|
|
ret bytes;
|
|
|
|
}
|
|
|
|
|
2012-02-14 17:21:53 -06:00
|
|
|
fn u64_to_be_bytes(n: u64, size: uint) -> [u8] {
|
|
|
|
assert size <= 8u;
|
2012-03-12 22:04:27 -05:00
|
|
|
let mut bytes: [u8] = [];
|
|
|
|
let mut i = size;
|
2012-02-14 17:21:53 -06:00
|
|
|
while i > 0u {
|
|
|
|
let shift = ((i - 1u) * 8u) as u64;
|
|
|
|
bytes += [(n >> shift) as u8];
|
|
|
|
i -= 1u;
|
|
|
|
}
|
2011-07-10 14:47:51 -05:00
|
|
|
ret bytes;
|
|
|
|
}
|
|
|
|
|
2012-02-14 17:21:53 -06:00
|
|
|
fn u64_from_be_bytes(data: [u8], start: uint, size: uint) -> u64 {
|
2012-03-12 22:04:27 -05:00
|
|
|
let mut sz = size;
|
2012-02-14 17:21:53 -06:00
|
|
|
assert (sz <= 8u);
|
2012-03-12 22:04:27 -05:00
|
|
|
let mut val = 0_u64;
|
|
|
|
let mut pos = start;
|
2012-02-14 17:21:53 -06:00
|
|
|
while sz > 0u {
|
|
|
|
sz -= 1u;
|
|
|
|
val += (data[pos] as u64) << ((sz * 8u) as u64);
|
|
|
|
pos += 1u;
|
|
|
|
}
|
|
|
|
ret val;
|
|
|
|
}
|
|
|
|
|
2012-01-11 08:15:54 -06:00
|
|
|
impl writer_util for writer {
|
2011-07-27 07:19:39 -05:00
|
|
|
fn write_char(ch: char) {
|
2012-01-11 08:15:54 -06:00
|
|
|
if ch as uint < 128u {
|
|
|
|
self.write([ch as u8]);
|
|
|
|
} else {
|
|
|
|
self.write(str::bytes(str::from_char(ch)));
|
|
|
|
}
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
2012-01-11 08:15:54 -06:00
|
|
|
fn write_str(s: str) { self.write(str::bytes(s)); }
|
|
|
|
fn write_line(s: str) { self.write(str::bytes(s + "\n")); }
|
|
|
|
fn write_int(n: int) { self.write(str::bytes(int::to_str(n, 10u))); }
|
|
|
|
fn write_uint(n: uint) { self.write(str::bytes(uint::to_str(n, 10u))); }
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn write_le_uint(n: uint, size: uint) {
|
2012-02-14 17:21:53 -06:00
|
|
|
self.write(u64_to_le_bytes(n as u64, size));
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
fn write_le_int(n: int, size: uint) {
|
2012-02-14 17:21:53 -06:00
|
|
|
self.write(u64_to_le_bytes(n as u64, size));
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
2012-02-14 17:21:53 -06:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn write_be_uint(n: uint, size: uint) {
|
2012-02-14 17:21:53 -06:00
|
|
|
self.write(u64_to_be_bytes(n as u64, size));
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
2012-02-14 17:21:53 -06:00
|
|
|
fn write_be_int(n: int, size: uint) {
|
|
|
|
self.write(u64_to_be_bytes(n as u64, size));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_be_u64(n: u64) { self.write(u64_to_be_bytes(n, 8u)); }
|
|
|
|
fn write_be_u32(n: u32) { self.write(u64_to_be_bytes(n as u64, 4u)); }
|
|
|
|
fn write_be_u16(n: u16) { self.write(u64_to_be_bytes(n as u64, 2u)); }
|
|
|
|
|
|
|
|
fn write_be_i64(n: i64) { self.write(u64_to_be_bytes(n as u64, 8u)); }
|
|
|
|
fn write_be_i32(n: i32) { self.write(u64_to_be_bytes(n as u64, 4u)); }
|
|
|
|
fn write_be_i16(n: i16) { self.write(u64_to_be_bytes(n as u64, 2u)); }
|
|
|
|
|
|
|
|
fn write_le_u64(n: u64) { self.write(u64_to_le_bytes(n, 8u)); }
|
|
|
|
fn write_le_u32(n: u32) { self.write(u64_to_le_bytes(n as u64, 4u)); }
|
|
|
|
fn write_le_u16(n: u16) { self.write(u64_to_le_bytes(n as u64, 2u)); }
|
|
|
|
|
|
|
|
fn write_le_i64(n: i64) { self.write(u64_to_le_bytes(n as u64, 8u)); }
|
|
|
|
fn write_le_i32(n: i32) { self.write(u64_to_le_bytes(n as u64, 4u)); }
|
|
|
|
fn write_le_i16(n: i16) { self.write(u64_to_le_bytes(n as u64, 2u)); }
|
|
|
|
|
|
|
|
fn write_u8(n: u8) { self.write([n]) }
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
|
|
|
|
2012-03-13 16:39:28 -05:00
|
|
|
fn file_writer(path: str, flags: [fileflag]) -> result<writer, str> {
|
2012-01-11 08:15:54 -06:00
|
|
|
result::chain(mk_file_writer(path, flags), { |w| result::ok(w)})
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-15 23:04:17 -05:00
|
|
|
// FIXME: fileflags // #2004
|
2012-03-13 16:39:28 -05:00
|
|
|
fn buffered_file_writer(path: str) -> result<writer, str> {
|
2012-03-12 22:04:27 -05:00
|
|
|
let f = os::as_c_charp(path) {|pathbuf|
|
|
|
|
os::as_c_charp("w") {|modebuf|
|
|
|
|
libc::fopen(pathbuf, modebuf)
|
|
|
|
}
|
|
|
|
};
|
2011-10-28 23:19:59 -05:00
|
|
|
ret if f as uint == 0u { result::err("error opening " + path) }
|
2012-01-11 08:15:54 -06:00
|
|
|
else { result::ok(FILE_writer(f, true)) }
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME it would be great if this could be a const
|
2012-03-12 22:04:27 -05:00
|
|
|
// FIXME why are these different from the way stdin() is implemented?
|
2012-03-15 23:04:17 -05:00
|
|
|
// #2004
|
2012-03-12 22:04:27 -05:00
|
|
|
fn stdout() -> writer { fd_writer(libc::STDOUT_FILENO as c_int, false) }
|
|
|
|
fn stderr() -> writer { fd_writer(libc::STDERR_FILENO as c_int, false) }
|
2011-07-10 14:47:51 -05:00
|
|
|
|
2011-10-14 05:44:40 -05:00
|
|
|
fn print(s: str) { stdout().write_str(s); }
|
2012-01-11 08:15:54 -06:00
|
|
|
fn println(s: str) { stdout().write_line(s); }
|
2011-12-28 02:30:04 -06:00
|
|
|
|
2012-03-26 20:35:18 -05:00
|
|
|
type mem_buffer = @{mut buf: [mut u8],
|
|
|
|
mut pos: uint};
|
2011-07-10 14:47:51 -05:00
|
|
|
|
2012-01-11 08:15:54 -06:00
|
|
|
impl of writer for mem_buffer {
|
2011-12-27 18:38:48 -06:00
|
|
|
fn write(v: [const u8]) {
|
2011-07-10 14:47:51 -05:00
|
|
|
// Fast path.
|
2012-01-11 08:15:54 -06:00
|
|
|
if self.pos == vec::len(self.buf) {
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(v) {|b| self.buf += [mut b]; }
|
2012-01-11 08:15:54 -06:00
|
|
|
self.pos += vec::len(v);
|
2011-07-10 14:47:51 -05:00
|
|
|
ret;
|
|
|
|
}
|
2012-03-15 23:04:17 -05:00
|
|
|
// FIXME: Optimize: These should be unique pointers. // #2004
|
2012-01-11 08:15:54 -06:00
|
|
|
let vlen = vec::len(v);
|
2012-03-12 22:04:27 -05:00
|
|
|
let mut vpos = 0u;
|
2011-07-27 07:19:39 -05:00
|
|
|
while vpos < vlen {
|
2011-08-19 17:16:48 -05:00
|
|
|
let b = v[vpos];
|
2012-01-11 08:15:54 -06:00
|
|
|
if self.pos == vec::len(self.buf) {
|
2012-03-26 20:35:18 -05:00
|
|
|
self.buf += [mut b];
|
2012-01-11 08:15:54 -06:00
|
|
|
} else { self.buf[self.pos] = b; }
|
|
|
|
self.pos += 1u;
|
2011-07-10 14:47:51 -05:00
|
|
|
vpos += 1u;
|
|
|
|
}
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
fn seek(offset: int, whence: seek_style) {
|
2012-01-11 08:15:54 -06:00
|
|
|
let pos = self.pos;
|
|
|
|
let len = vec::len(self.buf);
|
|
|
|
self.pos = seek_in_buf(offset, pos, len, whence);
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
2012-01-11 08:15:54 -06:00
|
|
|
fn tell() -> uint { self.pos }
|
|
|
|
fn flush() -> int { 0 }
|
2011-07-10 14:47:51 -05:00
|
|
|
}
|
|
|
|
|
2012-03-13 09:55:45 -05:00
|
|
|
fn mem_buffer() -> mem_buffer {
|
2012-03-26 20:35:18 -05:00
|
|
|
@{mut buf: [mut], mut pos: 0u}
|
2011-12-28 02:30:04 -06:00
|
|
|
}
|
2012-01-11 08:15:54 -06:00
|
|
|
fn mem_buffer_writer(b: mem_buffer) -> writer { b as writer }
|
|
|
|
fn mem_buffer_buf(b: mem_buffer) -> [u8] { vec::from_mut(b.buf) }
|
2012-01-25 03:41:23 -06:00
|
|
|
fn mem_buffer_str(b: mem_buffer) -> str {
|
2012-02-21 11:23:01 -06:00
|
|
|
let b_ = vec::from_mut(b.buf);
|
|
|
|
str::from_bytes(b_)
|
|
|
|
}
|
2012-02-21 11:22:50 -06:00
|
|
|
|
|
|
|
fn with_str_writer(f: fn(writer)) -> str {
|
2012-03-13 09:55:45 -05:00
|
|
|
let buf = mem_buffer();
|
2012-02-21 11:22:50 -06:00
|
|
|
let wr = mem_buffer_writer(buf);
|
|
|
|
f(wr);
|
|
|
|
io::mem_buffer_str(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn with_buf_writer(f: fn(writer)) -> [u8] {
|
2012-03-13 09:55:45 -05:00
|
|
|
let buf = mem_buffer();
|
2012-02-21 11:22:50 -06:00
|
|
|
let wr = mem_buffer_writer(buf);
|
|
|
|
f(wr);
|
|
|
|
io::mem_buffer_buf(buf)
|
2012-01-25 03:41:23 -06:00
|
|
|
}
|
2011-07-10 14:47:51 -05:00
|
|
|
|
|
|
|
// Utility functions
|
2011-07-27 07:19:39 -05:00
|
|
|
fn seek_in_buf(offset: int, pos: uint, len: uint, whence: seek_style) ->
|
|
|
|
uint {
|
2012-03-12 22:04:27 -05:00
|
|
|
let mut bpos = pos as int;
|
2011-07-27 07:19:39 -05:00
|
|
|
let blen = len as int;
|
|
|
|
alt whence {
|
2012-01-19 00:37:22 -06:00
|
|
|
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
|
|
|
|
2012-03-13 16:39:28 -05:00
|
|
|
fn read_whole_file_str(file: str) -> result<str, str> {
|
2011-10-28 23:19:59 -05:00
|
|
|
result::chain(read_whole_file(file), { |bytes|
|
2012-01-25 03:29:44 -06:00
|
|
|
result::ok(str::from_bytes(bytes))
|
2011-10-28 23:19:59 -05:00
|
|
|
})
|
2011-08-01 17:53:14 -05:00
|
|
|
}
|
|
|
|
|
2012-01-11 08:15:54 -06:00
|
|
|
// FIXME implement this in a low-level way. Going through the abstractions is
|
2012-03-15 23:04:17 -05:00
|
|
|
// pointless. // #2004
|
2012-03-13 16:39:28 -05:00
|
|
|
fn read_whole_file(file: str) -> result<[u8], str> {
|
2011-10-28 23:19:59 -05:00
|
|
|
result::chain(file_reader(file), { |rdr|
|
|
|
|
result::ok(rdr.read_whole_stream())
|
|
|
|
})
|
2011-08-01 17:53:14 -05:00
|
|
|
}
|
|
|
|
|
2011-11-16 16:14:18 -06:00
|
|
|
// fsync related
|
|
|
|
|
|
|
|
mod fsync {
|
|
|
|
|
2012-01-19 17:20:57 -06:00
|
|
|
enum level {
|
2011-11-16 16:14:18 -06:00
|
|
|
// whatever fsync does on that platform
|
2012-01-19 19:55:34 -06:00
|
|
|
fsync,
|
2011-11-16 16:14:18 -06:00
|
|
|
|
|
|
|
// fdatasync on linux, similiar or more on other platforms
|
2012-01-19 19:55:34 -06:00
|
|
|
fdatasync,
|
2011-11-16 16:14:18 -06:00
|
|
|
|
|
|
|
// full fsync
|
|
|
|
//
|
|
|
|
// You must additionally sync the parent directory as well!
|
2012-01-19 19:55:34 -06:00
|
|
|
fullfsync,
|
2011-11-16 16:14:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Resource of artifacts that need to fsync on destruction
|
|
|
|
resource res<t>(arg: arg<t>) {
|
|
|
|
alt arg.opt_level {
|
2012-01-19 00:37:22 -06:00
|
|
|
option::none { }
|
2012-01-11 08:15:54 -06:00
|
|
|
option::some(level) {
|
2011-11-16 16:14:18 -06:00
|
|
|
// fail hard if not succesful
|
|
|
|
assert(arg.fsync_fn(arg.val, level) != -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type arg<t> = {
|
|
|
|
val: t,
|
2012-01-31 19:05:20 -06:00
|
|
|
opt_level: option<level>,
|
2012-01-11 11:58:05 -06:00
|
|
|
fsync_fn: fn@(t, level) -> int
|
2011-11-16 16:14:18 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
// fsync file after executing blk
|
|
|
|
// FIXME find better way to create resources within lifetime of outer res
|
2012-03-15 23:04:17 -05:00
|
|
|
// #2004
|
2012-01-31 19:05:20 -06:00
|
|
|
fn FILE_res_sync(&&file: FILE_res, opt_level: option<level>,
|
2012-03-12 22:04:27 -05:00
|
|
|
blk: fn(&&res<*libc::FILE>)) {
|
2011-11-16 16:14:18 -06:00
|
|
|
blk(res({
|
|
|
|
val: *file, opt_level: opt_level,
|
2012-03-12 22:04:27 -05:00
|
|
|
fsync_fn: fn@(&&file: *libc::FILE, l: level) -> int {
|
|
|
|
ret os::fsync_fd(libc::fileno(file), l) as int;
|
2011-11-16 16:14:18 -06:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// fsync fd after executing blk
|
2012-01-31 19:05:20 -06:00
|
|
|
fn fd_res_sync(&&fd: fd_res, opt_level: option<level>,
|
2012-01-23 16:59:00 -06:00
|
|
|
blk: fn(&&res<fd_t>)) {
|
2011-11-16 16:14:18 -06:00
|
|
|
blk(res({
|
|
|
|
val: *fd, opt_level: opt_level,
|
2012-01-11 11:58:05 -06:00
|
|
|
fsync_fn: fn@(&&fd: fd_t, l: level) -> int {
|
2011-11-16 16:14:18 -06:00
|
|
|
ret os::fsync_fd(fd, l) as int;
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type of objects that may want to fsync
|
2012-01-11 08:15:54 -06:00
|
|
|
iface t { fn fsync(l: level) -> int; }
|
2011-11-16 16:14:18 -06:00
|
|
|
|
|
|
|
// Call o.fsync after executing blk
|
2012-01-31 19:05:20 -06:00
|
|
|
fn obj_sync(&&o: t, opt_level: option<level>, blk: fn(&&res<t>)) {
|
2011-11-16 16:14:18 -06:00
|
|
|
blk(res({
|
|
|
|
val: o, opt_level: opt_level,
|
2012-01-11 11:58:05 -06:00
|
|
|
fsync_fn: fn@(&&o: t, l: level) -> int { ret o.fsync(l); }
|
2011-11-16 16:14:18 -06:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
2011-08-01 17:53:14 -05:00
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_simple() {
|
|
|
|
let tmpfile: str = "tmp/lib-io-test-simple.tmp";
|
|
|
|
log(debug, tmpfile);
|
|
|
|
let frood: str = "A hoopy frood who really knows where his towel is.";
|
|
|
|
log(debug, frood);
|
|
|
|
{
|
|
|
|
let out: io::writer =
|
|
|
|
result::get(
|
|
|
|
io::file_writer(tmpfile, [io::create, io::truncate]));
|
|
|
|
out.write_str(frood);
|
|
|
|
}
|
|
|
|
let inp: io::reader = result::get(io::file_reader(tmpfile));
|
|
|
|
let frood2: str = inp.read_c_str();
|
|
|
|
log(debug, frood2);
|
|
|
|
assert (str::eq(frood, frood2));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_readchars_empty() {
|
2012-02-29 23:14:04 -06:00
|
|
|
let inp : io::reader = io::str_reader("");
|
2012-01-17 21:05:07 -06:00
|
|
|
let res : [char] = inp.read_chars(128u);
|
|
|
|
assert(vec::len(res) == 0u);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_readchars_wide() {
|
|
|
|
let wide_test = "生锈的汤匙切肉汤hello生锈的汤匙切肉汤";
|
|
|
|
let ivals : [int] = [
|
|
|
|
29983, 38152, 30340, 27748,
|
|
|
|
21273, 20999, 32905, 27748,
|
|
|
|
104, 101, 108, 108, 111,
|
|
|
|
29983, 38152, 30340, 27748,
|
|
|
|
21273, 20999, 32905, 27748];
|
|
|
|
fn check_read_ln(len : uint, s: str, ivals: [int]) {
|
2012-02-29 23:14:04 -06:00
|
|
|
let inp : io::reader = io::str_reader(s);
|
2012-01-17 21:05:07 -06:00
|
|
|
let res : [char] = inp.read_chars(len);
|
|
|
|
if (len <= vec::len(ivals)) {
|
|
|
|
assert(vec::len(res) == len);
|
|
|
|
}
|
|
|
|
assert(vec::slice(ivals, 0u, vec::len(res)) ==
|
|
|
|
vec::map(res, {|x| x as int}));
|
|
|
|
}
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut i = 0u;
|
2012-01-17 21:05:07 -06:00
|
|
|
while i < 8u {
|
|
|
|
check_read_ln(i, wide_test, ivals);
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
// check a long read for good measure
|
|
|
|
check_read_ln(128u, wide_test, ivals);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_readchar() {
|
2012-02-29 23:14:04 -06:00
|
|
|
let inp : io::reader = io::str_reader("生");
|
2012-01-17 21:05:07 -06:00
|
|
|
let res : char = inp.read_char();
|
|
|
|
assert(res as int == 29983);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_readchar_empty() {
|
2012-02-29 23:14:04 -06:00
|
|
|
let inp : io::reader = io::str_reader("");
|
2012-01-17 21:05:07 -06:00
|
|
|
let res : char = inp.read_char();
|
|
|
|
assert(res as int == -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn file_reader_not_exist() {
|
|
|
|
alt io::file_reader("not a file") {
|
|
|
|
result::err(e) {
|
|
|
|
assert e == "error opening not a file";
|
|
|
|
}
|
|
|
|
result::ok(_) { fail; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn file_writer_bad_name() {
|
|
|
|
alt io::file_writer("?/?", []) {
|
|
|
|
result::err(e) {
|
2012-03-20 13:58:56 -05:00
|
|
|
assert str::starts_with(e, "error opening ?/?");
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
result::ok(_) { fail; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn buffered_file_writer_bad_name() {
|
|
|
|
alt io::buffered_file_writer("?/?") {
|
|
|
|
result::err(e) {
|
|
|
|
assert e == "error opening ?/?";
|
|
|
|
}
|
|
|
|
result::ok(_) { fail; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-08-01 17:53:14 -05:00
|
|
|
|
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
|
|
|
|
// End:
|
|
|
|
//
|