Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<fstream>
- #include<vector>
- #include<boost/thread/thread.hpp>
- #include<boost/filesystem.hpp>
- #include<boost/tokenizer.hpp>
- #include <boost/algorithm/string.hpp>
- #include "subprocess.h"
- #include "Command.h"
- namespace fs = boost::filesystem;
- /*
- * Traverse directory till maxdepth
- * return path
- */
- std::vector<std::string> traverse_dir() {
- fs::path p("path");
- int maxdepth = 3;
- std::vector<std::string> traverse_dir;
- fs::recursive_directory_iterator end_iter;
- boost::system::error_code dir_error;
- for(fs::recursive_directory_iterator begin(p, dir_error); begin != end_iter; ++begin) {
- try {
- if(dir_error.value()) {
- std::cout<<"The directory has error"<<dir_error.message()<<std::endl;
- continue;
- }
- if(fs::is_symlink(begin->path())) {
- begin.no_push();
- }
- if(fs::is_directory(begin->status())){
- if(begin.level()== maxdepth)
- begin.no_push();
- traverse_dir.push_back(begin->path().string());
- }
- }catch(fs::filesystem_error &ex) {
- std::cout<<"Boost Filesystem Error"<< ex.what()<<std::endl;
- }
- catch(const std::exception &ex){
- std::cout<<"Standard Exception"<<ex.what()<<std::endl;
- }
- }
- return traverse_dir;
- }
- struct Dirinfo {
- uint64_t dir_size;
- std::string owner;
- std::string dir_name;
- std::string dir_path;
- std::string f_time;
- std::vector<Dirinfo*> list;
- Dirinfo *next;
- };
- class Dirmapping {
- private:
- Dirinfo *root = nullptr;
- std::vector<std::string> dirs;
- public:
- Dirmapping() = default;
- Dirmapping(const std::vector<std::string> &dir_name);
- void createmap();
- void printchild(Dirinfo *child, std::ofstream &outfile, std::string &line);
- void printmap(Dirinfo *root);
- };
- Dirmapping::Dirmapping(const std::vector<std::string> &dirs): dirs(dirs) {}
- void Dirmapping::createmap() {
- for(auto it = this->dirs.begin(); it != dirs.end(); ++it) {
- std::cout<<"processing path "<<*it<<std::endl;
- fs::path insert_path(*it);
- Dirinfo *dirinfo = new Dirinfo;
- dirinfo->dir_path = *it;
- dirinfo->dir_name = insert_path.filename();
- dirinfo->f_time = std::to_string(fs::last_write_time(insert_path));
- dirinfo->next = nullptr;
- dirinfo->owner = "";
- dirinfo->dir_size = 0;
- if (root == nullptr) {
- root = dirinfo;
- }
- else {
- Dirinfo *temp = root;
- bool found_root = false;
- bool found_child_root = false;
- while(temp != nullptr) {
- fs::path p(temp->dir_path);
- fs::path compare_path(*it);
- if(fs::equivalent(compare_path.parent_path(), fs::path(temp->dir_path))){
- found_root = true;
- break;
- }
- for(auto it_child = temp->list.begin(); it_child != temp->list.end(); ++it_child){
- std::string compare_path_string = compare_path.parent_path().string();
- if(compare_path_string.compare((*it_child)->dir_path)){
- (*it_child)->list.push_back(dirinfo);
- found_child_root = true;
- std::cout<<"found child node"<<std::endl;
- break;
- }
- compare_path = compare_path.parent_path();
- }
- if(found_root || found_child_root)
- break;
- if(temp->next == nullptr)
- break;
- temp= temp->next;
- }
- if(found_root){
- temp->list.push_back(dirinfo);
- }
- else if(!found_child_root){
- temp->next = dirinfo;
- }
- }
- }
- printmap(root);
- }
- void Dirmapping::printchild(Dirinfo *temp, std::ofstream &outfile, std::string& line) {
- outfile<<temp->dir_path<<" "<<"n";
- for(std::vector<Dirinfo*>::iterator it = temp->list.begin(); it != temp->list.end(); ++it){
- outfile<<line<<(*it)->dir_path<<"n";
- if((*it)->list.size())
- line += "====";
- printchild(*it, outfile, line);
- line = "";
- }
- }
- void Dirmapping::printmap(Dirinfo *root) {
- Dirinfo *temp = root;
- std::ofstream outfile("dir_structure");
- std::string line = "=";
- while(temp != nullptr){
- uint64_t size = 0;
- outfile<<"Root:"<<temp->dir_path<<"nn";
- if(temp->list.size()){
- printchild(temp, outfile, line);
- }
- temp = temp->next;
- }
- outfile.close();
- }
- int main() {
- std::vector<std::string> dir_names = traverse_dir();
- Dirmapping dirmap(dir_names);
- dirmap.createmap();
- return 0;
- }
Add Comment
Please, Sign In to add comment