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 <nisse@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#27910}
This commit is contained in:
Niels Möller
2019-05-10 12:47:25 +02:00
committed by Commit Bot
parent fb8c856afa
commit 0da11562dc
3 changed files with 53 additions and 70 deletions

View File

@ -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 <windows.h>
#include <shellapi.h> // must come after windows.h
// clang-format on
#include <string>
#include <vector>
#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<std::string> args_;
// Pointers, to get layout compatible with char** argv.
std::vector<const char*> 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) {