cpp-terminal 1.0.0
Small C++ library for writing multiplatform terminal applications
Loading...
Searching...
No Matches
Term::Arguments Class Reference

#include <cpp-terminal/args.hpp>

Public Member Functions

 Arguments ()
 
std::string operator[] (const std::size_t &arg) const
 

Static Public Member Functions

static std::size_t argc ()
 
static std::vector< std::string > argv ()
 

Static Private Member Functions

static void parse ()
 

Static Private Attributes

static std::vector< std::string > m_args = std::vector<std::string>()
 
static bool m_parsed = false
 

Detailed Description

Definition at line 18 of file args.hpp.

Constructor & Destructor Documentation

◆ Arguments()

Term::Arguments::Arguments ( )

Definition at line 15 of file args.cpp.

15{}

Member Function Documentation

◆ argc()

std::size_t Term::Arguments::argc ( )
static

Definition at line 83 of file args.cpp.

84{
85 parse();
86 return m_args.size();
87}
static void parse()
Definition args.cpp:32
static std::vector< std::string > m_args
Definition args.hpp:28

◆ argv()

std::vector< std::string > Term::Arguments::argv ( )
static

Definition at line 89 of file args.cpp.

90{
91 parse();
92 return m_args;
93}

◆ operator[]()

std::string Term::Arguments::operator[] ( const std::size_t & arg) const

Definition at line 23 of file args.cpp.

23{ return m_args[arg]; }

◆ parse()

void Term::Arguments::parse ( )
staticprivate

Definition at line 32 of file args.cpp.

33{
34 if(m_parsed) { return; }
35#if defined(_WIN32)
36 int argc{0};
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 {
40 m_parsed = false;
41 return;
42 }
43 else
44 {
45 m_args.reserve(static_cast<std::size_t>(argc));
46 for(std::size_t i = 0; i != static_cast<std::size_t>(argc); ++i) { m_args.push_back(Term::Private::to_narrow(&wargv.get()[i][0])); }
47 m_parsed = true;
48 }
49#elif defined(__APPLE__)
50 std::size_t argc{static_cast<std::size_t>(*_NSGetArgc())};
51 m_args.reserve(argc);
52 char** argv{*_NSGetArgv()};
53 for(std::size_t i = 0; i != argc; ++i) { m_args.push_back(argv[i]); }
54 m_parsed = true;
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())); //NOLINT(readability-container-data-pointer)
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())); }
71 m_parsed = true;
72 }
73 catch(...)
74 {
75 file_stream.exceptions(old_iostate);
76 if(file_stream.is_open()) { file_stream.close(); }
77 m_args.clear();
78 m_parsed = false;
79 }
80#endif
81}
static bool m_parsed
Definition args.hpp:29
static std::vector< std::string > argv()
Definition args.cpp:89
static std::size_t argc()
Definition args.cpp:83
std::string to_narrow(const std::wstring &wstr)
Definition unicode.cpp:26

Member Data Documentation

◆ m_args

std::vector< std::string > Term::Arguments::m_args = std::vector<std::string>()
staticprivate

Definition at line 28 of file args.hpp.

◆ m_parsed

bool Term::Arguments::m_parsed = false
staticprivate

Definition at line 29 of file args.hpp.


The documentation for this class was generated from the following files: