CringeDB/System Abstraction/win32/cdb_file.c

176 lines
6.8 KiB
C
Raw Normal View History

2024-02-18 17:42:05 +01:00
#include "../cdb_system.h"
2022-08-28 13:37:14 +02:00
#include <windows.h>
2024-02-18 17:42:05 +01:00
struct sys_File_impl{
void * memory;
2022-08-28 13:37:14 +02:00
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;
// }
2024-02-18 17:42:05 +01:00
// void * memory = MapViewOfFile(fileMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
// if(memory == NULL){
2022-08-28 13:37:14 +02:00
// CloseHandle(fileMap);
// }
2024-02-18 17:42:05 +01:00
// return memory;
2022-08-28 13:37:14 +02:00
//}
2024-02-18 17:42:05 +01:00
sys_File sys_fileOpenInMem(char * fileName, unsigned long parameter){
sys_File currentFile = sys_heapAlloc(sizeof(struct sys_File_impl));
2022-08-28 13:37:14 +02:00
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,
2024-02-18 17:42:05 +01:00
(parameter & SYS_FILE_NEW ? CREATE_NEW : OPEN_EXISTING), //Open the file only if it exists/if it DOES NOT EXIST to prevent user error
2022-08-28 13:37:14 +02:00
FILE_FLAG_RANDOM_ACCESS | //optimize for random access to file
2024-02-18 17:42:05 +01:00
((parameter & SYS_FILE_TEMP == SYS_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);
2024-02-18 17:42:05 +01:00
//If the file is empty, there is nothing to map into memory, so this code puts something into the file
2022-08-28 13:37:14 +02:00
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;
}
2024-02-18 17:42:05 +01:00
currentFile->memory = MapViewOfFile(currentFile->fileMapHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0);
if(currentFile->memory == NULL){
2022-08-28 13:37:14 +02:00
goto cleanup3;
}
return currentFile;
//if(currentAdress == NULL){
// //cleanup
// goto cleanup2;
//}
//return currentAdress;
cleanup3:
2022-08-28 13:37:14 +02:00
CloseHandle(currentFile->fileMapHandle);
cleanup2:
2022-08-28 13:37:14 +02:00
CloseHandle(currentFile->fileHandle);
cleanup1:
2024-02-18 17:42:05 +01:00
sys_heapFree(currentFile);
2022-08-28 13:37:14 +02:00
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;
//}
2024-02-18 17:42:05 +01:00
void * sys_fileFileToMemory(sys_File file){
return file->memory;
2022-08-28 13:37:14 +02:00
}
2024-02-18 17:42:05 +01:00
void sys_fileClose(sys_File file){
if(file->memory == NULL){
FlushViewOfFile(file->memory, 0);
UnmapViewOfFile(file->memory);
2022-08-29 16:41:13 +02:00
CloseHandle(file->fileMapHandle);
CloseHandle(file->fileHandle);
}
2024-02-18 17:42:05 +01:00
sys_heapFree(file);
2022-08-28 13:37:14 +02:00
}
2024-02-18 17:42:05 +01:00
void sys_fileFlush(sys_File file){
FlushViewOfFile(file->memory, 0);
2022-08-28 13:37:14 +02:00
}
2024-02-18 17:42:05 +01:00
void sys_fileRename(char * fileName, char * newFileName){
2022-08-28 13:37:14 +02:00
MoveFile(fileName, newFileName);
}
2024-02-18 17:42:05 +01:00
void sys_fileDelete(char * fileName){
2022-08-28 13:37:14 +02:00
DeleteFile(fileName);
}
2024-02-18 17:42:05 +01:00
sys_Bool sys_fileExists(char * fileName){
2022-08-28 13:37:14 +02:00
DWORD fileAttributes = GetFileAttributes(fileName);
2024-02-18 17:42:05 +01:00
return (fileAttributes == INVALID_FILE_ATTRIBUTES ? sys_True : sys_False);
2022-08-28 13:37:14 +02:00
}
2024-02-18 17:42:05 +01:00
void * sys_fileResize(sys_File file, signed long appendBytes){
FlushViewOfFile(file->memory, 0);
UnmapViewOfFile(file->memory);
2022-08-28 13:37:14 +02:00
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;
}
2024-02-18 17:42:05 +01:00
file->memory = MapViewOfFile(file->fileMapHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0);
if(file->memory == NULL){
2022-08-28 13:37:14 +02:00
goto cleanup3;
}
2024-02-18 17:42:05 +01:00
return file->memory;
2022-08-28 13:37:14 +02:00
cleanup3:
2022-08-28 13:37:14 +02:00
CloseHandle(file->fileMapHandle);
file->fileMapHandle = NULL;
cleanup2:
2022-08-28 13:37:14 +02:00
CloseHandle(file->fileHandle);
file->fileHandle = NULL;
2024-02-18 17:42:05 +01:00
//sys_heapFree(file);
file->memory = NULL;
2022-08-28 13:37:14 +02:00
return NULL;
}