From 98e8359c885483d557548c6038a784051d45ecc3 Mon Sep 17 00:00:00 2001 From: Marcel Nowicki Date: Sat, 12 Feb 2022 02:59:31 +0100 Subject: [PATCH] this added json output to stdout, to file and global json stats to file. tarstats is feature complete. --- main.cpp | 53 +++++++++++++++++++++++++++++++++++++++++------------ tarfunc.cpp | 39 +++++++++++++++++++++++++++++++++++++-- tarfunc.h | 12 ++++++++++-- 3 files changed, 88 insertions(+), 16 deletions(-) diff --git a/main.cpp b/main.cpp index c3e92de..37be045 100644 --- a/main.cpp +++ b/main.cpp @@ -128,11 +128,24 @@ int main(int argc, char** argv) { } gzclose(gzIn); global_sizeofallfiles += std::filesystem::file_size(archiveName); - tar::consolestats(typecount, std::filesystem::file_size(archiveName), - sizeof_allitems); - if (toFile) { - tar::txtfilestats(typecount, std::filesystem::file_size(archiveName), - sizeof_allitems, archiveName); + + if(toJSON){ + if(!toFile) { + tar::jsonconsolestats(typecount, std::filesystem::file_size(archiveName), + sizeof_allitems, archiveName, "gzarchive"); + } + if(toFile) { + tar::jsonfilestats(typecount, std::filesystem::file_size(archiveName), + sizeof_allitems, archiveName, "gzarchive"); + } + } + if(!toJSON) { + if(toFile) + tar::txtfilestats(typecount, std::filesystem::file_size(archiveName), + sizeof_allitems, archiveName); + if(!toFile) + tar::consolestats(typecount, std::filesystem::file_size(archiveName), + sizeof_allitems); } } } @@ -177,16 +190,32 @@ int main(int argc, char** argv) { file.close(); global_sizeofallfiles += std::filesystem::file_size(archiveName); - std::cout << toJSON << '\n'; - tar::consolestats(typecount, std::filesystem::file_size(archiveName), - sizeof_allitems); - if (toFile) { - tar::txtfilestats(typecount, std::filesystem::file_size(archiveName), - sizeof_allitems, archiveName); + if(toJSON){ + if(!toFile) { + tar::jsonconsolestats(typecount, std::filesystem::file_size(archiveName), + sizeof_allitems, archiveName, "archive"); + } + if(toFile) { + tar::jsonfilestats(typecount, std::filesystem::file_size(archiveName), + sizeof_allitems, archiveName, "archive"); + } + } + if(!toJSON) { + if(toFile) + tar::txtfilestats(typecount, std::filesystem::file_size(archiveName), + sizeof_allitems, archiveName); + if(!toFile) + tar::consolestats(typecount, std::filesystem::file_size(archiveName), + sizeof_allitems); } } } } - tar::consoleglobalstats(globaltypecount, global_sizeofallfiles, global_sizeofallitems, archiveFilename); + if(toFile && toJSON) + tar::jsonglobalfile(globaltypecount, global_sizeofallfiles, + global_sizeofallitems, archiveFilename.size()); + + tar::consoleglobalstats(globaltypecount, global_sizeofallfiles, + global_sizeofallitems, archiveFilename.size()); return 0; } diff --git a/tarfunc.cpp b/tarfunc.cpp index 89601fb..585cf56 100644 --- a/tarfunc.cpp +++ b/tarfunc.cpp @@ -87,9 +87,9 @@ void tar::consolestats (std::map &typecount, uintmax_t t } void tar::consoleglobalstats (std::map &typecount, uintmax_t filesize, - uintmax_t itemsize, std::vector &files) { + uintmax_t itemsize, size_t fileamount) { std::cout << "GLOBAL STATS" << '\n'; - std::cout << "File amount: " << files.size() << '\n'; + std::cout << "File amount: " << fileamount << '\n'; std::cout << "Size of all archives: " << filesize << " Bytes" << '\n'; std::cout << "Size of all items: " << itemsize << " Bytes" << '\n'; for (auto &i : typecount) { @@ -98,6 +98,41 @@ void tar::consoleglobalstats (std::map &typecount, uintm std::cout << '\n' << '\n'; } +void tar::jsonconsolestats (std::map &typecount, uintmax_t tarfilesize, uintmax_t sizeofall, + std::string &filename, std::string type) { + std::cout << "{\"type\": \"" << type << "\", \"name\": \"" << filename << "\", \"size\": " << tarfilesize + << ", \"itemsize\": " << sizeofall << ", \"files\": " << typecount[tarconstant::typeFile] + << ", \"dir\": " << typecount[tarconstant::typeDir] << ", \"symlinks\": " << typecount[tarconstant::typeSym] + << ", \"hardlinks\": " << typecount[tarconstant::typeHard] << ", \"other\": " + << typecount[tarconstant::typeOther] << "}" << '\n' << '\n'; +} + +void tar::jsonfilestats (std::map &typecount, uintmax_t tarfilesize, uintmax_t sizeofall, + std::string &filename, std::string type) { + std::string jsnname = filename + ".json"; + std::ofstream jsonfile(jsnname); + jsonfile << "{\"type\": \"" << type << "\", \"name\": \"" << filename << "\", \"size\": " << tarfilesize + << ", \"itemsize\": " << sizeofall << ", \"files\": " << typecount[tarconstant::typeFile] + << ", \"dir\": " << typecount[tarconstant::typeDir] << ", \"symlinks\": " << typecount[tarconstant::typeSym] + << ", \"hardlinks\": " << typecount[tarconstant::typeHard] << ", \"other\": " + << typecount[tarconstant::typeOther] << "}" << '\n'; + jsonfile.close(); + std::cout << "Stats written to " << jsnname << '\n' << '\n'; +} + +void tar::jsonglobalfile (std::map &typecount, uintmax_t itemsize, + uintmax_t filesize, size_t fileamount) { + std::ofstream jsonfile("global.json"); + jsonfile << "{\"itemamount\": " << fileamount << ", \"itemsize\": " << itemsize << ", \"filesize\": " + << filesize << ", \"files\": " << typecount[tarconstant::typeFile] + << ", \"dir\": " << typecount[tarconstant::typeDir] << ", \"symlinks\": " + << typecount[tarconstant::typeSym] << ", \"hardlinks\": " + << typecount[tarconstant::typeHard] << ", \"other\": " + << typecount[tarconstant::typeOther] << "}" << '\n'; + jsonfile.close(); + std::cout << "Global stats written to global.json" << '\n' << '\n'; +} + // writes default console output to txt file void tar::txtfilestats (std::map &typecount, uintmax_t tarfilesize, uintmax_t sizeofall, std::string archiveName) { diff --git a/tarfunc.h b/tarfunc.h index f4674e6..04b4916 100644 --- a/tarfunc.h +++ b/tarfunc.h @@ -33,8 +33,16 @@ namespace tar { // to write global stats to stdout void consoleglobalstats (std::map &typecount, uintmax_t itemfilesize, - uintmax_t szfiles, std::vector &files); - + uintmax_t szfiles, size_t fileamount); +// to write JSON stats of a file to stdout + void jsonconsolestats (std::map &typecount, uintmax_t tarfilesize, uintmax_t sizeofall, + std::string &filename, std::string type); +// to write JSON stats to file + void jsonfilestats (std::map &typecount, uintmax_t tarfilesize, uintmax_t sizeofall, + std::string &filename, std::string type); +// to write global stats to JSON file + void jsonglobalfile (std::map &typecount, uintmax_t itemsize, + uintmax_t filesize, size_t fileamount); // to write default console output to txt file void txtfilestats(std::map &typecount, uintmax_t tarfilesize, uintmax_t sizeofall, std::string archiveName);