Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "newmyfiles.h"
- bool myfiles_fs::read(char * &result, int &lenght) {
- fs.open(filename, ios::in | ios::binary);
- fs.seekp(0, ios::end);
- lenght = fs.tellp();
- fs.seekp(0, ios::beg); // back to start
- result = new char[lenght + 1];
- fs.read(result, lenght);
- result[lenght] = '\0';
- fs.close();
- return true;
- }
- bool myfiles_fs::write(char * result, int lenght) {
- fs.open(filename, ios::out | ios::binary);
- fs.write(result, lenght);
- fs.close();
- return true;
- }
- bool myfiles_v::read(char * &result, int &lenght) {
- fl = fopen(filename, "rb");
- fseek(fl, 0, SEEK_END);
- lenght = ftell(fl);
- fseek(fl, 0, SEEK_SET); // back to start
- result = new char[lenght + 1];
- fread(result, 1, lenght, fl);
- result[lenght] = '\0';
- fclose(fl);
- return true;
- }
- bool myfiles_v::write(char * result, int lenght) {
- fl = fopen(filename, "wb");
- fwrite(result, 1, lenght, fl);
- fclose(fl);
- return true;
- }
- bool myfiles_wapi::read(char * &result, int &lenght) {
- hFile = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
- if (hFile == INVALID_HANDLE_VALUE)
- return false;
- DWORD dwFileSize = GetFileSize(hFile, nullptr);
- if (dwFileSize == INVALID_FILE_SIZE) {
- CloseHandle(hFile);
- return false;
- }
- result = new char[dwFileSize];
- lenght = dwFileSize;
- DWORD dwCount;
- ReadFile(hFile, result, dwFileSize, &dwCount, NULL);
- if (dwCount != dwFileSize)
- return false;
- return true;
- }
- bool myfiles_wapi::write(char * result, int lenght) {
- DWORD dwPtr = SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
- if (dwPtr == INVALID_SET_FILE_POINTER)
- return false;
- DWORD lpNumberOfBytesWritten;
- WriteFile(hFile, result, lenght, &lpNumberOfBytesWritten, NULL);
- if (lpNumberOfBytesWritten != lenght)
- return false;
- if (!SetEndOfFile(hFile))
- return false;
- return true;
- }
- myfiles_wapi::~myfiles_wapi() {
- CloseHandle(hFile);
- }
- bool myfiles_mm::read(char * &result, int &lenght) {
- hFile = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
- if (hFile == INVALID_HANDLE_VALUE)
- return false;
- dwFileSize = GetFileSize(hFile, nullptr);
- if (dwFileSize == INVALID_FILE_SIZE || dwFileSize == 0) {
- CloseHandle(hFile);
- return false;
- }
- hMapping = CreateFileMapping(hFile, nullptr, PAGE_READWRITE, 0, 0, nullptr);
- if (hMapping == nullptr) {
- CloseHandle(hFile);
- return false;
- }
- dataPtr = MapViewOfFile(hMapping, FILE_MAP_ALL_ACCESS, 0, 0, dwFileSize);
- if (dataPtr == nullptr) {
- CloseHandle(hMapping);
- CloseHandle(hFile);
- return false;
- }
- lenght = dwFileSize;
- result = new char[lenght + 1];
- result = strncpy(result, (char*)dataPtr, lenght);
- result[lenght] = '\0';
- return true;
- }
- bool myfiles_mm::write(char * result, int lenght) {
- if (lenght > dwFileSize) {/* remapping to increase the size */
- UnmapViewOfFile(dataPtr);
- if (hMapping && hMapping != nullptr)
- CloseHandle(hMapping);
- hMapping = CreateFileMapping(hFile, nullptr, PAGE_READWRITE | SEC_COMMIT, 0, lenght, nullptr);
- if (hMapping == nullptr) {
- CloseHandle(hFile);
- return false;
- }
- dataPtr = MapViewOfFile(hMapping, FILE_MAP_ALL_ACCESS, 0, 0, lenght);
- if (dataPtr == nullptr) {
- CloseHandle(hMapping);
- CloseHandle(hFile);
- return false;
- }
- }
- strcpy((char*)dataPtr, result);
- SetFilePointer(hFile, lenght, NULL, FILE_BEGIN);
- return 0;
- }
- myfiles_mm::~myfiles_mm() {
- UnmapViewOfFile(dataPtr);
- CloseHandle(hMapping);
- SetEndOfFile(hFile);
- CloseHandle(hFile);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement