Use malloc to create some sprintf string values (long paths).

This commit is contained in:
Steve 2018-04-21 11:54:06 +01:00
parent c91aac41fe
commit f5bdb5e59a
19 changed files with 85 additions and 33 deletions

View File

@ -56,7 +56,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#define MAX_DESCRIPTION_LENGTH 512
#define MAX_LINE_LENGTH 1024
#define MAX_FILENAME_LENGTH 1024
#define MAX_PATH_LENGTH 4096
#define NUM_TEXTURE_BUCKETS 32
#define NUM_ATLAS_BUCKETS 64

View File

@ -275,7 +275,7 @@ static void loadMetaInfo(void)
void loadGame(void)
{
cJSON *root, *node, *statsJSON;
char *text, filename[MAX_PATH_LENGTH], *statName;
char *text, *filename, *statName;
int i;
Tuple *t;
Trophy *trophy;
@ -284,7 +284,7 @@ void loadGame(void)
initGame();
sprintf(filename, "%s/%d/game.json", app.saveDir, game.saveSlot);
filename = buildFormattedString("%s/%d/game.json", app.saveDir, game.saveSlot);
if (fileExists(filename))
{
@ -339,17 +339,19 @@ void loadGame(void)
free(text);
}
free(filename);
}
void saveGame(void)
{
cJSON *root, *statsJSON, *keysJSON, *keyJSON, *missionsJSON, *missionJSON, *trophiesJSON, *trophyJSON;
char filename[MAX_PATH_LENGTH], *out;
char *filename, *out;
Tuple *t;
Trophy *trophy;
int i;
sprintf(filename, "%s/%d/game.json", app.saveDir, game.saveSlot);
filename = buildFormattedString("%s/%d/game.json", app.saveDir, game.saveSlot);
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Saving game to '%s' ...", filename);
@ -407,6 +409,8 @@ void saveGame(void)
cJSON_Delete(root);
free(out);
free(filename);
}
/*
@ -416,10 +420,10 @@ void saveGame(void)
void restoreGameState(void)
{
cJSON *root, *node, *statsJSON;
char *text, filename[MAX_PATH_LENGTH];
char *text, *filename;
int i;
sprintf(filename, "%s/%d/game.json", app.saveDir, game.saveSlot);
filename = buildFormattedString("%s/%d/game.json", app.saveDir, game.saveSlot);
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Restoring game from '%s' ...", filename);
@ -451,6 +455,8 @@ void restoreGameState(void)
cJSON_Delete(root);
free(text);
free(filename);
}
char *getSaveWidgetLabel(char *filename)
@ -496,9 +502,9 @@ char *getSaveWidgetLabel(char *filename)
void deleteSaveSlot(int slot)
{
int i, numFiles;
char path[MAX_PATH_LENGTH], **filenames;
char *path, **filenames;
sprintf(path, "%s/%d", app.saveDir, slot);
path = buildFormattedString("%s/%d", app.saveDir, slot);
filenames = getFileList(path, &numFiles);
@ -516,6 +522,8 @@ void deleteSaveSlot(int slot)
}
free(filenames);
free(path);
}
static int sortItems(const void *a, const void *b)

View File

@ -33,6 +33,7 @@ extern int lookup(char *name);
extern char *readFile(const char *filename);
extern char *timeToString(int seconds, int showHours);
extern int writeFile(const char *filename, const char *data);
extern char *buildFormattedString(const char *format, ...);
extern App app;
extern Entity *self;

View File

@ -162,7 +162,7 @@ static void draw(void)
static int getRecentSave(void)
{
char filename[MAX_PATH_LENGTH];
char *filename;
int i, slot, curModTime, modTime;
slot = -1;
@ -170,7 +170,7 @@ static int getRecentSave(void)
for (i = 0 ; i < MAX_SAVE_SLOTS ; i++)
{
sprintf(filename, "%s/%d/game.json", app.saveDir, i);
filename = buildFormattedString("%s/%d/game.json", app.saveDir, i);
if (fileExists(filename))
{
@ -182,6 +182,8 @@ static int getRecentSave(void)
slot = i;
}
}
free(filename);
}
return slot;
@ -190,7 +192,7 @@ static int getRecentSave(void)
static void populateSaveSlotWidgets(void)
{
int i;
char name[MAX_NAME_LENGTH], filename[MAX_PATH_LENGTH];
char name[MAX_NAME_LENGTH], *filename;
for (i = 0 ; i < MAX_SAVE_SLOTS ; i++)
{
@ -198,7 +200,7 @@ static void populateSaveSlotWidgets(void)
save[i] = getWidget(name, "saveSlot");
sprintf(filename, "%s/%d/game.json", app.saveDir, i);
filename = buildFormattedString("%s/%d/game.json", app.saveDir, i);
if (fileExists(filename))
{
@ -214,6 +216,8 @@ static void populateSaveSlotWidgets(void)
save[i]->value[1] = i;
save[i]->action = &doSaveSlot;
free(filename);
}
}

View File

@ -54,6 +54,7 @@ extern void setSelectedWidget(char *name, char *group);
extern void showWidgetGroup(char *group);
extern void startSectionTransition(void);
extern void stopMusic(void);
extern char *buildFormattedString(const char *format, ...);
extern App app;
extern Colors colors;

View File

@ -24,8 +24,7 @@ static void mkpath(const char *path);
void createSaveFolder(void)
{
char *userHome;
char dir[MAX_PATH_LENGTH];
char *userHome, *dir;
int i;
userHome = getenv("HOME");
@ -40,12 +39,14 @@ void createSaveFolder(void)
for (i = 0 ; i < MAX_SAVE_SLOTS ; i++)
{
sprintf(dir, "%s/.local/share/blobwarsAttrition/%d", userHome, i);
dir = buildFormattedString("%s/.local/share/blobwarsAttrition/%d", userHome, i);
mkpath(dir);
free(dir);
}
sprintf(app.saveDir, "%s/.local/share/blobwarsAttrition", userHome);
app.saveDir = buildFormattedString("%s/.local/share/blobwarsAttrition", userHome);
}
static void mkpath(const char *path)

View File

@ -25,5 +25,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "../../common.h"
extern char *buildFormattedString(const char *format, ...);
extern App app;
extern Dev dev;

View File

@ -24,8 +24,7 @@ static void mkpath(const char *path);
void createSaveFolder(void)
{
char *userHome;
char dir[MAX_PATH_LENGTH];
char *userHome, *dir;
int i;
userHome = getenv("USERPROFILE");
@ -40,12 +39,14 @@ void createSaveFolder(void)
for (i = 0 ; i < MAX_SAVE_SLOTS ; i++)
{
sprintf(dir, "%s\\blobwarsAttrition\\%d", userHome, i);
dir = buildFormattedString("%s\\blobwarsAttrition\\%d", userHome, i);
mkpath(dir);
free(dir);
}
sprintf(app.saveDir, "%s\\blobwarsAttrition", userHome);
app.saveDir = buildFormattedString("%s\\blobwarsAttrition", userHome);
}
void mkpath(const char *path)

View File

@ -24,5 +24,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "../../common.h"
extern char *buildFormattedString(const char *format, ...);
extern App app;
extern Dev dev;

View File

@ -338,7 +338,7 @@ typedef struct {
} Config;
typedef struct {
char saveDir[MAX_FILENAME_LENGTH];
char *saveDir;
float scaleX;
float scaleY;
Mouse mouse;

View File

@ -261,20 +261,22 @@ static void initColor(SDL_Color *c, int r, int g, int b)
void saveScreenshot(char *name)
{
char filename[MAX_PATH_LENGTH];
char *filename;
SDL_Surface *screenshot;
if (name != NULL)
{
sprintf(filename, "%s/%d/%s.png", app.saveDir, game.saveSlot, name);
filename = buildFormattedString("%s/%d/%s.png", app.saveDir, game.saveSlot, name);
}
else
{
sprintf(filename, "%s/%d.png", dev.screenshotFolder, SDL_GetTicks());
filename = buildFormattedString("%s/%d.png", dev.screenshotFolder, SDL_GetTicks());
}
screenshot = SDL_CreateRGBSurface(0, SCREEN_WIDTH, SCREEN_HEIGHT, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
SDL_RenderReadPixels(app.renderer, NULL, SDL_PIXELFORMAT_ARGB8888, screenshot->pixels, screenshot->pitch);
SDL_SavePNG(screenshot, filename);
SDL_FreeSurface(screenshot);
free(filename);
}

View File

@ -22,6 +22,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "savepng.h"
extern void drawText(int x, int y, int size, int align, SDL_Color c, const char *format, ...);
extern char *buildFormattedString(const char *format, ...);
extern App app;
extern Colors colors;

View File

@ -229,11 +229,11 @@ static void loadConfig(void)
{
int i;
cJSON *root, *controlsJSON, *node;
char *text, filename[MAX_PATH_LENGTH];
char *text, *filename;
initDefaultConfig();
sprintf(filename, "%s/%s", app.saveDir, CONFIG_FILENAME);
filename = buildFormattedString("%s/%s", app.saveDir, CONFIG_FILENAME);
if (fileExists(filename))
{
@ -280,15 +280,17 @@ static void loadConfig(void)
cJSON_Delete(root);
free(text);
}
free(filename);
}
void saveConfig(void)
{
int i;
char *out, filename[MAX_PATH_LENGTH];
char *out, *filename;
cJSON *root, *controlsJSON, *keysJSON, *joypadJSON;
sprintf(filename, "%s/%s", app.saveDir, CONFIG_FILENAME);
filename = buildFormattedString("%s/%s", app.saveDir, CONFIG_FILENAME);
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Saving config ...");
@ -333,6 +335,8 @@ void saveConfig(void)
cJSON_Delete(root);
free(out);
free(filename);
}
void cleanup(void)

View File

@ -54,5 +54,6 @@ extern void presentScene(void);
extern char *readFile(const char *filename);
extern void setLanguage(char *applicationName, char *languageCode);
extern int writeFile(const char *filename, const char *data);
extern char *buildFormattedString(const char *format, ...);
extern App app;

View File

@ -81,6 +81,25 @@ void *resize(void *array, int oldSize, int newSize)
return newArray;
}
char *buildFormattedString(const char *format, ...)
{
int n;
char *s;
va_list args;
va_start(args, format);
n = vsnprintf(NULL, 0, format, args) + 1;
va_end(args);
s = malloc(sizeof(char) * n);
va_start(args, format);
vsprintf(s, format, args);
va_end(args);
return s;
}
/*
* public domain strtok_r() by Charlie Gordon
*

View File

@ -29,7 +29,7 @@ static void loadObjectives(cJSON *root);
void loadWorld(char *id)
{
cJSON *root;
char *text, filename[MAX_PATH_LENGTH];
char *text, *filename;
memset(&world, 0, sizeof(World));
@ -38,7 +38,7 @@ void loadWorld(char *id)
world.objectiveTail = &world.objectiveHead;
world.particleTail = &world.particleHead;
sprintf(filename, "%s/%d/%s.json", app.saveDir, game.saveSlot, id);
filename = buildFormattedString("%s/%d/%s.json", app.saveDir, game.saveSlot, id);
if (!game.isComplete && fileExists(filename))
{
@ -79,6 +79,8 @@ void loadWorld(char *id)
cJSON_Delete(root);
free(text);
free(filename);
}
static void loadEnemyTypes(char *enemyTypes)

View File

@ -24,6 +24,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
extern Entity *createEntity(char *typeStr);
extern int fileExists(const char *filename);
extern char *readFile(const char *filename);
extern char *buildFormattedString(const char *format, ...);
extern App app;
extern Dev dev;

View File

@ -30,9 +30,9 @@ static int canPersistEntity(void);
void saveWorld(void)
{
cJSON *root;
char filename[MAX_PATH_LENGTH], *out;
char *filename, *out;
sprintf(filename, "%s/%d/%s.json", app.saveDir, game.saveSlot, world.id);
filename = buildFormattedString("%s/%d/%s.json", app.saveDir, game.saveSlot, world.id);
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Saving world to '%s' ...", filename);
@ -66,6 +66,8 @@ void saveWorld(void)
cJSON_Delete(root);
free(out);
free(filename);
}
static void saveEnemyTypes(cJSON *root)

View File

@ -22,6 +22,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "../json/cJSON.h"
extern int writeFile(const char *filename, const char *data);
extern char *buildFormattedString(const char *format, ...);
extern App app;
extern Entity *self;