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:
parent
fb8c856afa
commit
0da11562dc
@ -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) {
|
||||
|
||||
@ -16,16 +16,6 @@
|
||||
|
||||
#include "rtc_base/checks.h"
|
||||
|
||||
#if defined(WEBRTC_WIN)
|
||||
// clang-format off
|
||||
// clang formating would change include order.
|
||||
#include <windows.h>
|
||||
#include <shellapi.h> // 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
|
||||
|
||||
@ -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_
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user