aboutsummaryrefslogtreecommitdiff
path: root/src/script.cc
diff options
context:
space:
mode:
authorfusion32 <marcopuzziello@gmail.com>2025-05-22 23:17:54 -0300
committerfusion32 <marcopuzziello@gmail.com>2025-05-22 23:17:54 -0300
commitb7432b4e74284138f1740f4577b9cb32e1f120d0 (patch)
tree055cdc4653396996ab13ced99198338f906294dc /src/script.cc
parent4e41f79a50b18b4e9b9fa00bbfa68ecb15fcb63e (diff)
downloadgame-b7432b4e74284138f1740f4577b9cb32e1f120d0.tar.gz
game-b7432b4e74284138f1740f4577b9cb32e1f120d0.zip
implement `SaveFile`
Diffstat (limited to 'src/script.cc')
-rw-r--r--src/script.cc55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/script.cc b/src/script.cc
index 009fd86..5dd3751 100644
--- a/src/script.cc
+++ b/src/script.cc
@@ -117,6 +117,61 @@ static char *findLast(char *s, char c){
return Last;
}
+static void SaveFile(const char *Filename){
+ if(Filename == NULL){
+ error("SaveFile: Filename ist NULL.\n");
+ return;
+ }
+
+ char BackupFilename[4096];
+
+ strcpy(BackupFilename, Filename);
+ strcat(BackupFilename, "#");
+
+ FILE *Source = fopen(Filename, "rb");
+ if(Source == NULL){
+ error("SaveFile: Quelldatei %s existiert nicht.\n", Filename);
+ return;
+ }
+
+ FILE *Dest = fopen(BackupFilename, "wb");
+ if(Dest == NULL){
+ error("SaveFile: Kann Zieldatei %s nicht anlegen.\n", BackupFilename);
+ fclose(Source);
+ return;
+ }
+
+ // NOTE(fusion): This function was in pretty bad shape after this point. The
+ // reason is probably because GHIDRA didn't make sense of it using `alloca`
+ // or some other mechanism to allocate a buffer on the stack to copy the whole
+ // file in one go. It's probably not a good idea to mimic that here so we'll
+ // do it gradually.
+
+ char Buffer[KB(16)];
+ while(true){
+ usize n = fread(Buffer, 1, sizeof(Buffer), Source);
+ if(n == 0){
+ if(ferror(Source)){
+ error("SaveFile: Fehler beim Lesen der Quelldatei %s.\n", Filename);
+ }
+ break;
+ }
+
+ if(fwrite(Buffer, 1, n, Dest) != n){
+ error("SaveFile: Fehler beim Schreiben der Zieldatei %s.\n", BackupFilename);
+ break;
+ }
+ }
+
+ if(fclose(Source) != 0){
+ error("SaveFile: Fehler %d beim Schließen der Quelldatei.\n", errno);
+ }
+
+ if(fclose(Dest) != 0){
+ error("SaveFile: Fehler %d beim Schließen der Zieldatei.\n", errno);
+ }
+}
+
// TReadScriptFile
//==============================================================================
TReadScriptFile::TReadScriptFile(void){