aboutsummaryrefslogtreecommitdiff
path: root/src/lib/_io.rs
diff options
context:
space:
mode:
authorGraydon Hoare <[email protected]>2010-08-20 12:12:11 -0700
committerGraydon Hoare <[email protected]>2010-08-20 12:12:11 -0700
commit1db0cb208de058ea2d1d5b2d2982a482d55de3b8 (patch)
treeb303fbc8d5c309df60cd115f7b23110d8c1355f9 /src/lib/_io.rs
parentRe-XFAIL destructor-ordering.rs. Valgrind doesn't like it (though oddly, does... (diff)
downloadrust-1db0cb208de058ea2d1d5b2d2982a482d55de3b8.tar.xz
rust-1db0cb208de058ea2d1d5b2d2982a482d55de3b8.zip
Add ungetc and re-indent _io.rs.
Diffstat (limited to 'src/lib/_io.rs')
-rw-r--r--src/lib/_io.rs194
1 files changed, 99 insertions, 95 deletions
diff --git a/src/lib/_io.rs b/src/lib/_io.rs
index 46ba21cf..2fb225dd 100644
--- a/src/lib/_io.rs
+++ b/src/lib/_io.rs
@@ -5,150 +5,154 @@ import std._vec;
type stdio_reader = unsafe obj {
fn getc() -> int;
+ fn ungetc(int i);
};
fn new_stdio_reader(str path) -> stdio_reader {
- unsafe obj stdio_FILE_reader(os.libc.FILE f) {
- fn getc() -> int {
- ret os.libc.fgetc(f);
- }
- drop {
- os.libc.fclose(f);
+ unsafe obj stdio_FILE_reader(os.libc.FILE f) {
+ fn getc() -> int {
+ ret os.libc.fgetc(f);
+ }
+ fn ungetc(int i) {
+ os.libc.ungetc(i, f);
+ }
+ drop {
+ os.libc.fclose(f);
+ }
}
- }
- ret stdio_FILE_reader(os.libc.fopen(_str.buf(path),
- _str.buf("r")));
+ ret stdio_FILE_reader(os.libc.fopen(_str.buf(path),
+ _str.buf("r")));
}
type buf_reader = unsafe obj {
- fn read() -> vec[u8];
+ fn read() -> vec[u8];
};
type buf_writer = unsafe obj {
- fn write(vec[u8] v);
+ fn write(vec[u8] v);
};
fn default_bufsz() -> uint {
- ret 4096u;
+ ret 4096u;
}
fn new_buf() -> vec[u8] {
- ret _vec.alloc[u8](default_bufsz());
+ ret _vec.alloc[u8](default_bufsz());
}
fn new_buf_reader(str path) -> buf_reader {
- unsafe obj fd_buf_reader(int fd, mutable vec[u8] buf) {
+ unsafe obj fd_buf_reader(int fd, mutable vec[u8] buf) {
- fn read() -> vec[u8] {
+ fn read() -> vec[u8] {
- // Ensure our buf is singly-referenced.
- if (_vec.rustrt.refcount[u8](buf) != 1u) {
- buf = new_buf();
- }
+ // 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);
+ 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;
- }
+ if (count < 0) {
+ log "error filling buffer";
+ log sys.rustrt.last_os_error();
+ fail;
+ }
+
+ _vec.len_set[u8](buf, count as uint);
+ ret buf;
+ }
- _vec.len_set[u8](buf, count as uint);
- ret buf;
+ drop {
+ os.libc.close(fd);
+ }
}
- drop {
- os.libc.close(fd);
+ auto fd = os.libc.open(_str.buf(path),
+ os.libc_constants.O_RDONLY() |
+ os.libc_constants.O_BINARY(),
+ 0u);
+
+ if (fd < 0) {
+ log "error opening file for reading";
+ log sys.rustrt.last_os_error();
+ fail;
}
- }
-
- auto fd = os.libc.open(_str.buf(path),
- os.libc_constants.O_RDONLY() |
- os.libc_constants.O_BINARY(),
- 0u);
-
- if (fd < 0) {
- log "error opening file for reading";
- log sys.rustrt.last_os_error();
- fail;
- }
- ret fd_buf_reader(fd, new_buf());
+ ret fd_buf_reader(fd, new_buf());
}
type fileflag = tag(append(), create(), truncate());
fn new_buf_writer(str path, vec[fileflag] flags) -> buf_writer {
- unsafe obj fd_buf_writer(int fd) {
-
- fn write(vec[u8] v) {
- 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;
+ unsafe obj fd_buf_writer(int fd) {
+
+ fn write(vec[u8] v) {
+ 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;
+ }
+ }
+
+ drop {
+ os.libc.close(fd);
}
- count += nout as uint;
- }
}
- drop {
- os.libc.close(fd);
+ let int fflags =
+ os.libc_constants.O_WRONLY() |
+ os.libc_constants.O_BINARY();
+
+ for (fileflag f in flags) {
+ alt (f) {
+ case (append()) { fflags |= os.libc_constants.O_APPEND(); }
+ case (create()) { fflags |= os.libc_constants.O_CREAT(); }
+ case (truncate()) { fflags |= os.libc_constants.O_TRUNC(); }
+ }
}
- }
- let int fflags =
- os.libc_constants.O_WRONLY() |
- os.libc_constants.O_BINARY();
+ auto fd = os.libc.open(_str.buf(path),
+ fflags,
+ os.libc_constants.S_IRUSR() |
+ os.libc_constants.S_IWUSR());
- for (fileflag f in flags) {
- alt (f) {
- case (append()) { fflags |= os.libc_constants.O_APPEND(); }
- case (create()) { fflags |= os.libc_constants.O_CREAT(); }
- case (truncate()) { fflags |= os.libc_constants.O_TRUNC(); }
+ if (fd < 0) {
+ log "error opening file for writing";
+ log sys.rustrt.last_os_error();
+ fail;
}
- }
-
- auto fd = os.libc.open(_str.buf(path),
- fflags,
- os.libc_constants.S_IRUSR() |
- os.libc_constants.S_IWUSR());
-
- if (fd < 0) {
- log "error opening file for writing";
- log sys.rustrt.last_os_error();
- fail;
- }
- ret fd_buf_writer(fd);
+ ret fd_buf_writer(fd);
}
type writer =
- unsafe obj {
- fn write_str(str s);
- fn write_int(int n);
- fn write_uint(uint n);
- };
+ unsafe obj {
+ fn write_str(str s);
+ fn write_int(int n);
+ fn write_uint(uint n);
+ };
fn file_writer(str path,
vec[fileflag] flags)
- -> writer
+ -> writer
{
- unsafe obj fw(buf_writer out) {
- fn write_str(str s) { out.write(_str.bytes(s)); }
- fn write_int(int n) { out.write(_str.bytes(_int.to_str(n, 10u))); }
- fn write_uint(uint n) { out.write(_str.bytes(_uint.to_str(n, 10u))); }
- }
- ret fw(new_buf_writer(path, flags));
+ unsafe obj fw(buf_writer out) {
+ fn write_str(str s) { out.write(_str.bytes(s)); }
+ fn write_int(int n) { out.write(_str.bytes(_int.to_str(n, 10u))); }
+ fn write_uint(uint n) { out.write(_str.bytes(_uint.to_str(n, 10u))); }
+ }
+ ret fw(new_buf_writer(path, flags));
}
//