During one of assignment, I need to directory and store into proper data structure. I did following way and create to store as tree with multiple child and child will have linked list. Any improvement or suggestion.
File format:
/path1/path2/path3
/path1/path2/path3/cell_lists
/path1/path2/path3/path4
/path1/path2/path3/path4/ALLCELLS
/path1/path2/path3/path4/ALLCELLS/cells
/path1/path2/path3/path4/ALLCELLS/cells/fild
/path1/path2/path3/path4/ALLCELLS/cells/fild2
source code
#include<iostream>
#include<fstream>
#include<vector>
#include<boost/thread/thread.hpp>
#include<boost/filesystem.hpp>
#include "subprocess.h"
#include "Command.h"
namespace fs = boost::filesystem;
void create_dir() {
fs::path p("/user_path");
std::ofstream outfile("dir_names_150");
int maxdepth = 1;
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(fs::is_directory(begin->status())){
if(begin.level()== maxdepth)
begin.no_push();
outfile<<begin->path().string()<<std::endl;
}
}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;
}
}
outfile.close();
}
std::vector<std::string> read_dir() {
std::string line;
std::vector<std::string> dir_names;
std::ifstream myfile("dir_names_150");
while(getline(myfile, line)) {
dir_names.push_back(line);
}
myfile.close();
return dir_names;
}
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;
std::vector<std::string> dirs;
public:
Dirmapping(){}
Dirmapping(std::vector<std::string> &dir_name);
void createmap();
void printmap();
};
Dirmapping::Dirmapping(std::vector<std::string> &dirs) {
this->dirs = dirs;
root = nullptr;
}
void Dirmapping::createmap() {
for(std::vector<std::string>::iterator it = this->dirs.begin(); it != dirs.end(); ++it) {
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;
fs::path compare_path(*it);
bool found_root = false;
while(temp->next != nullptr) {
fs::path p(temp->dir_path);
while (compare_path.string().length()>2){
if(fs::equivalent(compare_path, p)){
found_root = true;
break;
}
compare_path = compare_path.parent_path();
}
if(found_root)
break;
temp= temp->next;
}
if(found_root)
temp->list.push_back(dirinfo);
else
temp->next = dirinfo;
}
}
}
void Dirmapping::printmap() {
Dirinfo *temp = root;
while(temp != nullptr){
std::cout<<"Root::"<<temp->dir_path<<std::endl;
if(temp->list.size()){
std::cout<<"children"<<std::endl;
for(std::vector<Dirinfo*>::iterator it = temp->list.begin(); it != temp->list.end(); ++it){
std::cout<<"-->"<<(*it)->dir_path<<std::endl;
}
}
}
}
int main() {
create_dir();
std::vector<std::string> dir_names = read_dir();
Dirmapping dirmap(dir_names);
dirmap.createmap();
dirmap.printmap();
return 0;
}