AuroraRuntime/Source/Process/AuProcessEnvironment.NT.cpp

151 lines
3.7 KiB
C++
Raw Normal View History

/***
Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuProcessEnvironment.NT.cpp
Date: 2023-7-10
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "AuProcessEnvironment.hpp"
#include "AuProcessEnvironment.NT.hpp"
namespace Aurora::Process
{
AUKN_SYM AuList<AuPair<AuString, AuString>> EnvironmentGetAll()
{
AuList<AuPair<AuString, AuString>> ret;
auto pWideStrings = ::GetEnvironmentStringsW();
auto pFreeBase = pWideStrings;
while (auto uLength = ::wcslen(pWideStrings))
{
auto utfString = AuLocale::ConvertFromWChar(pWideStrings, uLength);
if (utfString.empty())
{
::FreeEnvironmentStringsW(pFreeBase);
SysPushErrorNested();
return {};
}
try
{
auto optSplit = EnvironmentSplitString(utfString);
if (optSplit)
{
ret.push_back(optSplit.value());
}
}
catch (...)
{
::FreeEnvironmentStringsW(pFreeBase);
SysPushErrorCatch();
return {};
}
pWideStrings += uLength;
pWideStrings += 1;
}
::FreeEnvironmentStringsW(pFreeBase);
return ret;
}
AUKN_SYM AuOptional<AuString> EnvironmentGetOne(const AuRONString &key)
{
if (key.empty())
{
SysPushErrorArg("Missing key");
return {};
}
auto keyString = AuLocale::ConvertFromUTF8(key);
if (keyString.empty())
{
return {};
}
auto uLength = ::GetEnvironmentVariableW(keyString.c_str(),
nullptr,
0);
if (uLength == 0)
{
if (GetLastError() == ERROR_ENVVAR_NOT_FOUND)
{
SysPushErrorResourceMissing();
return {};
}
else
{
return "";
}
}
std::wstring temp;
if (!AuTryResize(temp, uLength))
{
SysPushErrorMemory();
return {};
}
uLength = ::GetEnvironmentVariableW(keyString.c_str(),
temp.data(),
temp.size());
if (!uLength)
{
SysPushErrorGeneric();
return {};
}
temp.resize(uLength);
return AuLocale::ConvertFromWChar(temp.c_str());
}
AUKN_SYM bool EnvironmentSetOne(const AuRONString &key,
const AuRONString &value)
{
auto keyString = AuLocale::ConvertFromUTF8(key);
auto valString = AuLocale::ConvertFromUTF8(value);
if (key.empty())
{
SysPushErrorArg("Missing key");
return false;
}
if (keyString.empty() || (valString.empty() && !value.empty()))
{
return false;
}
return ::SetEnvironmentVariableW(keyString.c_str(),
valString.c_str());
}
AUKN_SYM bool EnvironmentRemoveOne(const AuRONString &key)
{
std::wstring keyString;
if (key.empty())
{
SysPushErrorArg("Missing key");
return false;
}
if ((keyString = AuLocale::ConvertFromUTF8(key)).size())
{
return ::SetEnvironmentVariableW(keyString.c_str(), nullptr);
}
else
{
return false;
}
}
}