From 0da11562dc699cbe5fd0a5e7104d6b2597a9fe3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=B6ller?= Date: Fri, 10 May 2019 12:47:25 +0200 Subject: [PATCH] Simplify WindowsCommandLineArguments, and move to example code. Eliminates one use of strcpyn. Bug: None Change-Id: I339a41d3d978f584fbb00ebfbffa31e4133ae33f Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/135741 Commit-Queue: Niels Moller Reviewed-by: Karl Wiberg Cr-Commit-Position: refs/heads/master@{#27910} --- examples/peerconnection/client/main.cc | 55 +++++++++++++++++++++++++- rtc_base/flags.cc | 41 ------------------- rtc_base/flags.h | 27 ------------- 3 files changed, 53 insertions(+), 70 deletions(-) diff --git a/examples/peerconnection/client/main.cc b/examples/peerconnection/client/main.cc index dc6b5d5508..6c7178af55 100644 --- a/examples/peerconnection/client/main.cc +++ b/examples/peerconnection/client/main.cc @@ -8,17 +8,68 @@ * be found in the AUTHORS file in the root of the source tree. */ +// clang-format off +// clang formating would change include order. +#include +#include // must come after windows.h +// clang-format on + +#include +#include + #include "examples/peerconnection/client/conductor.h" #include "examples/peerconnection/client/flag_defs.h" #include "examples/peerconnection/client/main_wnd.h" #include "examples/peerconnection/client/peer_connection_client.h" #include "rtc_base/checks.h" +#include "rtc_base/constructor_magic.h" #include "rtc_base/ssl_adapter.h" +#include "rtc_base/string_utils.h" // For ToUtf8 #include "rtc_base/win32_socket_init.h" #include "rtc_base/win32_socket_server.h" #include "system_wrappers/include/field_trial.h" #include "test/field_trial.h" +namespace { +// A helper class to translate Windows command line arguments into UTF8, +// which then allows us to just pass them to the flags system. +// This encapsulates all the work of getting the command line and translating +// it to an array of 8-bit strings; all you have to do is create one of these, +// and then call argc() and argv(). +class WindowsCommandLineArguments { + public: + WindowsCommandLineArguments(); + + int argc() { return argv_.size(); } + const char** argv() { return argv_.data(); } + + private: + // Owned argument strings. + std::vector args_; + // Pointers, to get layout compatible with char** argv. + std::vector argv_; + + private: + RTC_DISALLOW_COPY_AND_ASSIGN(WindowsCommandLineArguments); +}; + +WindowsCommandLineArguments::WindowsCommandLineArguments() { + // start by getting the command line. + LPCWSTR command_line = ::GetCommandLineW(); + // now, convert it to a list of wide char strings. + int argc; + LPWSTR* wide_argv = ::CommandLineToArgvW(command_line, &argc); + + // iterate over the returned wide strings; + for (int i = 0; i < argc; ++i) { + args_.push_back(rtc::ToUtf8(wide_argv[i], wcslen(wide_argv[i]))); + // make sure the argv array points to the string data. + argv_.push_back(args_.back().c_str()); + } + LocalFree(wide_argv); +} + +} // namespace int PASCAL wWinMain(HINSTANCE instance, HINSTANCE prev_instance, wchar_t* cmd_line, @@ -28,9 +79,9 @@ int PASCAL wWinMain(HINSTANCE instance, rtc::Win32Thread w32_thread(&w32_ss); rtc::ThreadManager::Instance()->SetCurrentThread(&w32_thread); - rtc::WindowsCommandLineArguments win_args; + WindowsCommandLineArguments win_args; int argc = win_args.argc(); - char** argv = win_args.argv(); + const char** argv = win_args.argv(); rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true); if (FLAG_help) { diff --git a/rtc_base/flags.cc b/rtc_base/flags.cc index 3bb0459581..ca32c730e5 100644 --- a/rtc_base/flags.cc +++ b/rtc_base/flags.cc @@ -16,16 +16,6 @@ #include "rtc_base/checks.h" -#if defined(WEBRTC_WIN) -// clang-format off -// clang formating would change include order. -#include -#include // must come after windows.h -// clang-format on - -#include "rtc_base/string_utils.h" // For ToUtf8 -#endif - namespace { bool FlagEq(const char* arg, const char* flag) { // Compare two flags for equality. @@ -289,35 +279,4 @@ void FlagList::Register(Flag* flag) { list_ = flag; } -#if defined(WEBRTC_WIN) -WindowsCommandLineArguments::WindowsCommandLineArguments() { - // start by getting the command line. - LPCWSTR command_line = ::GetCommandLineW(); - // now, convert it to a list of wide char strings. - LPWSTR* wide_argv = ::CommandLineToArgvW(command_line, &argc_); - // now allocate an array big enough to hold that many string pointers. - argv_ = new char*[argc_]; - - // iterate over the returned wide strings; - for (int i = 0; i < argc_; ++i) { - std::string s = rtc::ToUtf8(wide_argv[i], wcslen(wide_argv[i])); - char* buffer = new char[s.length() + 1]; - rtc::strcpyn(buffer, s.length() + 1, s.c_str()); - - // make sure the argv array has the right string at this point. - argv_[i] = buffer; - } - LocalFree(wide_argv); -} - -WindowsCommandLineArguments::~WindowsCommandLineArguments() { - // need to free each string in the array, and then the array. - for (int i = 0; i < argc_; i++) { - delete[] argv_[i]; - } - - delete[] argv_; -} -#endif // WEBRTC_WIN - } // namespace rtc diff --git a/rtc_base/flags.h b/rtc_base/flags.h index e152e2a979..61a95ffd53 100644 --- a/rtc_base/flags.h +++ b/rtc_base/flags.h @@ -24,10 +24,6 @@ #include "rtc_base/checks.h" -#if defined(WEBRTC_WIN) -#include "rtc_base/constructor_magic.h" -#endif - namespace rtc { // Internal use only. @@ -243,29 +239,6 @@ class FlagList { static Flag* list_; }; -#if defined(WEBRTC_WIN) -// A helper class to translate Windows command line arguments into UTF8, -// which then allows us to just pass them to the flags system. -// This encapsulates all the work of getting the command line and translating -// it to an array of 8-bit strings; all you have to do is create one of these, -// and then call argc() and argv(). -class WindowsCommandLineArguments { - public: - WindowsCommandLineArguments(); - ~WindowsCommandLineArguments(); - - int argc() { return argc_; } - char** argv() { return argv_; } - - private: - int argc_; - char** argv_; - - private: - RTC_DISALLOW_COPY_AND_ASSIGN(WindowsCommandLineArguments); -}; -#endif // WEBRTC_WIN - } // namespace rtc #endif // SHARED_COMMANDLINEFLAGS_FLAGS_H_