summaryrefslogtreecommitdiffstats
path: root/examples/c4ls_asChildPp.cpp
diff options
context:
space:
mode:
authorMarkus Mittendrein <git@maxmitti.tk>2019-03-20 17:47:51 +0100
committerMarkus Mittendrein <git@maxmitti.tk>2019-03-20 17:47:51 +0100
commitafaebd0407af3f88dfa9c46b74754a1d96f92ab5 (patch)
treeb67d7985362711f1c73e48f4551b5ccb7c0730f3 /examples/c4ls_asChildPp.cpp
parentbca1c49cf8aad47f5c1ecc2ba4f92f51115d7329 (diff)
downloadcc4group-afaebd0407af3f88dfa9c46b74754a1d96f92ab5.tar.gz
cc4group-afaebd0407af3f88dfa9c46b74754a1d96f92ab5.zip
Add openAsChild, currently without writing support
Diffstat (limited to 'examples/c4ls_asChildPp.cpp')
-rw-r--r--examples/c4ls_asChildPp.cpp79
1 files changed, 79 insertions, 0 deletions
diff --git a/examples/c4ls_asChildPp.cpp b/examples/c4ls_asChildPp.cpp
new file mode 100644
index 0000000..019b781
--- /dev/null
+++ b/examples/c4ls_asChildPp.cpp
@@ -0,0 +1,79 @@
+#include "cppc4group.hpp"
+#include <iostream>
+
+const char* formatTime(int32_t time)
+{
+ time_t tTime = time;
+ static char ret[128];
+ strftime(ret, sizeof(ret), "%c", localtime(&tTime));
+ return ret;
+}
+
+const char* formatSize(uint32_t size)
+{
+ static char ret[128];
+ if(size < 1000)
+ {
+ snprintf(ret, 128, "%4u", size);
+ return ret;
+ }
+
+ static const char prefixes[] = " KMGTPE";
+
+ const char* prefix = prefixes;
+ uint32_t fract = 0;
+
+ while(size >= 1000 && *(prefix + 1) != '\0')
+ {
+ ++prefix;
+ fract = size % 1000;
+ size /= 1000;
+ }
+
+ if(size < 10)
+ {
+ snprintf(ret, 128, "%1.1f%c", (float)size + (float)fract / 1000, *prefix);
+ }
+ else
+ {
+ snprintf(ret, 128, "%3u%c", size, *prefix);
+ }
+
+ return ret;
+}
+
+int main(int argc, char* argv[])
+{
+ if(argc != 3)
+ {
+ std::cerr << "USAGE: " << argv[0] << " <group> <subgroup>\n";
+ return EXIT_FAILURE;
+ }
+
+ CppC4Group g;
+ if(!g.openExisting(argv[1]))
+ {
+ std::cerr << "ERROR: Can't open group: " << g.getErrorMessage() << '\n';
+ return EXIT_FAILURE;
+ }
+
+ if(auto child = g.openAsChild(argv[2]))
+ {
+ if(auto entries = child->getEntryInfos())
+ {
+ for(const auto& entry : *entries)
+ {
+ std::cout << (entry.directory ? 'd' : entry.executable ? 'x' : ' ') << " " << formatTime(entry.modified) << ' ' << formatSize(entry.totalSize) << " " << entry.fileName << '\t' << entry.author << '\n';
+ }
+ }
+ else
+ {
+ std::cerr << "ERROR: Can't list subgroup contents: " << child->getErrorMessage();
+ }
+ }
+ else
+ {
+ std::cerr << "ERROR: Can't open subgroup: " << g.getErrorMessage();
+ return EXIT_FAILURE;
+ }
+} \ No newline at end of file