Commit 92865cee authored by Andreas Schuh's avatar Andreas Schuh

#51 #138 Use std::string instead const char * for global "constants"

parent 8ac4bc41
...@@ -1454,65 +1454,58 @@ void GetAllFlags(vector<CommandLineFlagInfo>* OUTPUT) { ...@@ -1454,65 +1454,58 @@ void GetAllFlags(vector<CommandLineFlagInfo>* OUTPUT) {
// These values are not protected by a Mutex because they are normally // These values are not protected by a Mutex because they are normally
// set only once during program startup. // set only once during program startup.
static const char* argv0 = "UNKNOWN"; // just the program name static string argv0("UNKNOWN"); // just the program name
static const char* cmdline = ""; // the entire command-line static string cmdline; // the entire command-line
static string program_usage;
static vector<string> argvs; static vector<string> argvs;
static uint32 argv_sum = 0; static uint32 argv_sum = 0;
static const char* program_usage = NULL;
void SetArgv(int argc, const char** argv) { void SetArgv(int argc, const char** argv) {
static bool called_set_argv = false; static bool called_set_argv = false;
if (called_set_argv) // we already have an argv for you if (called_set_argv) return;
return;
called_set_argv = true; called_set_argv = true;
assert(argc > 0); // every program has at least a progname assert(argc > 0); // every program has at least a name
argv0 = strdup(argv[0]); // small memory leak, but fn only called once argv0 = argv[0];
assert(argv0);
string cmdline_string; // easier than doing strcats cmdline.clear();
for (int i = 0; i < argc; i++) { for (int i = 0; i < argc; i++) {
if (i != 0) { if (i != 0) cmdline += " ";
cmdline_string += " "; cmdline += argv[i];
}
cmdline_string += argv[i];
argvs.push_back(argv[i]); argvs.push_back(argv[i]);
} }
cmdline = strdup(cmdline_string.c_str()); // another small memory leak
assert(cmdline);
// Compute a simple sum of all the chars in argv // Compute a simple sum of all the chars in argv
for (const char* c = cmdline; *c; c++) argv_sum = 0;
for (string::const_iterator c = cmdline.begin(); c != cmdline.end(); ++c) {
argv_sum += *c; argv_sum += *c;
}
} }
const vector<string>& GetArgvs() { return argvs; } const vector<string>& GetArgvs() { return argvs; }
const char* GetArgv() { return cmdline; } const char* GetArgv() { return cmdline.c_str(); }
const char* GetArgv0() { return argv0; } const char* GetArgv0() { return argv0.c_str(); }
uint32 GetArgvSum() { return argv_sum; } uint32 GetArgvSum() { return argv_sum; }
const char* ProgramInvocationName() { // like the GNU libc fn const char* ProgramInvocationName() { // like the GNU libc fn
return GetArgv0(); return GetArgv0();
} }
const char* ProgramInvocationShortName() { // like the GNU libc fn const char* ProgramInvocationShortName() { // like the GNU libc fn
const char* slash = strrchr(argv0, '/'); size_t pos = argv0.rfind('/');
#ifdef OS_WINDOWS #ifdef OS_WINDOWS
if (!slash) slash = strrchr(argv0, '\\'); if (pos == string::npos) pos = argv0.rfind('\\');
#endif #endif
return slash ? slash + 1 : argv0; return (pos == string::npos ? argv0.c_str() : (argv0.c_str() + pos + 1));
} }
void SetUsageMessage(const string& usage) { void SetUsageMessage(const string& usage) {
if (program_usage != NULL) program_usage = usage;
ReportError(DIE, "ERROR: SetUsageMessage() called twice\n");
program_usage = strdup(usage.c_str()); // small memory leak
} }
const char* ProgramUsage() { const char* ProgramUsage() {
if (program_usage) { if (program_usage.empty()) {
return program_usage; return "Warning: SetUsageMessage() never called";
} }
return "Warning: SetUsageMessage() never called"; return program_usage.c_str();
} }
// -------------------------------------------------------------------- // --------------------------------------------------------------------
...@@ -1520,16 +1513,14 @@ const char* ProgramUsage() { ...@@ -1520,16 +1513,14 @@ const char* ProgramUsage() {
// VersionString() // VersionString()
// -------------------------------------------------------------------- // --------------------------------------------------------------------
static const char* version_string = NULL; static string version_string;
void SetVersionString(const string& version) { void SetVersionString(const string& version) {
if (version_string != NULL) version_string = version;
ReportError(DIE, "ERROR: SetVersionString() called twice\n");
version_string = strdup(version.c_str()); // small memory leak
} }
const char* VersionString() { const char* VersionString() {
return version_string ? version_string : ""; return version_string.c_str();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment