aboutsummaryrefslogtreecommitdiff
path: root/src/zencore/commandline.cpp
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2024-11-19 09:59:04 +0100
committerGitHub Enterprise <[email protected]>2024-11-19 09:59:04 +0100
commite335ca10a6c6a1e37b155e2155f5c5908c0272ae (patch)
tree78b32384df0e58b22052dcfa7990c16921c6b650 /src/zencore/commandline.cpp
parent5.5.12 (diff)
downloadzen-e335ca10a6c6a1e37b155e2155f5c5908c0272ae.tar.xz
zen-e335ca10a6c6a1e37b155e2155f5c5908c0272ae.zip
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
Diffstat (limited to 'src/zencore/commandline.cpp')
-rw-r--r--src/zencore/commandline.cpp72
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