From cfd89c407598673bf55ea11525b1398c53cd3725 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 30 Jun 2013 08:25:16 -0700 Subject: [PATCH] std: remove os::as_c_charp --- src/libstd/io.rs | 27 +++++++++++------------ src/libstd/os.rs | 56 ++++++++++++++++++++++-------------------------- 2 files changed, 39 insertions(+), 44 deletions(-) diff --git a/src/libstd/io.rs b/src/libstd/io.rs index 6ceaf5caea3..6335588db50 100644 --- a/src/libstd/io.rs +++ b/src/libstd/io.rs @@ -63,7 +63,7 @@ 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 @@ fn wb() -> c_int { O_WRONLY as c_int } } } 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) } }; diff --git a/src/libstd/os.rs b/src/libstd/os.rs index 5981926fce3..2c3ce86ef78 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -87,10 +87,6 @@ pub fn getcwd() -> Path { // FIXME: move these to str perhaps? #2620 -pub fn as_c_charp(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 @@ 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 @@ fn load_self() -> Option<~str> { 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 @@ fn mkdir(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 @@ fn rmdir(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 @@ fn chdir(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 @@ fn do_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 @@ fn do_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 @@ fn unlink(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 @@ fn copy_file_ok() { 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 @@ fn lseek_(fd: c_int, size: uint) { } } - 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 -- 2.44.0