From ac8732867f57f59b8f4aec8d28b8e745046c9ac7 Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 20 Aug 2022 23:04:27 +0200 Subject: Added options to inspect archive content in dat utility Old PrintX methods were replaced by higher order functions ForEachX that are intended to give slighly more granual control and move the responsibility of what actually happens to the user without introducing a whole iterator. --- ArchiveEx/Archive.cpp | 19 ++++++++++--------- ArchiveEx/Archive.h | 5 +++-- ArchiveEx/dat.cpp | 15 ++++++++++++--- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/ArchiveEx/Archive.cpp b/ArchiveEx/Archive.cpp index 808b6fc..b39de88 100644 --- a/ArchiveEx/Archive.cpp +++ b/ArchiveEx/Archive.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -292,27 +293,27 @@ Archive::FindFreeSpot(const std::size_t bytes) const void -Archive::PrintNamesOfEntries() const +Archive::ForEachEntry(std::function func) const { for (const auto& entry : entries) - std::printf("%s\n", entry.name); + func(entry.name); } void -Archive::PrintBlocks() const +Archive::ForEachBlock(std::function func) const { - for (std::size_t i = 0; i < blocks.size(); ++i) { - std::printf("%ld\t", i); - switch (blocks[i]) { + for (const int content : blocks) { + switch (content) { case UNUSED_BLOCK: - std::printf("(unused)\n"); + func("(unused)"); break; case DIRECTORY_BLOCK: - std::printf("(dir)\n"); + func("(directory)"); break; default: - std::printf("%s\n", entries[blocks[i]].name); + func(entries[content].name); + break; } } } diff --git a/ArchiveEx/Archive.h b/ArchiveEx/Archive.h index 7ba32e4..9526ff0 100644 --- a/ArchiveEx/Archive.h +++ b/ArchiveEx/Archive.h @@ -2,6 +2,7 @@ #include #include +#include #include @@ -32,8 +33,8 @@ public: void WriteMeta(bool create=false); void GenerateBlockMap(); std::size_t FindFreeSpot(std::size_t bytes) const; - void PrintNamesOfEntries() const; - void PrintBlocks() const; + void ForEachEntry(std::function func) const; + void ForEachBlock(std::function func) const; std::size_t DirectoryBlocks() const; private: const char* path; diff --git a/ArchiveEx/dat.cpp b/ArchiveEx/dat.cpp index ae1304d..06beba1 100644 --- a/ArchiveEx/dat.cpp +++ b/ArchiveEx/dat.cpp @@ -13,6 +13,7 @@ enum class Action { NOTHING, LIST, + BLOCKS, UPDATE, EXTRACT, }; @@ -56,11 +57,15 @@ try { return 0; } ArchiveEx::Archive archive(opts.archive, opts.create); + std::size_t i = 0; switch (opts.action) { case Action::NOTHING: break; case Action::LIST: - archive.PrintNamesOfEntries(); + archive.ForEachEntry([](const char* name){ std::cout << name << std::endl; }); + break; + case Action::BLOCKS: + archive.ForEachBlock([&i](const char* name){ std::cout << i++ << "\t" << name << std::endl; }); break; case Action::UPDATE: for (const auto& file : opts.files) @@ -89,7 +94,7 @@ ParseArgs(int argc, char* argv[]) { int opt; Options opts; - while (-1 != (opt = getopt(argc, argv, ":hluxc"))) { + while (-1 != (opt = getopt(argc, argv, ":hlbuxc"))) { switch (opt) { case 'h': opts.help = true; @@ -97,6 +102,9 @@ ParseArgs(int argc, char* argv[]) case 'l': opts.action = Action::LIST; break; + case 'b': + opts.action = Action::BLOCKS; + break; case 'u': opts.action = Action::UPDATE; break; @@ -124,7 +132,7 @@ ParseArgs(int argc, char* argv[]) void PrintUsage(std::ostream& out) { - out << "Usage: " << program << " [-hluxc] archive [file [files...]]" << std::endl; + out << "Usage: " << program << " [-hlbuxc] archive [file [files...]]" << std::endl; } @@ -134,6 +142,7 @@ PrintHelp(std::ostream& out) out << "Options:" << std::endl; out << " -h\tPrints this help message and exits." << std::endl; out << " -l\tLists files in the archive." << std::endl; + out << " -b\tLists content of each block in archive." << std::endl; out << " -u\tInserts files from file system into archive." << std::endl; out << " -x\tExtracts files from the archive." << std::endl; out << " -c\tCreate the archive if it does not exist." << std::endl; -- cgit v1.1