// 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 #if ZEN_PLATFORM_MAC # include #endif #include namespace zen { void IterateCommandlineArgs(std::function& ProcessArg) { #if ZEN_PLATFORM_WINDOWS // It might seem odd to do this here in addition to at start of main functions but the InitGMalloc() function is called before main (via // static data) so we need to make sure we set the locale before parsing the command line setlocale(LC_ALL, "en_us.UTF8"); 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_MAC int ArgC = *_NSGetArgc(); char** ArgV = *_NSGetArgv(); if (ArgC > 1) { for (int i = 1; i < ArgC; ++i) { ProcessArg(ArgV[i]); } } #elif ZEN_PLATFORM_LINUX 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 } if (Arg) { // getdelim allocated one last arg buffer that we didn't use free(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