Advertisement
Guest User

test.cc

a guest
May 14th, 2022
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <stack>
  4. #include <string>
  5.  
  6. #include "archive.hh"
  7.  
  8. using namespace std;
  9.  
  10. int main(int argc, char **argv)
  11. {
  12.   auto archive = Archive(string(argv[1]));
  13.   auto currentFolder = archive.getRootFolder();
  14.  
  15.   while(true)
  16.   {
  17.     string command;
  18.     cout << "> ";
  19.     cin >> command;
  20.     if(command == "list")
  21.     {
  22.       currentFolder->iterChildren([](auto &entry)
  23.       {
  24.         cout << setw(8) << left
  25.              << (entry.getType() == Folder::Entry::Type::File ? "FILE:" : "FOLDER:")
  26.              << entry.getName()
  27.              << endl;
  28.       });
  29.     }
  30.     else if(command == "cd")
  31.     {
  32.       string name;
  33.       cin >> name;
  34.       if(name == "..")
  35.       {
  36.         currentFolder = currentFolder->getParentFolder();
  37.       }
  38.       else
  39.       {
  40.         auto success = currentFolder->iterChildrenUntil([&](auto &entry)
  41.         {
  42.           if(entry.getType() == Folder::Entry::Type::Folder && entry.getName() == name)
  43.           {
  44.             currentFolder = currentFolder->getChildFolder(entry);
  45.             return true;
  46.           }
  47.           return false;
  48.         });
  49.         if(!success)
  50.           cout << "not found!\n";
  51.       }
  52.     }
  53.     else if(command == "addFolder")
  54.     {
  55.       string name;
  56.       cin >> name;
  57.       currentFolder->addFolder(name);
  58.     }
  59.     else if(command == "addFile")
  60.     {
  61.       string name, path;
  62.       cin >> name >> path;
  63.       currentFolder->addFile(name, path);
  64.     }
  65.     else if(command == "extract")
  66.     {
  67.       string name, path;
  68.       cin >> name >> path;
  69.       auto success = currentFolder->iterChildrenUntil([&](auto &entry)
  70.       {
  71.         if(entry.getType() == Folder::Entry::Type::File && entry.getName() == name)
  72.         {
  73.           currentFolder->extract(entry, path);
  74.           return true;
  75.         }
  76.         return false;
  77.       });
  78.       if(!success)
  79.         cout << "not found!\n";
  80.     }
  81.     else if(command == "remove")
  82.     {
  83.       string name;
  84.       cin >> name;
  85.       auto success = currentFolder->iterChildrenUntil([&](auto &entry)
  86.       {
  87.         if(entry.getName() == name)
  88.         {
  89.           currentFolder->remove(entry);
  90.           return true;
  91.         }
  92.         return false;
  93.       });
  94.       if(!success)
  95.         cout << "not found!\n";
  96.     }
  97.     else if(command == "sync")
  98.     {
  99.       archive.sync();
  100.       currentFolder = archive.getRootFolder();
  101.     }
  102.   }
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement