forked from OctaForge/libostd
environment api for windows, and rename environ namespace to envvar because windows is retarded
This commit is contained in:
parent
dd35b0bf05
commit
b02966fb9d
|
@ -6,20 +6,41 @@
|
||||||
#ifndef OSTD_ENVIRON_HH
|
#ifndef OSTD_ENVIRON_HH
|
||||||
#define OSTD_ENVIRON_HH
|
#define OSTD_ENVIRON_HH
|
||||||
|
|
||||||
|
#include "ostd/platform.hh"
|
||||||
|
#include "ostd/internal/win32.hh"
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "ostd/maybe.hh"
|
#include "ostd/maybe.hh"
|
||||||
#include "ostd/string.hh"
|
#include "ostd/string.hh"
|
||||||
|
|
||||||
namespace ostd {
|
/* TODO: make POSIX version thread safe, the Windows version is... */
|
||||||
namespace environ {
|
|
||||||
|
|
||||||
inline Maybe<ConstCharRange> get(ConstCharRange name) {
|
namespace ostd {
|
||||||
|
namespace envvar {
|
||||||
|
|
||||||
|
inline Maybe<String> get(ConstCharRange name) {
|
||||||
char buf[256];
|
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;
|
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,
|
inline bool set(ConstCharRange name, ConstCharRange value,
|
||||||
|
@ -33,7 +54,13 @@ inline bool set(ConstCharRange name, ConstCharRange value,
|
||||||
buf[name.size()] = '\0';
|
buf[name.size()] = '\0';
|
||||||
memcpy(&buf[name.size() + 1], value.data(), value.size());
|
memcpy(&buf[name.size() + 1], value.data(), value.size());
|
||||||
buf[name.size() + value.size() + 1] = '\0';
|
buf[name.size() + value.size() + 1] = '\0';
|
||||||
|
#ifndef OSTD_PLATFORM_WIN32
|
||||||
bool ret = !setenv(buf, &buf[name.size() + 1], update);
|
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)
|
if (alloc)
|
||||||
delete[] buf;
|
delete[] buf;
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -44,12 +71,20 @@ inline bool unset(ConstCharRange name) {
|
||||||
if (name.size() < sizeof(buf)) {
|
if (name.size() < sizeof(buf)) {
|
||||||
memcpy(buf, name.data(), name.size());
|
memcpy(buf, name.data(), name.size());
|
||||||
buf[name.size()] = '\0';
|
buf[name.size()] = '\0';
|
||||||
|
#ifndef OSTD_PLATFORM_WIN32
|
||||||
return !unsetenv(buf);
|
return !unsetenv(buf);
|
||||||
|
#else
|
||||||
|
return !!SetEnvironmentVariable(buf, nullptr);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
#ifndef OSTD_PLATFORM_WIN32
|
||||||
return !unsetenv(String(name).data());
|
return !unsetenv(String(name).data());
|
||||||
|
#else
|
||||||
|
return !!SetEnvironmentVariable(String(name).data(), nullptr);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
} /* namespace environ */
|
} /* namespace envvar */
|
||||||
} /* namespace ostd */
|
} /* namespace ostd */
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -24,13 +24,13 @@ constexpr auto ColorEnd = "";
|
||||||
int main() {
|
int main() {
|
||||||
/* configurable section */
|
/* 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 "
|
auto cxxflags = "-std=c++14 -I. -Wall -Wextra -Wshadow -Wold-style-cast "
|
||||||
"-Wno-missing-braces"; /* clang false positive */
|
"-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 srcext = ".cc";
|
||||||
|
|
||||||
auto userflags = environ::get("CXXFLAGS").value_or("");
|
auto userflags = envvar::get("CXXFLAGS").value_or("");
|
||||||
|
|
||||||
/* do not change past this point */
|
/* do not change past this point */
|
||||||
|
|
||||||
|
@ -66,7 +66,6 @@ int main() {
|
||||||
cxxcmd.get() += ' ';
|
cxxcmd.get() += ' ';
|
||||||
cxxcmd.get() += userflags;
|
cxxcmd.get() += userflags;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ret = system(cxxcmd.get().data());
|
int ret = system(cxxcmd.get().data());
|
||||||
if (ret) {
|
if (ret) {
|
||||||
print_result("compile errror");
|
print_result("compile errror");
|
||||||
|
|
Loading…
Reference in a new issue