2011-03-09 19:29:18 -06:00
|
|
|
import os.libc;
|
2010-08-18 17:40:27 -05:00
|
|
|
|
2011-03-10 09:02:53 -06:00
|
|
|
native "rust" mod rustrt {
|
|
|
|
fn rust_get_stdin() -> os.libc.FILE;
|
|
|
|
fn rust_get_stdout() -> os.libc.FILE;
|
2010-08-18 17:40:27 -05:00
|
|
|
}
|
|
|
|
|
2011-03-10 09:02:53 -06:00
|
|
|
// Reading
|
2010-08-18 17:40:27 -05:00
|
|
|
|
2011-03-24 06:11:32 -05:00
|
|
|
// FIXME This is all buffered. We might need an unbuffered variant as well
|
2010-06-23 23:03:09 -05:00
|
|
|
|
2011-03-10 09:02:53 -06:00
|
|
|
tag seek_style {seek_set; seek_end; seek_cur;}
|
2010-07-13 16:24:47 -05:00
|
|
|
|
2011-03-25 15:54:54 -05:00
|
|
|
// 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.
|
2011-03-10 09:02:53 -06:00
|
|
|
type reader =
|
|
|
|
state obj {
|
2011-03-25 15:54:54 -05:00
|
|
|
// FIXME: This should inherit from buf_reader.
|
|
|
|
impure fn get_buf_reader() -> buf_reader;
|
|
|
|
|
2011-03-24 06:11:32 -05:00
|
|
|
impure fn read_byte() -> int;
|
|
|
|
impure fn unread_byte(int byte);
|
2011-03-10 09:02:53 -06:00
|
|
|
impure fn read_bytes(uint len) -> vec[u8];
|
2011-03-24 06:11:32 -05:00
|
|
|
impure fn read_char() -> char;
|
2011-03-11 06:30:18 -06:00
|
|
|
impure fn eof() -> bool;
|
|
|
|
impure fn read_line() -> str;
|
2011-03-10 09:02:53 -06:00
|
|
|
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);
|
2011-03-24 06:11:32 -05:00
|
|
|
impure fn tell() -> uint; // FIXME: eventually u64
|
2011-03-10 09:02:53 -06:00
|
|
|
};
|
2010-08-20 14:12:11 -05:00
|
|
|
|
2011-03-22 18:38:47 -05:00
|
|
|
fn convert_whence(seek_style whence) -> int {
|
|
|
|
alt (whence) {
|
|
|
|
case (seek_set) {ret 0;}
|
|
|
|
case (seek_cur) {ret 1;}
|
|
|
|
case (seek_end) {ret 2;}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-25 15:54:54 -05:00
|
|
|
state obj FILE_buf_reader(os.libc.FILE f, bool must_close) {
|
|
|
|
impure fn read(uint len) -> vec[u8] {
|
|
|
|
auto buf = _vec.alloc[u8](len);
|
|
|
|
auto read = os.libc.fread(_vec.buf[u8](buf), 1u, len, f);
|
|
|
|
_vec.len_set[u8](buf, read);
|
|
|
|
ret buf;
|
2011-03-24 06:11:32 -05:00
|
|
|
}
|
|
|
|
impure fn unread_byte(int byte) {
|
|
|
|
os.libc.ungetc(byte, f);
|
2011-03-10 09:02:53 -06:00
|
|
|
}
|
2011-03-25 15:54:54 -05:00
|
|
|
impure fn eof() -> bool {
|
|
|
|
ret os.libc.feof(f) != 0;
|
|
|
|
}
|
|
|
|
impure fn seek(int offset, seek_style whence) {
|
|
|
|
check (os.libc.fseek(f, offset, convert_whence(whence)) == 0);
|
|
|
|
}
|
|
|
|
impure fn tell() -> uint {
|
|
|
|
ret os.libc.ftell(f) as uint;
|
|
|
|
}
|
|
|
|
drop {
|
|
|
|
if (must_close) { os.libc.fclose(f); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
2011-03-10 09:02:53 -06:00
|
|
|
impure fn read_bytes(uint len) -> vec[u8] {
|
2011-03-25 15:54:54 -05:00
|
|
|
ret rdr.read(len);
|
2011-03-10 09:02:53 -06:00
|
|
|
}
|
2011-03-24 06:11:32 -05:00
|
|
|
impure fn read_char() -> char {
|
2011-03-25 15:54:54 -05:00
|
|
|
auto c0 = read_byte_from_buf_reader(rdr);
|
2011-03-24 06:11:32 -05:00
|
|
|
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;
|
2011-03-25 15:54:54 -05:00
|
|
|
auto next = read_byte_from_buf_reader(rdr);
|
2011-03-24 06:11:32 -05:00
|
|
|
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;
|
|
|
|
}
|
2011-03-11 06:30:18 -06:00
|
|
|
impure fn eof() -> bool {
|
2011-03-25 15:54:54 -05:00
|
|
|
ret rdr.eof();
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
|
|
|
impure fn read_line() -> str {
|
2011-03-24 06:11:32 -05:00
|
|
|
let vec[u8] buf = vec();
|
|
|
|
// No break yet in rustc
|
|
|
|
auto go_on = true;
|
|
|
|
while (go_on) {
|
2011-03-25 15:54:54 -05:00
|
|
|
auto ch = read_byte_from_buf_reader(rdr);
|
2011-03-24 06:11:32 -05:00
|
|
|
if (ch == -1 || ch == 10) {go_on = false;}
|
|
|
|
else {_vec.push[u8](buf, ch as u8);}
|
|
|
|
}
|
|
|
|
ret _str.unsafe_from_bytes(buf);
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
2011-03-10 09:02:53 -06:00
|
|
|
impure fn read_c_str() -> str {
|
2011-03-24 06:11:32 -05:00
|
|
|
let vec[u8] buf = vec();
|
|
|
|
auto go_on = true;
|
|
|
|
while (go_on) {
|
2011-03-25 15:54:54 -05:00
|
|
|
auto ch = read_byte_from_buf_reader(rdr);
|
2011-03-24 06:11:32 -05:00
|
|
|
if (ch < 1) {go_on = false;}
|
|
|
|
else {_vec.push[u8](buf, ch as u8);}
|
2010-08-20 14:12:11 -05:00
|
|
|
}
|
2011-03-24 06:11:32 -05:00
|
|
|
ret _str.unsafe_from_bytes(buf);
|
2011-03-10 09:02:53 -06:00
|
|
|
}
|
2011-03-24 06:11:32 -05:00
|
|
|
// FIXME deal with eof?
|
2011-03-10 09:02:53 -06:00
|
|
|
impure fn read_le_uint(uint size) -> uint {
|
|
|
|
auto val = 0u;
|
|
|
|
auto pos = 0u;
|
|
|
|
while (size > 0u) {
|
2011-03-25 15:54:54 -05:00
|
|
|
val += (read_byte_from_buf_reader(rdr) as uint) << pos;
|
2011-03-10 09:02:53 -06:00
|
|
|
pos += 8u;
|
|
|
|
size -= 1u;
|
|
|
|
}
|
|
|
|
ret val;
|
|
|
|
}
|
|
|
|
impure fn read_le_int(uint size) -> int {
|
|
|
|
auto val = 0u;
|
|
|
|
auto pos = 0u;
|
|
|
|
while (size > 0u) {
|
2011-03-25 15:54:54 -05:00
|
|
|
val += (read_byte_from_buf_reader(rdr) as uint) << pos;
|
2011-03-10 09:02:53 -06:00
|
|
|
pos += 8u;
|
|
|
|
size -= 1u;
|
2010-08-20 14:12:11 -05:00
|
|
|
}
|
2011-03-24 06:11:32 -05:00
|
|
|
ret val as int;
|
2011-03-10 09:02:53 -06:00
|
|
|
}
|
|
|
|
impure fn seek(int offset, seek_style whence) {
|
2011-03-25 15:54:54 -05:00
|
|
|
ret rdr.seek(offset, whence);
|
2011-03-10 09:02:53 -06:00
|
|
|
}
|
2011-03-21 18:40:26 -05:00
|
|
|
impure fn tell() -> uint {
|
2011-03-25 15:54:54 -05:00
|
|
|
ret rdr.tell();
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
2011-03-10 09:02:53 -06:00
|
|
|
}
|
2010-07-13 16:24:47 -05:00
|
|
|
|
2011-03-10 09:02:53 -06:00
|
|
|
fn stdin() -> reader {
|
2011-03-25 15:54:54 -05:00
|
|
|
ret new_reader(FILE_buf_reader(rustrt.rust_get_stdin(), false));
|
2011-03-10 09:02:53 -06:00
|
|
|
}
|
2010-08-20 14:12:11 -05:00
|
|
|
|
2011-03-10 09:02:53 -06:00
|
|
|
fn file_reader(str path) -> reader {
|
|
|
|
auto f = os.libc.fopen(_str.buf(path), _str.buf("r"));
|
2011-03-20 21:42:51 -05:00
|
|
|
if (f as uint == 0u) {
|
|
|
|
log "error opening " + path;
|
|
|
|
fail;
|
|
|
|
}
|
2011-03-25 15:54:54 -05:00
|
|
|
ret new_reader(FILE_buf_reader(f, true));
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
2010-08-04 14:59:48 -05:00
|
|
|
|
2011-03-25 15:54:54 -05:00
|
|
|
|
|
|
|
// Byte buffer readers
|
|
|
|
|
|
|
|
//state obj byte_buf_reader(vec[mutable? u8] buf) {
|
|
|
|
// fn read(
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
2011-03-10 09:02:53 -06:00
|
|
|
// Writing
|
|
|
|
|
2011-03-06 12:51:42 -06:00
|
|
|
tag fileflag {
|
|
|
|
append;
|
|
|
|
create;
|
|
|
|
truncate;
|
2011-03-10 08:56:51 -06:00
|
|
|
none;
|
2011-03-06 12:51:42 -06:00
|
|
|
}
|
2010-08-04 19:14:11 -05:00
|
|
|
|
2011-03-10 09:02:53 -06:00
|
|
|
type buf_writer = state obj {
|
|
|
|
fn write(vec[u8] v);
|
2011-03-22 18:38:47 -05:00
|
|
|
|
2011-03-25 15:54:54 -05:00
|
|
|
// FIXME: Seekable really should be orthogonal. We will need inheritance.
|
2011-03-22 18:38:47 -05:00
|
|
|
fn seek(int offset, seek_style whence);
|
2011-03-24 06:11:32 -05:00
|
|
|
fn tell() -> uint; // FIXME: eventually u64
|
2011-03-10 09:02:53 -06:00
|
|
|
};
|
|
|
|
|
2011-03-22 18:38:47 -05:00
|
|
|
state obj FILE_writer(os.libc.FILE f, bool must_close) {
|
|
|
|
fn write(vec[u8] v) {
|
|
|
|
auto len = _vec.len[u8](v);
|
|
|
|
auto vbuf = _vec.buf[u8](v);
|
|
|
|
auto nout = os.libc.fwrite(vbuf, len, 1u, f);
|
|
|
|
if (nout < 1u) {
|
|
|
|
log "error dumping buffer";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn seek(int offset, seek_style whence) {
|
|
|
|
check(os.libc.fseek(f, offset, convert_whence(whence)) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn tell() -> uint {
|
|
|
|
ret os.libc.ftell(f) as uint;
|
|
|
|
}
|
|
|
|
|
|
|
|
drop {
|
|
|
|
if (must_close) {os.libc.fclose(f);}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-09 03:53:45 -06:00
|
|
|
state obj fd_buf_writer(int fd, bool must_close) {
|
|
|
|
fn write(vec[u8] v) {
|
2011-03-09 04:41:50 -06:00
|
|
|
auto len = _vec.len[u8](v);
|
|
|
|
auto count = 0u;
|
|
|
|
auto vbuf;
|
|
|
|
while (count < len) {
|
|
|
|
vbuf = _vec.buf_off[u8](v, count);
|
|
|
|
auto nout = os.libc.write(fd, vbuf, len);
|
|
|
|
if (nout < 0) {
|
|
|
|
log "error dumping buffer";
|
|
|
|
log sys.rustrt.last_os_error();
|
|
|
|
fail;
|
|
|
|
}
|
|
|
|
count += nout as uint;
|
|
|
|
}
|
2011-03-09 03:53:45 -06:00
|
|
|
}
|
2010-08-20 14:12:11 -05:00
|
|
|
|
2011-03-22 18:38:47 -05:00
|
|
|
fn seek(int offset, seek_style whence) {
|
|
|
|
log "need 64-bit native calls for seek, sorry";
|
|
|
|
fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn tell() -> uint {
|
|
|
|
log "need 64-bit native calls for tell, sorry";
|
|
|
|
fail;
|
|
|
|
}
|
|
|
|
|
2011-03-09 03:53:45 -06:00
|
|
|
drop {
|
|
|
|
if (must_close) {os.libc.close(fd);}
|
2010-08-04 14:59:48 -05:00
|
|
|
}
|
2011-03-09 03:53:45 -06:00
|
|
|
}
|
2010-08-04 14:59:48 -05:00
|
|
|
|
2011-03-09 03:53:45 -06:00
|
|
|
fn file_buf_writer(str path, vec[fileflag] flags) -> buf_writer {
|
2010-08-20 14:12:11 -05:00
|
|
|
let int fflags =
|
|
|
|
os.libc_constants.O_WRONLY() |
|
|
|
|
os.libc_constants.O_BINARY();
|
|
|
|
|
|
|
|
for (fileflag f in flags) {
|
|
|
|
alt (f) {
|
2011-03-06 12:51:42 -06:00
|
|
|
case (append) { fflags |= os.libc_constants.O_APPEND(); }
|
|
|
|
case (create) { fflags |= os.libc_constants.O_CREAT(); }
|
|
|
|
case (truncate) { fflags |= os.libc_constants.O_TRUNC(); }
|
2011-03-10 08:56:51 -06:00
|
|
|
case (none) {}
|
2010-08-20 14:12:11 -05:00
|
|
|
}
|
2010-08-04 14:59:48 -05:00
|
|
|
}
|
|
|
|
|
2010-08-20 14:12:11 -05:00
|
|
|
auto fd = os.libc.open(_str.buf(path),
|
|
|
|
fflags,
|
|
|
|
os.libc_constants.S_IRUSR() |
|
|
|
|
os.libc_constants.S_IWUSR());
|
2010-08-04 19:14:11 -05:00
|
|
|
|
2010-08-20 14:12:11 -05:00
|
|
|
if (fd < 0) {
|
|
|
|
log "error opening file for writing";
|
|
|
|
log sys.rustrt.last_os_error();
|
|
|
|
fail;
|
2010-08-04 19:14:11 -05:00
|
|
|
}
|
2011-03-09 03:53:45 -06:00
|
|
|
ret fd_buf_writer(fd, true);
|
2010-08-04 14:59:48 -05:00
|
|
|
}
|
2010-08-05 01:09:25 -05:00
|
|
|
|
2010-08-06 17:48:23 -05:00
|
|
|
type writer =
|
2010-11-02 19:20:57 -05:00
|
|
|
state obj {
|
2011-03-22 19:52:03 -05:00
|
|
|
fn get_buf_writer() -> buf_writer;
|
2011-03-24 06:11:32 -05:00
|
|
|
// write_str will continue to do utf-8 output only. an alternative
|
|
|
|
// function will be provided for general encoded string output
|
2011-03-09 03:53:45 -06:00
|
|
|
impure fn write_str(str s);
|
2011-03-24 06:11:32 -05:00
|
|
|
impure fn write_char(char ch);
|
2011-03-09 03:53:45 -06:00
|
|
|
impure fn write_int(int n);
|
|
|
|
impure fn write_uint(uint n);
|
2011-03-10 09:02:53 -06:00
|
|
|
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);
|
2010-08-20 14:12:11 -05:00
|
|
|
};
|
2010-08-06 17:48:23 -05:00
|
|
|
|
2011-03-10 09:02:53 -06:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-03-09 03:53:45 -06:00
|
|
|
state obj new_writer(buf_writer out) {
|
2011-03-22 19:52:03 -05:00
|
|
|
fn get_buf_writer() -> buf_writer {
|
|
|
|
ret out;
|
|
|
|
}
|
2011-03-09 04:41:50 -06:00
|
|
|
impure fn write_str(str s) {
|
|
|
|
out.write(_str.bytes(s));
|
|
|
|
}
|
2011-03-24 06:11:32 -05:00
|
|
|
impure fn write_char(char ch) {
|
|
|
|
// FIXME needlessly consy
|
|
|
|
out.write(_str.bytes(_str.from_char(ch)));
|
|
|
|
}
|
2011-03-09 04:41:50 -06:00
|
|
|
impure fn write_int(int n) {
|
|
|
|
out.write(_str.bytes(_int.to_str(n, 10u)));
|
|
|
|
}
|
|
|
|
impure fn write_uint(uint n) {
|
|
|
|
out.write(_str.bytes(_uint.to_str(n, 10u)));
|
|
|
|
}
|
2011-03-10 09:02:53 -06:00
|
|
|
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));
|
|
|
|
}
|
2011-03-09 03:53:45 -06:00
|
|
|
}
|
|
|
|
|
2011-03-22 19:52:03 -05:00
|
|
|
// FIXME: Remove me once objects are exported.
|
|
|
|
fn new_writer_(buf_writer out) -> writer {
|
|
|
|
ret new_writer(out);
|
|
|
|
}
|
|
|
|
|
2011-03-09 03:53:45 -06:00
|
|
|
fn file_writer(str path, vec[fileflag] flags) -> writer {
|
|
|
|
ret new_writer(file_buf_writer(path, flags));
|
|
|
|
}
|
|
|
|
|
2011-03-24 06:11:32 -05:00
|
|
|
// FIXME: fileflags
|
2011-03-22 19:27:57 -05:00
|
|
|
fn buffered_file_buf_writer(str path) -> buf_writer {
|
|
|
|
auto f = os.libc.fopen(_str.buf(path), _str.buf("w"));
|
|
|
|
if (f as uint == 0u) {
|
|
|
|
log "error opening " + path;
|
|
|
|
fail;
|
|
|
|
}
|
|
|
|
ret FILE_writer(f, true);
|
|
|
|
}
|
|
|
|
|
2011-03-10 09:02:53 -06:00
|
|
|
// FIXME it would be great if this could be a const
|
|
|
|
fn stdout() -> writer {
|
2011-03-09 03:53:45 -06:00
|
|
|
ret new_writer(fd_buf_writer(1, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
type str_writer =
|
|
|
|
state obj {
|
|
|
|
fn get_writer() -> writer;
|
|
|
|
fn get_str() -> str;
|
|
|
|
};
|
|
|
|
|
2011-03-22 18:38:47 -05:00
|
|
|
type byte_buf = @rec(mutable vec[mutable u8] buf, mutable uint pos);
|
2011-03-10 09:02:53 -06:00
|
|
|
|
|
|
|
state obj byte_buf_writer(byte_buf buf) {
|
2011-03-22 18:38:47 -05:00
|
|
|
fn write(vec[u8] v) {
|
2011-03-24 06:11:32 -05:00
|
|
|
// FIXME: optimize
|
2011-03-22 18:38:47 -05:00
|
|
|
auto vlen = _vec.len[u8](v);
|
|
|
|
auto vpos = 0u;
|
|
|
|
while (vpos < vlen) {
|
|
|
|
auto b = v.(vpos);
|
|
|
|
if (buf.pos == _vec.len[mutable u8](buf.buf)) {
|
|
|
|
buf.buf += vec(mutable b);
|
|
|
|
} else {
|
|
|
|
buf.buf.(buf.pos) = b;
|
|
|
|
}
|
|
|
|
buf.pos += 1u;
|
|
|
|
vpos += 1u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn seek(int offset, seek_style whence) {
|
|
|
|
auto pos = buf.pos as int;
|
|
|
|
auto len = _vec.len[mutable u8](buf.buf) as int;
|
|
|
|
alt (whence) {
|
|
|
|
case (seek_set) { pos = offset; }
|
|
|
|
case (seek_cur) { pos += offset; }
|
|
|
|
case (seek_end) { pos = len + offset; }
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pos < 0) {
|
|
|
|
pos = 0;
|
|
|
|
} else if (pos > len) {
|
|
|
|
pos = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.pos = pos as uint;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn tell() -> uint { ret buf.pos; }
|
2011-03-10 09:02:53 -06:00
|
|
|
}
|
2011-03-09 03:53:45 -06:00
|
|
|
|
|
|
|
fn string_writer() -> str_writer {
|
2011-03-22 18:38:47 -05:00
|
|
|
// FIXME: yikes, this is bad. Needs fixing of mutable syntax.
|
|
|
|
let vec[mutable u8] b = vec(mutable 0u8);
|
|
|
|
_vec.pop[mutable u8](b);
|
|
|
|
|
|
|
|
let byte_buf buf = @rec(mutable buf = b, mutable pos = 0u);
|
2011-03-10 09:02:53 -06:00
|
|
|
state obj str_writer_wrap(writer wr, byte_buf buf) {
|
2011-03-09 03:53:45 -06:00
|
|
|
fn get_writer() -> writer {ret wr;}
|
2011-03-22 18:38:47 -05:00
|
|
|
fn get_str() -> str {ret _str.unsafe_from_mutable_bytes(buf.buf);}
|
2010-08-20 14:12:11 -05:00
|
|
|
}
|
2011-03-10 09:02:53 -06:00
|
|
|
ret str_writer_wrap(new_writer(byte_buf_writer(buf)), buf);
|
2010-08-05 01:09:25 -05:00
|
|
|
}
|
2010-08-18 17:40:27 -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 .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
|
|
|
// End:
|
|
|
|
//
|