diff options
| author | Markus Mittendrein <git@maxmitti.tk> | 2019-03-20 17:47:51 +0100 |
|---|---|---|
| committer | Markus Mittendrein <git@maxmitti.tk> | 2019-03-20 17:47:51 +0100 |
| commit | afaebd0407af3f88dfa9c46b74754a1d96f92ab5 (patch) | |
| tree | b67d7985362711f1c73e48f4551b5ccb7c0730f3 /examples/c4ls_asChildPp.cpp | |
| parent | bca1c49cf8aad47f5c1ecc2ba4f92f51115d7329 (diff) | |
| download | cc4group-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.cpp | 79 |
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 |
