1
Fork 0

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

View file

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