std: remove os::as_c_charp

This commit is contained in:
Erick Tryzelaar 2013-06-30 08:25:16 -07:00
parent 9ad815e063
commit cfd89c4075
2 changed files with 39 additions and 44 deletions

View File

@ -63,7 +63,7 @@ use iterator::IteratorUtil;
use ptr;
use result;
use str;
use str::StrSlice;
use str::{StrSlice, OwnedStr, StrUtil};
use to_str::ToStr;
use uint;
use vec;
@ -1031,17 +1031,16 @@ pub fn stdin() -> @Reader {
}
pub fn file_reader(path: &Path) -> Result<@Reader, ~str> {
unsafe {
let f = os::as_c_charp(path.to_str(), |pathbuf| {
os::as_c_charp("r", |modebuf|
libc::fopen(pathbuf, modebuf)
)
});
return if f as uint == 0u { result::Err(~"error opening "
+ path.to_str()) }
else {
result::Ok(FILE_reader(f, true))
let f = do path.to_str().as_c_str |pathbuf| {
do "r".as_c_str |modebuf| {
unsafe { libc::fopen(pathbuf, modebuf as *libc::c_char) }
}
};
if f as uint == 0u {
result::Err(~"error opening " + path.to_str())
} else {
result::Ok(FILE_reader(f, true))
}
}
@ -1282,7 +1281,7 @@ pub fn mk_file_writer(path: &Path, flags: &[FileFlag])
}
}
let fd = unsafe {
do os::as_c_charp(path.to_str()) |pathbuf| {
do path.to_str().as_c_str |pathbuf| {
libc::open(pathbuf, fflags,
(S_IRUSR | S_IWUSR) as c_int)
}
@ -1567,8 +1566,8 @@ pub fn file_writer(path: &Path, flags: &[FileFlag]) -> Result<@Writer, ~str> {
// FIXME: fileflags // #2004
pub fn buffered_file_writer(path: &Path) -> Result<@Writer, ~str> {
unsafe {
let f = do os::as_c_charp(path.to_str()) |pathbuf| {
do os::as_c_charp("w") |modebuf| {
let f = do path.to_str().as_c_str |pathbuf| {
do "w".as_c_str |modebuf| {
libc::fopen(pathbuf, modebuf)
}
};

View File

@ -87,10 +87,6 @@ pub fn getcwd() -> Path {
// FIXME: move these to str perhaps? #2620
pub fn as_c_charp<T>(s: &str, f: &fn(*c_char) -> T) -> T {
str::as_c_str(s, |b| f(b as *c_char))
}
pub fn fill_charp_buf(f: &fn(*mut c_char, size_t) -> bool)
-> Option<~str> {
let mut buf = vec::from_elem(TMPBUF_SZ, 0u8 as c_char);
@ -335,10 +331,10 @@ pub fn unsetenv(n: &str) {
}
pub fn fdopen(fd: c_int) -> *FILE {
unsafe {
return do as_c_charp("r") |modebuf| {
do "r".as_c_str |modebuf| {
unsafe {
libc::fdopen(fd, modebuf)
};
}
}
}
@ -471,7 +467,7 @@ pub fn self_exe_path() -> Option<Path> {
let mut path_str = str::with_capacity(TMPBUF_SZ);
let len = do str::as_c_str(path_str) |buf| {
let buf = buf as *mut c_char;
do as_c_charp("/proc/self/exe") |proc_self_buf| {
do "/proc/self/exe".as_c_str |proc_self_buf| {
readlink(proc_self_buf, buf, TMPBUF_SZ as size_t)
}
};
@ -654,9 +650,9 @@ pub fn make_dir(p: &Path, mode: c_int) -> bool {
#[cfg(unix)]
fn mkdir(p: &Path, mode: c_int) -> bool {
unsafe {
do as_c_charp(p.to_str()) |c| {
libc::mkdir(c, mode as libc::mode_t) == (0 as c_int)
do p.to_str().as_c_str |buf| {
unsafe {
libc::mkdir(buf, mode as libc::mode_t) == (0 as c_int)
}
}
}
@ -830,10 +826,10 @@ pub fn remove_dir(p: &Path) -> bool {
#[cfg(unix)]
fn rmdir(p: &Path) -> bool {
unsafe {
return do as_c_charp(p.to_str()) |buf| {
do p.to_str().as_c_str |buf| {
unsafe {
libc::rmdir(buf) == (0 as c_int)
};
}
}
}
}
@ -855,10 +851,10 @@ pub fn change_dir(p: &Path) -> bool {
#[cfg(unix)]
fn chdir(p: &Path) -> bool {
unsafe {
return do as_c_charp(p.to_str()) |buf| {
do p.to_str().as_c_str |buf| {
unsafe {
libc::chdir(buf) == (0 as c_int)
};
}
}
}
}
@ -883,8 +879,8 @@ pub fn copy_file(from: &Path, to: &Path) -> bool {
#[cfg(unix)]
fn do_copy_file(from: &Path, to: &Path) -> bool {
unsafe {
let istream = do as_c_charp(from.to_str()) |fromp| {
do as_c_charp("rb") |modebuf| {
let istream = do from.to_str().as_c_str |fromp| {
do "rb".as_c_str |modebuf| {
libc::fopen(fromp, modebuf)
}
};
@ -895,8 +891,8 @@ pub fn copy_file(from: &Path, to: &Path) -> bool {
let from_mode = from.get_mode().expect("copy_file: couldn't get permissions \
for source file");
let ostream = do as_c_charp(to.to_str()) |top| {
do as_c_charp("w+b") |modebuf| {
let ostream = do to.to_str().as_c_str |top| {
do "w+b".as_c_str |modebuf| {
libc::fopen(top, modebuf)
}
};
@ -955,9 +951,9 @@ pub fn remove_file(p: &Path) -> bool {
#[cfg(unix)]
fn unlink(p: &Path) -> bool {
unsafe {
return do as_c_charp(p.to_str()) |buf| {
do p.to_str().as_c_str |buf| {
libc::unlink(buf) == (0 as c_int)
};
}
}
}
}
@ -1703,7 +1699,7 @@ mod tests {
use libc;
use option::Some;
use option;
use os::{as_c_charp, env, getcwd, getenv, make_absolute, real_args};
use os::{env, getcwd, getenv, make_absolute, real_args};
use os::{remove_file, setenv, unsetenv};
use os;
use path::Path;
@ -1941,8 +1937,8 @@ mod tests {
let out = tempdir.push("out.txt");
/* Write the temp input file */
let ostream = do as_c_charp(in.to_str()) |fromp| {
do as_c_charp("w+b") |modebuf| {
let ostream = do in.to_str().as_c_str |fromp| {
do "w+b".as_c_str |modebuf| {
libc::fopen(fromp, modebuf)
}
};
@ -2020,16 +2016,16 @@ mod tests {
}
}
let p = tmpdir().push("mmap_file.tmp");
let path = tmpdir().push("mmap_file.tmp");
let size = page_size() * 2;
remove_file(&p);
remove_file(&path);
let fd = unsafe {
let fd = do as_c_charp(p.to_str()) |path| {
let fd = do path.to_str().as_c_str |path| {
open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR)
};
lseek_(fd, size);
do as_c_charp("x") |x| {
do "x".as_c_str |x| {
assert!(write(fd, x as *c_void, 1) == 1);
}
fd