Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <string>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. class dirTokenizer {
  9. public:
  10.  
  11. dirTokenizer(string fullname) : fullname(fullname + "/"), i(0) {
  12. };
  13. string next();
  14. std::string fullname;
  15. int i;
  16. };
  17.  
  18. string dirTokenizer::next() {
  19. int j = fullname.find("/", i + 1);
  20. if (j == -1) return "";
  21. string name = fullname.substr(i + 1, j - i - 1);
  22. i = j;
  23. return name;
  24. }
  25.  
  26. class dirNode {
  27. public:
  28. dirNode(string path, string name);
  29. void addChild(dirNode *newChild);
  30. void print(int level);
  31. dirNode *findNode(string pathname);
  32. std::string path;
  33. std::string name;
  34. dirNode *firstChild;
  35. dirNode *nextSibling;
  36. };
  37.  
  38. dirNode::dirNode(string path, string name) {
  39. this->path = path;
  40. this->name = name;
  41. this->firstChild = NULL;
  42. this->nextSibling = NULL;
  43. }
  44.  
  45. dirNode *dirNode::findNode(string pathname) {
  46. dirTokenizer path = dirTokenizer(pathname);
  47. string dir = path.next();
  48. dirNode *cur = this;
  49. if (cur->name != dir) return NULL;
  50. dir = path.next();
  51. if (dir == "") return cur;
  52. cur = cur->firstChild;
  53. while (cur != NULL) {
  54. if (cur->name != dir) {
  55. cur = cur->nextSibling;
  56. continue;
  57. }
  58. dir = path.next();
  59. if (dir == "") return cur;
  60. else cur = cur->firstChild;
  61. }
  62. return cur;
  63. }
  64.  
  65. void dirNode::print(int level) {
  66. for (int i = 0; i < level * 5; i++) cout << " ";
  67. cout << this->name << endl;
  68. dirNode *child = this->firstChild;
  69. while (child != NULL) {
  70. child->print(level + 1);
  71. child = child->nextSibling;
  72. }
  73. }
  74.  
  75. void dirNode::addChild(dirNode *newChild) {
  76. if(this->firstChild == NULL) this->firstChild = newChild;
  77. else {
  78. dirNode *tmp = this->firstChild;
  79. while(tmp->nextSibling != NULL)
  80. tmp = tmp->nextSibling;
  81. tmp->nextSibling = newChild;
  82. }
  83. }
  84.  
  85. //returns root node of tree built
  86.  
  87. dirNode *build() {
  88. ifstream inStream;
  89. inStream.open("ITUnix");
  90. string aline = "";
  91. int lineNum = 0;
  92. string currentDir = "";
  93.  
  94. dirNode *root = NULL;
  95. dirNode *curNode = NULL;
  96.  
  97. //create root node
  98. getline(inStream, aline);
  99. aline = aline.substr(0, aline.length() - 2); //get rid of /: at the end
  100. lineNum++;
  101. cout << lineNum << endl;
  102. dirTokenizer path(aline);
  103. currentDir = path.fullname;
  104. root = new dirNode(path.fullname, path.next());
  105. curNode = root;
  106.  
  107. //build tree off root
  108. while (!inStream.eof()) {
  109.  
  110. getline(inStream, aline);
  111.  
  112. if (aline.empty()) { // handles blank lines in files
  113. continue;
  114. } else if (aline[0] == '/') {
  115.  
  116. aline = aline.substr(0, aline.length() - 1);
  117. currentDir = aline;
  118. curNode = root->findNode(currentDir);
  119.  
  120. } else {
  121. curNode->addChild(new dirNode(currentDir + "/" + aline, aline));
  122.  
  123. }
  124. }
  125. return root;
  126. }
  127.  
  128. int main(int argc, char *argv[]) {
  129.  
  130. if (argc != 2) { //check if arguments were entered correctly
  131. cout << "Usage: ./list filePath" << endl;
  132. return 1;
  133. } else {
  134. string path = argv[1];
  135. if (path[0] != '/')
  136. path = "/" + path;
  137. cout << path << "<- given path" << endl;
  138. dirNode *root = build();
  139. dirNode *cur = NULL;
  140. if (root->findNode(path) == NULL) {
  141. cout << "ERROR, NO SUCH PATH EXISTS" << endl;
  142. return 2;
  143. } else {
  144. //root->print(0);
  145. cur = root->findNode(path);
  146. cur->print(0);
  147. }
  148. }
  149. return 0;
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement