From e9bbfdd9a115332deb40da10784d478f4930c68f Mon Sep 17 00:00:00 2001 From: Artur Ryt Date: Sat, 30 Mar 2019 15:34:59 +0100 Subject: cmcmd: Pass args vector by const& --- Source/cmcmd.cxx | 21 +++++++++++---------- Source/cmcmd.h | 14 +++++++------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/Source/cmcmd.cxx b/Source/cmcmd.cxx index 8d63971..757a46c 100644 --- a/Source/cmcmd.cxx +++ b/Source/cmcmd.cxx @@ -368,7 +368,7 @@ struct CoCompileJob }; // called when args[0] == "__run_co_compile" -int cmcmd::HandleCoCompileCommands(std::vector& args) +int cmcmd::HandleCoCompileCommands(std::vector const& args) { std::vector jobs; std::string sourceFile; // store --source= @@ -466,7 +466,7 @@ int cmcmd::HandleCoCompileCommands(std::vector& args) return ret; } -int cmcmd::ExecuteCMakeCommand(std::vector& args) +int cmcmd::ExecuteCMakeCommand(std::vector const& args) { // IF YOU ADD A NEW COMMAND, DOCUMENT IT ABOVE and in cmakemain.cxx if (args.size() > 1) { @@ -1262,7 +1262,8 @@ int cmcmd::ExecuteCMakeCommand(std::vector& args) return 1; } -int cmcmd::HashSumFile(std::vector& args, cmCryptoHash::Algo algo) +int cmcmd::HashSumFile(std::vector const& args, + cmCryptoHash::Algo algo) { if (args.size() < 3) { return -1; @@ -1289,7 +1290,7 @@ int cmcmd::HashSumFile(std::vector& args, cmCryptoHash::Algo algo) return retval; } -int cmcmd::SymlinkLibrary(std::vector& args) +int cmcmd::SymlinkLibrary(std::vector const& args) { int result = 0; std::string realName = args[2]; @@ -1313,7 +1314,7 @@ int cmcmd::SymlinkLibrary(std::vector& args) return result; } -int cmcmd::SymlinkExecutable(std::vector& args) +int cmcmd::SymlinkExecutable(std::vector const& args) { int result = 0; std::string const& realName = args[2]; @@ -1387,7 +1388,7 @@ static void cmcmdProgressReport(std::string const& dir, std::string const& num) } } -int cmcmd::ExecuteEchoColor(std::vector& args) +int cmcmd::ExecuteEchoColor(std::vector const& args) { // The arguments are // args[0] == @@ -1445,7 +1446,7 @@ int cmcmd::ExecuteEchoColor(std::vector& args) return 0; } -int cmcmd::ExecuteLinkScript(std::vector& args) +int cmcmd::ExecuteLinkScript(std::vector const& args) { // The arguments are // args[0] == @@ -1658,9 +1659,9 @@ std::ostream& operator<<(std::ostream& stream, return stream; } -static bool RunCommand(const char* comment, std::vector& command, - bool verbose, NumberFormat exitFormat, - int* retCodeOut = nullptr, +static bool RunCommand(const char* comment, + std::vector const& command, bool verbose, + NumberFormat exitFormat, int* retCodeOut = nullptr, bool (*retCodeOkay)(int) = nullptr) { if (verbose) { diff --git a/Source/cmcmd.h b/Source/cmcmd.h index d1e03d0..69a7ecb 100644 --- a/Source/cmcmd.h +++ b/Source/cmcmd.h @@ -16,18 +16,18 @@ public: * Execute commands during the build process. Supports options such * as echo, remove file etc. */ - static int ExecuteCMakeCommand(std::vector&); + static int ExecuteCMakeCommand(std::vector const&); protected: - static int HandleCoCompileCommands(std::vector& args); - static int HashSumFile(std::vector& args, + static int HandleCoCompileCommands(std::vector const& args); + static int HashSumFile(std::vector const& args, cmCryptoHash::Algo algo); - static int SymlinkLibrary(std::vector& args); - static int SymlinkExecutable(std::vector& args); + static int SymlinkLibrary(std::vector const& args); + static int SymlinkExecutable(std::vector const& args); static bool SymlinkInternal(std::string const& file, std::string const& link); - static int ExecuteEchoColor(std::vector& args); - static int ExecuteLinkScript(std::vector& args); + static int ExecuteEchoColor(std::vector const& args); + static int ExecuteLinkScript(std::vector const& args); static int WindowsCEEnvironment(const char* version, const std::string& name); static int VisualStudioLink(std::vector const& args, int type); -- cgit v0.12 From f13aef4de57a74db72c258edb78108da60a030f1 Mon Sep 17 00:00:00 2001 From: Artur Ryt Date: Sat, 30 Mar 2019 15:37:36 +0100 Subject: cmcmd: Modernize for loops with cmMakeRange Also minor loop variable renaming --- Source/cmcmd.cxx | 124 ++++++++++++++++++++++++++----------------------------- 1 file changed, 58 insertions(+), 66 deletions(-) diff --git a/Source/cmcmd.cxx b/Source/cmcmd.cxx index 757a46c..da84738 100644 --- a/Source/cmcmd.cxx +++ b/Source/cmcmd.cxx @@ -381,8 +381,7 @@ int cmcmd::HandleCoCompileCommands(std::vector const& args) std::vector orig_cmd; bool doing_options = true; - for (std::string::size_type i = 2; i < args.size(); ++i) { - std::string const& arg = args[i]; + for (std::string const& arg : cmMakeRange(args).advance(2)) { // if the arg is -- then the rest of the args after // go into orig_cmd if (arg == "--") { @@ -482,9 +481,9 @@ int cmcmd::ExecuteCMakeCommand(std::vector const& args) } // If error occurs we want to continue copying next files. bool return_value = false; - for (std::string::size_type cc = 2; cc < args.size() - 1; cc++) { - if (!cmsys::SystemTools::CopyFileAlways(args[cc], args.back())) { - std::cerr << "Error copying file \"" << args[cc] << "\" to \"" + for (auto const& arg : cmMakeRange(args).advance(2).retreat(1)) { + if (!cmsys::SystemTools::CopyFileAlways(arg, args.back())) { + std::cerr << "Error copying file \"" << arg << "\" to \"" << args.back() << "\".\n"; return_value = true; } @@ -504,9 +503,9 @@ int cmcmd::ExecuteCMakeCommand(std::vector const& args) } // If error occurs we want to continue copying next files. bool return_value = false; - for (std::string::size_type cc = 2; cc < args.size() - 1; cc++) { - if (!cmSystemTools::CopyFileIfDifferent(args[cc], args.back())) { - std::cerr << "Error copying file (if different) from \"" << args[cc] + for (auto const& arg : cmMakeRange(args).advance(2).retreat(1)) { + if (!cmSystemTools::CopyFileIfDifferent(arg, args.back())) { + std::cerr << "Error copying file (if different) from \"" << arg << "\" to \"" << args.back() << "\".\n"; return_value = true; } @@ -518,10 +517,10 @@ int cmcmd::ExecuteCMakeCommand(std::vector const& args) if (args[1] == "copy_directory" && args.size() > 3) { // If error occurs we want to continue copying next files. bool return_value = false; - for (std::string::size_type cc = 2; cc < args.size() - 1; cc++) { - if (!cmSystemTools::CopyADirectory(args[cc], args.back())) { - std::cerr << "Error copying directory from \"" << args[cc] - << "\" to \"" << args.back() << "\".\n"; + for (auto const& arg : cmMakeRange(args).advance(2).retreat(1)) { + if (!cmSystemTools::CopyADirectory(arg, args.back())) { + std::cerr << "Error copying directory from \"" << arg << "\" to \"" + << args.back() << "\".\n"; return_value = true; } } @@ -614,8 +613,8 @@ int cmcmd::ExecuteCMakeCommand(std::vector const& args) } if (args[1] == "env") { - std::vector::const_iterator ai = args.begin() + 2; - std::vector::const_iterator ae = args.end(); + auto ai = args.cbegin() + 2; + auto ae = args.cend(); for (; ai != ae; ++ai) { std::string const& a = *ai; if (cmHasLiteralPrefix(a, "--unset=")) { @@ -654,10 +653,8 @@ int cmcmd::ExecuteCMakeCommand(std::vector const& args) #if defined(CMAKE_BUILD_WITH_CMAKE) if (args[1] == "environment") { - std::vector env = cmSystemTools::GetEnvironmentVariables(); - std::vector::iterator it; - for (it = env.begin(); it != env.end(); ++it) { - std::cout << *it << std::endl; + for (auto const& env : cmSystemTools::GetEnvironmentVariables()) { + std::cout << env << std::endl; } return 0; } @@ -666,9 +663,9 @@ int cmcmd::ExecuteCMakeCommand(std::vector const& args) if (args[1] == "make_directory" && args.size() > 2) { // If error occurs we want to continue copying next files. bool return_value = false; - for (std::string::size_type cc = 2; cc < args.size(); cc++) { - if (!cmSystemTools::MakeDirectory(args[cc])) { - std::cerr << "Error creating directory \"" << args[cc] << "\".\n"; + for (auto const& arg : cmMakeRange(args).advance(2)) { + if (!cmSystemTools::MakeDirectory(arg)) { + std::cerr << "Error creating directory \"" << arg << "\".\n"; return_value = true; } } @@ -687,14 +684,14 @@ int cmcmd::ExecuteCMakeCommand(std::vector const& args) // Remove file if (args[1] == "remove" && args.size() > 2) { bool force = false; - for (std::string::size_type cc = 2; cc < args.size(); cc++) { - if (args[cc] == "\\-f" || args[cc] == "-f") { + for (auto const& arg : cmMakeRange(args).advance(2)) { + if (arg == "\\-f" || arg == "-f") { force = true; } else { // Complain if the file could not be removed, still exists, // and the -f option was not given. - if (!cmSystemTools::RemoveFile(args[cc]) && !force && - cmSystemTools::FileExists(args[cc])) { + if (!cmSystemTools::RemoveFile(arg) && !force && + cmSystemTools::FileExists(arg)) { return 1; } } @@ -704,10 +701,10 @@ int cmcmd::ExecuteCMakeCommand(std::vector const& args) // Touch file if (args[1] == "touch" && args.size() > 2) { - for (std::string::size_type cc = 2; cc < args.size(); cc++) { - if (!cmSystemTools::Touch(args[cc], true)) { + for (auto const& arg : cmMakeRange(args).advance(2)) { + if (!cmSystemTools::Touch(arg, true)) { std::cerr << "cmake -E touch: failed to update \""; - std::cerr << args[cc] << "\".\n"; + std::cerr << arg << "\".\n"; return 1; } } @@ -716,10 +713,10 @@ int cmcmd::ExecuteCMakeCommand(std::vector const& args) // Touch file if (args[1] == "touch_nocreate" && args.size() > 2) { - for (std::string::size_type cc = 2; cc < args.size(); cc++) { - if (!cmSystemTools::Touch(args[cc], false)) { + for (auto const& arg : cmMakeRange(args).advance(2)) { + if (!cmSystemTools::Touch(arg, false)) { std::cerr << "cmake -E touch_nocreate: failed to update \""; - std::cerr << args[cc] << "\".\n"; + std::cerr << arg << "\".\n"; return 1; } } @@ -744,15 +741,15 @@ int cmcmd::ExecuteCMakeCommand(std::vector const& args) // Sleep command if (args[1] == "sleep" && args.size() > 2) { double total = 0; - for (size_t i = 2; i < args.size(); ++i) { + for (auto const& arg : cmMakeRange(args).advance(2)) { double num = 0.0; char unit; char extra; - int n = sscanf(args[i].c_str(), "%lg%c%c", &num, &unit, &extra); + int n = sscanf(arg.c_str(), "%lg%c%c", &num, &unit, &extra); if ((n == 1 || (n == 2 && unit == 's')) && num >= 0) { total += num; } else { - std::cerr << "Unknown sleep time format \"" << args[i] << "\".\n"; + std::cerr << "Unknown sleep time format \"" << arg << "\".\n"; return 1; } } @@ -1047,8 +1044,7 @@ int cmcmd::ExecuteCMakeCommand(std::vector const& args) std::string mtime; std::string format; bool doing_options = true; - for (std::string::size_type cc = 4; cc < args.size(); cc++) { - std::string const& arg = args[cc]; + for (auto const& arg : cmMakeRange(args).advance(4)) { if (doing_options && cmHasLiteralPrefix(arg, "--")) { if (arg == "--") { doing_options = false; @@ -1180,17 +1176,15 @@ int cmcmd::ExecuteCMakeCommand(std::vector const& args) bool isDebug = false; std::string pipe; - for (size_t i = 2; i < args.size(); ++i) { - const std::string& a = args[i]; - - if (a == "--experimental") { + for (auto const& arg : cmMakeRange(args).advance(2)) { + if (arg == "--experimental") { supportExperimental = true; - } else if (a == "--debug") { + } else if (arg == "--debug") { pipe.clear(); isDebug = true; - } else if (a.substr(0, pipePrefix.size()) == pipePrefix) { + } else if (arg.substr(0, pipePrefix.size()) == pipePrefix) { isDebug = false; - pipe = a.substr(pipePrefix.size()); + pipe = arg.substr(pipePrefix.size()); if (pipe.empty()) { cmSystemTools::Error("No pipe given after --pipe="); return 2; @@ -1270,8 +1264,7 @@ int cmcmd::HashSumFile(std::vector const& args, } int retval = 0; - for (std::string::size_type cc = 2; cc < args.size(); cc++) { - const char* filename = args[cc].c_str(); + for (auto const& filename : cmMakeRange(args).advance(2)) { // Cannot compute sum of a directory if (cmSystemTools::FileIsDirectory(filename)) { std::cerr << "Error: " << filename << " is a directory" << std::endl; @@ -1398,48 +1391,47 @@ int cmcmd::ExecuteEchoColor(std::vector const& args) int color = cmsysTerminal_Color_Normal; bool newline = true; std::string progressDir; - for (unsigned int i = 2; i < args.size(); ++i) { - if (args[i].find("--switch=") == 0) { + for (auto const& arg : cmMakeRange(args).advance(2)) { + if (arg.find("--switch=") == 0) { // Enable or disable color based on the switch value. - std::string value = args[i].substr(9); + std::string value = arg.substr(9); if (!value.empty()) { enabled = cmSystemTools::IsOn(value); } - } else if (cmHasLiteralPrefix(args[i], "--progress-dir=")) { - progressDir = args[i].substr(15); - } else if (cmHasLiteralPrefix(args[i], "--progress-num=")) { + } else if (cmHasLiteralPrefix(arg, "--progress-dir=")) { + progressDir = arg.substr(15); + } else if (cmHasLiteralPrefix(arg, "--progress-num=")) { if (!progressDir.empty()) { - std::string const& progressNum = args[i].substr(15); + std::string const& progressNum = arg.substr(15); cmcmdProgressReport(progressDir, progressNum); } - } else if (args[i] == "--normal") { + } else if (arg == "--normal") { color = cmsysTerminal_Color_Normal; - } else if (args[i] == "--black") { + } else if (arg == "--black") { color = cmsysTerminal_Color_ForegroundBlack; - } else if (args[i] == "--red") { + } else if (arg == "--red") { color = cmsysTerminal_Color_ForegroundRed; - } else if (args[i] == "--green") { + } else if (arg == "--green") { color = cmsysTerminal_Color_ForegroundGreen; - } else if (args[i] == "--yellow") { + } else if (arg == "--yellow") { color = cmsysTerminal_Color_ForegroundYellow; - } else if (args[i] == "--blue") { + } else if (arg == "--blue") { color = cmsysTerminal_Color_ForegroundBlue; - } else if (args[i] == "--magenta") { + } else if (arg == "--magenta") { color = cmsysTerminal_Color_ForegroundMagenta; - } else if (args[i] == "--cyan") { + } else if (arg == "--cyan") { color = cmsysTerminal_Color_ForegroundCyan; - } else if (args[i] == "--white") { + } else if (arg == "--white") { color = cmsysTerminal_Color_ForegroundWhite; - } else if (args[i] == "--bold") { + } else if (arg == "--bold") { color |= cmsysTerminal_Color_ForegroundBold; - } else if (args[i] == "--no-newline") { + } else if (arg == "--no-newline") { newline = false; - } else if (args[i] == "--newline") { + } else if (arg == "--newline") { newline = true; } else { // Color is enabled. Print with the current color. - cmSystemTools::MakefileColorEcho(color, args[i].c_str(), newline, - enabled); + cmSystemTools::MakefileColorEcho(color, arg.c_str(), newline, enabled); } } -- cgit v0.12