diff --git a/src/generator.cpp b/src/generator.cpp index 32c322e..098429e 100644 --- a/src/generator.cpp +++ b/src/generator.cpp @@ -102,27 +102,32 @@ static uint64_t fnv1a_64(const void* data, size_t len) { // Output the recreate_file utility function output_recreate_file_utility(cpp); - // Embed file data - cpp << "static const unsigned char filedata[] = {"; - for (size_t i = 0; i < filedata.size(); ++i) { - if (i % 16 == 0) cpp << "\n "; - cpp << "0x" << std::hex << std::setw(2) << std::setfill('0') << (int)(unsigned char)filedata[i]; - if (i + 1 != filedata.size()) cpp << ", "; - } - cpp << "\n};\n"; - // Reset to decimal format - cpp << std::dec; - cpp << "static const size_t filedata_len = " << filedata.size() << ";\n"; - cpp << "static uint64_t file_hash = " << hash << "U;\n"; - cpp << "static std::filesystem::perms file_perms = std::filesystem::perms(" << static_cast(src_perms) << ");\n"; + // Write recreate_file function with embedded file data cpp << R"cpp( bool recreate_file(std::string destination_folder) { namespace fs = std::filesystem; fs::path outpath = fs::path(destination_folder) / ")cpp" << src.filename().string() << R"cpp("; - return _recreate_file_(outpath, file_hash, file_perms, filedata, filedata_len); -} -)cpp"; + + // File data embedded directly in the function + const unsigned char filedata[] = {)cpp"; + + // Embed file data directly + for (size_t i = 0; i < filedata.size(); ++i) { + if (i % 16 == 0) cpp << "\n "; + cpp << "0x" << std::hex << std::setw(2) << std::setfill('0') << (int)(unsigned char)filedata[i]; + if (i + 1 != filedata.size()) cpp << ", "; + } + + cpp << std::dec << "\n };\n"; + + // Direct call to _recreate_file_ with hash and permissions defined inline + cpp << " return _recreate_file_(outpath, " << hash << "ULL, " + << "std::filesystem::perms(" << static_cast(src_perms) << "), " + << "filedata, " << filedata.size() << ");\n"; + cpp << "}\n"; + cpp << "}\n"; + if (!silent) { std::cout << "[dehydrate] Generated: " << (dest / cppname) << ", " << (dest / hppname) << std::endl; } @@ -209,52 +214,51 @@ static uint64_t fnv1a_64(const void* data, size_t len) { // Output the recreate_file utility function output_recreate_file_utility(cpp); - // Embed all files + // Start writing recreate_tree - we'll embed file data directly in function + cpp << R"cpp( +bool recreate_tree(std::string destination_folder) { + namespace fs = std::filesystem; + bool any_written = false; +)cpp"; + + // Process each file for (const auto& file : files) { std::ifstream in(file, std::ios::binary); std::ostringstream oss; oss << in.rdbuf(); std::string filedata = oss.str(); uint64_t hash = fnv1a_64(filedata.data(), filedata.size()); - // Get file permissions fs::perms file_perms = fs::status(file).permissions(); - std::string rel = fs::relative(file, src).string(); std::string var = sanitize(rel); - cpp << "static const unsigned char data_" << var << "[] = {"; + + // Start a scope to limit data's lifetime + cpp << " {\n"; + cpp << " // File: " << rel << "\n"; + cpp << " fs::path outpath = fs::path(destination_folder) / \"" << rel << "\";\n"; + + // Embed file data directly in the function + cpp << " const unsigned char filedata[] = {"; for (size_t i = 0; i < filedata.size(); ++i) { - if (i % 16 == 0) cpp << "\n "; + if (i % 16 == 0) cpp << "\n "; cpp << "0x" << std::hex << std::setw(2) << std::setfill('0') << (int)(unsigned char)filedata[i]; if (i + 1 != filedata.size()) cpp << ", "; } - cpp << "\n};\n"; + cpp << "\n };\n"; // Reset to decimal format cpp << std::dec; - cpp << "static const size_t len_" << var << " = " << filedata.size() << ";\n"; - cpp << "static uint64_t hash_" << var << " = " << hash << "U;\n"; - cpp << "static const char* rel_" << var << " = \"" << rel << "\";\n"; - cpp << "static std::filesystem::perms perms_" << var << " = std::filesystem::perms(" << static_cast(file_perms) << ");\n"; + + // Direct call to _recreate_file_ with all parameters defined inline + cpp << " bool file_written = _recreate_file_(outpath, " + << hash << "ULL, std::filesystem::perms(" << static_cast(file_perms) << "), filedata, " << filedata.size() << ");\n"; + cpp << " any_written = any_written || file_written;\n"; + cpp << " }\n"; // Close scope to free memory } - // Write recreate_tree using the utility function - cpp << R"cpp( -bool recreate_tree(std::string destination_folder) { - namespace fs = std::filesystem; - bool any_written = false; -)cpp"; - for (const auto& file : files) { - std::string rel = fs::relative(file, src).string(); - std::string var = sanitize(rel); - cpp << R"cpp( { - fs::path outpath = fs::path(destination_folder) / )cpp" << "rel_" << var << R"cpp(; - bool file_written = _recreate_file_(outpath, )cpp" << "hash_" << var << ", perms_" << var << ", data_" << var << ", len_" << var << R"cpp(); - any_written = any_written || file_written; - } -)cpp"; - } - cpp << R"cpp( return any_written; -} -)cpp"; + + cpp << " return any_written;\n"; cpp << "}\n"; + cpp << "}\n"; + if (!silent) { std::cout << "[dehydrate] Generated: " << (dest / cppname) << ", " << (dest / hppname) << std::endl; }