Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. #ifndef FOLDER_H
  2. #define FOLDER_H
  3.  
  4. #include <string>
  5. #include <sys/stat.h>
  6. #include <sys/types.h>
  7. #include <dirent.h>
  8. #include <vector>
  9. #include <algorithm>
  10. #include "node.h"
  11.  
  12. using namespace std;
  13.  
  14. class Folder: public Node {
  15. public:
  16. Folder(std::string path): Node(path) {
  17. m_path = path;
  18. FILE* fp = fopen(path.c_str(), "r+");
  19. if(fp != NULL){
  20. std::string s = "It is not Folder!";
  21. fclose(fp);
  22. throw s;
  23. }
  24. DIR* dir = opendir(path.c_str());
  25. if(dir == NULL){
  26. std::string s = "Node is not exist!";
  27. throw s;
  28. }
  29. else closedir(dir);
  30. }
  31.  
  32. void addChild(Node* child) {
  33. _v.push_back(child);
  34. }
  35.  
  36. Node* getChild(int num) {
  37. return _v[num];
  38. }
  39.  
  40. int infoByte() {
  41. int total = 0;
  42. for(int i = 0; i < _v.size(); i++){
  43. total += _v.at(i)->infoByte();
  44. }
  45. return total;
  46. }
  47.  
  48. std::string name(){
  49. return getFileLastName(m_path);
  50. }
  51.  
  52. std::string listNode(){
  53. std::string s;
  54. std::string name[_v.size()];
  55. for(int i=0; i<_v.size(); i++){
  56. name[i] = getFileLastName(_v[i]->getFileName());
  57. }
  58. sort(name, name+_v.size());
  59. s = s + name[0];
  60. for(int i=1; i<_v.size(); i++){
  61. s = s + " " + name[i];
  62. }
  63. return s;
  64. }
  65.  
  66. int getChildSize(){
  67. return _v.size();
  68. }
  69.  
  70. std::string folderFindNode(Node* node, std::string name){
  71. std::string s;
  72. for(int i=0; i<node->getChildSize() && node->getChild(i) != nullptr; i++){
  73. if(name == getFileLastName(node->getChild(i)->getFileName()) && name.find(".") == std::string::npos){
  74. s = s + node->getChild(i)->getFileName() + "\n" + node->getChild(i)->folderFindNode(node->getChild(i), name);
  75. }
  76. else if(name != getFileLastName(node->getChild(i)->getFileName()) && name.find(".") == std::string::npos){
  77. s = s + node->getChild(i)->folderFindNode(node->getChild(i), name);
  78. }
  79. }
  80. return s;
  81. }
  82.  
  83. std::string findNode(std::string name){
  84. std::string s;
  85. std::string e_s;
  86. int i = 0;
  87. while(i < _v.size() && name.find(".") != std::string::npos){
  88. if(_v[i]->getChild(0) != nullptr) e_s = _v[i]->findNode(name);
  89. if(name == getFileLastName(_v[i]->getFileName()) && !e_s.empty() && name.find("/") == std::string::npos){
  90. std::string final_path = s + _v[i]->getFileName()+ "\n" + e_s;
  91. size_t pos = 0;
  92. std::string token;
  93. std::string replace_c = "./";
  94. while ((pos = final_path.find(name)) != std::string::npos) {
  95. token = final_path.substr(0, pos);
  96. break;
  97. }
  98. string::size_type pos_s = 0;
  99. string::size_type srclen = token.size();
  100. string::size_type dstlen = replace_c.size();
  101. while( (pos_s=final_path.find(token, pos_s)) != string::npos){
  102. final_path.replace(pos_s, srclen, replace_c);
  103. pos_s += dstlen;
  104. }
  105. return final_path;
  106. }
  107. if(name == getFileLastName(_v[i]->getFileName()) && !e_s.empty()) return s + _v[i]->getFileName()+ "\n" + e_s;
  108. if(name == getFileLastName(_v[i]->getFileName()) && e_s.empty()) return s + _v[i]->getFileName();
  109. i++;
  110. }
  111. if(name.find(".") == std::string::npos){ //find folder
  112. std::string s;
  113. for(int i=0; i<_v.size() && _v[i]->getChild(i) != nullptr; i++){
  114. if(name == getFileLastName(_v[i]->getFileName()) && name.find(".") == std::string::npos){
  115. s = s + _v[i]->getFileName() + "*" + _v[i]->folderFindNode(_v[i], name);
  116. }
  117. else if(name != getFileLastName(_v[i]->getFileName()) && name.find(".") == std::string::npos){
  118. s = s + _v[i]->folderFindNode(_v[i], name);
  119. }
  120. }
  121. string final_path;
  122. std::string delimiter = "*";
  123. int replace_size = getFileName().length();
  124. size_t pos = 0;
  125. std::string token;
  126. while ((pos = s.find(delimiter)) != std::string::npos) {
  127. token = s.substr(0, pos);
  128. final_path = final_path + "." + token.substr(replace_size, token.size()) + "\n";
  129. s.erase(0, pos + delimiter.length());
  130. }
  131. final_path = final_path + "." + s.substr(replace_size, s.size());
  132. final_path = final_path.substr(0, final_path.length()-1);
  133. return final_path;
  134. }
  135. }
  136.  
  137. private:
  138. std::string m_path;
  139. std::vector<Node*> _v;
  140. };
  141. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement