]> 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
index 7d2792c3c6fc1c7422ce2fe67d080d64e08e8f48..3dfc2e18e8b8426d1dd31ea02bf377d67c87722d 100644 (file)
@@ -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
@@ -42,14 +43,14 @@ void initSaveSlots()
 
                if (!fp)
                {
-                       strncpy(string, "%.2d - %s", sizeof string);
+                       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)
                        {
-                               strncpy(string, "%.2d - %s", sizeof string);
+                               strlcpy(string, "%.2d - %s", sizeof string);
                                snprintf(engine.saveSlot[i], sizeof engine.saveSlot[i], string, (i + 1), _("Corrupt Save Data"));
                        }
                        else
@@ -106,7 +107,7 @@ 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);
@@ -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]);
-               strncpy(stageName, string[0], sizeof stageName);
+               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);
-                       
-                       strncpy(persistData->data, line, sizeof persistData->data);
+
+                       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));
                        
@@ -237,7 +250,7 @@ int confirmSave()
        for (int i = 0 ; i < 5 ; i++)
        {
                snprintf(widgetName, sizeof widgetName, "slot%d", i + 1);
-               strncpy(engine.getWidgetByName(widgetName)->label, engine.saveSlot[i], sizeof engine.getWidgetByName(widgetName)->label);
+               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();
@@ -343,10 +356,14 @@ void saveGame()
        
        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);
        
@@ -356,7 +373,7 @@ void saveGame()
        
        if (!fp)
        {
-               graphics.showErrorAndExit("File write error whilst saving game", "");
+               return graphics.showErrorAndExit("File write error whilst saving game", "");
        }
        
        createPersistantMapData();