From b9075c23c0c1d87296b338869bce37ca709cbce2 Mon Sep 17 00:00:00 2001 From: Roy Frostig Date: Wed, 4 Aug 2010 11:24:09 -0700 Subject: Address _io.new_buf FIXME now that issue #93 is closed. --- src/lib/_io.rs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) (limited to 'src/lib') diff --git a/src/lib/_io.rs b/src/lib/_io.rs index 94021aa6..d78f8835 100644 --- a/src/lib/_io.rs +++ b/src/lib/_io.rs @@ -7,15 +7,7 @@ fn default_bufsz() -> uint { } fn new_buf() -> vec[u8] { - let vec[u8] v = vec(); - let uint i = default_bufsz(); - while (i > 0u) { - i -= 1u; - v += vec(0u8); - } - // FIXME (issue #93): should be: - // ret _vec.alloc[u8](default_bufsz()); - ret v; + ret _vec.alloc[u8](default_bufsz()); } fn new_buf_reader(str s) -> buf_reader { -- cgit v1.2.3 From 1a8d609e89ed647204b68bfe40e83357644476cb Mon Sep 17 00:00:00 2001 From: Roy Frostig Date: Wed, 4 Aug 2010 12:59:48 -0700 Subject: Add a buffered writer to stdlib _io module. --- src/lib/_io.rs | 44 +++++++++++++++++++++++++++++++++++++++++--- src/lib/_vec.rs | 9 +++++++-- 2 files changed, 48 insertions(+), 5 deletions(-) (limited to 'src/lib') diff --git a/src/lib/_io.rs b/src/lib/_io.rs index d78f8835..096c768a 100644 --- a/src/lib/_io.rs +++ b/src/lib/_io.rs @@ -2,6 +2,10 @@ type buf_reader = unsafe obj { fn read() -> vec[u8]; }; +type buf_writer = unsafe obj { + fn write(vec[u8] v); +}; + fn default_bufsz() -> uint { ret 4096u; } @@ -10,7 +14,7 @@ fn new_buf() -> vec[u8] { ret _vec.alloc[u8](default_bufsz()); } -fn new_buf_reader(str s) -> buf_reader { +fn new_buf_reader(str path) -> buf_reader { unsafe obj fd_buf_reader(int fd, mutable vec[u8] buf) { @@ -39,11 +43,45 @@ fn new_buf_reader(str s) -> buf_reader { } } - auto fd = os.libc.open(_str.buf(s), 0); + auto fd = os.libc.open(_str.buf(path), 0); if (fd < 0) { - log "error opening file"; + log "error opening file for reading"; log sys.rustrt.last_os_error(); fail; } ret fd_buf_reader(fd, new_buf()); } + +fn new_buf_writer(str path) -> 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; + } + count += nout as uint; + } + } + + drop { + os.libc.close(fd); + } + } + + auto fd = os.libc.open(_str.buf(path), 0); + if (fd < 0) { + log "error opening file for writing"; + log sys.rustrt.last_os_error(); + fail; + } + ret fd_buf_writer(fd); +} diff --git a/src/lib/_vec.rs b/src/lib/_vec.rs index 43779015..e374bf52 100644 --- a/src/lib/_vec.rs +++ b/src/lib/_vec.rs @@ -3,7 +3,7 @@ import op = util.operator; native "rust" mod rustrt { type vbuf; - fn vec_buf[T](vec[T] v) -> vbuf; + fn vec_buf[T](vec[T] v, uint offset) -> vbuf; fn vec_len[T](vec[T] v) -> uint; /* The T in vec_alloc[T, U] is the type of the vec to allocate. The * U is the type of an element in the vec. So to allocate a vec[U] we @@ -50,7 +50,12 @@ fn len[T](vec[T] v) -> uint { } fn buf[T](vec[T] v) -> vbuf { - ret rustrt.vec_buf[T](v); + ret rustrt.vec_buf[T](v, 0u); +} + +fn buf_off[T](vec[T] v, uint offset) -> vbuf { + check (offset < len[T](v)); + ret rustrt.vec_buf[T](v, offset); } // Returns elements from [start..end) from v. -- cgit v1.2.3 From c17ea956a26a05341ff24736105b43f2a16a1ad2 Mon Sep 17 00:00:00 2001 From: Roy Frostig Date: Wed, 4 Aug 2010 17:14:11 -0700 Subject: Add per-platform file-open flags to std.os. Open buffers as desired in std._io. --- src/lib/_io.rs | 28 +++++++++++++++++++++++++--- src/lib/linux_os.rs | 17 ++++++++++++++++- src/lib/macos_os.rs | 17 ++++++++++++++++- src/lib/win32_os.rs | 17 ++++++++++++++++- 4 files changed, 73 insertions(+), 6 deletions(-) (limited to 'src/lib') diff --git a/src/lib/_io.rs b/src/lib/_io.rs index 096c768a..142f808a 100644 --- a/src/lib/_io.rs +++ b/src/lib/_io.rs @@ -43,7 +43,11 @@ fn new_buf_reader(str path) -> buf_reader { } } - auto fd = os.libc.open(_str.buf(path), 0); + 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(); @@ -52,7 +56,9 @@ fn new_buf_reader(str path) -> buf_reader { ret fd_buf_reader(fd, new_buf()); } -fn new_buf_writer(str path) -> buf_writer { +type fileflag = tag(append(), create(), truncate()); + +fn new_buf_writer(str path, vec[fileflag] flags) -> buf_writer { unsafe obj fd_buf_writer(int fd) { @@ -77,7 +83,23 @@ fn new_buf_writer(str path) -> buf_writer { } } - auto fd = os.libc.open(_str.buf(path), 0); + 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(); } + } + } + + 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(); diff --git a/src/lib/linux_os.rs b/src/lib/linux_os.rs index a775a97a..3f096e99 100644 --- a/src/lib/linux_os.rs +++ b/src/lib/linux_os.rs @@ -3,7 +3,7 @@ import _vec.vbuf; native mod libc = "libc.so.6" { - fn open(sbuf s, int flags) -> int; + fn open(sbuf s, int flags, uint mode) -> int; fn read(int fd, vbuf buf, uint count) -> int; fn write(int fd, vbuf buf, uint count) -> int; fn close(int fd) -> int; @@ -17,3 +17,18 @@ native mod libc = "libc.so.6" { fn setenv(sbuf n, sbuf v, int overwrite) -> int; fn unsetenv(sbuf n) -> int; } + +mod libc_constants { + fn O_RDONLY() -> int { ret 0x0000; } + fn O_WRONLY() -> int { ret 0x0001; } + fn O_RDWR() -> int { ret 0x0002; } + fn O_APPEND() -> int { ret 0x0400; } + fn O_CREAT() -> int { ret 0x0040; } + fn O_EXCL() -> int { ret 0x0080; } + fn O_TRUNC() -> int { ret 0x0200; } + fn O_TEXT() -> int { ret 0x0000; } // nonexistent in linux libc + fn O_BINARY() -> int { ret 0x0000; } // nonexistent in linux libc + + fn S_IRUSR() -> uint { ret 0x0100u; } + fn S_IWUSR() -> uint { ret 0x0080u; } +} diff --git a/src/lib/macos_os.rs b/src/lib/macos_os.rs index 8b30c8bc..2ada5c07 100644 --- a/src/lib/macos_os.rs +++ b/src/lib/macos_os.rs @@ -3,7 +3,7 @@ import _vec.vbuf; native mod libc = "libc.dylib" { - fn open(sbuf s, int flags) -> int; + fn open(sbuf s, int flags, uint mode) -> int; fn read(int fd, vbuf buf, uint count) -> int; fn write(int fd, vbuf buf, uint count) -> int; fn close(int fd) -> int; @@ -17,3 +17,18 @@ native mod libc = "libc.dylib" { fn setenv(sbuf n, sbuf v, int overwrite) -> int; fn unsetenv(sbuf n) -> int; } + +mod libc_constants { + fn O_RDONLY() -> int { ret 0x0000; } + fn O_WRONLY() -> int { ret 0x0001; } + fn O_RDWR() -> int { ret 0x0002; } + fn O_APPEND() -> int { ret 0x0008; } + fn O_CREAT() -> int { ret 0x0200; } + fn O_EXCL() -> int { ret 0x0800; } + fn O_TRUNC() -> int { ret 0x0400; } + fn O_TEXT() -> int { ret 0x0000; } // nonexistent in darwin libc + fn O_BINARY() -> int { ret 0x0000; } // nonexistent in darwin libc + + fn S_IRUSR() -> uint { ret 0x0400u; } + fn S_IWUSR() -> uint { ret 0x0200u; } +} diff --git a/src/lib/win32_os.rs b/src/lib/win32_os.rs index f770a5de..3d8e5f3a 100644 --- a/src/lib/win32_os.rs +++ b/src/lib/win32_os.rs @@ -2,8 +2,23 @@ import _str.sbuf; import _vec.vbuf; native mod libc = "msvcrt.dll" { - fn open(sbuf s, int flags) -> int = "_open"; + fn open(sbuf s, int flags, uint mode) -> int = "_open"; fn read(int fd, vbuf buf, uint count) -> int = "_read"; fn write(int fd, vbuf buf, uint count) -> int = "_write"; fn close(int fd) -> int = "_close"; } + +mod libc_constants { + fn O_RDONLY() -> int { ret 0x0000; } + fn O_WRONLY() -> int { ret 0x0001; } + fn O_RDWR() -> int { ret 0x0002; } + fn O_APPEND() -> int { ret 0x0400; } + fn O_CREAT() -> int { ret 0x0040; } + fn O_EXCL() -> int { ret 0x0080; } + fn O_TRUNC() -> int { ret 0x0200; } + fn O_TEXT() -> int { ret 0x4000; } + fn O_BINARY() -> int { ret 0x8000; } + + fn S_IRUSR() -> uint { ret 0x0100u; } // really _S_IREAD in win32 + fn S_IWUSR() -> uint { ret 0x0080u; } // really _S_IWRITE in win32 +} -- cgit v1.2.3