diff options
-rw-r--r-- | ArchiveEx/Archive.cpp | 19 | ||||
-rw-r--r-- | ArchiveEx/Archive.h | 5 | ||||
-rw-r--r-- | 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 <cstdint> #include <cstdio> #include <cstring> +#include <functional> #include <memory> #include <zlib.h> @@ -292,27 +293,27 @@ Archive::FindFreeSpot(const std::size_t bytes) const void -Archive::PrintNamesOfEntries() const +Archive::ForEachEntry(std::function<void(const char*)> func) const { for (const auto& entry : entries) - std::printf("%s\n", entry.name); + func(entry.name); } void -Archive::PrintBlocks() const +Archive::ForEachBlock(std::function<void(const char*)> 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 <cstddef> #include <cstdint> +#include <functional> #include <vector> @@ -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<void(const char*)> func) const; + void ForEachBlock(std::function<void(const char*)> 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; |