From 6a541317665c4afee0530717d50d26279f8687c0 Mon Sep 17 00:00:00 2001 From: hyperion Date: Tue, 10 Nov 2026 17:09:10 +0100 Subject: [PATCH] added file size to the json --- main.cpp | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index d9b9af1..4c27f7d 100644 --- a/main.cpp +++ b/main.cpp @@ -28,6 +28,7 @@ struct TreeNode { int ctime; int atime; std::optional> content; + int size; std::vector children; }; @@ -57,6 +58,7 @@ TreeNode parse(const std::filesystem::path& p, int& next_inode, int& parent_inod node.mtime = static_cast(buf.st_mtime); node.atime = static_cast(buf.st_atime); node.ctime = static_cast(buf.st_ctime); + node.size = static_cast(buf.st_size); } if(node.type == std::filesystem::file_type::directory) { @@ -271,17 +273,40 @@ JsonValue to_json(const TreeNode& node) { childrenArray.push_back(to_json(child)); } + obj["size"] = node.size; obj["children"] = std::move(childrenArray); return obj; } int main(int argc, char* argv[]) { - if(argc != 2) { + std::cout << argc; + if(argc < 2 || argc > 3) { std::cerr << "Wrong number of arguments passed\n"; return 1; } + + const std::string flag = "-c"; + + if(argv[1] == flag) { + std::cout << "Compact mode\n"; + if(!std::filesystem::exists(argv[2])) { + std::cerr << "The path doesnt exist\n"; + return 1; + } + + const std::filesystem::path rootPath = argv[2]; + + TreeNode root = parse_tree(rootPath); + JsonValue json = to_json(root); + std::string json_str = serialize(json, ""); + + std::cout << json_str << "\n"; + + return 0; + } + if(!std::filesystem::exists(argv[1])) { std::cerr << "The path doesnt exist\n"; return 1;