From 50692c3e291d2856b0dfe5ed22051c021039bb99 Mon Sep 17 00:00:00 2001 From: q66 Date: Fri, 2 Jun 2017 18:33:56 +0200 Subject: [PATCH] remove the temp_cstr stuff --- ostd/string.hh | 69 -------------------------------------------------- src/environ.cc | 35 ++++--------------------- 2 files changed, 5 insertions(+), 99 deletions(-) diff --git a/ostd/string.hh b/ostd/string.hh index 4f2dcf8..97b40fa 100644 --- a/ostd/string.hh +++ b/ostd/string.hh @@ -707,75 +707,6 @@ inline namespace string_literals { } } -template -struct temp_c_string { -private: - std::remove_cv_t> *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> *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>[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> const *() const { - return p_buf; - } - std::remove_cv_t> 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 -inline void swap(temp_c_string &a, temp_c_string &b) { - a.swap(b); -} - -template -inline temp_c_string to_temp_cstr( - R input, std::remove_cv_t> *buf, std::size_t bufsize -) { - return temp_c_string(input, buf, bufsize); -} - /** @} */ } /* namespace ostd */ diff --git a/src/environ.cc b/src/environ.cc index d45be4c..1424be8 100644 --- a/src/environ.cc +++ b/src/environ.cc @@ -17,9 +17,7 @@ namespace ostd { OSTD_EXPORT std::optional 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 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