CringeDB/File-Page Abstraction/cdb_file-page.c

95 lines
2.9 KiB
C
Raw Permalink Normal View History

2022-08-29 11:05:24 +02:00
#include "cdb_file-page.h"
2024-02-18 18:46:34 +01:00
#include "../System Abstraction/cdb_system.h"
2022-08-29 11:05:24 +02:00
struct fp_File_impl{
2024-02-18 17:42:05 +01:00
sys_File file;
2022-08-29 11:05:24 +02:00
unsigned long pageSize;
};
//Creates a new file, opens it, and resizes it to the specified page size
fp_File fp_fileNew(char * fileName, unsigned long pageSize){ //there shouldn't be a page, longer than 4gb
2024-02-18 17:42:05 +01:00
sys_File currentFile = sys_fileOpenInMem(fileName, SYS_FILE_NEW);
2022-08-29 11:05:24 +02:00
if(currentFile == NULL){
return NULL;
}
2024-02-18 17:42:05 +01:00
fp_File returnable = sys_heapAlloc(sizeof(struct fp_File_impl));
2022-08-29 11:05:24 +02:00
returnable->pageSize = pageSize;
returnable->file = currentFile;
2024-02-18 17:42:05 +01:00
//The new File will be initialized with one memory Unit (Byte), so this code resizes it to the pageSize
if(sys_fileResize(returnable->file, pageSize - 1) == NULL){
sys_heapFree(returnable);
2022-08-29 11:05:24 +02:00
return NULL;
}
return returnable;
}
fp_File fp_fileTemp(char * fileName, unsigned long pageSize){
2024-02-18 17:42:05 +01:00
sys_File currentFile = sys_fileOpenInMem(fileName, SYS_FILE_TEMP);
2022-08-29 11:05:24 +02:00
if(currentFile == NULL){
return NULL;
}
2024-02-18 17:42:05 +01:00
fp_File returnable = sys_heapAlloc(sizeof(struct fp_File_impl));
2022-08-29 11:05:24 +02:00
returnable->pageSize = pageSize;
returnable->file = currentFile;
2024-02-18 17:42:05 +01:00
//The new File will be initialized with one memory Unit (Byte), so this code resizes it to the pageSize
if(sys_fileResize(returnable->file, pageSize - 1) == NULL){
sys_heapFree(returnable);
2022-08-29 11:05:24 +02:00
return NULL;
}
return returnable;
}
2022-08-29 17:27:24 +02:00
2022-08-29 11:05:24 +02:00
fp_File fp_fileOpen(char * fileName){
2024-02-18 17:42:05 +01:00
sys_File currentFile = sys_fileOpenInMem(fileName, SYS_FILE_NOTHING);
2022-08-29 11:05:24 +02:00
if(currentFile == NULL){
return NULL;
}
2024-02-18 17:42:05 +01:00
fp_File returnable = sys_heapAlloc(sizeof(struct fp_File_impl));
2022-08-29 11:05:24 +02:00
//returnable->pageSize = pageSize;
returnable->file = currentFile;
//since this function does not read the header, there is no way of knowing, what the page size is. Somebody else has to call the fp_fileInit function for this
return returnable;
}
//Since someone else has to read the header, this function sets the read page size after fp_fileOpen has been called
void fp_fileInit(fp_File file, unsigned long pageSize){
file->pageSize = pageSize;
}
//negative values indicate the removable of the latest pages; so this library doesn't need more functions
void * fp_fileAppendPages(fp_File file, long numberOfPages){
2024-02-18 17:42:05 +01:00
void *newMem = sys_fileResize(file->file, file->pageSize * numberOfPages);
2022-08-29 17:27:24 +02:00
if(newMem == NULL) {
2024-02-18 17:42:05 +01:00
sys_fileClose(file->file);
2022-08-29 17:27:24 +02:00
file->file = NULL;
file->pageSize = 0;
return NULL;
}
return newMem;
2022-08-29 11:05:24 +02:00
}
2024-02-18 17:42:05 +01:00
void * fp_fileToMemory(fp_File file){
return sys_fileFileToMemory(file->file);
2022-08-29 11:05:24 +02:00
}
2022-08-29 17:27:24 +02:00
void fp_fileFlush(fp_File file) {
2024-02-18 17:42:05 +01:00
sys_fileFlush(file->file);
2022-08-29 17:27:24 +02:00
}
2022-08-29 11:05:24 +02:00
void fp_fileClose(fp_File file){
2022-08-29 17:27:24 +02:00
if(file->file != NULL) {
2024-02-18 17:42:05 +01:00
sys_fileClose(file->file);
2022-08-29 17:27:24 +02:00
}
2024-02-18 17:42:05 +01:00
sys_heapFree(file);
2022-08-29 11:05:24 +02:00
}