Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <iostream>
- #include <string>
- #include <fstream>
- using namespace std;
- class dirTokenizer {
- public:
- dirTokenizer(string fullname) : fullname(fullname + "/"), i(0) {
- };
- string next();
- std::string fullname;
- int i;
- };
- string dirTokenizer::next() {
- int j = fullname.find("/", i + 1);
- if (j == -1) return "";
- string name = fullname.substr(i + 1, j - i - 1);
- i = j;
- return name;
- }
- class dirNode {
- public:
- dirNode(string path, string name);
- void addChild(dirNode *newChild);
- void print(int level);
- dirNode *findNode(string pathname);
- std::string path;
- std::string name;
- dirNode *firstChild;
- dirNode *nextSibling;
- };
- dirNode::dirNode(string path, string name) {
- this->path = path;
- this->name = name;
- this->firstChild = NULL;
- this->nextSibling = NULL;
- }
- dirNode *dirNode::findNode(string pathname) {
- dirTokenizer path = dirTokenizer(pathname);
- string dir = path.next();
- dirNode *cur = this;
- if (cur->name != dir) return NULL;
- dir = path.next();
- if (dir == "") return cur;
- cur = cur->firstChild;
- while (cur != NULL) {
- if (cur->name != dir) {
- cur = cur->nextSibling;
- continue;
- }
- dir = path.next();
- if (dir == "") return cur;
- else cur = cur->firstChild;
- }
- return cur;
- }
- void dirNode::print(int level) {
- for (int i = 0; i < level * 5; i++) cout << " ";
- cout << this->name << endl;
- dirNode *child = this->firstChild;
- while (child != NULL) {
- child->print(level + 1);
- child = child->nextSibling;
- }
- }
- void dirNode::addChild(dirNode *newChild) {
- if(this->firstChild == NULL) this->firstChild = newChild;
- else {
- dirNode *tmp = this->firstChild;
- while(tmp->nextSibling != NULL)
- tmp = tmp->nextSibling;
- tmp->nextSibling = newChild;
- }
- }
- //returns root node of tree built
- dirNode *build() {
- ifstream inStream;
- inStream.open("ITUnix");
- string aline = "";
- int lineNum = 0;
- string currentDir = "";
- dirNode *root = NULL;
- dirNode *curNode = NULL;
- //create root node
- getline(inStream, aline);
- aline = aline.substr(0, aline.length() - 2); //get rid of /: at the end
- lineNum++;
- cout << lineNum << endl;
- dirTokenizer path(aline);
- currentDir = path.fullname;
- root = new dirNode(path.fullname, path.next());
- curNode = root;
- //build tree off root
- while (!inStream.eof()) {
- getline(inStream, aline);
- if (aline.empty()) { // handles blank lines in files
- continue;
- } else if (aline[0] == '/') {
- aline = aline.substr(0, aline.length() - 1);
- currentDir = aline;
- curNode = root->findNode(currentDir);
- } else {
- curNode->addChild(new dirNode(currentDir + "/" + aline, aline));
- }
- }
- return root;
- }
- int main(int argc, char *argv[]) {
- if (argc != 2) { //check if arguments were entered correctly
- cout << "Usage: ./list filePath" << endl;
- return 1;
- } else {
- string path = argv[1];
- if (path[0] != '/')
- path = "/" + path;
- cout << path << "<- given path" << endl;
- dirNode *root = build();
- dirNode *cur = NULL;
- if (root->findNode(path) == NULL) {
- cout << "ERROR, NO SUCH PATH EXISTS" << endl;
- return 2;
- } else {
- //root->print(0);
- cur = root->findNode(path);
- cur->print(0);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement