/*
-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
//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)
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");
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");
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], ¶m[0], ¶m[1]);
data = new Data();
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));
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));
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");
void saveGame()
{
- char message[100];
+ char message[256];
SDL_FillRect(graphics.screen, NULL, graphics.black);
graphics.updateScreen();
return;
graphics.setFontSize(1);
- sprintf(message, _("Saving Game to Save Slot #%d. Please Wait..."), slot + 1);
+ graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
+ snprintf(message, sizeof message, _("Saving Game to Save Slot #%d. Please Wait..."), slot + 1);
graphics.drawString(message, 320, 220, true, graphics.screen);
graphics.updateScreen();
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();