Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "File.h"
- #include "FileSystemObject.h"
- #include "FileSystemObjectsContainer.h"
- #include "Directory.h"
- #include "ByteContainer.h"
- #include "Shortcuts.h"
- #include <string>
- #include <vector>
- #include <sstream>
- #include <stack>
- class Explorer
- {
- private:
- std::shared_ptr<FileSystemObjectsContainer> currLocation;
- std::vector<std::shared_ptr<FileSystemObject>>& objects;
- std::stack<std::shared_ptr<FileSystemObject>> clipboard;
- std::shared_ptr<FileSystemObject> getFile(std::string path)
- {
- std::shared_ptr<FileSystemObject> result;
- for(int i = 0;i < objects.size();i++)
- {
- if(objects[i]->getName() == path)
- {
- return objects[i];
- }else
- {
- std::shared_ptr<FileSystemObjectsContainer> container = std::dynamic_pointer_cast<FileSystemObjectsContainer>(objects[i]);
- if (container != nullptr)
- {
- walkInAndFind(container, path,result);
- }
- }
- }
- }
- void walkInAndFind(std::shared_ptr<FileSystemObjectsContainer>& container, std::string& path, std::shared_ptr<FileSystemObject>& result)
- {
- if(result != nullptr)
- {
- return;
- }
- std::vector<std::shared_ptr<FileSystemObject>> filesInContainer(container->begin(), container->end());
- for(int i = 0;i < filesInContainer.size();i++)
- {
- std::shared_ptr<FileSystemObjectsContainer> con = std::dynamic_pointer_cast<FileSystemObjectsContainer>(filesInContainer[i]);
- if (con != nullptr)
- {
- std::shared_ptr<Directory> directory = std::dynamic_pointer_cast<Directory>(container);
- if(directory != nullptr)
- {
- if(directory->getName() == path)
- {
- result = directory;
- break;
- }else
- {
- walkInAndFind(con,path,result);
- }
- }
- }
- }
- }
- void moveFileAtCurrentLocation(const std::shared_ptr<FileSystemObject>& file)
- {
- std::shared_ptr<FileSystemObjectsContainer> fileParent = std::dynamic_pointer_cast<FileSystemObjectsContainer>(file->getParent().lock());
- std::weak_ptr<FileSystemObject> newParent = std::dynamic_pointer_cast<FileSystemObject>(currLocation);
- file->setParent(newParent);
- currLocation->add(file);
- if(fileParent != nullptr)
- {
- fileParent->remove(file);
- }else
- {
- objects.erase(std::find(objects.begin(), objects.end(), file));
- }
- }
- public:
- Explorer(std::vector<std::shared_ptr<FileSystemObject>>& rootObjects) : objects(rootObjects), currLocation(nullptr)
- {
- }
- void createFile(const std::string& filename, const std::string& content)
- {
- std::shared_ptr<File> newFile = std::make_shared<File>(File(filename, content));
- if(currLocation == nullptr)
- {
- objects.push_back(newFile);
- }else
- {
- std::weak_ptr<FileSystemObject> parent = std::dynamic_pointer_cast<FileSystemObject>(currLocation);
- newFile->setParent(parent);
- currLocation->add(newFile);
- }
- }
- void createDirectory(const std::string& name)
- {
- std::shared_ptr<Directory> newDir = std::make_shared<Directory>(Directory(name));
- if (currLocation == nullptr)
- {
- objects.push_back(newDir);
- }
- else
- {
- std::weak_ptr<FileSystemObject> parent = std::dynamic_pointer_cast<FileSystemObject>(currLocation);
- newDir->setParent(parent);
- currLocation->add(newDir);
- }
- }
- void cut(const std::string& name)
- {
- std::shared_ptr<FileSystemObject> file = this->getFile(name);
- this->clipboard.push(file);
- }
- void paste()
- {
- if(!this->clipboard.empty())
- {
- std::shared_ptr<FileSystemObject> file = clipboard.top();
- moveFileAtCurrentLocation(file);
- clipboard.pop();
- }
- }
- void navigate(std::string path)
- {
- std::shared_ptr<FileSystemObject> currLocationAsObject = std::dynamic_pointer_cast<FileSystemObject>(currLocation);
- if(path == "..")
- {
- if(currLocationAsObject->getParent().lock() != nullptr)
- {
- currLocationAsObject = currLocationAsObject->getParent().lock();
- }else
- {
- currLocationAsObject = nullptr;
- }
- }else
- {
- currLocationAsObject = getFile(path);
- }
- currLocation = std::dynamic_pointer_cast<FileSystemObjectsContainer>(currLocationAsObject);
- }
- void createShortcut(const std::string& name)
- {
- //TODO
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement