浏览代码

sys: implement lol::sys::getenv() for safety.

legacy
Sam Hocevar 5 年前
父节点
当前提交
e05e9ff143
共有 3 个文件被更改,包括 21 次插入4 次删除
  1. +1
    -2
      src/base/features.cpp
  2. +1
    -2
      src/base/log.cpp
  3. +19
    -0
      src/lol/sys/init.h

+ 1
- 2
src/base/features.cpp 查看文件

@@ -17,8 +17,7 @@ namespace lol

bool has_threads()
{
static char const *var = getenv("LOL_NOTHREADS");
static bool const disable_threads = var && var[0];
static bool const disable_threads = sys::getenv("LOL_NOTHREADS").size() > 0;
#if defined __EMSCRIPTEN__ && !defined __EMSCRIPTEN_PTHREADS__
// For some reason hardware_concurrency() will return the actual number
// of threads/cores even though the system cannot spawn threads.


+ 1
- 2
src/base/log.cpp 查看文件

@@ -80,8 +80,7 @@ void msg::helper(message_type type, char const *fmt, va_list ap)
#if !defined LOL_BUILD_DEBUG && !_DEBUG
if (type == message_type::debug)
{
static char const *var = getenv("LOL_DEBUG");
static bool const disable_debug = !var || !var[0];
static bool const disable_debug = sys::getenv("LOL_DEBUG").size() > 0;
if (disable_debug)
return;
}


+ 19
- 0
src/lol/sys/init.h 查看文件

@@ -19,6 +19,7 @@

#include <string>
#include <cstdint>
#include <cstdlib>

namespace lol
{
@@ -56,6 +57,24 @@ extern void init(int argc, char *argv[],
extern void add_data_dir(std::string const &dir);
extern array<std::string> get_path_list(std::string const &file);

static inline std::string getenv(std::string const &var)
{
#if _MSC_VER
char *buf = nullptr;
size_t count = 0;
if (_dupenv_s(&buf, &count, var.c_str()) == 0 && buf)
{
std::string ret(buf);
free(buf);
return ret;
}
#else
if (auto var = std::getenv(var.c_str()))
return std::string(var);
#endif
return std::string();
}

} /* namespace sys */

} /* namespace lol */


正在加载...
取消
保存