bulkrename

Bulk file/directory renaming utility, similar to ranger's built-in bulkrename command
git clone git://git.deurzen.net/bulkrename
Log | Files | Refs | LICENSE

node.cc (2158B)


      1 #include "node.hh"
      2 
      3 void
      4 dir_t::populate(::std::filesystem::directory_iterator dir_it, bool recurse)
      5 {
      6     for (auto& entry : dir_it) {
      7         if (::std::filesystem::is_directory(entry)) {
      8             if (recurse) {
      9                 dir_ptr_t dir = new dir_t(entry.path(), parent);
     10                 children.push_back(dir);
     11                 dir->populate(::std::filesystem::directory_iterator(entry), recurse);
     12             } else {
     13                 file_ptr_t file = new file_t(entry.path(), parent);
     14                 children.push_back(file);
     15             }
     16         } else if (::std::filesystem::is_regular_file(entry)) {
     17             file_ptr_t file = new file_t(entry.path(), parent);
     18             children.push_back(file);
     19         }
     20     }
     21 }
     22 
     23 
     24 void
     25 nodetree_t::populate(::std::filesystem::directory_iterator dir_it)
     26 {
     27     root->populate(dir_it, recurse);
     28 }
     29 
     30 void
     31 file_t::print(::std::ostream& out) const
     32 {
     33     out << path.filename().string() << ::std::endl;
     34 }
     35 
     36 void
     37 dir_t::print(::std::ostream& out) const
     38 {
     39     for (auto& child : children)
     40         child->print(out);
     41 }
     42 
     43 void
     44 nodetree_t::print(::std::ostream& out) const
     45 {
     46     root->print(out);
     47 }
     48 
     49 ::std::vector<file_ptr_t>
     50 dir_t::file_leaves() const
     51 {
     52     ::std::vector<file_ptr_t> files;
     53     for (auto& child : children)
     54         if (child->get_type() == nodetype_t::dir)
     55             for (auto& file : dynamic_cast<dir_ptr_t>(child)->file_leaves())
     56                 files.push_back(file);
     57         else
     58             files.push_back(dynamic_cast<file_ptr_t>(child));
     59 
     60     return files;
     61 }
     62 
     63 void
     64 file_t::rename() const
     65 {
     66     if (name != new_name)
     67         ::std::filesystem::rename(path, path.parent_path().string() + "/" + new_name);
     68 }
     69 
     70 ::std::vector<file_ptr_t>
     71 nodetree_t::get_files() const
     72 {
     73     return root->file_leaves();
     74 }
     75 
     76 
     77 ::std::ostream&
     78 operator<<(::std::ostream& out, node_ptr_t node)
     79 {
     80     node->print(out);
     81     return out;
     82 }
     83 
     84 ::std::ostream&
     85 operator<<(::std::ostream& out, file_ptr_t file)
     86 {
     87 
     88     return out;
     89 }
     90 
     91 ::std::ostream&
     92 operator<<(::std::ostream& out, dir_ptr_t dir)
     93 {
     94 
     95     return out;
     96 }
     97 
     98 ::std::ostream&
     99 operator<<(::std::ostream& out, nodetree_t tree)
    100 {
    101     tree.print(out);
    102     return out;
    103 }