From b02966fb9db18d402bf6fb597972e9e7809922a1 Mon Sep 17 00:00:00 2001 From: q66 Date: Wed, 6 Jul 2016 20:40:43 +0200 Subject: [PATCH] environment api for windows, and rename environ namespace to envvar because windows is retarded --- ostd/environ.hh | 47 +++++++++++++++++++++++++++++++++++++++++------ test_runner.cc | 7 +++---- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/ostd/environ.hh b/ostd/environ.hh index 795b719..47ac05f 100644 --- a/ostd/environ.hh +++ b/ostd/environ.hh @@ -6,20 +6,41 @@ #ifndef OSTD_ENVIRON_HH #define OSTD_ENVIRON_HH +#include "ostd/platform.hh" +#include "ostd/internal/win32.hh" + #include #include #include "ostd/maybe.hh" #include "ostd/string.hh" -namespace ostd { -namespace environ { +/* TODO: make POSIX version thread safe, the Windows version is... */ -inline Maybe get(ConstCharRange name) { +namespace ostd { +namespace envvar { + +inline Maybe get(ConstCharRange name) { char buf[256]; - char const *ret = getenv(to_temp_cstr(name, buf, sizeof(buf)).get()); + auto tbuf = to_temp_cstr(name, buf, sizeof(buf)); +#ifndef OSTD_PLATFORM_WIN32 + char const *ret = getenv(tbuf.get()); if (!ret) return ostd::nothing; - return ostd::move(ConstCharRange(ret)); + return ostd::move(String(ret)); +#else + String rbuf; + for (;;) { + auto ret = GetEnvironmentVariable(tbuf.get(), rbuf.data(), + rbuf.capacity() + 1); + if (!ret) return ostd::nothing; + if (ret <= rbuf.capacity()) { + rbuf.advance(ret); + break; + } + rbuf.reserve(ret - 1); + } + return ostd::move(rbuf); +#endif } inline bool set(ConstCharRange name, ConstCharRange value, @@ -33,7 +54,13 @@ inline bool set(ConstCharRange name, ConstCharRange value, buf[name.size()] = '\0'; memcpy(&buf[name.size() + 1], value.data(), value.size()); buf[name.size() + value.size() + 1] = '\0'; +#ifndef OSTD_PLATFORM_WIN32 bool ret = !setenv(buf, &buf[name.size() + 1], update); +#else + if (!update && GetEnvironmentVariable(buf, nullptr, 0)) + return true; + bool ret = !!SetEnvironmentVariable(buf, &buf[name.size() + 1]); +#endif if (alloc) delete[] buf; return ret; @@ -44,12 +71,20 @@ inline bool unset(ConstCharRange name) { if (name.size() < sizeof(buf)) { memcpy(buf, name.data(), name.size()); buf[name.size()] = '\0'; +#ifndef OSTD_PLATFORM_WIN32 return !unsetenv(buf); +#else + return !!SetEnvironmentVariable(buf, nullptr); +#endif } +#ifndef OSTD_PLATFORM_WIN32 return !unsetenv(String(name).data()); +#else + return !!SetEnvironmentVariable(String(name).data(), nullptr); +#endif } -} /* namespace environ */ +} /* namespace envvar */ } /* namespace ostd */ #endif diff --git a/test_runner.cc b/test_runner.cc index 89f4865..d1efff7 100644 --- a/test_runner.cc +++ b/test_runner.cc @@ -24,13 +24,13 @@ constexpr auto ColorEnd = ""; int main() { /* configurable section */ - auto compiler = environ::get("CXX").value_or("c++"); + auto compiler = envvar::get("CXX").value_or("c++"); auto cxxflags = "-std=c++14 -I. -Wall -Wextra -Wshadow -Wold-style-cast " "-Wno-missing-braces"; /* clang false positive */ - auto testdir = environ::get("TESTDIR").value_or("tests"); + auto testdir = envvar::get("TESTDIR").value_or("tests"); auto srcext = ".cc"; - auto userflags = environ::get("CXXFLAGS").value_or(""); + auto userflags = envvar::get("CXXFLAGS").value_or(""); /* do not change past this point */ @@ -66,7 +66,6 @@ int main() { cxxcmd.get() += ' '; cxxcmd.get() += userflags; } - int ret = system(cxxcmd.get().data()); if (ret) { print_result("compile errror");