fuad_cs22

File System

Jan 13th, 2021
651
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <regex>
  5.  
  6. class Encode {
  7. protected:
  8.     static void _encode(std::string& data) {
  9.         std::string temp = data;
  10.         for (size_t i = 0; i < temp.size(); i++) {
  11.             data[i] = (temp[i] + 23);
  12.         }
  13.     }
  14. };
  15.  
  16. class Decode {
  17. protected:
  18.     static void _decode(std::string& data) {
  19.         std::string temp = data;
  20.         for (size_t i = 0; i < temp.size(); i++) {
  21.             data[i] = (temp[i] - 23);
  22.         }
  23.     }
  24. };
  25.  
  26. class Hash : public Encode, public Decode {
  27. public:
  28.     static void encode(std::string& data) {
  29.         _encode(data);
  30.     }
  31.     static void decode(std::string& data) {
  32.         _decode(data);
  33.     }
  34. };
  35.  
  36. struct File {
  37.     File(std::string name) {
  38.         file_name = name;
  39.         file.open(name, std::ios::in | std::ios::out | std::ios::binary);
  40.         getline(file, file_data);
  41.     }
  42.     ~File() {
  43.         file.close();
  44.     }
  45. private:
  46.     std::string file_data;
  47.     std::fstream file;
  48.     std::string file_name;
  49.     friend class File_System;
  50. };
  51. class File_System {
  52. private:
  53.     std::fstream FILE_SYSTEM_STORAGE;
  54.     std::string FILE_SYSTEM_NAME;
  55.  
  56. public:
  57.     File_System(const std::string& name) {
  58.         FILE_SYSTEM_NAME = name;
  59.     }
  60.     void File_Write(File& file) {
  61.         std::stringstream ss;
  62.         FILE_SYSTEM_STORAGE.open(FILE_SYSTEM_NAME, std::ios::in | std::ios::out | std::ios::binary);
  63.         ss << "[" << file.file_name << "]" << file.file_data << "${eof}" << std::endl;
  64.         std::string encrypted_data = ss.str();
  65.         Hash::encode(encrypted_data);
  66.         FILE_SYSTEM_STORAGE << encrypted_data;
  67.         std::cout << "Data Written" << std::endl;
  68.         FILE_SYSTEM_STORAGE.close();
  69.     }
  70.     std::stringstream File_Read() {
  71.         FILE_SYSTEM_STORAGE.open(FILE_SYSTEM_NAME, std::ios::in | std::ios::out | std::ios::binary);
  72.         std::stringstream ss;
  73.         ss << FILE_SYSTEM_STORAGE.rdbuf();
  74.         FILE_SYSTEM_STORAGE.close();
  75.         return ss;
  76.     }
  77. };
  78.  
  79. int main()
  80. {
  81.     File F("data.txt");
  82.     File_System file("filesystem.txt");
  83.     file.File_Write(F);
  84.     std::cin.get();
  85.     std::smatch match;
  86.  
  87.     std::string s = file.File_Read().str();
  88.     Hash::decode(s);
  89.     //std::cout << "found : " << s << std::endl;
  90.     if (std::regex_search(s, match, std::regex(R"(\[data\.txt\])"))) {
  91.         s = match.suffix();
  92.         //std::cout << "found 1: " << s << std::endl;
  93.         if (std::regex_search(s, match, std::regex(R"(\$\{eof\})"))) {
  94.             s = match.prefix();
  95.             //std::cout << "found 2: " << s << std::endl;
  96.         }
  97.     }
  98.     else {
  99.         //std::cout << "Nothing found" << std::endl;
  100.     }
  101.     std::fstream nFile;
  102.     nFile.open("newfile.txt", std::ios::out, std::ios::trunc);
  103.     nFile << s;
  104.     std::cout << "found : " << s << std::endl;
  105.     std::cout << "Data Written!" << std::endl;
  106.     nFile.close();
  107.     std::cin.get();
  108. }
RAW Paste Data