use ptr;
use result;
use str;
-use str::StrSlice;
+use str::{StrSlice, OwnedStr, StrUtil};
use to_str::ToStr;
use uint;
use vec;
}
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))
}
}
}
}
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)
}
// 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)
}
};
// 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);
}
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)
- };
+ }
}
}
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)
}
};
#[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)
}
}
}
#[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)
- };
+ }
}
}
}
#[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)
- };
+ }
}
}
}
#[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)
}
};
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)
}
};
#[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)
- };
+ }
}
}
}
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;
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)
}
};
}
}
- 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