Remove last users of str::sbuf. Issue #855

This commit is contained in:
Brian Anderson 2011-08-31 17:34:50 -07:00
parent b714150487
commit 34d197de97
8 changed files with 91 additions and 73 deletions

View File

@ -1,12 +1,13 @@
import str::sbuf;
import istr::sbuf;
#[cfg(target_os = "linux")]
#[cfg(target_os = "macos")]
fn getenv(n: &istr) -> option::t<istr> {
let n = istr::to_estr(n);
let s = os::libc::getenv(str::buf(n));
ret if s as int == 0 {
let s = istr::as_buf(n, { |buf|
os::libc::getenv(buf)
});
ret if unsafe::reinterpret_cast(s) == 0 {
option::none::<istr>
} else {
let s = unsafe::reinterpret_cast(s);
@ -17,27 +18,30 @@ fn getenv(n: &istr) -> option::t<istr> {
#[cfg(target_os = "linux")]
#[cfg(target_os = "macos")]
fn setenv(n: &istr, v: &istr) {
let n = istr::to_estr(n);
let v = istr::to_estr(v);
let nbuf = str::buf(n);
let vbuf = str::buf(v);
os::libc::setenv(nbuf, vbuf, 1);
// FIXME (868)
let _: () = istr::as_buf(n, { |nbuf|
// FIXME (868)
let _: () = istr::as_buf(v, { |vbuf|
os::libc::setenv(nbuf, vbuf, 1);
});
});
}
#[cfg(target_os = "win32")]
fn getenv(n: &istr) -> option::t<istr> {
let n = istr::to_estr(n);
let nbuf = str::buf(n);
let nsize = 256u;
while true {
let vstr = str::alloc(nsize - 1u);
let vbuf = str::buf(vstr);
let res = os::kernel32::GetEnvironmentVariableA(nbuf, vbuf, nsize);
let v: [u8] = [];
vec::reserve(v, nsize);
let res = istr::as_buf(n, { |nbuf|
let vbuf = vec::to_ptr(v);
os::kernel32::GetEnvironmentVariableA(nbuf, vbuf, nsize)
});
if res == 0u {
ret option::none;
} else if res < nsize {
let vbuf = unsafe::reinterpret_cast(vbuf);
ret option::some(istr::str_from_cstr(vbuf));
vec::unsafe::set_len(v, res);
ret option::some(istr::unsafe_from_bytes(v));
} else { nsize = res; }
}
fail;
@ -45,11 +49,12 @@ fn getenv(n: &istr) -> option::t<istr> {
#[cfg(target_os = "win32")]
fn setenv(n: &istr, v: &istr) {
let n = istr::to_estr(n);
let v = istr::to_estr(v);
let nbuf = str::buf(n);
let vbuf = str::buf(v);
os::kernel32::SetEnvironmentVariableA(nbuf, vbuf);
// FIXME (868)
let _: () = istr::as_buf(n, { |nbuf|
let _: () = istr::as_buf(v, { |vbuf|
os::kernel32::SetEnvironmentVariableA(nbuf, vbuf);
});
});
}
// Local Variables:

View File

@ -175,10 +175,12 @@ fn stdin() -> reader {
}
fn file_reader(path: &istr) -> reader {
let path = istr::to_estr(path);
let mode = "r";
let f = os::libc::fopen(str::buf(path), str::buf(mode));
if f as uint == 0u { log_err "error opening " + path; fail; }
let f = istr::as_buf(path, { |pathbuf|
istr::as_buf(~"r", { |modebuf|
os::libc::fopen(pathbuf, modebuf)
})
});
if f as uint == 0u { log_err ~"error opening " + path; fail; }
ret new_reader(FILE_buf_reader(f, option::some(@FILE_res(f))));
}
@ -278,7 +280,6 @@ obj fd_buf_writer(fd: int, res: option::t<@fd_res>) {
}
fn file_buf_writer(path: &istr, flags: &[fileflag]) -> buf_writer {
let path = istr::to_estr(path);
let fflags: int =
os::libc_constants::O_WRONLY() | os::libc_constants::O_BINARY();
for f: fileflag in flags {
@ -289,10 +290,11 @@ fn file_buf_writer(path: &istr, flags: &[fileflag]) -> buf_writer {
none. { }
}
}
let fd =
os::libc::open(str::buf(path), fflags,
let fd = istr::as_buf(path, { |pathbuf|
os::libc::open(pathbuf, fflags,
os::libc_constants::S_IRUSR() |
os::libc_constants::S_IWUSR());
os::libc_constants::S_IWUSR())
});
if fd < 0 {
log_err "error opening file for writing";
log_err sys::rustrt::last_os_error();
@ -365,10 +367,12 @@ fn file_writer(path: &istr, flags: &[fileflag]) -> writer {
// FIXME: fileflags
fn buffered_file_buf_writer(path: &istr) -> buf_writer {
let path = istr::to_estr(path);
let mode = "w";
let f = os::libc::fopen(str::buf(path), str::buf(mode));
if f as uint == 0u { log_err "error opening " + path; fail; }
let f = istr::as_buf(path, { |pathbuf|
istr::as_buf(~"w", { |modebuf|
os::libc::fopen(pathbuf, modebuf)
})
});
if f as uint == 0u { log_err ~"error opening " + path; fail; }
ret FILE_writer(f, option::some(@FILE_res(f)));
}

View File

@ -1,6 +1,4 @@
import str::sbuf;
// FIXME Somehow merge stuff duplicated here and macosx_os.rs. Made difficult
// by https://github.com/graydon/rust/issues#issue/268
@ -9,11 +7,11 @@ native "cdecl" mod libc = "" {
fn write(fd: int, buf: *u8, count: uint) -> int;
fn fread(buf: *u8, size: uint, n: uint, f: libc::FILE) -> uint;
fn fwrite(buf: *u8, size: uint, n: uint, f: libc::FILE) -> uint;
fn open(s: sbuf, flags: int, mode: uint) -> int;
fn open(s: istr::sbuf, flags: int, mode: uint) -> int;
fn close(fd: int) -> int;
type FILE;
fn fopen(path: sbuf, mode: sbuf) -> FILE;
fn fdopen(fd: int, mode: sbuf) -> FILE;
fn fopen(path: istr::sbuf, mode: istr::sbuf) -> FILE;
fn fdopen(fd: int, mode: istr::sbuf) -> FILE;
fn fclose(f: FILE);
fn fgetc(f: FILE) -> int;
fn ungetc(c: int, f: FILE);
@ -21,13 +19,13 @@ native "cdecl" mod libc = "" {
fn fseek(f: FILE, offset: int, whence: int) -> int;
fn ftell(f: FILE) -> int;
type dir;
fn opendir(d: sbuf) -> dir;
fn opendir(d: istr::sbuf) -> dir;
fn closedir(d: dir) -> int;
type dirent;
fn readdir(d: dir) -> dirent;
fn getenv(n: sbuf) -> sbuf;
fn setenv(n: sbuf, v: sbuf, overwrite: int) -> int;
fn unsetenv(n: sbuf) -> int;
fn getenv(n: istr::sbuf) -> istr::sbuf;
fn setenv(n: istr::sbuf, v: istr::sbuf, overwrite: int) -> int;
fn unsetenv(n: istr::sbuf) -> int;
fn pipe(buf: *mutable int) -> int;
fn waitpid(pid: int, status: &mutable int, options: int) -> int;
}
@ -64,7 +62,11 @@ fn pipe() -> {in: int, out: int} {
ret {in: fds.in, out: fds.out};
}
fn fd_FILE(fd: int) -> libc::FILE { ret libc::fdopen(fd, str::buf("r")); }
fn fd_FILE(fd: int) -> libc::FILE {
ret istr::as_buf(~"r", { |modebuf|
libc::fdopen(fd, modebuf)
});
}
fn waitpid(pid: int) -> int {
let status = 0;

View File

@ -1,16 +1,14 @@
import str::sbuf;
native "cdecl" mod libc = "" {
fn read(fd: int, buf: *u8, count: uint) -> int;
fn write(fd: int, buf: *u8, count: uint) -> int;
fn fread(buf: *u8, size: uint, n: uint, f: libc::FILE) -> uint;
fn fwrite(buf: *u8, size: uint, n: uint, f: libc::FILE) -> uint;
fn open(s: sbuf, flags: int, mode: uint) -> int;
fn open(s: istr::sbuf, flags: int, mode: uint) -> int;
fn close(fd: int) -> int;
type FILE;
fn fopen(path: sbuf, mode: sbuf) -> FILE;
fn fdopen(fd: int, mode: sbuf) -> FILE;
fn fopen(path: istr::sbuf, mode: istr::sbuf) -> FILE;
fn fdopen(fd: int, mode: istr::sbuf) -> FILE;
fn fclose(f: FILE);
fn fgetc(f: FILE) -> int;
fn ungetc(c: int, f: FILE);
@ -18,13 +16,13 @@ native "cdecl" mod libc = "" {
fn fseek(f: FILE, offset: int, whence: int) -> int;
fn ftell(f: FILE) -> int;
type dir;
fn opendir(d: sbuf) -> dir;
fn opendir(d: istr::sbuf) -> dir;
fn closedir(d: dir) -> int;
type dirent;
fn readdir(d: dir) -> dirent;
fn getenv(n: sbuf) -> sbuf;
fn setenv(n: sbuf, v: sbuf, overwrite: int) -> int;
fn unsetenv(n: sbuf) -> int;
fn getenv(n: istr::sbuf) -> istr::sbuf;
fn setenv(n: istr::sbuf, v: istr::sbuf, overwrite: int) -> int;
fn unsetenv(n: istr::sbuf) -> int;
fn pipe(buf: *mutable int) -> int;
fn waitpid(pid: int, status: &mutable int, options: int) -> int;
}
@ -61,7 +59,11 @@ fn pipe() -> {in: int, out: int} {
ret {in: fds.in, out: fds.out};
}
fn fd_FILE(fd: int) -> libc::FILE { ret libc::fdopen(fd, str::buf("r")); }
fn fd_FILE(fd: int) -> libc::FILE {
ret istr::as_buf(~"r", { |modebuf|
libc::fdopen(fd, modebuf)
});
}
fn waitpid(pid: int) -> int {
let status = 0;

View File

@ -1,5 +1,5 @@
import str::sbuf;
import istr::sbuf;
export program;
export run_program;
@ -12,19 +12,21 @@ native "rust" mod rustrt {
int;
}
fn arg_vec(prog: str, args: &[str]) -> [sbuf] {
let argptrs = [str::buf(prog)];
for arg: str in args { argptrs += [str::buf(arg)]; }
argptrs += [0 as sbuf];
fn arg_vec(prog: &istr, args: &[@istr]) -> [sbuf] {
let argptrs = istr::as_buf(prog, { |buf| [buf] });
for arg in args {
argptrs += istr::as_buf(*arg, { |buf| [buf] });
}
argptrs += [unsafe::reinterpret_cast(0)];
ret argptrs;
}
fn spawn_process(prog: &istr, args: &[istr], in_fd: int, out_fd: int,
err_fd: int) -> int {
let prog = istr::to_estr(prog);
let args = istr::to_estrs(args);
// Note: we have to hold on to this vector reference while we hold a
// pointer to its buffer
// Note: we have to hold on to these vector references while we hold a
// pointer to their buffers
let prog = prog;
let args = vec::map({ |&arg| @arg }, args);
let argv = arg_vec(prog, args);
let pid =
rustrt::rust_run_program(vec::unsafe::to_ptr(argv),

View File

@ -1,7 +1,7 @@
import rustrt::sbuf;
import uint::le;
export sbuf;
// export sbuf;
// export rustrt;
// export eq;
// export lteq;
@ -10,7 +10,7 @@ export sbuf;
// export is_ascii;
export alloc;
// export byte_len;
export buf;
// export buf;
// export bytes;
// export unsafe_from_byte;
// export str_from_cstr;

View File

@ -1,16 +1,14 @@
import str::sbuf;
native "cdecl" mod libc = "" {
fn read(fd: int, buf: *u8, count: uint) -> int;
fn write(fd: int, buf: *u8, count: uint) -> int;
fn fread(buf: *u8, size: uint, n: uint, f: libc::FILE) -> uint;
fn fwrite(buf: *u8, size: uint, n: uint, f: libc::FILE) -> uint;
fn open(s: sbuf, flags: int, mode: uint) -> int = "_open";
fn open(s: istr::sbuf, flags: int, mode: uint) -> int = "_open";
fn close(fd: int) -> int = "_close";
type FILE;
fn fopen(path: sbuf, mode: sbuf) -> FILE;
fn _fdopen(fd: int, mode: sbuf) -> FILE;
fn fopen(path: istr::sbuf, mode: istr::sbuf) -> FILE;
fn _fdopen(fd: int, mode: istr::sbuf) -> FILE;
fn fclose(f: FILE);
fn fgetc(f: FILE) -> int;
fn ungetc(c: int, f: FILE);
@ -42,8 +40,9 @@ mod libc_constants {
}
native "x86stdcall" mod kernel32 {
fn GetEnvironmentVariableA(n: sbuf, v: sbuf, nsize: uint) -> uint;
fn SetEnvironmentVariableA(n: sbuf, v: sbuf) -> int;
fn GetEnvironmentVariableA(n: istr::sbuf, v: istr::sbuf,
nsize: uint) -> uint;
fn SetEnvironmentVariableA(n: istr::sbuf, v: istr::sbuf) -> int;
}
fn exec_suffix() -> istr { ret ~".exe"; }
@ -69,7 +68,11 @@ fn pipe() -> {in: int, out: int} {
ret {in: fds.in, out: fds.out};
}
fn fd_FILE(fd: int) -> libc::FILE { ret libc::_fdopen(fd, str::buf("r")); }
fn fd_FILE(fd: int) -> libc::FILE {
ret istr::as_buf(~"r", { |modebuf|
libc::_fdopen(fd, modebuf)
});
}
native "rust" mod rustrt {
fn rust_process_wait(handle: int) -> int;

View File

@ -1,4 +1,4 @@
// error-pattern:expected native but found native
// error-pattern:expected *Mb but found native
use std;
fn main() {