#include <iostream>
#include <vector>
#include <sstream>
#include <queue>
class Node
{
public:
Node(std::string name) :
name_(name)
{}
~Node()
{
for (Node* child : children_)
{
delete child;
}
children_.clear();
}
void addChild(Node* child)
{
children_.push_back(child);
}
bool hasChild(const std::string name)
{
for (Node* child : children_)
{
if (child->getName() == name)
{
return true;
}
}
return false;
}
std::string getName() const
{
return name_;
}
std::vector<Node*> getChildren() const
{
return children_;
}
Node* getChild(const std::string name) const
{
for (Node* child : children_)
{
if (child->getName() == name)
{
return child;
}
}
return nullptr;
}
void printChildren(int level)
{
for (Node* child : children_)
{
for (int i = 0; i < level; i++)
{
std::cout << ' ';
}
std::cout << child->getName() << std::endl;
child->printChildren(level + 1);
}
}
private:
std::string name_;
std::vector<Node*> children_;
};
class FileSys
{
public:
FileSys() :
root_(new Node(""))
{}
~FileSys()
{
delete root_;
}
void add(std::vector<std::string> paths)
{
for (std::string path : paths)
{
std::stringstream ss(path);
std::string dir;
std::queue<std::string> dirs;
while (std::getline(ss, dir, '/'))
{
dirs.push(dir);
}
dirs.pop();
Node* parent = root_;
while (!dirs.empty())
{
Node* current = new Node(dirs.front());
if (!parent->hasChild(dirs.front()))
{
parent->addChild(current);
parent = current;
}
else
{
parent = parent->getChild(dirs.front());
}
dirs.pop();
}
}
}
Node* getRoot() const
{
return root_;
}
void print()
{
root_->printChildren(0);
}
private:
Node* root_;
};
int main(int argc, char** argv)
{
std::vector<std::string> vec;
vec.push_back("/Users/Vlad/my/file");
vec.push_back("/bin/bash");
vec.push_back("/usr/local/bin");
vec.push_back("/usr/local/lib");
FileSys fileSys;
fileSys.add(vec);
fileSys.print();
return 0;
}