node.hh (2588B)
1 #ifndef __BULKRENAME_NODE_GUARD__ 2 #define __BULKRENAME_NODE_GUARD__ 3 4 #include <filesystem> 5 #include <fstream> 6 #include <iostream> 7 #include <vector> 8 9 10 enum class nodetype_t 11 { 12 file, 13 dir 14 }; 15 16 17 typedef class node_t* node_ptr_t; 18 19 typedef class node_t 20 { 21 public: 22 /* node_t(nodetype_t _type, node_ptr_t _parent, const ::std::string& _name) */ 23 node_t(nodetype_t _type, node_ptr_t _parent, const ::std::filesystem::path& _path) 24 : type(_type), parent(_parent), path(_path) 25 {} 26 27 virtual bool has_next() const = 0; 28 virtual void print(::std::ostream& out) const = 0; 29 30 const nodetype_t get_type() const { return type; } 31 const ::std::filesystem::path get_path() const { return path; } 32 33 protected: 34 nodetype_t type; 35 node_ptr_t parent; 36 ::std::filesystem::path path; 37 ::std::string name; 38 39 }* node_ptr_t; 40 41 ::std::ostream& operator<<(::std::ostream&, node_ptr_t); 42 43 44 typedef class file_t : public node_t 45 { 46 public: 47 explicit file_t(const ::std::filesystem::path& path, node_ptr_t parent) 48 : node_t(nodetype_t::file, parent, path), new_name(path.string()) 49 {} 50 51 bool has_next() const override { return false; } 52 void print(::std::ostream& out) const override; 53 54 void set_name(const ::std::string& name) { new_name = name; } 55 56 void rename() const; 57 58 private: 59 ::std::string new_name; 60 61 }* file_ptr_t; 62 63 ::std::ostream& operator<<(::std::ostream&, file_ptr_t); 64 65 66 typedef class dir_t : public node_t 67 { 68 public: 69 explicit dir_t(const ::std::filesystem::path& path, node_ptr_t parent) 70 : node_t(nodetype_t::dir, parent, path) 71 {} 72 73 void populate(::std::filesystem::directory_iterator, bool recurse = false); 74 bool has_next() const override { return !children.empty(); } 75 void print(::std::ostream& out) const override; 76 77 ::std::vector<file_ptr_t> file_leaves() const; 78 79 private: 80 ::std::vector<node_ptr_t> children; 81 82 }* dir_ptr_t; 83 84 ::std::ostream& operator<<(::std::ostream&, dir_ptr_t); 85 86 87 class nodetree_t 88 { 89 public: 90 explicit nodetree_t(bool recurse, const ::std::string& _root_name) 91 : recurse(recurse), root_name(_root_name), root(new dir_t(_root_name, nullptr)) 92 {} 93 94 nodetree_t& operator=(const nodetree_t&) = delete; 95 nodetree_t(const nodetree_t&) = delete; 96 97 void populate(::std::filesystem::directory_iterator); 98 void print(::std::ostream& out) const; 99 100 ::std::vector<file_ptr_t> get_files() const; 101 102 private: 103 bool recurse; 104 ::std::string root_name; 105 dir_ptr_t root; 106 107 }; 108 109 ::std::ostream& operator<<(::std::ostream&, nodetree_t); 110 111 112 #endif//__BULKRENAME_NODE_GUARD__