remove internal/win32.hh and put environ stuff into a source file
parent
baa8ff1e64
commit
94814dc61e
2
build.cc
2
build.cc
|
@ -40,7 +40,7 @@ static std::string ASM_SOURCES[] = {
|
||||||
|
|
||||||
static fs::path CXX_SOURCE_DIR = "src";
|
static fs::path CXX_SOURCE_DIR = "src";
|
||||||
static std::string CXX_SOURCES[] = {
|
static std::string CXX_SOURCES[] = {
|
||||||
"context_stack", "io", "concurrency"
|
"context_stack", "environ", "io", "concurrency"
|
||||||
};
|
};
|
||||||
|
|
||||||
static fs::path TEST_DIR = "tests";
|
static fs::path TEST_DIR = "tests";
|
||||||
|
|
|
@ -19,15 +19,11 @@
|
||||||
#define OSTD_ENVIRON_HH
|
#define OSTD_ENVIRON_HH
|
||||||
|
|
||||||
#include "ostd/platform.hh"
|
#include "ostd/platform.hh"
|
||||||
#include "ostd/internal/win32.hh"
|
|
||||||
|
|
||||||
#include <cstdlib>
|
|
||||||
#include <cstring>
|
|
||||||
#include <vector>
|
|
||||||
#include <optional>
|
|
||||||
|
|
||||||
#include "ostd/string.hh"
|
#include "ostd/string.hh"
|
||||||
|
|
||||||
|
#include <optional>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
namespace ostd {
|
namespace ostd {
|
||||||
|
|
||||||
/** @addtogroup Utilities
|
/** @addtogroup Utilities
|
||||||
|
@ -46,15 +42,7 @@ namespace ostd {
|
||||||
*
|
*
|
||||||
* @see env_set(), env_unset()
|
* @see env_set(), env_unset()
|
||||||
*/
|
*/
|
||||||
inline std::optional<std::string> env_get(string_range name) {
|
OSTD_EXPORT std::optional<std::string> env_get(string_range name);
|
||||||
char buf[256];
|
|
||||||
auto tbuf = to_temp_cstr(name, buf, sizeof(buf));
|
|
||||||
char const *ret = std::getenv(tbuf.get());
|
|
||||||
if (!ret) {
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
return std::string{ret};
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @brief Sets an environment variable.
|
/** @brief Sets an environment variable.
|
||||||
*
|
*
|
||||||
|
@ -70,32 +58,7 @@ inline std::optional<std::string> env_get(string_range name) {
|
||||||
*
|
*
|
||||||
* @see env_get(), env_unset()
|
* @see env_get(), env_unset()
|
||||||
*/
|
*/
|
||||||
inline bool env_set(
|
OSTD_EXPORT bool env_set(string_range name, string_range value, bool update = true);
|
||||||
string_range name, string_range value, bool update = true
|
|
||||||
) {
|
|
||||||
char sbuf[2048];
|
|
||||||
char *buf = sbuf;
|
|
||||||
bool alloc = (name.size() + value.size() + 2) > sizeof(sbuf);
|
|
||||||
if (alloc) {
|
|
||||||
buf = new char[name.size() + value.size() + 2];
|
|
||||||
}
|
|
||||||
memcpy(buf, name.data(), name.size());
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @brief Unsets an environment varible.
|
/** @brief Unsets an environment varible.
|
||||||
*
|
*
|
||||||
|
@ -107,23 +70,7 @@ inline bool env_set(
|
||||||
*
|
*
|
||||||
* @see env_get(), env_set()
|
* @see env_get(), env_set()
|
||||||
*/
|
*/
|
||||||
inline bool env_unset(string_range name) {
|
OSTD_EXPORT bool env_unset(string_range name);
|
||||||
char buf[256];
|
|
||||||
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(std::string{name}.data());
|
|
||||||
#else
|
|
||||||
return !!SetEnvironmentVariable(std::string{name}.data(), nullptr);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @} */
|
/** @} */
|
||||||
|
|
||||||
|
|
|
@ -1,15 +0,0 @@
|
||||||
/* Windows includes.
|
|
||||||
*
|
|
||||||
* This file is part of libostd. See COPYING.md for futher information.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef OSTD_INTERNAL_WIN32_HH
|
|
||||||
#define OSTD_INTERNAL_WIN32_HH
|
|
||||||
|
|
||||||
#if defined(WIN32) || defined(_WIN32) || (defined(__WIN32) && !defined(__CYGWIN__))
|
|
||||||
#define WIN32_LEAN_AND_MEAN
|
|
||||||
#define NOMINMAX
|
|
||||||
#include <windows.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
|
|
@ -8,7 +8,6 @@
|
||||||
#include <new>
|
#include <new>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
|
||||||
#include "ostd/internal/win32.hh"
|
|
||||||
#include "ostd/platform.hh"
|
#include "ostd/platform.hh"
|
||||||
#include "ostd/context_stack.hh"
|
#include "ostd/context_stack.hh"
|
||||||
|
|
||||||
|
@ -18,6 +17,9 @@
|
||||||
# include <sys/resource.h>
|
# include <sys/resource.h>
|
||||||
# include <sys/time.h>
|
# include <sys/time.h>
|
||||||
# include <signal.h>
|
# include <signal.h>
|
||||||
|
#else
|
||||||
|
# define WIN32_LEAN_AND_MEAN
|
||||||
|
# include <windows.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace ostd {
|
namespace ostd {
|
||||||
|
|
|
@ -0,0 +1,72 @@
|
||||||
|
/* Environment handling implementation bits.
|
||||||
|
*
|
||||||
|
* This file is part of libostd. See COPYING.md for futher information.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <cstring>
|
||||||
|
#include <new>
|
||||||
|
|
||||||
|
#include "ostd/environ.hh"
|
||||||
|
|
||||||
|
#ifdef OSTD_PLATFORM_WIN32
|
||||||
|
# define WIN32_LEAN_AND_MEAN
|
||||||
|
# include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace ostd {
|
||||||
|
|
||||||
|
OSTD_EXPORT std::optional<std::string> env_get(string_range name) {
|
||||||
|
char buf[256];
|
||||||
|
auto tbuf = to_temp_cstr(name, buf, sizeof(buf));
|
||||||
|
char const *ret = std::getenv(tbuf.get());
|
||||||
|
if (!ret) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
return std::string{ret};
|
||||||
|
}
|
||||||
|
|
||||||
|
OSTD_EXPORT bool env_set(string_range name, string_range value, bool update) {
|
||||||
|
char sbuf[2048];
|
||||||
|
char *buf = sbuf;
|
||||||
|
bool alloc = (name.size() + value.size() + 2) > sizeof(sbuf);
|
||||||
|
if (alloc) {
|
||||||
|
buf = new char[name.size() + value.size() + 2];
|
||||||
|
}
|
||||||
|
std::memcpy(buf, name.data(), name.size());
|
||||||
|
buf[name.size()] = '\0';
|
||||||
|
std::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;
|
||||||
|
}
|
||||||
|
|
||||||
|
OSTD_EXPORT bool env_unset(string_range name) {
|
||||||
|
char buf[256];
|
||||||
|
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(std::string{name}.data());
|
||||||
|
#else
|
||||||
|
return !!SetEnvironmentVariable(std::string{name}.data(), nullptr);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
} /* namespace ostd */
|
Loading…
Reference in New Issue