AuroraRuntime/Source/CmdLine/CmdLine.cpp
2022-03-30 20:26:24 +01:00

145 lines
3.8 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: CmdLine.cpp
Date: 2022-1-31
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "CmdLine.hpp"
#include "CmdLinePlatform.hpp"
namespace Aurora::CmdLine
{
static const AuString kEmptyString;
static AuList<AuString> gCmdFlags;
static AuList<AuString> gCmdValues;
static AuHashMap<AuString, AuString> gCmdValueMap;
static AuHashMap<AuString, bool> gCmdFlagLookup;
static AuList<AuString> gCmdLineString;
AUKN_SYM const AuList<AuString> &GetCommandLineArguments()
{
return gCmdLineString;
}
AUKN_SYM bool HasFlag(const AuString &key)
{
return gCmdFlagLookup.find(key) != gCmdFlagLookup.end();
}
AUKN_SYM bool HasValue(const AuString &key)
{
return gCmdValueMap.find(key) != gCmdValueMap.end();
}
AUKN_SYM const AuString &GetValue(const AuString &key, const AuString &defaultValue)
{
auto itr = gCmdValueMap.find(key);
if (itr == gCmdValueMap.end()) return defaultValue;
return itr->second;
}
AUKN_SYM const AuString &GetValue(const AuString &key)
{
return GetValue(key, kEmptyString);
}
AUKN_SYM const AuList<AuString> &GetFlags()
{
return gCmdFlags;
}
AUKN_SYM const AuList<AuString> &GetValues()
{
return gCmdValues;
}
static void ProcessArgs()
{
AuString extendedLine;
AuString key;
//for (const auto &arg : gCmdLineString)
for (int i = 0; i < gCmdLineString.size(); i++)
{
const auto &arg = gCmdLineString[i];
#if defined(AURORA_PLATFORM_WIN32)
if (arg[arg.size() - 1] == '\\' && (arg.size() > 1))
{
extendedLine += arg.substr(0, arg.size() - 1);
if (arg[arg.size() - 2] != '\\')
{
extendedLine += ' ';
continue;
}
}
else
#endif
{
extendedLine += arg;
}
auto doesStartWithSwitch = [](auto &in)
{
return (in[0] == '/') ||
(in.find("--") == 0);
};
auto containsAssign = [](auto &in)
{
return (in.find("=") != AuString::npos);
};
auto valueAssignment = extendedLine.find('=');
if (valueAssignment == extendedLine.npos)
{
if (doesStartWithSwitch(extendedLine))
{
if (i != gCmdLineString.size() - 1)
{
const auto &next = gCmdLineString[i + 1];
if (!doesStartWithSwitch(next) && !containsAssign(next))
{
extendedLine += '=';
continue;
}
}
extendedLine = extendedLine.substr(1 + (extendedLine[0] != '/'));
}
gCmdFlags.push_back(extendedLine);
gCmdFlagLookup[extendedLine] = true;
}
else
{
key = extendedLine.substr(0, valueAssignment);
extendedLine = extendedLine.substr(valueAssignment + 1);
if (doesStartWithSwitch(key))
{
key = key.substr(1 + (key[0] != '/'));
}
gCmdValues.push_back(key);
gCmdValueMap[key] = extendedLine;
}
extendedLine.clear();
}
}
void Init()
{
gCmdLineString = GetCmdString();
if (gCmdLineString.size())
{
gCmdLineString.erase(gCmdLineString.begin());
}
ProcessArgs();
}
}