71 lines
1.6 KiB
C++
71 lines
1.6 KiB
C++
#pragma once
|
|
#include <chrono>
|
|
#include <cstdlib>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <map>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
inline std::string fix_path(const std::string &path)
|
|
{
|
|
if (path.find('\\') == path.npos)
|
|
return path;
|
|
std::string ret = path;
|
|
std::replace(ret.begin(), ret.end(), '\\', '/');
|
|
return ret;
|
|
}
|
|
|
|
inline std::string stupid_hash(const std::string &file)
|
|
{
|
|
std::ifstream iff(file);
|
|
if(!iff.is_open()) {
|
|
return std::to_string(rand());
|
|
}
|
|
unsigned long long check_sum = 0x0ULL;
|
|
char tmp;
|
|
while (iff.get(tmp))
|
|
{
|
|
check_sum += (unsigned long long)tmp;
|
|
}
|
|
iff.close();
|
|
std::stringstream ret;
|
|
ret << std::hex << check_sum;
|
|
return ret.str();
|
|
}
|
|
|
|
inline std::map<std::string, std::string>
|
|
createHashes(const std::vector<std::string> &dirs,
|
|
const std::string &extension)
|
|
{
|
|
std::map<std::string, std::string> hashes;
|
|
for (auto const &it : dirs)
|
|
{
|
|
for (const auto &file : std::filesystem::directory_iterator(it))
|
|
{
|
|
if (file.is_regular_file() && file.path().extension() == extension)
|
|
{
|
|
std::string path = fix_path(file.path().string());
|
|
hashes[path] = stupid_hash(path);
|
|
}
|
|
}
|
|
}
|
|
return hashes;
|
|
}
|
|
|
|
inline std::vector<std::filesystem::path>
|
|
getChangedFiles(const std::map<std::string, std::string> &oldHashes)
|
|
{
|
|
std::vector<std::filesystem::path> changedFiles;
|
|
for (const auto &file : oldHashes)
|
|
{
|
|
std::string newHash = stupid_hash(file.first);
|
|
if (newHash != file.second)
|
|
{
|
|
changedFiles.push_back(std::filesystem::path(file.first));
|
|
}
|
|
}
|
|
return changedFiles;
|
|
}
|