2 * Copyright 2006 The WebRTC Project Authors. All rights reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
16 #if defined(WEBRTC_WIN)
17 #include "webrtc/base/win32.h"
21 #include "webrtc/base/flags.h"
24 // -----------------------------------------------------------------------------
25 // Implementation of Flag
27 Flag::Flag(const char* file, const char* name, const char* comment,
28 Type type, void* variable, FlagValue default__)
33 variable_(reinterpret_cast<FlagValue*>(variable)),
35 FlagList::Register(this);
39 void Flag::SetToDefault() {
40 // Note that we cannot simply do '*variable_ = default_;' since
41 // flag variables are not really of type FlagValue and thus may
42 // be smaller! The FlagValue union is simply 'overlayed' on top
43 // of a flag variable for convenient access. Since union members
44 // are guarantee to be aligned at the beginning, this works.
47 variable_->b = default_.b;
50 variable_->i = default_.i;
53 variable_->f = default_.f;
56 variable_->s = default_.s;
59 FATAL() << "unreachable code";
63 static const char* Type2String(Flag::Type type) {
65 case Flag::BOOL: return "bool";
66 case Flag::INT: return "int";
67 case Flag::FLOAT: return "float";
68 case Flag::STRING: return "string";
70 FATAL() << "unreachable code";
74 static void PrintFlagValue(Flag::Type type, FlagValue* p) {
77 printf("%s", (p->b ? "true" : "false"));
89 FATAL() << "unreachable code";
93 void Flag::Print(bool print_current_value) {
94 printf(" --%s (%s) type: %s default: ", name_, comment_,
96 PrintFlagValue(type_, &default_);
97 if (print_current_value) {
98 printf(" current value: ");
99 PrintFlagValue(type_, variable_);
105 // -----------------------------------------------------------------------------
106 // Implementation of FlagList
108 Flag* FlagList::list_ = NULL;
111 FlagList::FlagList() {
115 void FlagList::Print(const char* file, bool print_current_value) {
116 // Since flag registration is likely by file (= C++ file),
117 // we don't need to sort by file and still get grouped output.
118 const char* current = NULL;
119 for (Flag* f = list_; f != NULL; f = f->next()) {
120 if (file == NULL || file == f->file()) {
121 if (current != f->file()) {
122 printf("Flags from %s:\n", f->file());
125 f->Print(print_current_value);
131 Flag* FlagList::Lookup(const char* name) {
133 while (f != NULL && strcmp(name, f->name()) != 0)
139 void FlagList::SplitArgument(const char* arg,
140 char* buffer, int buffer_size,
141 const char** name, const char** value,
148 // find the begin of the flag name
149 arg++; // remove 1st '-'
151 arg++; // remove 2nd '-'
152 if (arg[0] == 'n' && arg[1] == 'o') {
153 arg += 2; // remove "no"
158 // find the end of the flag name
159 while (*arg != '\0' && *arg != '=')
162 // get the value if any
164 // make a copy so we can NUL-terminate flag name
165 int n = static_cast<int>(arg - *name);
166 CHECK_LT(n, buffer_size);
167 memcpy(buffer, *name, n * sizeof(char));
177 int FlagList::SetFlagsFromCommandLine(int* argc, const char** argv,
180 for (int i = 1; i < *argc; /* see below */) {
182 const char* arg = argv[i++];
184 // split arg into flag components
189 SplitArgument(arg, buffer, sizeof buffer, &name, &value, &is_bool);
193 Flag* flag = Lookup(name);
195 fprintf(stderr, "Error: unrecognized flag %s\n", arg);
199 // if we still need a flag value, use the next argument if available
200 if (flag->type() != Flag::BOOL && value == NULL) {
204 fprintf(stderr, "Error: missing value for flag %s of type %s\n",
205 arg, Type2String(flag->type()));
211 char empty[] = { '\0' };
213 switch (flag->type()) {
215 *flag->bool_variable() = !is_bool;
218 *flag->int_variable() = strtol(value, &endp, 10);
221 *flag->float_variable() = strtod(value, &endp);
224 *flag->string_variable() = value;
229 if ((flag->type() == Flag::BOOL && value != NULL) ||
230 (flag->type() != Flag::BOOL && is_bool) ||
232 fprintf(stderr, "Error: illegal value for flag %s of type %s\n",
233 arg, Type2String(flag->type()));
237 // remove the flag & value from the command
244 // shrink the argument list
247 for (int i = 1; i < *argc; i++) {
254 // parsed all flags successfully
258 void FlagList::Register(Flag* flag) {
259 assert(flag != NULL && strlen(flag->name()) > 0);
260 CHECK(!Lookup(flag->name())) << "flag " << flag->name() << " declared twice";
265 #if defined(WEBRTC_WIN)
266 WindowsCommandLineArguments::WindowsCommandLineArguments() {
267 // start by getting the command line.
268 LPTSTR command_line = ::GetCommandLine();
269 // now, convert it to a list of wide char strings.
270 LPWSTR *wide_argv = ::CommandLineToArgvW(command_line, &argc_);
271 // now allocate an array big enough to hold that many string pointers.
272 argv_ = new char*[argc_];
274 // iterate over the returned wide strings;
275 for(int i = 0; i < argc_; ++i) {
276 std::string s = rtc::ToUtf8(wide_argv[i], wcslen(wide_argv[i]));
277 char *buffer = new char[s.length() + 1];
278 rtc::strcpyn(buffer, s.length() + 1, s.c_str());
280 // make sure the argv array has the right string at this point.
283 LocalFree(wide_argv);
286 WindowsCommandLineArguments::~WindowsCommandLineArguments() {
287 // need to free each string in the array, and then the array.
288 for(int i = 0; i < argc_; i++) {