mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-01-16 14:31:59 +00:00
lib/Support/CommandLine.cpp:
Many changes suggested by Chris. It's okay, I'll recover from the emotional damage...maybe someday. :-) Collapse ParseCStringVector into ParseStringVector. Comment it. Make it take a const input. Use std::string::npos instead of -1 (what a mouthful!) Make ParseEnvironmentOptions take const inputs. Check its args at the very beginning. Strdup all the contents of newArgv and free them all at the end. include/Support/CommandLine.h: Constify progName and envVar arguments to ParseEnvironmentOptions(). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@7905 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
b60fe7791e
commit
c48ef2ae36
@ -35,7 +35,7 @@ void ParseCommandLineOptions(int &argc, char **argv,
|
|||||||
// ParseEnvironmentOptions - Environment variable option processing alternate
|
// ParseEnvironmentOptions - Environment variable option processing alternate
|
||||||
// entry point.
|
// entry point.
|
||||||
//
|
//
|
||||||
void ParseEnvironmentOptions(char *progName, char *envvar,
|
void ParseEnvironmentOptions(const char *progName, const char *envvar,
|
||||||
const char *Overview = 0);
|
const char *Overview = 0);
|
||||||
|
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
@ -35,7 +35,7 @@ void ParseCommandLineOptions(int &argc, char **argv,
|
|||||||
// ParseEnvironmentOptions - Environment variable option processing alternate
|
// ParseEnvironmentOptions - Environment variable option processing alternate
|
||||||
// entry point.
|
// entry point.
|
||||||
//
|
//
|
||||||
void ParseEnvironmentOptions(char *progName, char *envvar,
|
void ParseEnvironmentOptions(const char *progName, const char *envvar,
|
||||||
const char *Overview = 0);
|
const char *Overview = 0);
|
||||||
|
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
@ -152,44 +152,48 @@ static bool EatsUnboundedNumberOfValues(const Option *O) {
|
|||||||
O->getNumOccurrencesFlag() == cl::OneOrMore;
|
O->getNumOccurrencesFlag() == cl::OneOrMore;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// ParseStringVector - Break INPUT up wherever one or more characters
|
/// ParseCStringVector - Break INPUT up wherever one or more
|
||||||
/// from DELIMS are found, and store the resulting tokens in OUTPUT.
|
/// whitespace characters are found, and store the resulting tokens in
|
||||||
///
|
/// OUTPUT. The tokens stored in OUTPUT are dynamically allocated
|
||||||
static void ParseStringVector (std::vector<std::string> &output,
|
/// using strdup (), so it is the caller's responsibility to free ()
|
||||||
std::string &input, const char *delims) {
|
/// them later.
|
||||||
std::string work (input);
|
|
||||||
int pos = work.find_first_not_of (delims);
|
|
||||||
if (pos == -1) return;
|
|
||||||
work = work.substr (pos);
|
|
||||||
pos = work.find_first_of (delims);
|
|
||||||
while (!work.empty() && pos != -1) {
|
|
||||||
if (pos == -1) break;
|
|
||||||
output.push_back (work.substr (0,pos));
|
|
||||||
int nextpos = work.find_first_not_of (delims, pos + 1);
|
|
||||||
if (nextpos != -1) {
|
|
||||||
work = work.substr (work.find_first_not_of (delims, pos + 1));
|
|
||||||
pos = work.find_first_of (delims);
|
|
||||||
} else {
|
|
||||||
work = "";
|
|
||||||
pos = -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!work.empty ()) {
|
|
||||||
output.push_back (work);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// ParseCStringVector - Same effect as ParseStringVector, but the
|
|
||||||
/// resulting output vector contains dynamically-allocated pointers to
|
|
||||||
/// char, instead of standard C++ strings.
|
|
||||||
///
|
///
|
||||||
static void ParseCStringVector (std::vector<char *> &output,
|
static void ParseCStringVector (std::vector<char *> &output,
|
||||||
std::string &input, const char *delims) {
|
const char *input) {
|
||||||
std::vector<std::string> work;
|
// Characters which will be treated as token separators:
|
||||||
ParseStringVector (work, input, delims);
|
static const char *delims = " \v\f\t\r\n";
|
||||||
for (std::vector<std::string>::iterator i = work.begin(), e = work.end();
|
|
||||||
i != e; ++i) {
|
std::string work (input);
|
||||||
output.push_back (strdup (i->c_str ()));
|
// Skip past any delims at head of input string.
|
||||||
|
size_t pos = work.find_first_not_of (delims);
|
||||||
|
// If the string consists entirely of delims, then exit early.
|
||||||
|
if (pos == std::string::npos) return;
|
||||||
|
// Otherwise, jump forward to beginning of first word.
|
||||||
|
work = work.substr (pos);
|
||||||
|
// Find position of first delimiter.
|
||||||
|
pos = work.find_first_of (delims);
|
||||||
|
|
||||||
|
while (!work.empty() && pos != std::string::npos) {
|
||||||
|
// Everything from 0 to POS is the next word to copy.
|
||||||
|
output.push_back (strdup (work.substr (0,pos).c_str ()));
|
||||||
|
// Is there another word in the string?
|
||||||
|
size_t nextpos = work.find_first_not_of (delims, pos + 1);
|
||||||
|
if (nextpos != std::string::npos) {
|
||||||
|
// Yes? Then remove delims from beginning ...
|
||||||
|
work = work.substr (work.find_first_not_of (delims, pos + 1));
|
||||||
|
// and find the end of the word.
|
||||||
|
pos = work.find_first_of (delims);
|
||||||
|
} else {
|
||||||
|
// No? (Remainder of string is delims.) End the loop.
|
||||||
|
work = "";
|
||||||
|
pos = std::string::npos;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If `input' ended with non-delim char, then we'll get here with
|
||||||
|
// the last word of `input' in `work'; copy it now.
|
||||||
|
if (!work.empty ()) {
|
||||||
|
output.push_back (strdup (work.c_str ()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -198,32 +202,33 @@ static void ParseCStringVector (std::vector<char *> &output,
|
|||||||
/// from the caller (as PROGNAME) and its command-line arguments from
|
/// from the caller (as PROGNAME) and its command-line arguments from
|
||||||
/// an environment variable (whose name is given in ENVVAR).
|
/// an environment variable (whose name is given in ENVVAR).
|
||||||
///
|
///
|
||||||
void cl::ParseEnvironmentOptions (char *progName, char *envvar,
|
void cl::ParseEnvironmentOptions (const char *progName, const char *envVar,
|
||||||
const char *Overview) {
|
const char *Overview) {
|
||||||
|
// Check args.
|
||||||
|
assert (progName && "Program name not specified");
|
||||||
|
assert (envVar && "Environment variable name missing");
|
||||||
|
|
||||||
|
// Get the environment variable they want us to parse options out of.
|
||||||
|
const char *envValue = getenv (envVar);
|
||||||
|
if (!envValue)
|
||||||
|
return;
|
||||||
|
|
||||||
// Get program's "name", which we wouldn't know without the caller
|
// Get program's "name", which we wouldn't know without the caller
|
||||||
// telling us.
|
// telling us.
|
||||||
assert (progName && "Program name not specified");
|
std::vector<char *> newArgv;
|
||||||
static std::vector<char *> newargv; // Maybe making it "static" is a hack.
|
newArgv.push_back (strdup (progName));
|
||||||
int newargc;
|
|
||||||
newargv.push_back (progName);
|
|
||||||
|
|
||||||
// Get the environment variable they want us to parse options out of.
|
|
||||||
assert (envvar && "Environment variable name missing");
|
|
||||||
char *envvalue = getenv (envvar);
|
|
||||||
if (envvalue == NULL) {
|
|
||||||
// Env var not set --> act like there are no more command line
|
|
||||||
// arguments.
|
|
||||||
newargc = newargv.size ();
|
|
||||||
ParseCommandLineOptions (newargc, &newargv[0], Overview);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
std::string envvaluestr (envvalue);
|
|
||||||
|
|
||||||
// Parse the value of the environment variable into a "command line"
|
// Parse the value of the environment variable into a "command line"
|
||||||
// and hand it off to ParseCommandLineOptions().
|
// and hand it off to ParseCommandLineOptions().
|
||||||
ParseCStringVector (newargv, envvaluestr, " \v\f\t\r\n");
|
ParseCStringVector (newArgv, envValue);
|
||||||
newargc = newargv.size ();
|
int newArgc = newArgv.size ();
|
||||||
ParseCommandLineOptions (newargc, &newargv[0], Overview);
|
ParseCommandLineOptions (newArgc, &newArgv[0], Overview);
|
||||||
|
|
||||||
|
// Free all the strdup()ed strings.
|
||||||
|
for (std::vector<char *>::iterator i = newArgv.begin (), e = newArgv.end ();
|
||||||
|
i != e; ++i) {
|
||||||
|
free (*i);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void cl::ParseCommandLineOptions(int &argc, char **argv,
|
void cl::ParseCommandLineOptions(int &argc, char **argv,
|
||||||
|
@ -152,44 +152,48 @@ static bool EatsUnboundedNumberOfValues(const Option *O) {
|
|||||||
O->getNumOccurrencesFlag() == cl::OneOrMore;
|
O->getNumOccurrencesFlag() == cl::OneOrMore;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// ParseStringVector - Break INPUT up wherever one or more characters
|
/// ParseCStringVector - Break INPUT up wherever one or more
|
||||||
/// from DELIMS are found, and store the resulting tokens in OUTPUT.
|
/// whitespace characters are found, and store the resulting tokens in
|
||||||
///
|
/// OUTPUT. The tokens stored in OUTPUT are dynamically allocated
|
||||||
static void ParseStringVector (std::vector<std::string> &output,
|
/// using strdup (), so it is the caller's responsibility to free ()
|
||||||
std::string &input, const char *delims) {
|
/// them later.
|
||||||
std::string work (input);
|
|
||||||
int pos = work.find_first_not_of (delims);
|
|
||||||
if (pos == -1) return;
|
|
||||||
work = work.substr (pos);
|
|
||||||
pos = work.find_first_of (delims);
|
|
||||||
while (!work.empty() && pos != -1) {
|
|
||||||
if (pos == -1) break;
|
|
||||||
output.push_back (work.substr (0,pos));
|
|
||||||
int nextpos = work.find_first_not_of (delims, pos + 1);
|
|
||||||
if (nextpos != -1) {
|
|
||||||
work = work.substr (work.find_first_not_of (delims, pos + 1));
|
|
||||||
pos = work.find_first_of (delims);
|
|
||||||
} else {
|
|
||||||
work = "";
|
|
||||||
pos = -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!work.empty ()) {
|
|
||||||
output.push_back (work);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// ParseCStringVector - Same effect as ParseStringVector, but the
|
|
||||||
/// resulting output vector contains dynamically-allocated pointers to
|
|
||||||
/// char, instead of standard C++ strings.
|
|
||||||
///
|
///
|
||||||
static void ParseCStringVector (std::vector<char *> &output,
|
static void ParseCStringVector (std::vector<char *> &output,
|
||||||
std::string &input, const char *delims) {
|
const char *input) {
|
||||||
std::vector<std::string> work;
|
// Characters which will be treated as token separators:
|
||||||
ParseStringVector (work, input, delims);
|
static const char *delims = " \v\f\t\r\n";
|
||||||
for (std::vector<std::string>::iterator i = work.begin(), e = work.end();
|
|
||||||
i != e; ++i) {
|
std::string work (input);
|
||||||
output.push_back (strdup (i->c_str ()));
|
// Skip past any delims at head of input string.
|
||||||
|
size_t pos = work.find_first_not_of (delims);
|
||||||
|
// If the string consists entirely of delims, then exit early.
|
||||||
|
if (pos == std::string::npos) return;
|
||||||
|
// Otherwise, jump forward to beginning of first word.
|
||||||
|
work = work.substr (pos);
|
||||||
|
// Find position of first delimiter.
|
||||||
|
pos = work.find_first_of (delims);
|
||||||
|
|
||||||
|
while (!work.empty() && pos != std::string::npos) {
|
||||||
|
// Everything from 0 to POS is the next word to copy.
|
||||||
|
output.push_back (strdup (work.substr (0,pos).c_str ()));
|
||||||
|
// Is there another word in the string?
|
||||||
|
size_t nextpos = work.find_first_not_of (delims, pos + 1);
|
||||||
|
if (nextpos != std::string::npos) {
|
||||||
|
// Yes? Then remove delims from beginning ...
|
||||||
|
work = work.substr (work.find_first_not_of (delims, pos + 1));
|
||||||
|
// and find the end of the word.
|
||||||
|
pos = work.find_first_of (delims);
|
||||||
|
} else {
|
||||||
|
// No? (Remainder of string is delims.) End the loop.
|
||||||
|
work = "";
|
||||||
|
pos = std::string::npos;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If `input' ended with non-delim char, then we'll get here with
|
||||||
|
// the last word of `input' in `work'; copy it now.
|
||||||
|
if (!work.empty ()) {
|
||||||
|
output.push_back (strdup (work.c_str ()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -198,32 +202,33 @@ static void ParseCStringVector (std::vector<char *> &output,
|
|||||||
/// from the caller (as PROGNAME) and its command-line arguments from
|
/// from the caller (as PROGNAME) and its command-line arguments from
|
||||||
/// an environment variable (whose name is given in ENVVAR).
|
/// an environment variable (whose name is given in ENVVAR).
|
||||||
///
|
///
|
||||||
void cl::ParseEnvironmentOptions (char *progName, char *envvar,
|
void cl::ParseEnvironmentOptions (const char *progName, const char *envVar,
|
||||||
const char *Overview) {
|
const char *Overview) {
|
||||||
|
// Check args.
|
||||||
|
assert (progName && "Program name not specified");
|
||||||
|
assert (envVar && "Environment variable name missing");
|
||||||
|
|
||||||
|
// Get the environment variable they want us to parse options out of.
|
||||||
|
const char *envValue = getenv (envVar);
|
||||||
|
if (!envValue)
|
||||||
|
return;
|
||||||
|
|
||||||
// Get program's "name", which we wouldn't know without the caller
|
// Get program's "name", which we wouldn't know without the caller
|
||||||
// telling us.
|
// telling us.
|
||||||
assert (progName && "Program name not specified");
|
std::vector<char *> newArgv;
|
||||||
static std::vector<char *> newargv; // Maybe making it "static" is a hack.
|
newArgv.push_back (strdup (progName));
|
||||||
int newargc;
|
|
||||||
newargv.push_back (progName);
|
|
||||||
|
|
||||||
// Get the environment variable they want us to parse options out of.
|
|
||||||
assert (envvar && "Environment variable name missing");
|
|
||||||
char *envvalue = getenv (envvar);
|
|
||||||
if (envvalue == NULL) {
|
|
||||||
// Env var not set --> act like there are no more command line
|
|
||||||
// arguments.
|
|
||||||
newargc = newargv.size ();
|
|
||||||
ParseCommandLineOptions (newargc, &newargv[0], Overview);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
std::string envvaluestr (envvalue);
|
|
||||||
|
|
||||||
// Parse the value of the environment variable into a "command line"
|
// Parse the value of the environment variable into a "command line"
|
||||||
// and hand it off to ParseCommandLineOptions().
|
// and hand it off to ParseCommandLineOptions().
|
||||||
ParseCStringVector (newargv, envvaluestr, " \v\f\t\r\n");
|
ParseCStringVector (newArgv, envValue);
|
||||||
newargc = newargv.size ();
|
int newArgc = newArgv.size ();
|
||||||
ParseCommandLineOptions (newargc, &newargv[0], Overview);
|
ParseCommandLineOptions (newArgc, &newArgv[0], Overview);
|
||||||
|
|
||||||
|
// Free all the strdup()ed strings.
|
||||||
|
for (std::vector<char *>::iterator i = newArgv.begin (), e = newArgv.end ();
|
||||||
|
i != e; ++i) {
|
||||||
|
free (*i);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void cl::ParseCommandLineOptions(int &argc, char **argv,
|
void cl::ParseCommandLineOptions(int &argc, char **argv,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user