Advertisement
Guest User

newmyfiles.cpp

a guest
Oct 13th, 2019
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "newmyfiles.h"
  2.  
  3. bool myfiles_fs::read(char * &result, int &lenght) {
  4.     fs.open(filename, ios::in | ios::binary);
  5.     fs.seekp(0, ios::end);
  6.     lenght = fs.tellp();
  7.     fs.seekp(0, ios::beg);  // back to start   
  8.     result = new char[lenght + 1];
  9.     fs.read(result, lenght);
  10.     result[lenght] = '\0';
  11.     fs.close();
  12.     return true;
  13. }
  14.  
  15. bool myfiles_fs::write(char * result, int lenght) {
  16.     fs.open(filename, ios::out | ios::binary);
  17.     fs.write(result, lenght);
  18.     fs.close();
  19.     return true;
  20. }
  21.  
  22. bool myfiles_v::read(char * &result, int &lenght) {
  23.     fl = fopen(filename, "rb");
  24.     fseek(fl, 0, SEEK_END);
  25.     lenght = ftell(fl);
  26.     fseek(fl, 0, SEEK_SET); // back to start
  27.     result = new char[lenght + 1];
  28.     fread(result, 1, lenght, fl);
  29.     result[lenght] = '\0';
  30.     fclose(fl);
  31.     return true;
  32. }
  33.  
  34. bool myfiles_v::write(char * result, int lenght) {
  35.     fl = fopen(filename, "wb");
  36.     fwrite(result, 1, lenght, fl);
  37.     fclose(fl);
  38.     return true;
  39. }
  40.  
  41. bool myfiles_wapi::read(char * &result, int &lenght) {
  42.     hFile = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
  43.     if (hFile == INVALID_HANDLE_VALUE)
  44.         return false;
  45.  
  46.     DWORD dwFileSize = GetFileSize(hFile, nullptr);
  47.     if (dwFileSize == INVALID_FILE_SIZE) {
  48.         CloseHandle(hFile);
  49.         return false;
  50.     }
  51.  
  52.     result = new char[dwFileSize];
  53.     lenght = dwFileSize;
  54.     DWORD dwCount;
  55.     ReadFile(hFile, result, dwFileSize, &dwCount, NULL);
  56.  
  57.     if (dwCount != dwFileSize)
  58.         return false;
  59.  
  60.     return true;
  61. }
  62.  
  63. bool myfiles_wapi::write(char * result, int lenght) {
  64.     DWORD dwPtr = SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
  65.     if (dwPtr == INVALID_SET_FILE_POINTER)
  66.         return false;
  67.  
  68.     DWORD lpNumberOfBytesWritten;
  69.     WriteFile(hFile, result, lenght, &lpNumberOfBytesWritten, NULL);
  70.  
  71.     if (lpNumberOfBytesWritten != lenght)
  72.         return false;
  73.  
  74.     if (!SetEndOfFile(hFile))
  75.         return false;
  76.  
  77.     return true;
  78. }
  79.  
  80. myfiles_wapi::~myfiles_wapi() {
  81.     CloseHandle(hFile);
  82. }
  83.  
  84. bool myfiles_mm::read(char * &result, int &lenght) {
  85.     hFile = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
  86.     if (hFile == INVALID_HANDLE_VALUE)
  87.         return false;
  88.  
  89.     dwFileSize = GetFileSize(hFile, nullptr);
  90.     if (dwFileSize == INVALID_FILE_SIZE || dwFileSize == 0) {
  91.         CloseHandle(hFile);
  92.         return false;
  93.     }
  94.  
  95.     hMapping = CreateFileMapping(hFile, nullptr, PAGE_READWRITE, 0, 0, nullptr);
  96.     if (hMapping == nullptr) {
  97.         CloseHandle(hFile);
  98.         return false;
  99.     }
  100.  
  101.     dataPtr = MapViewOfFile(hMapping, FILE_MAP_ALL_ACCESS, 0, 0, dwFileSize);
  102.     if (dataPtr == nullptr) {
  103.         CloseHandle(hMapping);
  104.         CloseHandle(hFile);
  105.         return false;
  106.     }
  107.  
  108.     lenght = dwFileSize;
  109.     result = new char[lenght + 1];
  110.     result = strncpy(result, (char*)dataPtr, lenght);
  111.     result[lenght] = '\0';
  112.  
  113.     return true;
  114. }
  115.  
  116. bool myfiles_mm::write(char * result, int lenght) {
  117.     if (lenght > dwFileSize) {/* remapping to increase the size */
  118.         UnmapViewOfFile(dataPtr);
  119.         if (hMapping && hMapping != nullptr)
  120.             CloseHandle(hMapping);
  121.  
  122.         hMapping = CreateFileMapping(hFile, nullptr, PAGE_READWRITE | SEC_COMMIT, 0, lenght, nullptr);
  123.         if (hMapping == nullptr) {
  124.             CloseHandle(hFile);
  125.             return false;
  126.         }
  127.  
  128.         dataPtr = MapViewOfFile(hMapping, FILE_MAP_ALL_ACCESS, 0, 0, lenght);
  129.         if (dataPtr == nullptr) {
  130.             CloseHandle(hMapping);
  131.             CloseHandle(hFile);
  132.             return false;
  133.         }
  134.     }
  135.  
  136.     strcpy((char*)dataPtr, result);
  137.     SetFilePointer(hFile, lenght, NULL, FILE_BEGIN);
  138.  
  139.     return 0;
  140. }
  141.  
  142. myfiles_mm::~myfiles_mm() {
  143.     UnmapViewOfFile(dataPtr);
  144.     CloseHandle(hMapping);
  145.     SetEndOfFile(hFile);
  146.     CloseHandle(hFile);
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement