fix hash - uint64_t

This commit is contained in:
Your Name 2025-05-17 09:28:41 +12:00
parent 4dbd55fbed
commit 7cdb08746e
2 changed files with 26 additions and 13 deletions

View File

@ -15,6 +15,16 @@ static std::string sanitize(const std::string& name) {
return out;
}
static uint64_t fnv1a_64(const void* data, size_t len) {
const uint8_t* p = static_cast<const uint8_t*>(data);
uint64_t h = 0xcbf29ce484222325ULL;
for (size_t i = 0; i < len; ++i)
h = (h ^ p[i]) * 0x100000001b3ULL;
return h;
}
void generate_file_code(const std::string& source, const std::string& destfolder, bool silent) {
fs::path src(source);
fs::path dest(destfolder);
@ -26,7 +36,7 @@ void generate_file_code(const std::string& source, const std::string& destfolder
std::ostringstream oss;
oss << in.rdbuf();
std::string filedata = oss.str();
std::string hash = hash_data(filedata);
uint64_t hash = fnv1a_64(filedata.data(), filedata.size());
// Write HPP
std::ofstream hpp(dest / hppname);
hpp << "#pragma once\n#include <string>\nnamespace " << ns << " {\nbool recreate_file(std::string destination_folder);\n}\n";
@ -65,16 +75,13 @@ static uint64_t fnv1a_64(const void* data, size_t len) {
bool recreate_file(std::string destination_folder) {
namespace fs = std::filesystem;
fs::path outpath = fs::path(destination_folder) / ")cpp" << src.filename().string() << R"cpp(";
std::string existing_hash;
uint64_t existing_hash;
if (fs::exists(outpath)) {
std::ifstream in(outpath, std::ios::binary);
std::ostringstream oss;
oss << in.rdbuf();
std::string data = oss.str();
uint64_t h = fnv1a_64(data.data(), data.size());
std::ostringstream hex;
hex << std::hex << std::setw(16) << std::setfill('0') << h;
existing_hash = hex.str();
existing_hash = fnv1a_64(data.data(), data.size());
}
bool needs_write = !fs::exists(outpath) || existing_hash != file_hash;
if (needs_write) {
@ -106,6 +113,7 @@ void walk_dir(const fs::path& dir, F&& f) {
}
}
void generate_folder_code(const std::string& source, const std::string& destfolder, bool silent) {
fs::path src(source);
fs::path dest(destfolder);
@ -181,7 +189,7 @@ static uint64_t fnv1a_64(const void* data, size_t len) {
std::ostringstream oss;
oss << in.rdbuf();
std::string filedata = oss.str();
std::string hash = hash_data(filedata);
uint64_t hash = fnv1a_64(filedata.data(), filedata.size());
std::string rel = fs::relative(file, src).string();
std::string var = sanitize(rel);
cpp << "static const unsigned char data_" << var << "[] = {";
@ -194,7 +202,7 @@ static uint64_t fnv1a_64(const void* data, size_t len) {
// Reset to decimal format
cpp << std::dec;
cpp << "static const size_t len_" << var << " = " << filedata.size() << ";\n";
cpp << "static const char* hash_" << var << " = \"" << hash << "\";\n";
cpp << "static uint64_t hash_" << var << " = \"" << hash << "\";\n";
cpp << "static const char* rel_" << var << " = \"" << rel << "\";\n";
}
// Write recreate_tree using heredoc style
@ -209,15 +217,12 @@ bool recreate_tree(std::string destination_folder) {
cpp << R"cpp( {
fs::path outpath = fs::path(destination_folder) / )cpp" << "rel_" << var << R"cpp(;
fs::create_directories(outpath.parent_path());
std::string existing_hash;
uint64_t existing_hash;
if (fs::exists(outpath)) {
std::ifstream in(outpath, std::ios::binary);
std::ostringstream oss; oss << in.rdbuf();
std::string data = oss.str();
uint64_t h = fnv1a_64(data.data(), data.size());
std::ostringstream hex;
hex << std::hex << std::setw(16) << std::setfill('0') << h;
existing_hash = hex.str();
existing_hash = fnv1a_64(data.data(), data.size());
}
bool needs_write = !fs::exists(outpath) || existing_hash != )cpp" << "hash_" << var << R"cpp(;
if (needs_write) {

View File

@ -30,5 +30,13 @@ g++ -std=c++17 -I $TEST_DIR -o $TEST_DIR/testexe $TEST_DIR/main.cpp $TEST_DIR/_s
$TEST_DIR/testexe
# second time should be no changes (hash should match)
$TEST_DIR/testexe
# third time just two files changed
rm $TEST_DIR/temp/generator.cpp
echo "whee!" >> $TEST_DIR/temp/hash.cpp
$TEST_DIR/testexe