From 9636b03cca8b812fa2e33adf9b30ab5268b6eefb Mon Sep 17 00:00:00 2001 From: Artur Ryt Date: Tue, 26 Feb 2019 21:25:00 +0100 Subject: cmFileCommand: Refactor HandleCMakePathCommand Make use of cmJoin and fresh cmRange::transform to reduce function complexity. Move conversion logic to named functions. --- Source/cmFileCommand.cxx | 47 +++++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/Source/cmFileCommand.cxx b/Source/cmFileCommand.cxx index 59ef48d..a2018c3 100644 --- a/Source/cmFileCommand.cxx +++ b/Source/cmFileCommand.cxx @@ -2500,44 +2500,43 @@ bool cmFileCommand::HandleRemove(std::vector const& args, return true; } +namespace { +std::string ToNativePath(const std::string& path) +{ + const auto& outPath = cmSystemTools::ConvertToOutputPath(path); + if (outPath.size() > 1 && outPath.front() == '\"' && + outPath.back() == '\"') { + return outPath.substr(1, outPath.size() - 2); + } + return outPath; +} + +std::string ToCMakePath(const std::string& path) +{ + auto temp = path; + cmSystemTools::ConvertToUnixSlashes(temp); + return temp; +} +} + bool cmFileCommand::HandleCMakePathCommand( std::vector const& args, bool nativePath) { - std::vector::const_iterator i = args.begin(); if (args.size() != 3) { this->SetError("FILE([TO_CMAKE_PATH|TO_NATIVE_PATH] path result) must be " "called with exactly three arguments."); return false; } - i++; // Get rid of subcommand #if defined(_WIN32) && !defined(__CYGWIN__) char pathSep = ';'; #else char pathSep = ':'; #endif - std::vector path = cmSystemTools::SplitString(*i, pathSep); - i++; - const char* var = i->c_str(); - std::string value; - for (std::vector::iterator j = path.begin(); j != path.end(); - ++j) { - if (j != path.begin()) { - value += ";"; - } - if (!nativePath) { - cmSystemTools::ConvertToUnixSlashes(*j); - } else { - *j = cmSystemTools::ConvertToOutputPath(*j); - // remove double quotes in the path - std::string& s = *j; + std::vector path = cmSystemTools::SplitString(args[1], pathSep); - if (s.size() > 1 && s.front() == '\"' && s.back() == '\"') { - s = s.substr(1, s.size() - 2); - } - } - value += *j; - } - this->Makefile->AddDefinition(var, value.c_str()); + std::string value = cmJoin( + cmMakeRange(path).transform(nativePath ? ToNativePath : ToCMakePath), ";"); + this->Makefile->AddDefinition(args[2], value.c_str()); return true; } -- cgit v0.12