Convert std::generic_os to istrs. Issue #855

This commit is contained in:
Brian Anderson 2011-08-24 23:34:10 -07:00
parent ccc68fc18b
commit 85b4253bc1
6 changed files with 41 additions and 25 deletions

View File

@ -5,6 +5,7 @@
* interface.
*/
import std::vec;
import std::istr;
import std::option;
import std::generic_os;
import base::*;
@ -26,9 +27,11 @@ fn expand_syntax_ext(cx: &ext_ctxt, sp: codemap::span, arg: @ast::expr,
// option::t<str> rather than just an maybe-empty string.
let var = expr_to_str(cx, args[0], "#env requires a string");
alt generic_os::getenv(var) {
alt generic_os::getenv(istr::from_estr(var)) {
option::none. { ret make_new_str(cx, sp, ""); }
option::some(s) { ret make_new_str(cx, sp, s); }
option::some(s) {
ret make_new_str(cx, sp, istr::to_estr(s));
}
}
}

View File

@ -3,23 +3,30 @@ import str::sbuf;
#[cfg(target_os = "linux")]
#[cfg(target_os = "macos")]
fn getenv(n: str) -> option::t<str> {
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 {
option::none::<str>
} else { option::some::<str>(str::str_from_cstr(s)) };
option::none::<istr>
} else {
let s = unsafe::reinterpret_cast(s);
option::some::<istr>(istr::str_from_cstr(s))
};
}
#[cfg(target_os = "linux")]
#[cfg(target_os = "macos")]
fn setenv(n: str, v: str) {
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);
}
#[cfg(target_os = "win32")]
fn getenv(n: str) -> option::t<str> {
fn getenv(n: &istr) -> option::t<istr> {
let n = istr::to_estr(n);
let nbuf = str::buf(n);
let nsize = 256u;
while true {
@ -29,14 +36,17 @@ fn getenv(n: str) -> option::t<str> {
if res == 0u {
ret option::none;
} else if res < nsize {
ret option::some(str::str_from_cstr(vbuf));
let vbuf = unsafe::reinterpret_cast(vbuf);
ret option::some(istr::str_from_cstr(vbuf));
} else { nsize = res; }
}
fail;
}
#[cfg(target_os = "win32")]
fn setenv(n: str, v: str) {
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);

View File

@ -49,10 +49,10 @@ fn reset(writer: io::buf_writer) {
fn color_supported() -> bool {
let supported_terms = [~"xterm-color", ~"xterm", ~"screen-bce"];
ret alt generic_os::getenv("TERM") {
ret alt generic_os::getenv(~"TERM") {
option::some(env) {
for term: istr in supported_terms {
if istr::eq(term, istr::from_estr(env)) { ret true; }
if istr::eq(term, env) { ret true; }
}
false
}

View File

@ -153,11 +153,11 @@ fn worker(p: port<request>) {
}
fn with_lib_path<@T>(path: &str, f: fn() -> T) -> T {
let maybe_oldpath = getenv(util::lib_path_env_var());
let maybe_oldpath = getenv(istr::from_estr(util::lib_path_env_var()));
append_lib_path(path);
let res = f();
if option::is_some(maybe_oldpath) {
export_lib_path(option::get(maybe_oldpath));
export_lib_path(istr::to_estr(option::get(maybe_oldpath)));
} else {
// FIXME: This should really be unset but we don't have that yet
export_lib_path("");
@ -167,7 +167,9 @@ fn with_lib_path<@T>(path: &str, f: fn() -> T) -> T {
fn append_lib_path(path: &str) { export_lib_path(util::make_new_path(path)); }
fn export_lib_path(path: &str) { setenv(util::lib_path_env_var(), path); }
fn export_lib_path(path: &str) {
setenv(istr::from_estr(util::lib_path_env_var()), istr::from_estr(path));
}
fn clone_vecstr(v: &[str]) -> [[u8]] {
let r = [];

View File

@ -1,6 +1,7 @@
import std::option;
import std::generic_os::getenv;
import std::io;
import std::istr;
import common::config;
@ -8,8 +9,8 @@ fn make_new_path(path: &str) -> str {
// Windows just uses PATH as the library search path, so we have to
// maintain the current value while adding our own
alt getenv(lib_path_env_var()) {
option::some(curr) { #fmt["%s:%s", path, curr] }
alt getenv(istr::from_estr(lib_path_env_var())) {
option::some(curr) { #fmt["%s:%s", path, istr::to_estr(curr)] }
option::none. { path }
}
}

View File

@ -6,26 +6,26 @@ import std::option;
fn test_setenv() {
// NB: Each test of setenv needs to use different variable names or the
// tests will not be threadsafe
setenv("NAME1", "VALUE");
assert (getenv("NAME1") == option::some("VALUE"));
setenv(~"NAME1", ~"VALUE");
assert (getenv(~"NAME1") == option::some(~"VALUE"));
}
#[test]
fn test_setenv_overwrite() {
setenv("NAME2", "1");
setenv("NAME2", "2");
assert (getenv("NAME2") == option::some("2"));
setenv(~"NAME2", ~"1");
setenv(~"NAME2", ~"2");
assert (getenv(~"NAME2") == option::some(~"2"));
}
// Windows GetEnvironmentVariable requires some extra work to make sure
// the buffer the variable is copied into is the right size
#[test]
fn test_getenv_big() {
let s = "";
let s = ~"";
let i = 0;
while i < 100 { s += "aaaaaaaaaa"; i += 1; }
setenv("NAME3", s);
assert (getenv("NAME3") == option::some(s));
while i < 100 { s += ~"aaaaaaaaaa"; i += 1; }
setenv(~"NAME3", s);
assert (getenv(~"NAME3") == option::some(s));
}
// Local Variables: