3
* Copyright 2006, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
8
* 1. Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
13
* 3. The name of the author may not be used to endorse or promote products
14
* derived from this software without specific prior written permission.
16
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
#include "talk/base/win32.h"
38
#include "talk/base/flags.h"
41
// -----------------------------------------------------------------------------
42
// Implementation of Flag
44
Flag::Flag(const char* file, const char* name, const char* comment,
45
Type type, void* variable, FlagValue default__)
50
variable_(reinterpret_cast<FlagValue*>(variable)),
52
FlagList::Register(this);
56
void Flag::SetToDefault() {
57
// Note that we cannot simply do '*variable_ = default_;' since
58
// flag variables are not really of type FlagValue and thus may
59
// be smaller! The FlagValue union is simply 'overlayed' on top
60
// of a flag variable for convenient access. Since union members
61
// are guarantee to be aligned at the beginning, this works.
64
variable_->b = default_.b;
67
variable_->i = default_.i;
70
variable_->f = default_.f;
73
variable_->s = default_.s;
80
static const char* Type2String(Flag::Type type) {
82
case Flag::BOOL: return "bool";
83
case Flag::INT: return "int";
84
case Flag::FLOAT: return "float";
85
case Flag::STRING: return "string";
92
static void PrintFlagValue(Flag::Type type, FlagValue* p) {
95
printf("%s", (p->b ? "true" : "false"));
111
void Flag::Print(bool print_current_value) {
112
printf(" --%s (%s) type: %s default: ", name_, comment_,
114
PrintFlagValue(type_, &default_);
115
if (print_current_value) {
116
printf(" current value: ");
117
PrintFlagValue(type_, variable_);
123
// -----------------------------------------------------------------------------
124
// Implementation of FlagList
126
Flag* FlagList::list_ = NULL;
129
FlagList::FlagList() {
133
void FlagList::Print(const char* file, bool print_current_value) {
134
// Since flag registration is likely by file (= C++ file),
135
// we don't need to sort by file and still get grouped output.
136
const char* current = NULL;
137
for (Flag* f = list_; f != NULL; f = f->next()) {
138
if (file == NULL || file == f->file()) {
139
if (current != f->file()) {
140
printf("Flags from %s:\n", f->file());
143
f->Print(print_current_value);
149
Flag* FlagList::Lookup(const char* name) {
151
while (f != NULL && strcmp(name, f->name()) != 0)
157
void FlagList::SplitArgument(const char* arg,
158
char* buffer, int buffer_size,
159
const char** name, const char** value,
166
// find the begin of the flag name
167
arg++; // remove 1st '-'
169
arg++; // remove 2nd '-'
170
if (arg[0] == 'n' && arg[1] == 'o') {
171
arg += 2; // remove "no"
176
// find the end of the flag name
177
while (*arg != '\0' && *arg != '=')
180
// get the value if any
182
// make a copy so we can NUL-terminate flag name
183
int n = static_cast<int>(arg - *name);
184
if (n >= buffer_size)
185
Fatal(__FILE__, __LINE__, "CHECK(%s) failed", "n < buffer_size");
186
memcpy(buffer, *name, n * sizeof(char));
196
int FlagList::SetFlagsFromCommandLine(int* argc, const char** argv,
199
for (int i = 1; i < *argc; /* see below */) {
201
const char* arg = argv[i++];
203
// split arg into flag components
208
SplitArgument(arg, buffer, sizeof buffer, &name, &value, &is_bool);
212
Flag* flag = Lookup(name);
214
fprintf(stderr, "Error: unrecognized flag %s\n", arg);
218
// if we still need a flag value, use the next argument if available
219
if (flag->type() != Flag::BOOL && value == NULL) {
223
fprintf(stderr, "Error: missing value for flag %s of type %s\n",
224
arg, Type2String(flag->type()));
230
char empty[] = { '\0' };
232
switch (flag->type()) {
234
*flag->bool_variable() = !is_bool;
237
*flag->int_variable() = strtol(value, &endp, 10);
240
*flag->float_variable() = strtod(value, &endp);
243
*flag->string_variable() = value;
248
if ((flag->type() == Flag::BOOL && value != NULL) ||
249
(flag->type() != Flag::BOOL && is_bool) ||
251
fprintf(stderr, "Error: illegal value for flag %s of type %s\n",
252
arg, Type2String(flag->type()));
256
// remove the flag & value from the command
263
// shrink the argument list
266
for (int i = 1; i < *argc; i++) {
273
// parsed all flags successfully
277
void FlagList::Register(Flag* flag) {
278
assert(flag != NULL && strlen(flag->name()) > 0);
279
if (Lookup(flag->name()) != NULL)
280
Fatal(flag->file(), 0, "flag %s declared twice", flag->name());
286
WindowsCommandLineArguments::WindowsCommandLineArguments() {
287
// start by getting the command line.
288
LPTSTR command_line = ::GetCommandLine();
289
// now, convert it to a list of wide char strings.
290
LPWSTR *wide_argv = ::CommandLineToArgvW(command_line, &argc_);
291
// now allocate an array big enough to hold that many string pointers.
292
argv_ = new char*[argc_];
294
// iterate over the returned wide strings;
295
for(int i = 0; i < argc_; ++i) {
296
std::string s = talk_base::ToUtf8(wide_argv[i], wcslen(wide_argv[i]));
297
char *buffer = new char[s.length() + 1];
298
talk_base::strcpyn(buffer, s.length() + 1, s.c_str());
300
// make sure the argv array has the right string at this point.
303
LocalFree(wide_argv);
306
WindowsCommandLineArguments::~WindowsCommandLineArguments() {
307
// need to free each string in the array, and then the array.
308
for(int i = 0; i < argc_; i++) {