Skip to content

Commit

Permalink
Use ASCII strings for switch names.
Browse files Browse the repository at this point in the history
Review URL: http://codereview.chromium.org/270062

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@28779 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
evan@chromium.org committed Oct 13, 2009
1 parent 1976d41 commit b7e0a2a
Show file tree
Hide file tree
Showing 69 changed files with 679 additions and 652 deletions.
2 changes: 1 addition & 1 deletion app/app_switches.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ namespace switches {

// The language file that we want to try to open. Of the form
// language[-country] where language is the 2 letter code from ISO-639.
const wchar_t kLang[] = L"lang";
const char kLang[] = "lang";

} // namespace switches
2 changes: 1 addition & 1 deletion app/app_switches.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace switches {

extern const wchar_t kLang[];
extern const char kLang[];

} // namespace switches

Expand Down
16 changes: 8 additions & 8 deletions base/base_switches.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@ namespace switches {
// If the program includes chrome/common/debug_on_start.h, the process will
// start the JIT system-registered debugger on itself and will wait for 60
// seconds for the debugger to attach to itself. Then a break point will be hit.
const wchar_t kDebugOnStart[] = L"debug-on-start";
const char kDebugOnStart[] = "debug-on-start";

// Will wait for 60 seconds for a debugger to come to attach to the process.
const wchar_t kWaitForDebugger[] = L"wait-for-debugger";
const char kWaitForDebugger[] = "wait-for-debugger";

// Suppresses all error dialogs when present.
const wchar_t kNoErrorDialogs[] = L"noerrdialogs";
const char kNoErrorDialogs[] = "noerrdialogs";

// Disables the crash reporting.
const wchar_t kDisableBreakpad[] = L"disable-breakpad";
const char kDisableBreakpad[] = "disable-breakpad";

// Generates full memory crash dump.
const wchar_t kFullMemoryCrashReport[] = L"full-memory-crash-report";
const char kFullMemoryCrashReport[] = "full-memory-crash-report";

// The value of this switch determines whether the process is started as a
// renderer or plugin host. If it's empty, it's the browser.
const wchar_t kProcessType[] = L"type";
const char kProcessType[] = "type";

// Enable DCHECKs in release mode.
const wchar_t kEnableDCHECK[] = L"enable-dcheck";
const char kEnableDCHECK[] = "enable-dcheck";

// Disable win_util::MessageBox. This is useful when running as part of
// scripts that do not have a user interface.
const wchar_t kNoMessageBox[] = L"no-message-box";
const char kNoMessageBox[] = "no-message-box";

} // namespace switches
16 changes: 8 additions & 8 deletions base/base_switches.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

namespace switches {

extern const wchar_t kDebugOnStart[];
extern const wchar_t kWaitForDebugger[];
extern const wchar_t kDisableBreakpad[];
extern const wchar_t kFullMemoryCrashReport[];
extern const wchar_t kNoErrorDialogs[];
extern const wchar_t kProcessType[];
extern const wchar_t kEnableDCHECK[];
extern const wchar_t kNoMessageBox[];
extern const char kDebugOnStart[];
extern const char kWaitForDebugger[];
extern const char kDisableBreakpad[];
extern const char kFullMemoryCrashReport[];
extern const char kNoErrorDialogs[];
extern const char kProcessType[];
extern const char kEnableDCHECK[];
extern const char kNoMessageBox[];

} // namespace switches

Expand Down
54 changes: 26 additions & 28 deletions base/command_line.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const char kSwitchValueSeparator[] = "=";
// Lowercase a string. This is used to lowercase switch names.
// Is this what we really want? It seems crazy to me. I've left it in
// for backwards compatibility on Windows.
static void Lowercase(std::wstring* parameter) {
static void Lowercase(std::string* parameter) {
transform(parameter->begin(), parameter->end(), parameter->begin(),
tolower);
}
Expand Down Expand Up @@ -175,8 +175,8 @@ bool CommandLine::IsSwitch(const StringType& parameter_string,
*switch_value = parameter_string.substr(equals_position + 1);
}
#if defined(OS_WIN)
Lowercase(&switch_native);
*switch_string = WideToASCII(switch_native);
Lowercase(switch_string);
#else
*switch_string = switch_native;
#endif
Expand Down Expand Up @@ -225,23 +225,23 @@ void CommandLine::Reset() {
current_process_commandline_ = NULL;
}

bool CommandLine::HasSwitch(const std::wstring& switch_string) const {
std::wstring lowercased_switch(switch_string);
bool CommandLine::HasSwitch(const std::string& switch_string) const {
std::string lowercased_switch(switch_string);
#if defined(OS_WIN)
Lowercase(&lowercased_switch);
#endif
return switches_.find(WideToASCII(lowercased_switch)) != switches_.end();
return switches_.find(lowercased_switch) != switches_.end();
}

std::wstring CommandLine::GetSwitchValue(
const std::wstring& switch_string) const {
std::wstring lowercased_switch(switch_string);
const std::string& switch_string) const {
std::string lowercased_switch(switch_string);
#if defined(OS_WIN)
Lowercase(&lowercased_switch);
#endif

std::map<std::string, StringType>::const_iterator result =
switches_.find(WideToASCII(lowercased_switch));
switches_.find(lowercased_switch);

if (result == switches_.end()) {
return L"";
Expand Down Expand Up @@ -277,39 +277,39 @@ std::wstring CommandLine::program() const {

// static
std::wstring CommandLine::PrefixedSwitchString(
const std::wstring& switch_string) {
const std::string& switch_string) {
#if defined(OS_WIN)
return kSwitchPrefixes[0] + switch_string;
return kSwitchPrefixes[0] + ASCIIToWide(switch_string);
#else
return ASCIIToWide(kSwitchPrefixes[0]) + switch_string;
return ASCIIToWide(kSwitchPrefixes[0] + switch_string);
#endif
}

// static
std::wstring CommandLine::PrefixedSwitchStringWithValue(
const std::wstring& switch_string, const std::wstring& value_string) {
const std::string& switch_string, const std::wstring& value_string) {
if (value_string.empty()) {
return PrefixedSwitchString(switch_string);
}

return PrefixedSwitchString(switch_string +
#if defined(OS_WIN)
kSwitchValueSeparator +
WideToASCII(kSwitchValueSeparator)
#else
ASCIIToWide(kSwitchValueSeparator) +
kSwitchValueSeparator
#endif
value_string);
) + value_string;
}

#if defined(OS_WIN)
void CommandLine::AppendSwitch(const std::wstring& switch_string) {
void CommandLine::AppendSwitch(const std::string& switch_string) {
std::wstring prefixed_switch_string = PrefixedSwitchString(switch_string);
command_line_string_.append(L" ");
command_line_string_.append(prefixed_switch_string);
switches_[WideToASCII(switch_string)] = L"";
switches_[switch_string] = L"";
}

void CommandLine::AppendSwitchWithValue(const std::wstring& switch_string,
void CommandLine::AppendSwitchWithValue(const std::string& switch_string,
const std::wstring& value_string) {
std::wstring value_string_edit;

Expand All @@ -326,12 +326,12 @@ void CommandLine::AppendSwitchWithValue(const std::wstring& switch_string,
}

std::wstring combined_switch_string =
PrefixedSwitchStringWithValue(switch_string, value_string_edit);
PrefixedSwitchStringWithValue(switch_string, value_string_edit);

command_line_string_.append(L" ");
command_line_string_.append(combined_switch_string);

switches_[WideToASCII(switch_string)] = value_string;
switches_[switch_string] = value_string;
}

void CommandLine::AppendLooseValue(const std::wstring& value) {
Expand Down Expand Up @@ -361,20 +361,18 @@ void CommandLine::PrependWrapper(const std::wstring& wrapper) {
}

#elif defined(OS_POSIX)
void CommandLine::AppendSwitch(const std::wstring& switch_string) {
std::string ascii_switch = WideToASCII(switch_string);
argv_.push_back(kSwitchPrefixes[0] + ascii_switch);
switches_[ascii_switch] = "";
void CommandLine::AppendSwitch(const std::string& switch_string) {
argv_.push_back(kSwitchPrefixes[0] + switch_string);
switches_[switch_string] = "";
}

void CommandLine::AppendSwitchWithValue(const std::wstring& switch_string,
void CommandLine::AppendSwitchWithValue(const std::string& switch_string,
const std::wstring& value_string) {
std::string ascii_switch = WideToASCII(switch_string);
std::string mb_value = base::SysWideToNativeMB(value_string);

argv_.push_back(kSwitchPrefixes[0] + ascii_switch +
argv_.push_back(kSwitchPrefixes[0] + switch_string +
kSwitchValueSeparator + mb_value);
switches_[ascii_switch] = mb_value;
switches_[switch_string] = mb_value;
}

void CommandLine::AppendLooseValue(const std::wstring& value) {
Expand Down
30 changes: 24 additions & 6 deletions base/command_line.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "base/basictypes.h"
#include "base/file_path.h"
#include "base/logging.h"
#include "base/string_util.h"

class InProcessBrowserTest;

Expand Down Expand Up @@ -88,12 +89,25 @@ class CommandLine {

// Returns true if this command line contains the given switch.
// (Switch names are case-insensitive.)
bool HasSwitch(const std::wstring& switch_string) const;
bool HasSwitch(const std::string& switch_string) const;

// Deprecated version of the above.
bool HasSwitch(const std::wstring& switch_string) const {
return HasSwitch(WideToASCII(switch_string));
}

// Returns the value associated with the given switch. If the
// switch has no value or isn't present, this method returns
// the empty string.
std::wstring GetSwitchValue(const std::wstring& switch_string) const;
std::wstring GetSwitchValue(const std::string& switch_string) const;
std::string GetSwitchValueASCII(const std::string& switch_string) const {
return WideToASCII(GetSwitchValue(switch_string));
}

// Deprecated version of the above.
std::wstring GetSwitchValue(const std::wstring& switch_string) const {
return GetSwitchValue(WideToASCII(switch_string));
}

// Get the number of switches in this process.
size_t GetSwitchCount() const { return switches_.size(); }
Expand Down Expand Up @@ -125,22 +139,26 @@ class CommandLine {

// Return a copy of the string prefixed with a switch prefix.
// Used internally.
static std::wstring PrefixedSwitchString(const std::wstring& switch_string);
static std::wstring PrefixedSwitchString(const std::string& switch_string);

// Return a copy of the string prefixed with a switch prefix,
// and appended with the given value. Used internally.
static std::wstring PrefixedSwitchStringWithValue(
const std::wstring& switch_string,
const std::string& switch_string,
const std::wstring& value_string);

// Appends the given switch string (preceded by a space and a switch
// prefix) to the given string.
void AppendSwitch(const std::wstring& switch_string);
void AppendSwitch(const std::string& switch_string);

// Appends the given switch string (preceded by a space and a switch
// prefix) to the given string, with the given value attached.
void AppendSwitchWithValue(const std::wstring& switch_string,
void AppendSwitchWithValue(const std::string& switch_string,
const std::wstring& value_string);
void AppendSwitchWithValue(const std::string& switch_string,
const std::string& value_string) {
AppendSwitchWithValue(switch_string, ASCIIToWide(value_string));
}

// Append a loose value to the command line.
void AppendLooseValue(const std::wstring& value);
Expand Down
50 changes: 25 additions & 25 deletions base/command_line_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,33 @@ TEST(CommandLineTest, CommandLineConstructor) {
"in the time of submarines..."};
CommandLine cl(arraysize(argv), argv);
#endif
EXPECT_FALSE(cl.HasSwitch(L"cruller"));
EXPECT_FALSE(cl.HasSwitch(L"flim"));
EXPECT_FALSE(cl.HasSwitch(L"program"));
EXPECT_FALSE(cl.HasSwitch(L"dog"));
EXPECT_FALSE(cl.HasSwitch(L"cat"));
EXPECT_FALSE(cl.HasSwitch(L"output-rotation"));
EXPECT_FALSE(cl.HasSwitch(L"not-a-switch"));
EXPECT_FALSE(cl.HasSwitch(L"--"));
EXPECT_FALSE(cl.HasSwitch("cruller"));
EXPECT_FALSE(cl.HasSwitch("flim"));
EXPECT_FALSE(cl.HasSwitch("program"));
EXPECT_FALSE(cl.HasSwitch("dog"));
EXPECT_FALSE(cl.HasSwitch("cat"));
EXPECT_FALSE(cl.HasSwitch("output-rotation"));
EXPECT_FALSE(cl.HasSwitch("not-a-switch"));
EXPECT_FALSE(cl.HasSwitch("--"));

EXPECT_EQ(L"program", cl.program());

EXPECT_TRUE(cl.HasSwitch(L"foo"));
EXPECT_TRUE(cl.HasSwitch(L"bar"));
EXPECT_TRUE(cl.HasSwitch(L"baz"));
EXPECT_TRUE(cl.HasSwitch(L"spaetzle"));
EXPECT_TRUE(cl.HasSwitch("foo"));
EXPECT_TRUE(cl.HasSwitch("bar"));
EXPECT_TRUE(cl.HasSwitch("baz"));
EXPECT_TRUE(cl.HasSwitch("spaetzle"));
#if defined(OS_WIN)
EXPECT_TRUE(cl.HasSwitch(L"SPAETZLE"));
EXPECT_TRUE(cl.HasSwitch("SPAETZLE"));
#endif
EXPECT_TRUE(cl.HasSwitch(L"other-switches"));
EXPECT_TRUE(cl.HasSwitch(L"input-translation"));
EXPECT_TRUE(cl.HasSwitch("other-switches"));
EXPECT_TRUE(cl.HasSwitch("input-translation"));

EXPECT_EQ(L"Crepe", cl.GetSwitchValue(L"spaetzle"));
EXPECT_EQ(L"", cl.GetSwitchValue(L"Foo"));
EXPECT_EQ(L"", cl.GetSwitchValue(L"bar"));
EXPECT_EQ(L"", cl.GetSwitchValue(L"cruller"));
EXPECT_EQ(L"--dog=canine --cat=feline", cl.GetSwitchValue(L"other-switches"));
EXPECT_EQ(L"45--output-rotation", cl.GetSwitchValue(L"input-translation"));
EXPECT_EQ(L"Crepe", cl.GetSwitchValue("spaetzle"));
EXPECT_EQ(L"", cl.GetSwitchValue("Foo"));
EXPECT_EQ(L"", cl.GetSwitchValue("bar"));
EXPECT_EQ(L"", cl.GetSwitchValue("cruller"));
EXPECT_EQ(L"--dog=canine --cat=feline", cl.GetSwitchValue("other-switches"));
EXPECT_EQ(L"45--output-rotation", cl.GetSwitchValue("input-translation"));

std::vector<std::wstring> loose_values = cl.GetLooseValues();
ASSERT_EQ(5U, loose_values.size());
Expand Down Expand Up @@ -97,12 +97,12 @@ TEST(CommandLineTest, EmptyString) {

// Test methods for appending switches to a command line.
TEST(CommandLineTest, AppendSwitches) {
std::wstring switch1 = L"switch1";
std::wstring switch2 = L"switch2";
std::string switch1 = "switch1";
std::string switch2 = "switch2";
std::wstring value = L"value";
std::wstring switch3 = L"switch3";
std::string switch3 = "switch3";
std::wstring value3 = L"a value with spaces";
std::wstring switch4 = L"switch4";
std::string switch4 = "switch4";
std::wstring value4 = L"\"a value with quotes\"";

#if defined(OS_WIN)
Expand Down
6 changes: 5 additions & 1 deletion base/debug_on_start.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
// The code is not that bright and will find things like ---argument or
// /-/argument.
// Note: command_line is non-destructively modified.
bool DebugOnStart::FindArgument(wchar_t* command_line, const wchar_t* argument)
bool DebugOnStart::FindArgument(wchar_t* command_line, const char* argument_c)
{
wchar_t argument[50];
for (int i = 0; argument_c[i]; ++i)
argument[i] = argument_c[i];

int argument_len = lstrlen(argument);
int command_line_len = lstrlen(command_line);
while (command_line_len > argument_len) {
Expand Down
2 changes: 1 addition & 1 deletion base/debug_on_start.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class DebugOnStart {

// Returns true if the 'argument' is present in the 'command_line'. It does
// not use the CRT, only Kernel32 functions.
static bool FindArgument(wchar_t* command_line, const wchar_t* argument);
static bool FindArgument(wchar_t* command_line, const char* argument);
};

// Set the function pointer to our function to look for a crash on start. The
Expand Down
2 changes: 1 addition & 1 deletion base/multiprocess_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

// Command line switch to invoke a child process rather than
// to run the normal test suite.
static const wchar_t kRunClientProcess[] = L"client";
static const char kRunClientProcess[] = "client";

// A MultiProcessTest is a test class which makes it easier to
// write a test which requires code running out of process.
Expand Down
2 changes: 1 addition & 1 deletion base/test/perf_test_suite.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class PerfTestSuite : public TestSuite {
// Initialize the perf timer log
FilePath log_path;
std::wstring log_file =
CommandLine::ForCurrentProcess()->GetSwitchValue(L"log-file");
CommandLine::ForCurrentProcess()->GetSwitchValue("log-file");
if (log_file.empty()) {
FilePath exe;
PathService::Get(base::FILE_EXE, &exe);
Expand Down
Loading

0 comments on commit b7e0a2a

Please sign in to comment.