From a87f5b2e70e16d41834f5e475112933e8b65d580 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sat, 17 Oct 2020 01:57:10 +0300 Subject: CommandLine: refactoring --- include/winapi/cmd_line.hpp | 20 ++++++++++---------- src/cmd_line.cpp | 39 ++++++++++++++++----------------------- 2 files changed, 26 insertions(+), 33 deletions(-) diff --git a/include/winapi/cmd_line.hpp b/include/winapi/cmd_line.hpp index c6e8c65..8ea1a92 100644 --- a/include/winapi/cmd_line.hpp +++ b/include/winapi/cmd_line.hpp @@ -23,15 +23,15 @@ public: CommandLine() = default; - CommandLine(const std::string& argv0, const std::vector& args = {}) - : argv0(argv0), args(args) {} + explicit CommandLine(const std::string& argv0, const std::vector& args = {}) + : m_argv0(argv0), m_args(args) {} - CommandLine(std::string&& argv0, std::vector&& args = {}) - : argv0(std::move(argv0)), args(std::move(args)) {} + explicit CommandLine(std::string&& argv0, std::vector&& args = {}) + : m_argv0(std::move(argv0)), m_args(std::move(args)) {} - CommandLine(const std::vector& argv); + explicit CommandLine(const std::vector& argv); - CommandLine(std::vector&& argv); + explicit CommandLine(std::vector&& argv); static std::string escape(const std::string&); @@ -41,11 +41,11 @@ public: std::string args_to_string() const; - std::string get_argv0() const { return argv0; } + std::string get_argv0() const { return m_argv0; } bool has_args() const { return !get_args().empty(); } - const std::vector& get_args() const { return args; } + const std::vector& get_args() const { return m_args; } std::vector get_argv() const; @@ -58,8 +58,8 @@ private: std::vector escape_argv() const; - std::string argv0; - std::vector args; + std::string m_argv0; + std::vector m_args; }; } // namespace winapi diff --git a/src/cmd_line.cpp b/src/cmd_line.cpp index 2f9be41..eaf505f 100644 --- a/src/cmd_line.cpp +++ b/src/cmd_line.cpp @@ -26,6 +26,14 @@ namespace winapi { namespace { +std::vector narrow_all(int argc, wchar_t** argv) { + std::vector utf; + utf.reserve(argc); + for (int i = 0; i < argc; ++i) + utf.emplace_back(narrow(argv[i])); + return utf; +} + struct LocalDelete { void operator()(wchar_t* argv[]) const { ::LocalFree(argv); } }; @@ -39,22 +47,14 @@ CommandLine do_parse(std::wstring src) { int argc = 0; std::unique_ptr argv{::CommandLineToArgvW(src.c_str(), &argc)}; - if (argv.get() == NULL) + if (argv.get() == NULL) { throw error::windows(GetLastError(), "CommandLineToArgvW"); - + } if (argc == 0) { throw std::runtime_error{"Command line must contain at least one token"}; } - std::string argv0{narrow(argv.get()[0])}; - - std::vector args; - args.reserve(argc - 1); - - for (int i = 1; i < argc; ++i) - args.emplace_back(narrow(argv.get()[i])); - - return {std::move(argv0), std::move(args)}; + return CommandLine{narrow_all(argc, argv.get())}; } std::string split_argv0(std::vector& argv) { @@ -87,22 +87,15 @@ CommandLine CommandLine::parse(const std::string& src) { CommandLine CommandLine::from_main(int argc, wchar_t* argv[]) { if (argc < 1) throw std::range_error{"argc must be a positive number"}; - - std::vector utf8_argv; - utf8_argv.reserve(argc); - - for (int i = 0; i < argc; ++i) - utf8_argv.emplace_back(narrow(argv[i])); - - return {std::move(utf8_argv)}; + return CommandLine{narrow_all(argc, argv)}; } -CommandLine::CommandLine(const std::vector& argv) : args(argv) { - argv0 = split_argv0(args); +CommandLine::CommandLine(const std::vector& argv) : m_args(argv) { + m_argv0 = split_argv0(m_args); } -CommandLine::CommandLine(std::vector&& argv) : args(std::move(argv)) { - argv0 = split_argv0(args); +CommandLine::CommandLine(std::vector&& argv) : m_args(std::move(argv)) { + m_argv0 = split_argv0(m_args); } std::string CommandLine::escape(const std::string& arg) { -- cgit v1.2.3