guhg
This commit is contained in:
parent
24b6a72614
commit
1a2fba7fb8
29 changed files with 1676 additions and 1401 deletions
43
src/core/util/helpers.hpp
Normal file
43
src/core/util/helpers.hpp
Normal file
|
@ -0,0 +1,43 @@
|
|||
#pragma once
|
||||
|
||||
#include "defs.hpp"
|
||||
#include "error.hpp"
|
||||
#include "types.hpp"
|
||||
|
||||
namespace util::helpers {
|
||||
using types::Result, types::String, types::CStr, types::UniquePointer, types::Err;
|
||||
|
||||
/**
|
||||
* @brief Safely retrieves an environment variable.
|
||||
* @param name The name of the environment variable to retrieve.
|
||||
* @return A Result containing the value of the environment variable as a String,
|
||||
* or an EnvError if an error occurred.
|
||||
*/
|
||||
[[nodiscard]] inline fn GetEnv(CStr name) -> Result<String, error::DraconisError> {
|
||||
#ifdef _WIN32
|
||||
char* rawPtr = nullptr;
|
||||
usize bufferSize = 0;
|
||||
|
||||
// Use _dupenv_s to safely retrieve environment variables on Windows
|
||||
const i32 err = _dupenv_s(&rawPtr, &bufferSize, name);
|
||||
|
||||
const UniquePointer<char, decltype(&free)> ptrManager(rawPtr, free);
|
||||
|
||||
if (err != 0)
|
||||
return Err(DraconisError(DraconisErrorCode::PermissionDenied, "Failed to retrieve environment variable"));
|
||||
|
||||
if (!ptrManager)
|
||||
return Err(DraconisError(DraconisErrorCode::NotFound, "Environment variable not found"));
|
||||
|
||||
return ptrManager.get();
|
||||
#else
|
||||
// Use std::getenv to retrieve environment variables on POSIX systems
|
||||
const CStr value = std::getenv(name);
|
||||
|
||||
if (!value)
|
||||
return Err(error::DraconisError(error::DraconisErrorCode::NotFound, "Environment variable not found"));
|
||||
|
||||
return value;
|
||||
#endif
|
||||
}
|
||||
} // namespace util::helpers
|
Loading…
Add table
Add a link
Reference in a new issue