Advertisement
Guest User

explorer

a guest
Nov 6th, 2018
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.15 KB | None | 0 0
  1. #pragma once
  2.  
  3.  
  4. #include "File.h"
  5. #include "FileSystemObject.h"
  6. #include "FileSystemObjectsContainer.h"
  7. #include "Directory.h"
  8. #include "ByteContainer.h"
  9. #include "Shortcuts.h"
  10.  
  11. #include <string>
  12. #include <vector>
  13. #include <sstream>
  14. #include <stack>
  15.  
  16. class Explorer
  17. {
  18. private:
  19.     std::shared_ptr<FileSystemObjectsContainer> currLocation;
  20.     std::vector<std::shared_ptr<FileSystemObject>>& objects;
  21.     std::stack<std::shared_ptr<FileSystemObject>> clipboard;
  22.  
  23.  
  24.     std::shared_ptr<FileSystemObject> getFile(std::string path)
  25.     {
  26.         std::shared_ptr<FileSystemObject> result;
  27.         for(int i = 0;i < objects.size();i++)
  28.         {
  29.             if(objects[i]->getName() == path)
  30.             {
  31.                 return objects[i];
  32.             }else
  33.             {
  34.                 std::shared_ptr<FileSystemObjectsContainer> container = std::dynamic_pointer_cast<FileSystemObjectsContainer>(objects[i]);
  35.                 if (container != nullptr)
  36.                 {
  37.                     walkInAndFind(container, path,result);
  38.                 }
  39.             }
  40.            
  41.         }
  42.     }
  43.  
  44.     void walkInAndFind(std::shared_ptr<FileSystemObjectsContainer>& container, std::string& path, std::shared_ptr<FileSystemObject>& result)
  45.     {
  46.         if(result != nullptr)
  47.         {
  48.             return;
  49.         }
  50.  
  51.         std::vector<std::shared_ptr<FileSystemObject>> filesInContainer(container->begin(), container->end());
  52.         for(int i = 0;i < filesInContainer.size();i++)
  53.         {
  54.             std::shared_ptr<FileSystemObjectsContainer> con = std::dynamic_pointer_cast<FileSystemObjectsContainer>(filesInContainer[i]);
  55.             if (con != nullptr)
  56.             {
  57.                 std::shared_ptr<Directory> directory = std::dynamic_pointer_cast<Directory>(container);
  58.                 if(directory != nullptr)
  59.                 {
  60.                     if(directory->getName() == path)
  61.                     {
  62.                         result = directory;
  63.                         break;
  64.                     }else
  65.                     {
  66.                         walkInAndFind(con,path,result);
  67.                     }
  68.                 }
  69.                
  70.             }
  71.            
  72.         }
  73.     }
  74.  
  75.     void moveFileAtCurrentLocation(const std::shared_ptr<FileSystemObject>& file)
  76.     {
  77.         std::shared_ptr<FileSystemObjectsContainer> fileParent = std::dynamic_pointer_cast<FileSystemObjectsContainer>(file->getParent().lock());
  78.         std::weak_ptr<FileSystemObject> newParent = std::dynamic_pointer_cast<FileSystemObject>(currLocation);
  79.         file->setParent(newParent);
  80.  
  81.         currLocation->add(file);
  82.  
  83.         if(fileParent != nullptr)
  84.         {
  85.             fileParent->remove(file);
  86.         }else
  87.         {
  88.             objects.erase(std::find(objects.begin(), objects.end(), file));
  89.         }
  90.     }
  91.  
  92. public:
  93.     Explorer(std::vector<std::shared_ptr<FileSystemObject>>& rootObjects) : objects(rootObjects), currLocation(nullptr)
  94.     {
  95.     }
  96.  
  97.     void createFile(const std::string& filename, const std::string& content)
  98.     {
  99.         std::shared_ptr<File> newFile = std::make_shared<File>(File(filename, content));
  100.         if(currLocation == nullptr)
  101.         {
  102.             objects.push_back(newFile);
  103.         }else
  104.         {
  105.             std::weak_ptr<FileSystemObject> parent = std::dynamic_pointer_cast<FileSystemObject>(currLocation);
  106.             newFile->setParent(parent);
  107.             currLocation->add(newFile);
  108.         }
  109.     }
  110.  
  111.     void createDirectory(const std::string& name)
  112.     {
  113.         std::shared_ptr<Directory> newDir = std::make_shared<Directory>(Directory(name));
  114.         if (currLocation == nullptr)
  115.         {
  116.             objects.push_back(newDir);
  117.         }
  118.         else
  119.         {
  120.             std::weak_ptr<FileSystemObject> parent = std::dynamic_pointer_cast<FileSystemObject>(currLocation);
  121.             newDir->setParent(parent);
  122.             currLocation->add(newDir);
  123.         }
  124.     }
  125.  
  126.     void cut(const std::string& name)
  127.     {
  128.         std::shared_ptr<FileSystemObject> file = this->getFile(name);
  129.         this->clipboard.push(file);
  130.     }
  131.  
  132.  
  133.     void paste()
  134.     {
  135.         if(!this->clipboard.empty())
  136.         {
  137.             std::shared_ptr<FileSystemObject> file = clipboard.top();
  138.             moveFileAtCurrentLocation(file);
  139.             clipboard.pop();
  140.         }
  141.     }
  142.  
  143.     void navigate(std::string path)
  144.     {
  145.         std::shared_ptr<FileSystemObject> currLocationAsObject = std::dynamic_pointer_cast<FileSystemObject>(currLocation);
  146.         if(path == "..")
  147.         {
  148.             if(currLocationAsObject->getParent().lock() != nullptr)
  149.             {
  150.                 currLocationAsObject = currLocationAsObject->getParent().lock();
  151.             }else
  152.             {
  153.                 currLocationAsObject = nullptr;
  154.             }
  155.            
  156.         }else
  157.         {
  158.             currLocationAsObject = getFile(path);
  159.         }
  160.  
  161.         currLocation = std::dynamic_pointer_cast<FileSystemObjectsContainer>(currLocationAsObject);
  162.     }
  163.  
  164.     void createShortcut(const std::string& name)
  165.     {
  166.         //TODO
  167.     }
  168. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement