From 4bc4114ee97d2a8171bb202ce541ee70e334770d Mon Sep 17 00:00:00 2001 From: The Arrayser <68914060+TheArrayser@users.noreply.github.com> Date: Sun, 28 Aug 2022 13:37:14 +0200 Subject: [PATCH] unit tests did NOT show any errors --- System Abstraction/win32/cdb_file.c | 173 ++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 System Abstraction/win32/cdb_file.c diff --git a/System Abstraction/win32/cdb_file.c b/System Abstraction/win32/cdb_file.c new file mode 100644 index 0000000..63169d8 --- /dev/null +++ b/System Abstraction/win32/cdb_file.c @@ -0,0 +1,173 @@ +#include "../cdb_Sustem.h" +#include + +struct sus_File_impl{ + void * memroy; + HANDLE fileHandle; + HANDLE fileMapHandle; +}; + +//static void * internal_fileOpeninMem(HANDLE file){ +// HANDLE fileMap = CreateFileMappingA(file, NULL, PAGE_READWRITE, 0, 0, NULL); //error if file is empty +// if(fileMap == NULL || fileMap == INVALID_HANDLE_VALUE){ +// return NULL; +// } +// void * memroy = MapViewOfFile(fileMap, FILE_MAP_ALL_ACCESS, 0, 0, 0); +// if(memroy == NULL){ +// CloseHandle(fileMap); +// } +// return memroy; +//} + +sus_File sus_fileOpenInMem(char * fileName, unsigned long parameter){ + sus_File currentFile = sus_heapAlloc(sizeof(struct sus_File_impl)); + if(currentFile == NULL) return NULL; + + currentFile->fileHandle = CreateFileA(fileName, + GENERIC_READ | GENERIC_WRITE, //Open for reading and writing + FILE_SHARE_READ, //It wouldn't be a good idea, if a different process wrote to the file at the same time + NULL, + (parameter & SUS_FILE_NEW ? CREATE_NEW : OPEN_EXISTING), //Open the file only if it exists/if it DOES NOT EXIST to prevent user error + FILE_FLAG_RANDOM_ACCESS | //optimize for random access to file + (parameter & SUS_FILE_TEMP ? (FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE) : FILE_ATTRIBUTE_NORMAL), // just a normal file :D / automatically remove the file, if no longer needed + NULL); + if(currentFile->fileHandle == NULL || currentFile->fileHandle == INVALID_HANDLE_VALUE){ + //debug + printf("Windows error: %i\n", GetLastError()); + + goto cleanup1; + } + //void * currentAdress = internal_fileOpeninMem(currentFile); + + //If the file is empty, there is nothing to map into memroy, so this code puts something into the file + if(GetFileSize(currentFile->fileHandle, NULL) == 0){ + if(!WriteFile(currentFile->fileHandle, "", sizeof(""), NULL, NULL)){ + goto cleanup2; + } + } + + currentFile->fileMapHandle = CreateFileMappingA(currentFile->fileHandle, NULL, PAGE_READWRITE, 0, 0, NULL); //error if file is empty + if(currentFile->fileMapHandle == NULL || currentFile->fileMapHandle == INVALID_HANDLE_VALUE){ + //return NULL; + goto cleanup2; + } + currentFile->memroy = MapViewOfFile(currentFile->fileMapHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0); + if(currentFile->memroy == NULL){ + goto cleanup3; + } + return currentFile; + + //if(currentAdress == NULL){ + // //cleanup + // goto cleanup2; + //} + //return currentAdress; + + cleanup3: + CloseHandle(currentFile->fileMapHandle); + cleanup2: + CloseHandle(currentFile->fileHandle); + cleanup1: + sus_heapFree(currentFile); + + return NULL; +} + +//static void * internal_fileOpenInMem(char * fileName){ +// HANDLE currentFile = CreateFileA(fileName, +// GENERIC_READ | GENERIC_WRITE, //Open for reading and writing +// FILE_SHARE_READ, //It wouldn't be a good idea, if a different process wrote to the file at the same time +// NULL, +// OPEN_EXISTING, //Open the file only if it exists to prevent user error +// FILE_FLAG_RANDOM_ACCESS | //optimize for random access to file +// FILE_ATTRIBUTE_NORMAL, // just a normal file :D +// NULL); +// if(currentFile == NULL || currentFile == INVALID_HANDLE_VALUE){ +// return NULL; +// } +// void * currentAdress = internal_fileOpeninMem(currentFile); +// if(currentAdress == NULL){ +// //cleanup +// CloseHandle(currentFile); +// } +// return currentAdress; +//} +// +//static void * internal_fileOpenNewinMem(char * fileName){ +// HANDLE currentFile = CreateFileA(fileName, +// GENERIC_READ | GENERIC_WRITE, //Open for reading and writing +// FILE_SHARE_READ, //It wouldn't be a good idea, if a different process wrote to the file at the same time +// NULL, +// CREATE_NEW, //Open the file only if it DOES NOT exist to prevent user error +// FILE_FLAG_RANDOM_ACCESS | //optimize for random access to file +// FILE_ATTRIBUTE_NORMAL, // just a normal file :D +// NULL); +// if(currentFile == NULL || currentFile == INVALID_HANDLE_VALUE){ +// return NULL; +// } +// void * currentAdress = internal_fileOpeninMem(currentFile); +// if(currentAdress == NULL){ +// //cleanup +// CloseHandle(currentFile); +// } +// return currentAdress; +//} + +void * sus_fileFileToMemroy(sus_File file){ + return file->memroy; +} + +void sus_fileClose(sus_File file){ + FlushViewOfFile(file->memroy, 0); + UnmapViewOfFile(file->memroy); + CloseHandle(file->fileMapHandle); + CloseHandle(file->fileHandle); + sus_heapFree(file); +} + +void sus_fileFlush(sus_File file){ + FlushViewOfFile(file->memroy, 0); +} + +void sus_fileRename(char * fileName, char * newFileName){ + MoveFile(fileName, newFileName); +} + +void sus_fileDelete(char * fileName){ + DeleteFile(fileName); +} + +sus_Bool sus_fileExists(char * fileName){ + DWORD fileAttributes = GetFileAttributes(fileName); + return (fileAttributes == INVALID_FILE_ATTRIBUTES ? sus_True : sus_False); +} + +void * sus_fileResize(sus_File file, signed long appendBytes){ + FlushViewOfFile(file->memroy, 0); + UnmapViewOfFile(file->memroy); + CloseHandle(file->fileMapHandle); + SetFilePointer(file->fileHandle, appendBytes, NULL, FILE_END); //Very cursed Unix style... + SetEndOfFile(file->fileHandle); + + + file->fileMapHandle = CreateFileMappingA(file->fileHandle, NULL, PAGE_READWRITE, 0, 0, NULL); //error if file is empty + if(file->fileMapHandle == NULL || file->fileMapHandle == INVALID_HANDLE_VALUE){ + //return NULL; + goto cleanup2; + } + file->memroy = MapViewOfFile(file->fileMapHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0); + if(file->memroy == NULL){ + goto cleanup3; + } + return file->memroy; + + cleanup3: + CloseHandle(file->fileMapHandle); + file->fileMapHandle = NULL; + cleanup2: + CloseHandle(file->fileHandle); + file->fileHandle = NULL; + //sus_heapFree(file); + + return NULL; +} \ No newline at end of file