summaryrefslogtreecommitdiffstats
path: root/examples
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
parentbca1c49cf8aad47f5c1ecc2ba4f92f51115d7329 (diff)
downloadcc4group-afaebd0407af3f88dfa9c46b74754a1d96f92ab5.tar.gz
cc4group-afaebd0407af3f88dfa9c46b74754a1d96f92ab5.zip
Add openAsChild, currently without writing support
Diffstat (limited to 'examples')
-rw-r--r--examples/c4ls_asChild.c100
-rw-r--r--examples/c4ls_asChildPp.cpp79
2 files changed, 179 insertions, 0 deletions
diff --git a/examples/c4ls_asChild.c b/examples/c4ls_asChild.c
new file mode 100644
index 0000000..8a9d00b
--- /dev/null
+++ b/examples/c4ls_asChild.c
@@ -0,0 +1,100 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <time.h>
+#include <locale.h>
+
+#include "cc4group.h"
+
+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)
+ {
+ fprintf(stderr, "USAGE: %s <group> <subgroup>\n", argv[0]);
+ return EXIT_FAILURE;
+ }
+
+ CC4Group* group = cc4group.new();
+ bool success = cc4group.openExisting(group, argv[1]);
+ if(!success)
+ {
+ fprintf(stderr, "ERROR: Can not open group file \"%s\": %s\n", argv[1], cc4group.getErrorMessage(group));
+ }
+ else
+ {
+ CC4Group* child = cc4group.openAsChild(group, argv[2]);
+ if(child == NULL)
+ {
+ fprintf(stderr, "ERROR: Can not open as child: %s\n", cc4group.getErrorMessage(group));
+ cc4group.delete(group);
+ return EXIT_FAILURE;
+ }
+ else
+ {
+ cc4group.delete(group);
+
+ CC4Group_EntryInfo* infos;
+ size_t entries;
+
+ success = cc4group.getEntryInfos(child, NULL, &infos, &entries);
+
+ if(!success)
+ {
+ fprintf(stderr, "ERROR: Can not list group entries: %s\n", cc4group.getErrorMessage(child));
+ }
+ else
+ {
+ for(size_t i = 0; i < entries; ++i)
+ {
+ printf("%c %s %s %s\t%s\n", infos[i].directory ? 'd' : infos[i].executable ? 'x' : ' ', formatTime(infos[i].modified), formatSize(infos[i].totalSize), infos[i].fileName, infos[i].author);
+ }
+
+ free(infos);
+ }
+
+ cc4group.delete(child);
+ }
+ }
+
+ return success ? EXIT_SUCCESS : EXIT_FAILURE;
+}
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