Commit db730f3e authored by Brad King's avatar Brad King Committed by Kitware Robot
Browse files

Merge topic 'file-path-checksum-cpp11'

23ad08dd

 Migrate cmFilePathChecksum to C++11
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Merge-request: !1334
parents 32a54d23 23ad08dd
......@@ -13,10 +13,10 @@ cmFilePathChecksum::cmFilePathChecksum()
{
}
cmFilePathChecksum::cmFilePathChecksum(const std::string& currentSrcDir,
const std::string& currentBinDir,
const std::string& projectSrcDir,
const std::string& projectBinDir)
cmFilePathChecksum::cmFilePathChecksum(std::string const& currentSrcDir,
std::string const& currentBinDir,
std::string const& projectSrcDir,
std::string const& projectBinDir)
{
setupParentDirs(currentSrcDir, currentBinDir, projectSrcDir, projectBinDir);
}
......@@ -29,36 +29,35 @@ cmFilePathChecksum::cmFilePathChecksum(cmMakefile* makefile)
makefile->GetHomeOutputDirectory());
}
void cmFilePathChecksum::setupParentDirs(const std::string& currentSrcDir,
const std::string& currentBinDir,
const std::string& projectSrcDir,
const std::string& projectBinDir)
void cmFilePathChecksum::setupParentDirs(std::string const& currentSrcDir,
std::string const& currentBinDir,
std::string const& projectSrcDir,
std::string const& projectBinDir)
{
parentDirs[0].first = cmsys::SystemTools::GetRealPath(currentSrcDir);
parentDirs[1].first = cmsys::SystemTools::GetRealPath(currentBinDir);
parentDirs[2].first = cmsys::SystemTools::GetRealPath(projectSrcDir);
parentDirs[3].first = cmsys::SystemTools::GetRealPath(projectBinDir);
this->parentDirs[0].first = cmsys::SystemTools::GetRealPath(currentSrcDir);
this->parentDirs[1].first = cmsys::SystemTools::GetRealPath(currentBinDir);
this->parentDirs[2].first = cmsys::SystemTools::GetRealPath(projectSrcDir);
this->parentDirs[3].first = cmsys::SystemTools::GetRealPath(projectBinDir);
parentDirs[0].second = "CurrentSource";
parentDirs[1].second = "CurrentBinary";
parentDirs[2].second = "ProjectSource";
parentDirs[3].second = "ProjectBinary";
this->parentDirs[0].second = "CurrentSource";
this->parentDirs[1].second = "CurrentBinary";
this->parentDirs[2].second = "ProjectSource";
this->parentDirs[3].second = "ProjectBinary";
}
std::string cmFilePathChecksum::get(const std::string& filePath) const
std::string cmFilePathChecksum::get(std::string const& filePath) const
{
std::string relPath;
std::string relSeed;
{
const std::string fileReal = cmsys::SystemTools::GetRealPath(filePath);
std::string const fileReal = cmsys::SystemTools::GetRealPath(filePath);
std::string parentDir;
// Find closest project parent directory
for (size_t ii = 0; ii != numParentDirs; ++ii) {
const std::string& pDir = parentDirs[ii].first;
if (!pDir.empty() &&
cmsys::SystemTools::IsSubDirectory(fileReal, pDir)) {
relSeed = parentDirs[ii].second;
parentDir = pDir;
for (auto const& pDir : this->parentDirs) {
if (!pDir.first.empty() &&
cmsys::SystemTools::IsSubDirectory(fileReal, pDir.first)) {
parentDir = pDir.first;
relSeed = pDir.second;
break;
}
}
......@@ -77,11 +76,11 @@ std::string cmFilePathChecksum::get(const std::string& filePath) const
cmCryptoHash(cmCryptoHash::AlgoSHA256).ByteHashString(relSeed + relPath);
// Convert binary checksum to string
return cmBase32Encoder().encodeString(&hashBytes[0], hashBytes.size(),
return cmBase32Encoder().encodeString(&hashBytes.front(), hashBytes.size(),
false);
}
std::string cmFilePathChecksum::getPart(const std::string& filePath,
std::string cmFilePathChecksum::getPart(std::string const& filePath,
size_t length) const
{
return get(filePath).substr(0, length);
......
......@@ -5,6 +5,7 @@
#include "cmConfigure.h" // IWYU pragma: keep
#include <array>
#include <stddef.h>
#include <string>
#include <utility>
......@@ -29,37 +30,35 @@ public:
cmFilePathChecksum();
/// @brief Initilizes the parent directories manually
cmFilePathChecksum(const std::string& currentSrcDir,
const std::string& currentBinDir,
const std::string& projectSrcDir,
const std::string& projectBinDir);
cmFilePathChecksum(std::string const& currentSrcDir,
std::string const& currentBinDir,
std::string const& projectSrcDir,
std::string const& projectBinDir);
/// @brief Initilizes the parent directories from a makefile
cmFilePathChecksum(cmMakefile* makefile);
/// @brief Allows parent directories setup after construction
///
void setupParentDirs(const std::string& currentSrcDir,
const std::string& currentBinDir,
const std::string& projectSrcDir,
const std::string& projectBinDir);
void setupParentDirs(std::string const& currentSrcDir,
std::string const& currentBinDir,
std::string const& projectSrcDir,
std::string const& projectBinDir);
/* @brief Calculates the path checksum for the parent directory of a file
*
*/
std::string get(const std::string& filePath) const;
std::string get(std::string const& filePath) const;
/* @brief Same as get() but returns only the first length characters
*
*/
std::string getPart(const std::string& filePath,
std::string getPart(std::string const& filePath,
size_t length = partLengthDefault) const;
private:
/// Size of the parent directory list
static const size_t numParentDirs = 4;
/// List of (directory name, seed name) pairs
std::pair<std::string, std::string> parentDirs[numParentDirs];
std::array<std::pair<std::string, std::string>, 4> parentDirs;
};
#endif
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