cppadvanced90 minutes

Advanced File System Simulation with C++

Create an advanced mini-project that simulates a hierarchical file system including files and folders with operations like add, delete, move, and search, using efficient data structures in C++.

Challenge prompt

Build a C++ program that simulates a file system supporting folders and files in a hierarchical structure. Your program should allow users to create folders and files, delete them, move them between folders, and search files or folders by name. Implement the following: - A class structure representing folders and files, with appropriate inheritance or composition. - An efficient way to store and navigate the hierarchy (e.g., trees). - Functions to add a file or folder inside a given folder path. - Functions to delete a file or folder by path. - Functions to move items from one folder to another. - A search function to find all files/folders matching a name substring anywhere in the tree, returning their full paths. Your solution should handle edge cases such as duplicate names in different folders, invalid paths, and empty searches gracefully.

Guidance

  • Design a Node class that can represent both files and folders, with metadata and pointers to children for folders.
  • Use recursion or iterative methods with stacks/queues to traverse the file hierarchy for operations like search and delete.
  • Implement path parsing to convert string paths into navigable nodes within your structure.

Hints

  • Consider using smart pointers or careful memory management to avoid leaks when deleting nodes.
  • Use a vector or list to hold children nodes within a folder for flexibility but optimize search by considering indexing or maps if needed.
  • When moving a node, ensure to unlink it from its current parent before linking it to the new parent.

Starter code

#include <iostream>
#include <vector>
#include <string>
#include <memory>

// Define a base Node class with virtual functions, and derive File and Folder from it.
class Node {
public:
    std::string name;
    Node(std::string name) : name(name) {}
    virtual ~Node() {}
    virtual bool isFile() const = 0;
};

// Implement Folder class holding children
class Folder : public Node {
public:
    std::vector<std::unique_ptr<Node>> children;
    Folder(std::string name) : Node(name) {}
    bool isFile() const override { return false; }
    // TODO: add methods to add, delete, move, and search
};

// Implement File class
class File : public Node {
public:
    File(std::string name) : Node(name) {}
    bool isFile() const override { return true; }
};

int main() {
    // Example root folder
    Folder root("root");
    // TODO: implement user interaction or test cases
    return 0;
}

Expected output

This project does not have a fixed output but should allow operations such as: - Adding folders/files under given paths - Removing them - Moving them between folders - Searching names and printing full paths Example: Searching "doc" may print: /root/docs /root/docs/file1.txt /root/projects/documentation If user tries to delete a non-existing path, an error message should appear.

Core concepts

Object-Oriented ProgrammingData Structures (Trees)RecursionMemory Management

Challenge a Friend

Send this duel to someone else and see if they can solve it.