diff options
Diffstat (limited to 'src/zencore/commandline.cpp')
| -rw-r--r-- | src/zencore/commandline.cpp | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/zencore/commandline.cpp b/src/zencore/commandline.cpp new file mode 100644 index 000000000..c801bf151 --- /dev/null +++ b/src/zencore/commandline.cpp @@ -0,0 +1,72 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#include <zencore/commandline.h> +#include <zencore/string.h> + +#if ZEN_PLATFORM_WINDOWS +# include <zencore/windows.h> +ZEN_THIRD_PARTY_INCLUDES_START +# include <shellapi.h> // For command line parsing +ZEN_THIRD_PARTY_INCLUDES_END +#endif + +#include <functional> + +namespace zen { + +void +IterateCommandlineArgs(std::function<void(const std::string_view& Arg)>& ProcessArg) +{ +#if ZEN_PLATFORM_WINDOWS + int ArgC = 0; + const LPWSTR CmdLine = ::GetCommandLineW(); + const LPWSTR* ArgV = ::CommandLineToArgvW(CmdLine, &ArgC); + + if (ArgC > 1) + { + for (int i = 1; i < ArgC; ++i) + { + StringBuilder<4096> ArgString8; + + WideToUtf8(ArgV[i], ArgString8); + + ProcessArg(ArgString8); + } + } + + ::LocalFree(HLOCAL(ArgV)); +#elif ZEN_PLATFORM_LINUX || ZEN_PLATFORM_MAC + if (FILE* CmdLineFile = fopen("/proc/self/cmdline", "r")) + { + const char* ArgV[255] = {}; + int ArgC = 0; + + char* Arg = nullptr; + size_t Size = 0; + while (getdelim(&Arg, &Size, 0, CmdLineFile) != -1) + { + ArgV[ArgC++] = Arg; + Arg = nullptr; // getdelim will allocate buffer for next Arg + } + fclose(CmdLineFile); + + if (ArgC > 1) + { + for (int i = 1; i < ArgC; ++i) + { + ProcessArg(ArgV[i]); + } + } + + // cleanup after getdelim + while (ArgC > 0) + { + free((void*)ArgV[--ArgC]); + } + } +#else +# error Unknown platform +#endif +} + +} // namespace zen |