aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/include
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2020-10-14 12:13:32 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2020-10-14 12:13:32 +0300
commit5abfe27617075e117f1e168f00500367069ff327 (patch)
treefd640067946ccf6a6422424a2f3ea3c17b1150d1 /include
parentcopy cmd_line.hpp from privilege-check (diff)
downloadwinapi-common-5abfe27617075e117f1e168f00500367069ff327.tar.gz
winapi-common-5abfe27617075e117f1e168f00500367069ff327.zip
clang-format
Diffstat (limited to 'include')
-rw-r--r--include/winapi/cmd_line.hpp96
1 files changed, 30 insertions, 66 deletions
diff --git a/include/winapi/cmd_line.hpp b/include/winapi/cmd_line.hpp
index 18ea496..4d652e9 100644
--- a/include/winapi/cmd_line.hpp
+++ b/include/winapi/cmd_line.hpp
@@ -1,6 +1,6 @@
-// Copyright (c) 2016 Egor Tensin <Egor.Tensin@gmail.com>
-// This file is part of the "Privilege check" project.
-// For details, see https://github.com/egor-tensin/privilege-check.
+// Copyright (c) 2020 Egor Tensin <Egor.Tensin@gmail.com>
+// This file is part of the "winapi-common" project.
+// For details, see https://github.com/egor-tensin/winapi-common.
// Distributed under the MIT License.
#pragma once
@@ -8,28 +8,24 @@
#include "error.hpp"
#include "string.hpp"
-#include <Windows.h>
#include <shellapi.h>
+#include <windows.h>
#include <cstddef>
-
#include <memory>
-#include <stdexcept>
#include <sstream>
+#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
-class CommandLine
-{
+namespace winapi {
+
+class CommandLine {
public:
- static CommandLine query()
- {
- return build_from_string(GetCommandLine());
- }
+ static CommandLine query() { return build_from_string(GetCommandLine()); }
- static CommandLine build_from_main(int argc, wchar_t* argv[])
- {
+ static CommandLine build_from_main(int argc, wchar_t* argv[]) {
if (argc < 1)
throw std::range_error{"invalid argc value"};
@@ -48,33 +44,17 @@ public:
CommandLine() = default;
- bool has_argv0() const
- {
- return !argv0.empty();
- }
+ bool has_argv0() const { return !argv0.empty(); }
- std::wstring get_argv0() const
- {
- return argv0;
- }
+ std::wstring get_argv0() const { return argv0; }
- std::wstring escape_argv0() const
- {
- return escape(get_argv0());
- }
+ std::wstring escape_argv0() const { return escape(get_argv0()); }
- bool has_args() const
- {
- return !get_args().empty();
- }
+ bool has_args() const { return !get_args().empty(); }
- const std::vector<std::wstring>& get_args() const
- {
- return args;
- }
+ const std::vector<std::wstring>& get_args() const { return args; }
- std::vector<std::wstring> escape_args() const
- {
+ std::vector<std::wstring> escape_args() const {
std::vector<std::wstring> safe;
safe.reserve(args.size());
for (const auto& arg : args)
@@ -84,13 +64,9 @@ public:
static constexpr wchar_t sep() { return L' '; }
- std::wstring join_args() const
- {
- return string::join(sep(), escape_args());
- }
+ std::wstring join_args() const { return string::join(sep(), escape_args()); }
- std::wstring join() const
- {
+ std::wstring join() const {
if (!has_argv0())
throw std::logic_error{"argv[0] isn't defined"};
std::wostringstream oss;
@@ -101,8 +77,7 @@ public:
}
private:
- static CommandLine build_from_string(std::wstring src)
- {
+ static CommandLine build_from_string(std::wstring src) {
string::trim(src);
if (src.empty())
return {};
@@ -127,8 +102,7 @@ private:
return {std::move(argv0), std::move(args)};
}
- inline std::wstring escape_for_cmd(const std::wstring& arg)
- {
+ inline std::wstring escape_for_cmd(const std::wstring& arg) {
static constexpr auto escape_symbol = L'^';
static constexpr auto dangerous_symbols = L"!\"%&()<>^|";
@@ -137,29 +111,25 @@ private:
return safe;
}
- static std::wstring escape(const std::wstring& arg)
- {
+ static std::wstring escape(const std::wstring& arg) {
std::wstring safe;
safe.reserve(arg.length() + 2);
safe.push_back(L'"');
- for (auto it = arg.cbegin(); it != arg.cend(); ++it)
- {
+ for (auto it = arg.cbegin(); it != arg.cend(); ++it) {
std::size_t numof_backslashes = 0;
for (; it != arg.cend() && *it == L'\\'; ++it)
++numof_backslashes;
- if (it == arg.cend())
- {
+ if (it == arg.cend()) {
safe.reserve(safe.capacity() + numof_backslashes);
safe.append(2 * numof_backslashes, L'\\');
break;
}
- switch (*it)
- {
+ switch (*it) {
case L'"':
safe.reserve(safe.capacity() + numof_backslashes + 1);
safe.append(2 * numof_backslashes + 1, L'\\');
@@ -177,23 +147,17 @@ private:
return safe;
}
- struct LocalDelete
- {
- void operator()(wchar_t* argv[]) const
- {
- LocalFree(argv);
- }
+ struct LocalDelete {
+ void operator()(wchar_t* argv[]) const { LocalFree(argv); }
};
- CommandLine(std::vector<std::wstring>&& args)
- : args{std::move(args)}
- { }
+ CommandLine(std::vector<std::wstring>&& args) : args{std::move(args)} {}
CommandLine(std::wstring&& argv0, std::vector<std::wstring>&& args = {})
- : argv0{std::move(argv0)}
- , args{std::move(args)}
- { }
+ : argv0{std::move(argv0)}, args{std::move(args)} {}
const std::wstring argv0;
const std::vector<std::wstring> args;
};
+
+} // namespace winapi