From e335ca10a6c6a1e37b155e2155f5c5908c0272ae Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Tue, 19 Nov 2024 09:59:04 +0100 Subject: memory/string support cleanup and additions (#220) * removed unused memory classes * added align.h alignment helpers used in upcoming changes * added char16_t StringLength * avoid memory alloc in SetCurrentThreadName * added command line parsing helpers to zencore/commandline.h * removed IoBuffer direct VirtualAlloc path --- src/zencore/commandline.cpp | 72 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/zencore/commandline.cpp (limited to 'src/zencore/commandline.cpp') 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 +#include + +#if ZEN_PLATFORM_WINDOWS +# include +ZEN_THIRD_PARTY_INCLUDES_START +# include // For command line parsing +ZEN_THIRD_PARTY_INCLUDES_END +#endif + +#include + +namespace zen { + +void +IterateCommandlineArgs(std::function& 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 -- cgit v1.2.3