aboutsummaryrefslogtreecommitdiff
path: root/src/lib/io.rs
diff options
context:
space:
mode:
authorBrian Anderson <[email protected]>2011-03-13 19:40:25 -0400
committerBrian Anderson <[email protected]>2011-03-13 19:40:25 -0400
commitbbb6836da003be71744b6e6ea7af1fd4674f8291 (patch)
treefc7780f767666cced8406818806216bee3863c91 /src/lib/io.rs
parentRemove extra blocks from the translation of expr_block (diff)
parentAdd llvmext/include to the list of include directories to hopefully put out t... (diff)
downloadrust-bbb6836da003be71744b6e6ea7af1fd4674f8291.tar.xz
rust-bbb6836da003be71744b6e6ea7af1fd4674f8291.zip
Merge branch 'master' into recursive-elseif
Conflicts: src/comp/middle/typeck.rs
Diffstat (limited to 'src/lib/io.rs')
-rw-r--r--src/lib/io.rs105
1 files changed, 64 insertions, 41 deletions
diff --git a/src/lib/io.rs b/src/lib/io.rs
index 0c4eb39e..34c4a98d 100644
--- a/src/lib/io.rs
+++ b/src/lib/io.rs
@@ -1,7 +1,4 @@
-import std.os.libc;
-import std._str;
-import std._vec;
-
+import os.libc;
type stdio_reader = state obj {
fn getc() -> int;
@@ -91,35 +88,29 @@ tag fileflag {
truncate;
}
-fn writefd(int fd, 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;
+state obj fd_buf_writer(int fd, bool must_close) {
+ 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;
}
- count += nout as uint;
}
-}
-
-fn new_buf_writer(str path, vec[fileflag] flags) -> buf_writer {
-
- state obj fd_buf_writer(int fd) {
- fn write(vec[u8] v) {
- writefd(fd, v);
- }
-
- drop {
- os.libc.close(fd);
- }
+ drop {
+ if (must_close) {os.libc.close(fd);}
}
+}
+fn file_buf_writer(str path, vec[fileflag] flags) -> buf_writer {
let int fflags =
os.libc_constants.O_WRONLY() |
os.libc_constants.O_BINARY();
@@ -142,26 +133,58 @@ fn new_buf_writer(str path, vec[fileflag] flags) -> buf_writer {
log sys.rustrt.last_os_error();
fail;
}
- ret fd_buf_writer(fd);
+ ret fd_buf_writer(fd, true);
}
type writer =
state obj {
- fn write_str(str s);
- fn write_int(int n);
- fn write_uint(uint n);
+ impure fn write_str(str s);
+ impure fn write_int(int n);
+ impure fn write_uint(uint n);
};
-fn file_writer(str path,
- vec[fileflag] flags)
- -> writer
-{
- state 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))); }
+state obj new_writer(buf_writer out) {
+ impure fn write_str(str s) {
+ out.write(_str.bytes(s));
+ }
+ 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)));
+ }
+}
+
+fn file_writer(str path, vec[fileflag] flags) -> writer {
+ ret new_writer(file_buf_writer(path, flags));
+}
+
+// FIXME it would be great if this could be a const named stdout
+fn stdout_writer() -> writer {
+ ret new_writer(fd_buf_writer(1, false));
+}
+
+type str_writer =
+ state obj {
+ fn get_writer() -> writer;
+ fn get_str() -> str;
+ };
+
+type str_buf = @rec(mutable str buf);
+
+// TODO awkward! it's not possible to implement a writer with an extra method
+fn string_writer() -> str_writer {
+ auto buf = @rec(mutable buf = "");
+ state obj str_writer_writer(str_buf buf) {
+ impure fn write_str(str s) { buf.buf += s; }
+ 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_str() -> str {ret buf.buf;}
}
- ret fw(new_buf_writer(path, flags));
+ ret str_writer_wrap(str_writer_writer(buf), buf);
}
//