summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2022-08-20 23:04:27 +0200
committerAki <please@ignore.pl>2022-08-20 23:04:27 +0200
commitac8732867f57f59b8f4aec8d28b8e745046c9ac7 (patch)
tree68cfa9093dbbb31dc4af3cffd33eb5d0cd178079
parentbc9502fbc36946f1d96d3f188e18346baddbf1d5 (diff)
downloadstarshatter-ac8732867f57f59b8f4aec8d28b8e745046c9ac7.zip
starshatter-ac8732867f57f59b8f4aec8d28b8e745046c9ac7.tar.gz
starshatter-ac8732867f57f59b8f4aec8d28b8e745046c9ac7.tar.bz2
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.
-rw-r--r--ArchiveEx/Archive.cpp19
-rw-r--r--ArchiveEx/Archive.h5
-rw-r--r--ArchiveEx/dat.cpp15
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;