From afaebd0407af3f88dfa9c46b74754a1d96f92ab5 Mon Sep 17 00:00:00 2001 From: Markus Mittendrein Date: Wed, 20 Mar 2019 17:47:51 +0100 Subject: Add openAsChild, currently without writing support --- examples/c4ls_asChild.c | 100 ++++++++++++++++++++++++++++++++++++++++++++ examples/c4ls_asChildPp.cpp | 79 ++++++++++++++++++++++++++++++++++ 2 files changed, 179 insertions(+) create mode 100644 examples/c4ls_asChild.c create mode 100644 examples/c4ls_asChildPp.cpp (limited to 'examples') 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 +#include +#include +#include + +#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 \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 + +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] << " \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 -- cgit v1.2.3-54-g00ecf