Advertisement
Guest User

archive.hh

a guest
May 14th, 2022
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. #ifndef ARCHIVE_HH
  2. #define ARCHIVE_HH
  3.  
  4. #include <iostream>
  5. #include <fstream>
  6. #include <vector>
  7. #include <string>
  8. #include <functional>
  9. #include <cstdint>
  10.  
  11. class Folder;
  12. class FolderHandle;
  13.  
  14. class Archive
  15. {
  16. public:
  17.   Archive(std::string path);
  18.   FolderHandle getRootFolder();
  19.   void sync();
  20.  
  21. private:
  22.   std::uint16_t addFolder(std::uint16_t parentIndex);
  23.   void removeFolder(std::uint16_t index);
  24.   void loadFolders();
  25.   void syncRemoveFiles();
  26.   void syncAddFiles();
  27.   void syncFolders();
  28.   void syncSize();
  29.   void setSynced(bool synced);
  30.  
  31.   std::string m_path;
  32.   std::fstream m_file;
  33.   std::uint64_t m_endDataOffset;
  34.   std::vector<Folder> m_folders;
  35.   std::vector<Folder> m_deletedFolders;
  36.   bool m_synced;
  37.  
  38.   friend class Folder;
  39.   friend class FolderHandle;
  40. };
  41.  
  42. class Folder
  43. {
  44. public:
  45.   Folder(Archive *archive, std::uint16_t selfIndex);
  46.  
  47.   struct Entry
  48.   {
  49.   public:
  50.     enum class Type { File = 0, Folder = 1 };
  51.  
  52.     Type getType() const { return type; }
  53.     const std::string &getName() const { return name; }
  54.     std::uint64_t getSize() const { return size; };
  55.  
  56.   private:
  57.     void read(std::istream &in);
  58.     void write(std::ostream &out);
  59.  
  60.     Type type;
  61.     std::string name, externalPath;
  62.     std::uint64_t offset, size;
  63.     bool created = false;
  64.     bool deleted = false;
  65.  
  66.     friend class Folder;
  67.     friend class Archive;
  68.   };
  69.  
  70.   void iterChildren(std::function<void(Entry&)> fn)
  71.   {
  72.     for(auto &entry : m_entries)
  73.       if(!entry.deleted)
  74.         fn(entry);
  75.   }
  76.  
  77.   bool iterChildrenUntil(std::function<bool(Entry&)> fn)
  78.   {
  79.     for(auto &entry : m_entries)
  80.       if(!entry.deleted)
  81.         if(fn(entry))
  82.           return true;
  83.     return false;
  84.   }
  85.  
  86.   FolderHandle getChildFolder(Entry &entry);
  87.   FolderHandle getParentFolder();
  88.   void addFolder(std::string name);
  89.   void addFile(std::string name, std::string path);
  90.   void extract(Entry &entry, std::string path);
  91.   void remove(Entry &entry);
  92.  
  93. private:
  94.   void remove(std::uint16_t index);
  95.   void read();
  96.   void write();
  97.   void setSynced(bool synced);
  98.  
  99.   Archive *m_archive;
  100.   std::uint16_t m_selfIndex;
  101.   std::uint16_t m_parentIndex;
  102.   std::vector<Entry> m_entries;
  103.   bool m_created = false;
  104.   bool m_deleted = false;
  105.   bool m_synced = false;
  106.  
  107.   friend class Archive;
  108. };
  109.  
  110. class FolderHandle
  111. {
  112. public:
  113.   FolderHandle(Archive *archive, std::uint16_t folderIndex)
  114.     : m_archive(archive)
  115.     , m_folderIndex(folderIndex)
  116.   {
  117.   }
  118.  
  119.   Folder &operator*()
  120.   {
  121.     return m_archive->m_folders[m_folderIndex];
  122.   }
  123.  
  124.   Folder *operator->()
  125.   {
  126.     return &m_archive->m_folders[m_folderIndex];
  127.   }
  128.  
  129. private:
  130.   Archive *m_archive;
  131.   std::uint16_t m_folderIndex;
  132. };
  133.  
  134. #endif
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement