diff options
Diffstat (limited to 'Source/cmSystemTools.cxx')
-rw-r--r-- | Source/cmSystemTools.cxx | 86 |
1 files changed, 64 insertions, 22 deletions
diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index 5c63d98..4118664 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -353,8 +353,8 @@ bool cmSystemTools::IsInternallyOn(const char* val) return false; } - for (std::string::iterator c = v.begin(); c != v.end(); c++) { - *c = static_cast<char>(toupper(*c)); + for (char& c : v) { + c = static_cast<char>(toupper(c)); } return v == "I_ON"; } @@ -378,8 +378,8 @@ bool cmSystemTools::IsOn(const char* val) onValues.insert("TRUE"); onValues.insert("Y"); } - for (std::string::iterator c = v.begin(); c != v.end(); c++) { - *c = static_cast<char>(toupper(*c)); + for (char& c : v) { + c = static_cast<char>(toupper(c)); } return (onValues.count(v) > 0); } @@ -414,8 +414,8 @@ bool cmSystemTools::IsOff(const char* val) } // Try and avoid toupper(). std::string v(val, len); - for (std::string::iterator c = v.begin(); c != v.end(); c++) { - *c = static_cast<char>(toupper(*c)); + for (char& c : v) { + c = static_cast<char>(toupper(c)); } return (offValues.count(v) > 0); } @@ -603,6 +603,56 @@ std::vector<std::string> cmSystemTools::ParseArguments(const char* command) return args; } +bool cmSystemTools::SplitProgramFromArgs(std::string const& command, + std::string& program, + std::string& args) +{ + const char* c = command.c_str(); + + // Skip leading whitespace. + while (isspace(static_cast<unsigned char>(*c))) { + ++c; + } + + // Parse one command-line element up to an unquoted space. + bool in_escape = false; + bool in_double = false; + bool in_single = false; + for (; *c; ++c) { + if (in_single) { + if (*c == '\'') { + in_single = false; + } else { + program += *c; + } + } else if (in_escape) { + in_escape = false; + program += *c; + } else if (*c == '\\') { + in_escape = true; + } else if (in_double) { + if (*c == '"') { + in_double = false; + } else { + program += *c; + } + } else if (*c == '"') { + in_double = true; + } else if (*c == '\'') { + in_single = true; + } else if (isspace(static_cast<unsigned char>(*c))) { + break; + } else { + program += *c; + } + } + + // The remainder of the command line holds unparsed arguments. + args = c; + + return !in_single && !in_escape && !in_double; +} + size_t cmSystemTools::CalculateCommandLineLengthLimit() { size_t sz = @@ -650,9 +700,8 @@ bool cmSystemTools::RunSingleCommand(std::vector<std::string> const& command, double timeout, Encoding encoding) { std::vector<const char*> argv; - for (std::vector<std::string>::const_iterator a = command.begin(); - a != command.end(); ++a) { - argv.push_back(a->c_str()); + for (std::string const& cmd : command) { + argv.push_back(cmd.c_str()); } argv.push_back(nullptr); @@ -814,11 +863,10 @@ bool cmSystemTools::DoesFileExistWithExtensions( { std::string hname; - for (std::vector<std::string>::const_iterator ext = headerExts.begin(); - ext != headerExts.end(); ++ext) { + for (std::string const& headerExt : headerExts) { hname = name; hname += "."; - hname += *ext; + hname += headerExt; if (cmSystemTools::FileExists(hname.c_str())) { return true; } @@ -1367,9 +1415,8 @@ std::vector<std::string> cmSystemTools::GetEnvironmentVariables() void cmSystemTools::AppendEnv(std::vector<std::string> const& env) { - for (std::vector<std::string>::const_iterator eit = env.begin(); - eit != env.end(); ++eit) { - cmSystemTools::PutEnv(*eit); + for (std::string const& eit : env) { + cmSystemTools::PutEnv(eit); } } @@ -1382,10 +1429,7 @@ cmSystemTools::SaveRestoreEnvironment::~SaveRestoreEnvironment() { // First clear everything in the current environment: std::vector<std::string> currentEnv = GetEnvironmentVariables(); - for (std::vector<std::string>::const_iterator eit = currentEnv.begin(); - eit != currentEnv.end(); ++eit) { - std::string var(*eit); - + for (std::string var : currentEnv) { std::string::size_type pos = var.find('='); if (pos != std::string::npos) { var = var.substr(0, pos); @@ -1464,9 +1508,7 @@ bool cmSystemTools::CreateTar(const char* outFileName, a.SetMTime(mtime); a.SetVerbose(verbose); - for (std::vector<std::string>::const_iterator i = files.begin(); - i != files.end(); ++i) { - std::string path = *i; + for (auto path : files) { if (cmSystemTools::FileIsFullPath(path.c_str())) { // Get the relative path to the file. path = cmSystemTools::RelativePath(cwd.c_str(), path.c_str()); |