Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef FOLDER_H
- #define FOLDER_H
- #include <string>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <dirent.h>
- #include <vector>
- #include <algorithm>
- #include "node.h"
- using namespace std;
- class Folder: public Node {
- public:
- Folder(std::string path): Node(path) {
- m_path = path;
- FILE* fp = fopen(path.c_str(), "r+");
- if(fp != NULL){
- std::string s = "It is not Folder!";
- fclose(fp);
- throw s;
- }
- DIR* dir = opendir(path.c_str());
- if(dir == NULL){
- std::string s = "Node is not exist!";
- throw s;
- }
- else closedir(dir);
- }
- void addChild(Node* child) {
- _v.push_back(child);
- }
- Node* getChild(int num) {
- return _v[num];
- }
- int infoByte() {
- int total = 0;
- for(int i = 0; i < _v.size(); i++){
- total += _v.at(i)->infoByte();
- }
- return total;
- }
- std::string name(){
- return getFileLastName(m_path);
- }
- std::string listNode(){
- std::string s;
- std::string name[_v.size()];
- for(int i=0; i<_v.size(); i++){
- name[i] = getFileLastName(_v[i]->getFileName());
- }
- sort(name, name+_v.size());
- s = s + name[0];
- for(int i=1; i<_v.size(); i++){
- s = s + " " + name[i];
- }
- return s;
- }
- int getChildSize(){
- return _v.size();
- }
- std::string folderFindNode(Node* node, std::string name){
- std::string s;
- for(int i=0; i<node->getChildSize() && node->getChild(i) != nullptr; i++){
- if(name == getFileLastName(node->getChild(i)->getFileName()) && name.find(".") == std::string::npos){
- s = s + node->getChild(i)->getFileName() + "\n" + node->getChild(i)->folderFindNode(node->getChild(i), name);
- }
- else if(name != getFileLastName(node->getChild(i)->getFileName()) && name.find(".") == std::string::npos){
- s = s + node->getChild(i)->folderFindNode(node->getChild(i), name);
- }
- }
- return s;
- }
- std::string findNode(std::string name){
- std::string s;
- std::string e_s;
- int i = 0;
- while(i < _v.size() && name.find(".") != std::string::npos){
- if(_v[i]->getChild(0) != nullptr) e_s = _v[i]->findNode(name);
- if(name == getFileLastName(_v[i]->getFileName()) && !e_s.empty() && name.find("/") == std::string::npos){
- std::string final_path = s + _v[i]->getFileName()+ "\n" + e_s;
- size_t pos = 0;
- std::string token;
- std::string replace_c = "./";
- while ((pos = final_path.find(name)) != std::string::npos) {
- token = final_path.substr(0, pos);
- break;
- }
- string::size_type pos_s = 0;
- string::size_type srclen = token.size();
- string::size_type dstlen = replace_c.size();
- while( (pos_s=final_path.find(token, pos_s)) != string::npos){
- final_path.replace(pos_s, srclen, replace_c);
- pos_s += dstlen;
- }
- return final_path;
- }
- if(name == getFileLastName(_v[i]->getFileName()) && !e_s.empty()) return s + _v[i]->getFileName()+ "\n" + e_s;
- if(name == getFileLastName(_v[i]->getFileName()) && e_s.empty()) return s + _v[i]->getFileName();
- i++;
- }
- if(name.find(".") == std::string::npos){ //find folder
- std::string s;
- for(int i=0; i<_v.size() && _v[i]->getChild(i) != nullptr; i++){
- if(name == getFileLastName(_v[i]->getFileName()) && name.find(".") == std::string::npos){
- s = s + _v[i]->getFileName() + "*" + _v[i]->folderFindNode(_v[i], name);
- }
- else if(name != getFileLastName(_v[i]->getFileName()) && name.find(".") == std::string::npos){
- s = s + _v[i]->folderFindNode(_v[i], name);
- }
- }
- string final_path;
- std::string delimiter = "*";
- int replace_size = getFileName().length();
- size_t pos = 0;
- std::string token;
- while ((pos = s.find(delimiter)) != std::string::npos) {
- token = s.substr(0, pos);
- final_path = final_path + "." + token.substr(replace_size, token.size()) + "\n";
- s.erase(0, pos + delimiter.length());
- }
- final_path = final_path + "." + s.substr(replace_size, s.size());
- final_path = final_path.substr(0, final_path.length()-1);
- return final_path;
- }
- }
- private:
- std::string m_path;
- std::vector<Node*> _v;
- };
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement