33{
35#if defined(_WIN32)
37 std::unique_ptr<LPWSTR[], void (*)(
wchar_t**)> wargv = std::unique_ptr<LPWSTR[],
void (*)(
wchar_t**)>(CommandLineToArgvW(GetCommandLineW(), &
argc), [](
wchar_t** ptr) { LocalFree(ptr); });
38 if(wargv == nullptr)
39 {
41 return;
42 }
43 else
44 {
45 m_args.reserve(
static_cast<std::size_t
>(
argc));
48 }
49#elif defined(__APPLE__)
50 std::size_t
argc{
static_cast<std::size_t
>(*_NSGetArgc())};
52 char**
argv{*_NSGetArgv()};
53 for(std::size_t i = 0; i !=
argc; ++i) {
m_args.push_back(
argv[i]); }
55#else
56 std::string cmdline;
57 std::fstream file_stream;
58 const std::fstream::iostate old_iostate{file_stream.exceptions()};
59 try
60 {
61 file_stream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
62 file_stream.open("/proc/self/cmdline", std::fstream::in | std::fstream::binary);
63 file_stream.ignore(std::numeric_limits<std::streamsize>::max());
64 cmdline.resize(static_cast<std::size_t>(file_stream.gcount()));
65 file_stream.seekg(0, std::ios_base::beg);
66 file_stream.get(&cmdline[0], static_cast<std::streamsize>(cmdline.size()));
67 file_stream.exceptions(old_iostate);
68 if(file_stream.is_open()) { file_stream.close(); }
69 m_args.reserve(
static_cast<std::size_t
>(std::count(cmdline.begin(), cmdline.end(),
'\0')));
70 for(std::string::iterator it = cmdline.begin(); it != cmdline.end(); it = std::find(it, cmdline.end(),
'\0') + 1) {
m_args.emplace_back(cmdline.data() + (it - cmdline.begin())); }
72 }
73 catch(...)
74 {
75 file_stream.exceptions(old_iostate);
76 if(file_stream.is_open()) { file_stream.close(); }
79 }
80#endif
81}
static std::vector< std::string > argv()
static std::size_t argc()
std::string to_narrow(const std::wstring &wstr)