Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <tchar.h>
- #include <windows.h>
- #include <windowsx.h>
- #include <fstream>
- using namespace std;
- #define F_FS 1
- #define F_V 2
- #define F_WAPI 3
- #define F_MM 4
- class myfiles{
- private:
- int now_type;
- char* filename;
- fstream fs;
- FILE * fl;
- HANDLE hFile;
- HANDLE hMapping;
- LPVOID dataPtr;
- DWORD dwFileSize;
- public:
- myfiles(int type, char* filename){
- this->now_type = type;
- this->filename = filename;
- }
- ~myfiles(){
- switch(now_type){
- /*case F_FS:
- fs.close();
- break;
- case F_V:
- fclose(fl);
- break;*/
- case F_WAPI:
- CloseHandle(hFile);
- break;
- case F_MM:
- UnmapViewOfFile(dataPtr);
- CloseHandle(hMapping);
- SetEndOfFile(hFile);
- CloseHandle(hFile);
- }
- }
- bool read(char * &result, int &lenght){
- switch(now_type){
- case F_FS:
- return read_fstreamFiles(result, lenght);
- case F_V:
- return read_variablesFiles(result, lenght);
- case F_WAPI:
- return read_winApiFiles(result, lenght);
- case F_MM:
- return read_memoryMappedFiles(result, lenght);
- }
- }
- bool write(char * result, int lenght){
- switch(now_type){
- case F_FS:
- return write_fstreamFiles(result, lenght);
- case F_V:
- return write_variablesFiles(result, lenght);
- case F_WAPI:
- return write_winApiFiles(result, lenght);
- case F_MM:
- return write_memoryMappedFiles(result, lenght);
- }
- }
- private:
- bool read_fstreamFiles(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 read_variablesFiles(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 read_winApiFiles(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) {
- 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 read_memoryMappedFiles(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) { // yes, NULL, not INVALID_HANDLE_VALUE, see MSDN
- 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 write_fstreamFiles(char * result, int lenght){
- fs.open(filename, ios::out|ios::binary|ios::trunc);
- fs.write(result, lenght);
- fs.close();
- return true;
- }
- bool write_variablesFiles(char * result, int lenght){
- fl = fopen(filename, "wb");
- fwrite(result, 1, lenght, fl);
- return true;
- }
- bool write_winApiFiles(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;
- }
- bool write_memoryMappedFiles(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 true;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement