2023-07-10 15:29:38 +00:00
|
|
|
/***
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-04-28 12:22:47 +00:00
|
|
|
AUKN_SYM AuOptional<AuString> EnvironmentGetOne(const AuRONString &key)
|
2023-07-10 15:29:38 +00:00
|
|
|
{
|
2024-04-28 12:22:47 +00:00
|
|
|
if (key.empty())
|
|
|
|
{
|
|
|
|
SysPushErrorArg("Missing key");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-07-10 15:29:38 +00:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2024-04-28 12:22:47 +00:00
|
|
|
AUKN_SYM bool EnvironmentSetOne(const AuRONString &key,
|
|
|
|
const AuRONString &value)
|
2023-07-10 15:29:38 +00:00
|
|
|
{
|
|
|
|
auto keyString = AuLocale::ConvertFromUTF8(key);
|
|
|
|
auto valString = AuLocale::ConvertFromUTF8(value);
|
|
|
|
|
2024-04-28 12:22:47 +00:00
|
|
|
if (key.empty())
|
|
|
|
{
|
|
|
|
SysPushErrorArg("Missing key");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keyString.empty() || (valString.empty() && !value.empty()))
|
2023-07-10 15:29:38 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ::SetEnvironmentVariableW(keyString.c_str(),
|
|
|
|
valString.c_str());
|
|
|
|
}
|
|
|
|
|
2024-04-28 12:22:47 +00:00
|
|
|
AUKN_SYM bool EnvironmentRemoveOne(const AuRONString &key)
|
2023-07-10 15:29:38 +00:00
|
|
|
{
|
|
|
|
std::wstring keyString;
|
|
|
|
|
2024-04-28 12:22:47 +00:00
|
|
|
if (key.empty())
|
|
|
|
{
|
|
|
|
SysPushErrorArg("Missing key");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-07-10 15:29:38 +00:00
|
|
|
if ((keyString = AuLocale::ConvertFromUTF8(key)).size())
|
|
|
|
{
|
|
|
|
return ::SetEnvironmentVariableW(keyString.c_str(), nullptr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|