2022-08-29 15:01:03 +02:00
# include "../cdb_sustem.h"
2022-08-28 13:37:14 +02:00
# include <windows.h>
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
2022-08-28 14:52:18 +02:00
( ( parameter & SUS_FILE_TEMP = = 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
2022-08-28 13:37:14 +02:00
NULL ) ;
if ( currentFile - > fileHandle = = NULL | | currentFile - > fileHandle = = INVALID_HANDLE_VALUE ) {
//debug
2022-08-28 13:44:24 +02:00
//printf("Windows error: %i\n", GetLastError());
2022-08-28 13:37:14 +02:00
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;
2022-08-28 13:56:45 +02:00
cleanup3 :
2022-08-28 13:37:14 +02:00
CloseHandle ( currentFile - > fileMapHandle ) ;
2022-08-28 13:56:45 +02:00
cleanup2 :
2022-08-28 13:37:14 +02:00
CloseHandle ( currentFile - > fileHandle ) ;
2022-08-28 13:56:45 +02:00
cleanup1 :
2022-08-28 13:37:14 +02:00
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 ;
2022-08-28 13:56:45 +02:00
cleanup3 :
2022-08-28 13:37:14 +02:00
CloseHandle ( file - > fileMapHandle ) ;
file - > fileMapHandle = NULL ;
2022-08-28 13:56:45 +02:00
cleanup2 :
2022-08-28 13:37:14 +02:00
CloseHandle ( file - > fileHandle ) ;
file - > fileHandle = NULL ;
//sus_heapFree(file);
2022-08-28 15:03:42 +02:00
file - > memroy = NULL ;
2022-08-28 13:37:14 +02:00
return NULL ;
}