forked from OctaForge/libostd
remove the temp_cstr stuff
parent
3df3ece0d2
commit
50692c3e29
|
@ -707,75 +707,6 @@ inline namespace string_literals {
|
|||
}
|
||||
}
|
||||
|
||||
template<typename R>
|
||||
struct temp_c_string {
|
||||
private:
|
||||
std::remove_cv_t<range_value_t<R>> *p_buf;
|
||||
bool p_allocated;
|
||||
|
||||
public:
|
||||
temp_c_string() = delete;
|
||||
temp_c_string(temp_c_string const &) = delete;
|
||||
temp_c_string(temp_c_string &&s):
|
||||
p_buf(s.p_buf), p_allocated(s.p_allocated)
|
||||
{
|
||||
s.p_buf = nullptr;
|
||||
s.p_allocated = false;
|
||||
}
|
||||
temp_c_string(
|
||||
R input, std::remove_cv_t<range_value_t<R>> *sbuf, std::size_t bufsize
|
||||
): p_buf(nullptr), p_allocated(false) {
|
||||
if (input.empty()) {
|
||||
return;
|
||||
}
|
||||
if (input.size() >= bufsize) {
|
||||
p_buf = new std::remove_cv_t<range_value_t<R>>[input.size() + 1];
|
||||
p_allocated = true;
|
||||
} else {
|
||||
p_buf = sbuf;
|
||||
}
|
||||
char_range bufr{p_buf, p_buf + input.size() + 1};
|
||||
range_put_all(bufr, input);
|
||||
bufr.put('\0');
|
||||
}
|
||||
~temp_c_string() {
|
||||
if (p_allocated) {
|
||||
delete[] p_buf;
|
||||
}
|
||||
}
|
||||
|
||||
temp_c_string &operator=(temp_c_string const &) = delete;
|
||||
temp_c_string &operator=(temp_c_string &&s) {
|
||||
swap(s);
|
||||
return *this;
|
||||
}
|
||||
|
||||
operator std::remove_cv_t<range_value_t<R>> const *() const {
|
||||
return p_buf;
|
||||
}
|
||||
std::remove_cv_t<range_value_t<R>> const *get() const {
|
||||
return p_buf;
|
||||
}
|
||||
|
||||
void swap(temp_c_string &s) {
|
||||
using std::swap;
|
||||
swap(p_buf, s.p_buf);
|
||||
swap(p_allocated, s.p_allocated);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename R>
|
||||
inline void swap(temp_c_string<R> &a, temp_c_string<R> &b) {
|
||||
a.swap(b);
|
||||
}
|
||||
|
||||
template<typename R>
|
||||
inline temp_c_string<R> to_temp_cstr(
|
||||
R input, std::remove_cv_t<range_value_t<R>> *buf, std::size_t bufsize
|
||||
) {
|
||||
return temp_c_string<R>(input, buf, bufsize);
|
||||
}
|
||||
|
||||
/** @} */
|
||||
|
||||
} /* namespace ostd */
|
||||
|
|
|
@ -17,9 +17,7 @@
|
|||
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());
|
||||
char const *ret = std::getenv(std::string{name}.data());
|
||||
if (!ret) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
@ -27,41 +25,18 @@ OSTD_EXPORT std::optional<std::string> env_get(string_range name) {
|
|||
}
|
||||
|
||||
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);
|
||||
return !setenv(std::string{name}.data(), std::string{value}.data(), update);
|
||||
#else
|
||||
if (!update && GetEnvironmentVariable(buf, nullptr, 0)) {
|
||||
std::string nstr{name};
|
||||
if (!update && GetEnvironmentVariable(nstr.data(), nullptr, 0)) {
|
||||
return true;
|
||||
}
|
||||
bool ret = !!SetEnvironmentVariable(buf, &buf[name.size() + 1]);
|
||||
return !!SetEnvironmentVariable(nstr.data(), std::string{value}.data());
|
||||
#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
|
||||
|
|
Loading…
Reference in New Issue