]> git.mxchange.org Git - quix0rs-blobwars.git/blobdiff - src/loadSave.cpp
Added .gitignore to ignore certain files + fixed access rights on Makefile* as
[quix0rs-blobwars.git] / src / loadSave.cpp
old mode 100755 (executable)
new mode 100644 (file)
index cfb509d..3dfc2e1
@@ -1,5 +1,6 @@
 /*
-Copyright (C) 2004 Parallel Realities
+Copyright (C) 2004-2011 Parallel Realities
+Copyright (C) 2011-2015 Perpendicular Dimensions
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
@@ -36,25 +37,25 @@ void initSaveSlots()
        //READ SAVE GAME DATA
        for (int i = 0 ; i < 5 ; i++)
        {
-               sprintf(filename, "%ssave%d.dat", engine.userHomeDirectory, i);
+               snprintf(filename, sizeof filename, "%ssave%d.dat", engine.userHomeDirectory, i);
 
                fp = fopen(filename, "rb");
 
                if (!fp)
                {
-                       strcpy(string, "%.2d - %s");
-                       sprintf(engine.saveSlot[i], string, (i + 1), _("Empty"));
+                       strlcpy(string, "%.2d - %s", sizeof string);
+                       snprintf(engine.saveSlot[i], sizeof engine.saveSlot[i], string, (i + 1), _("Empty"));
                }
                else
                {
                        if (fread(&tempGame, sizeof(Game), 1, fp) != 1)
                        {
-                               strcpy(string, "%.2d - %s");
-                               sprintf(engine.saveSlot[i], string, (i + 1), _("Corrupt Save Data"));
+                               strlcpy(string, "%.2d - %s", sizeof string);
+                               snprintf(engine.saveSlot[i], sizeof engine.saveSlot[i], string, (i + 1), _("Corrupt Save Data"));
                        }
                        else
                        {
-                               sprintf(engine.saveSlot[i], "%.2d - %s (%.2d:%.2d:%.2d)", (i + 1), _(tempGame.stageName), tempGame.totalHours, tempGame.totalMinutes, tempGame.totalSeconds);
+                               snprintf(engine.saveSlot[i], sizeof engine.saveSlot[i], "%.2d - %s (%.2d:%.2d:%.2d)", (i + 1), _(tempGame.stageName), tempGame.totalHours, tempGame.totalMinutes, tempGame.totalSeconds);
                        }
 
                        if (stat(filename, &fileInfo) != -1)
@@ -94,7 +95,7 @@ bool loadGame(int slot)
        
        int sanity = 0;
 
-       sprintf(filename, "%ssave%d.dat", engine.userHomeDirectory, slot);
+       snprintf(filename, sizeof filename, "%ssave%d.dat", engine.userHomeDirectory, slot);
 
        fp = fopen(filename, "rb");
        
@@ -106,12 +107,12 @@ bool loadGame(int slot)
        if (fread(&game, sizeof(Game), 1, fp) != 1)
        {
                fclose(fp);
-               graphics.showErrorAndExit("The save data loaded was not in the format expected", "");
+               return graphics.showErrorAndExit("The save data loaded was not in the format expected", ""), false;
        }
        
        fclose(fp);
        
-       sprintf(filename, "%spersistant%d.dat", engine.userHomeDirectory, slot);
+       snprintf(filename, sizeof filename, "%spersistant%d.dat", engine.userHomeDirectory, slot);
 
        fp = fopen(filename, "rb");
        
@@ -122,8 +123,11 @@ bool loadGame(int slot)
        
        while (true)
        {
-               fgets(line, 1024, fp);
-               
+               if (!fgets(line, 1024, fp)) {
+                       fclose(fp);
+                       return graphics.showErrorAndExit("Unexpected end of file reading save data", ""), false;
+               }
+
                sscanf(line, "%*c %[^\"] %*c %*c %[^\"] %*c %d %d", string[0], string[1], &param[0], &param[1]);
                
                data = new Data();
@@ -160,17 +164,23 @@ bool loadGame(int slot)
        
        while (true)
        {
-               fgets(line, 1024, fp);
-               
+               if (!fgets(line, 1024, fp)) {
+                       fclose(fp);
+                       graphics.showErrorAndExit("Unexpected end of file reading save data", "");
+               }
+
                sscanf(line, "%[^\n\r]", string[0]);
-               strcpy(stageName, string[0]);
+               strlcpy(stageName, string[0], sizeof stageName);
                
                if (strcmp(stageName, "@EOF@") == 0)
                {
                        break;
                }
-               
-               fgets(line, 1024, fp);
+
+               if (!fgets(line, 1024, fp)) {
+                       fclose(fp);
+                       graphics.showErrorAndExit("Unexpected end of file reading save data", "");
+               }
                sscanf(line, "%d", &numberOfLines);
                
                debug(("Read %s with %d lines.\n", stageName, numberOfLines));
@@ -180,10 +190,13 @@ bool loadGame(int slot)
                for (int i = 0 ; i < numberOfLines ; i++)
                {
                        persistData = new PersistData();
-                       
-                       fgets(line, 1024, fp);
-                       
-                       strcpy(persistData->data, line);
+
+                       if (!fgets(line, 1024, fp)) {
+                               fclose(fp);
+                               graphics.showErrorAndExit("Unexpected end of file reading save data", "");
+                       }
+
+                       strlcpy(persistData->data, line, sizeof persistData->data);
                        
                        //debug(("Read %d: %s", i, persistData->data));
                        
@@ -232,12 +245,12 @@ int confirmSave()
        engine.setWidgetVariable("contno", &quitNo);
        
        char widgetName[10];
-       strcpy(widgetName, "");
+       widgetName[0] = 0;
        
        for (int i = 0 ; i < 5 ; i++)
        {
-               sprintf(widgetName, "slot%d", i + 1);
-               strcpy(engine.getWidgetByName(widgetName)->label, engine.saveSlot[i]);
+               snprintf(widgetName, sizeof widgetName, "slot%d", i + 1);
+               strlcpy(engine.getWidgetByName(widgetName)->label, engine.saveSlot[i], sizeof engine.getWidgetByName(widgetName)->label);
        }
        
        engine.highlightWidget("slot1");
@@ -316,7 +329,7 @@ int confirmSave()
 
 void saveGame()
 {
-       char message[100];
+       char message[256];
 
        SDL_FillRect(graphics.screen, NULL, graphics.black);
        graphics.updateScreen();
@@ -329,7 +342,7 @@ void saveGame()
 
        graphics.setFontSize(1);
        graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
-       sprintf(message, _("Saving Game to Save Slot #%d. Please Wait..."), slot + 1);
+       snprintf(message, sizeof message, _("Saving Game to Save Slot #%d. Please Wait..."), slot + 1);
        graphics.drawString(message, 320, 220, true, graphics.screen);
        graphics.updateScreen();
 
@@ -337,26 +350,30 @@ void saveGame()
 
        FILE *fp;
 
-       sprintf(filename, "%ssave%d.dat", engine.userHomeDirectory, slot);
+       snprintf(filename, sizeof filename, "%ssave%d.dat", engine.userHomeDirectory, slot);
 
        fp = fopen(filename, "wb");
        
        if (!fp)
        {
-               graphics.showErrorAndExit("File write error whilst saving game", "");
+               return graphics.showErrorAndExit("File write error whilst saving game", "");
        }
 
-       fwrite(&game, sizeof(Game), 1, fp);
+       if (fwrite(&game, sizeof(Game), 1, fp) != 1)
+       {
+               fclose(fp);
+               return graphics.showErrorAndExit("File write error whilst saving game", strerror(errno));
+       }
        
        fclose(fp);
        
-       sprintf(filename, "%spersistant%d.dat", engine.userHomeDirectory, slot);
+       snprintf(filename, sizeof filename, "%spersistant%d.dat", engine.userHomeDirectory, slot);
 
        fp = fopen(filename, "wt");
        
        if (!fp)
        {
-               graphics.showErrorAndExit("File write error whilst saving game", "");
+               return graphics.showErrorAndExit("File write error whilst saving game", "");
        }
        
        createPersistantMapData();