ref: 564d42dbd23a369d7d575b176e93fc0721fb407d
parent: 32a879ca58c048a3d09380d6d74d242ffe7bffd2
author: Gabriel Ravier <gabravier@gmail.com>
date: Mon Jun 29 12:13:14 EDT 2020
src: Change from using asprintf-based code to using std::string-based code Signed-off-by: Gabriel Ravier <gabravier@gmail.com>
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -171,14 +171,6 @@
"src/Backends/Controller.h"
"src/Backends/Misc.h"
"src/Backends/Rendering.h"
- "src/Helpers/Asprintf.cpp"
- "src/Helpers/Asprintf.h"
- "src/Helpers/Vasprintf.cpp"
- "src/Helpers/Vasprintf.h"
- "src/Helpers/FopenFormatted.cpp"
- "src/Helpers/FopenFormatted.h"
- "src/Helpers/Strdup.cpp"
- "src/Helpers/Strdup.h"
)
set(RESOURCES
--- a/src/ArmsItem.cpp
+++ b/src/ArmsItem.cpp
@@ -416,12 +416,9 @@
int CampLoop(void)
{
RECT rcView = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT};
- int ret = enum_ESCRETURN_continue;
// Save the current script path (to restore it when we get out of the inventory)
- char *old_script_path = GetTextScriptPath();
- if (old_script_path == NULL)
- return enum_ESCRETURN_exit;
+ std::string old_script_path = GetTextScriptPath();
// Load the inventory script
LoadTextScript2("ArmsItem.tsc");
@@ -452,11 +449,9 @@
switch (Call_Escape())
{
case enum_ESCRETURN_exit:
- free(old_script_path);
return enum_ESCRETURN_exit;
case enum_ESCRETURN_restart:
- free(old_script_path);
return enum_ESCRETURN_restart;
}
}
@@ -467,11 +462,9 @@
switch (TextScriptProc())
{
case enum_ESCRETURN_exit:
- free(old_script_path);
return enum_ESCRETURN_exit;
case enum_ESCRETURN_restart:
- free(old_script_path);
return enum_ESCRETURN_restart;
}
@@ -500,16 +493,12 @@
}
if (!Flip_SystemTask())
- {
- free(old_script_path);
return enum_ESCRETURN_exit;
- }
}
// Resume original script
- LoadTextScript_Stage(old_script_path);
+ LoadTextScript_Stage(old_script_path.c_str());
gArmsEnergyX = 32; // Displays weapon rotation animation in case the weapon was changed
- free(old_script_path);
return enum_ESCRETURN_continue;
}
--- a/src/Back.cpp
+++ b/src/Back.cpp
@@ -13,7 +13,6 @@
#include "Main.h"
#include "Map.h"
#include "Stage.h"
-#include "Helpers/FopenFormatted.h"
BACK gBack;
int gWaterY;
@@ -26,7 +25,7 @@
color_black = GetCortBoxColor(RGB(0, 0, 0x10));
// Get width and height
- FILE *fp = fopenFormatted("rb", "%s/%s.pbm", gDataPath, fName);
+ FILE *fp = fopen((gDataPath + '/' + fName + ".pbm").c_str(), "rb");
if (fp == NULL)
return FALSE;
--- a/src/Backends/Misc.h
+++ b/src/Backends/Misc.h
@@ -1,6 +1,7 @@
#pragma once
#include "../Attributes.h"
+#include <string>
enum
{
@@ -86,7 +87,7 @@
bool Backend_Init(void);
void Backend_Deinit(void);
void Backend_PostWindowCreation(void);
-bool Backend_GetBasePath(char **string_buffer);
+bool Backend_GetBasePath(std::string *string_buffer);
void Backend_HideMouse(void);
void Backend_SetWindowIcon(const unsigned char *rgb_pixels, unsigned int width, unsigned int height);
void Backend_SetCursor(const unsigned char *rgb_pixels, unsigned int width, unsigned int height);
--- a/src/Backends/Platform/GLFW3.cpp
+++ b/src/Backends/Platform/GLFW3.cpp
@@ -181,7 +181,7 @@
glfwSetWindowSizeCallback(window, WindowSizeCallback);
}
-bool Backend_GetBasePath(char **string_buffer)
+bool Backend_GetBasePath(std::string *string_buffer)
{
(void)string_buffer;
--- a/src/Backends/Platform/Null.cpp
+++ b/src/Backends/Platform/Null.cpp
@@ -1,4 +1,5 @@
#include "../Misc.h"
+#include <string>
bool Backend_Init(void)
{
@@ -15,7 +16,7 @@
}
-bool Backend_GetBasePath(char **string_buffer)
+bool Backend_GetBasePath(std::string *string_buffer)
{
(void)string_buffer;
--- a/src/Backends/Platform/SDL2.cpp
+++ b/src/Backends/Platform/SDL2.cpp
@@ -13,7 +13,6 @@
#include "../../Organya.h"
#include "../../Profile.h"
#include "../../Resource.h"
-#include "../../Helpers/Strdup.h"
#define DO_KEY(SDL_KEY, BACKEND_KEY) \
case SDL_KEY: \
@@ -85,7 +84,7 @@
}
-bool Backend_GetBasePath(char **string_buffer)
+bool Backend_GetBasePath(std::string *string_buffer)
{
#ifdef _WIN32
// SDL_GetBasePath returns a UTF-8 string, but Windows' fopen uses (extended?) ASCII.
@@ -92,6 +91,8 @@
// This is apparently a problem for accented characters, as they will make fopen fail.
// So, instead, we rely on argv[0], as that will put the accented characters in a
// format Windows will understand.
+ (void)string_buffer;
+
return false;
#else
char *base_path = SDL_GetBasePath();
@@ -101,7 +102,7 @@
// Trim the trailing '/'
size_t base_path_length = strlen(base_path);
base_path[base_path_length - 1] = '\0';
- *string_buffer = strdup(base_path);
+ *string_buffer = base_path;
SDL_free(base_path);
return true;
--- a/src/Backends/Platform/WiiU.cpp
+++ b/src/Backends/Platform/WiiU.cpp
@@ -56,12 +56,12 @@
}
-bool Backend_GetBasePath(char **string_buffer)
+bool Backend_GetBasePath(std::string *string_buffer)
{
#ifdef JAPANESE
- *string_buffer = asprintf("%s/CSE-portable-jp", WHBGetSdCardMountPath());
+ *string_buffer = std::string{WHBGetSdCardMountPath()} + "/CSE-portable-jp";
#else
- *string_buffer = asprintf("%s/CSE-portable-en", WHBGetSdCardMountPath());
+ *string_buffer = std::string{WHBGetSdCardMountPath()} + "/CSE-portable-en";
#endif
return true;
}
--- a/src/Config.cpp
+++ b/src/Config.cpp
@@ -7,7 +7,6 @@
#include "Config.h"
#include "File.h"
#include "Main.h"
-#include "Helpers/FopenFormatted.h"
const char* const gConfigName = "Config.dat";
const char* const gProof = "DOUKUTSU20041206";
@@ -18,7 +17,7 @@
memset(conf, 0, sizeof(CONFIG));
// Open file
- FILE *fp = fopenFormatted("rb", "%s/%s", gModulePath, gConfigName);
+ FILE *fp = fopen((gModulePath + '/' + gConfigName).c_str(), "rb");
if (fp == NULL)
return FALSE;
--- a/src/Draw.cpp
+++ b/src/Draw.cpp
@@ -18,7 +18,6 @@
#include "MapName.h"
#include "Resource.h"
#include "TextScr.h"
-#include "Helpers/Asprintf.h"
typedef enum SurfaceType
{
@@ -251,15 +250,11 @@
// TODO - Inaccurate stack frame
BOOL MakeSurface_File(const char *name, SurfaceID surf_no)
{
- char *path;
+ std::string path = gDataPath + '/' + name + ".pbm";
- if (asprintf(&path, "%s/%s.pbm", gDataPath, name) < 0)
- return FALSE;
-
- if (!IsEnableBitmap(path))
+ if (!IsEnableBitmap(path.c_str()))
{
- ErrorLog(path, 0);
- free(path);
+ ErrorLog(path.c_str(), 0);
return FALSE;
}
@@ -270,7 +265,6 @@
#endif
{
ErrorLog("surface no", surf_no);
- free(path);
return FALSE;
}
@@ -277,20 +271,17 @@
if (surf[surf_no] != NULL)
{
ErrorLog("existing", surf_no);
- free(path);
return FALSE;
}
unsigned int width, height;
- unsigned char *image_buffer = DecodeBitmapFromFile(path, &width, &height);
+ unsigned char *image_buffer = DecodeBitmapFromFile(path.c_str(), &width, &height);
if (image_buffer == NULL)
{
- ErrorLog(path, 1);
- free(path);
+ ErrorLog(path.c_str(), 1);
return FALSE;
}
- free(path);
surf[surf_no] = RenderBackend_CreateSurface(width * mag, height * mag, false);
@@ -349,14 +340,11 @@
// TODO - Inaccurate stack frame
BOOL ReloadBitmap_File(const char *name, SurfaceID surf_no)
{
- char *path;
- if (asprintf(&path, "%s/%s.pbm", gDataPath, name) < 0)
- return FALSE;
+ std::string path = gDataPath + '/' + name + ".pbm";
- if (!IsEnableBitmap(path))
+ if (!IsEnableBitmap(path.c_str()))
{
- ErrorLog(path, 0);
- free(path);
+ ErrorLog(path.c_str(), 0);
return FALSE;
}
@@ -367,20 +355,17 @@
#endif
{
ErrorLog("surface no", surf_no);
- free(path);
return FALSE;
}
unsigned int width, height;
- unsigned char *image_buffer = DecodeBitmapFromFile(path, &width, &height);
+ unsigned char *image_buffer = DecodeBitmapFromFile(path.c_str(), &width, &height);
if (image_buffer == NULL)
{
- ErrorLog(path, 1);
- free(path);
+ ErrorLog(path.c_str(), 1);
return FALSE;
}
- free(path);
if (!ScaleAndUploadSurface(image_buffer, width, height, surf_no))
{
@@ -659,10 +644,6 @@
{
(void)name; // Unused in this branch
- char *path;
- if (asprintf(&path, "%s/Font/font", gDataPath) < 0)
- return;
-
// Get font size
unsigned int width, height;
@@ -679,7 +660,7 @@
break;
}
- font = LoadFont(path, width, height);
+ font = LoadFont((gDataPath + "/Font/font").c_str(), width, height);
}
void PutText(int x, int y, const char *text, unsigned long color)
--- a/src/Ending.cpp
+++ b/src/Ending.cpp
@@ -16,7 +16,6 @@
#include "Organya.h"
#include "Stage.h"
#include "TextScr.h"
-#include "Helpers/Asprintf.h"
struct CREDIT
{
@@ -226,27 +225,17 @@
}
// Open file
- char *path;
- if (asprintf(&path, "%s/%s", gDataPath, credit_script) < 0)
- return FALSE;
-
- Credit.size = GetFileSizeLong(path);
+ std::string path = gDataPath + '/' + credit_script;
+ Credit.size = GetFileSizeLong(path.c_str());
if (Credit.size == -1)
- {
- free(path);
return FALSE;
- }
// Allocate buffer data
Credit.pData = (char*)malloc(Credit.size);
if (Credit.pData == NULL)
- {
- free(path);
return FALSE;
- }
- fp = fopen(path, "rb");
- free(path);
+ fp = fopen(path.c_str(), "rb");
if (fp == NULL)
{
free(Credit.pData);
--- a/src/Game.cpp
+++ b/src/Game.cpp
@@ -44,7 +44,6 @@
#include "Star.h"
#include "TextScr.h"
#include "ValueView.h"
-#include "Helpers/Asprintf.h"
int g_GameFlags;
int gCounter;
@@ -702,13 +701,9 @@
PlaySoundObject(7, -1);
- char *path;
- if (asprintf(&path, "%s/npc.tbl", gDataPath) < 0)
- return FALSE;
+ std::string path = gDataPath + "/npc.tbl";
- BOOL load_npc_table_succeeded = LoadNpcTable(path);
- free(path);
- if (!load_npc_table_succeeded)
+ if (!LoadNpcTable(path.c_str()))
{
#ifdef JAPANESE
Backend_ShowMessageBox("エラー", "NPCテーブルが読めない");
--- a/src/Generic.cpp
+++ b/src/Generic.cpp
@@ -8,8 +8,6 @@
#include "WindowsWrapper.h"
#include "Main.h"
-#include "Helpers/Asprintf.h"
-#include "Helpers/FopenFormatted.h"
void GetCompileDate(int *year, int *month, int *day)
{
@@ -52,19 +50,12 @@
void DeleteLog(void)
{
- char *path;
-
- if (asprintf(&path, "%s/debug.txt", gModulePath) < 0)
- return;
-
- remove(path);
- free(path);
+ remove((gModulePath + "/debug.txt").c_str());
}
BOOL WriteLog(const char *string, int value1, int value2, int value3)
{
- FILE *fp = fopenFormatted("a+", "%s/debug.txt", gModulePath);
-
+ FILE *fp = fopen((gModulePath + "/debug.txt").c_str(), "a+");
if (fp == NULL)
return FALSE;
@@ -75,8 +66,7 @@
BOOL IsKeyFile(const char *name)
{
- FILE *file = fopenFormatted("rb", "%s/%s", gModulePath, name);
-
+ FILE *file = fopen((gModulePath + '/' + name).c_str(), "rb");
if (file == NULL)
return FALSE;
@@ -104,15 +94,12 @@
BOOL ErrorLog(const char *string, int value)
{
- char *path;
- if (asprintf(&path, "%s/%s", gModulePath, "error.log") < 0)
- return FALSE;
+ std::string path = gModulePath + "/error.log";
- if (GetFileSizeLong(path) > 0x19000) // Purge the error log if it gets too big, I guess
- remove(path);
+ if (GetFileSizeLong(path.c_str()) > 0x19000) // Purge the error log if it gets too big, I guess
+ remove(path.c_str());
- FILE *fp = fopen(path, "a+");
- free(path);
+ FILE *fp = fopen(path.c_str(), "a+");
if (fp == NULL)
return FALSE;
--- a/src/Helpers/Asprintf.cpp
+++ /dev/null
@@ -1,18 +1,0 @@
-#ifndef _GNU_SOURCE
-
-#include "Asprintf.h"
-#include "Vasprintf.h"
-#include "../Attributes.h"
-#include <stdarg.h>
-
-ATTRIBUTE_FORMAT(printf, 2, 3) ATTRIBUTE_WARN_UNUSED_RESULT int asprintf(char **result_string, const char *format_string, ...)
-{
- // Handle variadic arguments and redirect to vasprintf
- va_list arguments_local;
- va_start(arguments_local, format_string);
- int retVal = vasprintf(result_string, format_string, arguments_local);
- va_end(arguments_local); // Destroy arguments_local
- return retVal;
-}
-
-#endif
--- a/src/Helpers/Asprintf.h
+++ /dev/null
@@ -1,15 +1,0 @@
-#pragma once
-
-// If <stdio.h> defines asprintf for us, use its definition
-#ifdef _GNU_SOURCE
-
-#include <stdio.h>
-
-#else
-
-#include "../Attributes.h"
-#define asprintf Portable_asprintf
-
-ATTRIBUTE_FORMAT(printf, 2, 3) ATTRIBUTE_WARN_UNUSED_RESULT int asprintf(char **resultString, const char *formatString, ...);
-
-#endif
--- a/src/Helpers/FopenFormatted.cpp
+++ /dev/null
@@ -1,23 +1,0 @@
-#include "FopenFormatted.h"
-#include "../Attributes.h"
-#include <stdio.h>
-#include <stdarg.h>
-#include <stdlib.h>
-
-ATTRIBUTE_FORMAT(printf, 2, 3) ATTRIBUTE_WARN_UNUSED_RESULT FILE *fopenFormatted(const char *mode, const char *format_string, ...)
-{
- // Handle variadic arguments and redirect to vasprintf
- va_list arguments_local;
- va_start(arguments_local, format_string);
- char *path;
- int vasprintf_retval = vasprintf(&path, format_string, arguments_local);
- va_end(arguments_local); // Destroy arguments_local
-
- if (vasprintf_retval < 0)
- return NULL;
-
- FILE *file = fopen(path, mode);
- free(path);
-
- return file;
-}
--- a/src/Helpers/FopenFormatted.h
+++ /dev/null
@@ -1,5 +1,0 @@
-#pragma once
-#include "../Attributes.h"
-#include <stdio.h>
-
-ATTRIBUTE_FORMAT(printf, 2, 3) ATTRIBUTE_WARN_UNUSED_RESULT FILE *fopenFormatted(const char *mode, const char *formatString, ...);
--- a/src/Helpers/Strdup.cpp
+++ /dev/null
@@ -1,20 +1,0 @@
-#include <assert.h>
-#if !(_XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200809L)
-
-#include "Strdup.h"
-#include "../Attributes.h"
-#include <string.h>
-#include <stdlib.h>
-
-ATTRIBUTE_MALLOC ATTRIBUTE_NONNULL((1)) char *strdup(const char *duplicated_string)
-{
- size_t duplicatedStringLength = strlen(duplicated_string) + sizeof(char); // Length of the whole string, plus the terminator
- char *returnedString = (char *)malloc(duplicatedStringLength); // Memory for the new string is obtained with malloc. malloc also sets errno to ENOMEM on failure, which is exactly what we want for conformance
-
- if (returnedString) // If the result of malloc was NULL, allocation failed copying into it would be UB
- memcpy(returnedString, duplicated_string, duplicatedStringLength); // Copy the entire duplicated string (including the null terminator)
-
- return returnedString; // Returns a pointer to the duplicated string on success, NULL if insufficient memory was available
-}
-
-#endif
--- a/src/Helpers/Strdup.h
+++ /dev/null
@@ -1,16 +1,0 @@
-#pragma once
-#include <assert.h> // Probably the smallest header that will include <features.h> on systems with it
-
-// If <string.h> defines strdup for us, use its definition
-#if (_XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200809L)
-
-#include <string.h>
-
-#else
-
-#include "../Attributes.h"
-#define strdup Portable_strdup
-
-ATTRIBUTE_MALLOC ATTRIBUTE_NONNULL(1) char *strdup(const char *duplicatedString);
-
-#endif
--- a/src/Helpers/Vasprintf.cpp
+++ /dev/null
@@ -1,32 +1,0 @@
-#ifndef _GNU_SOURCE
-
-#include "Vasprintf.h"
-#include "../Attributes.h"
-#include <stdarg.h>
-#include <stddef.h>
-#include <stdlib.h>
-#include <stdio.h>
-
-ATTRIBUTE_FORMAT(printf, 2, 0) ATTRIBUTE_WARN_UNUSED_RESULT int vasprintf(char **result_string, const char *format_string, va_list arguments)
-{
- const int error_return_value = -1; // According to the man page, this is returned on error
- va_list arguments_local;
- va_copy(arguments_local, arguments); // Copy the arguments so we can actually use them
-
- int size = vsnprintf(NULL, 0, format_string, arguments_local); // Get the amount of bytes we need for the output buffer
-
- va_end(arguments_local); // Destroy arguments_local
-
- if (size < 0) // If an output error is encountered, vsnprintf returns a negative value
- return error_return_value;
-
- *result_string = (char *)malloc(size + 1); // Allocate enough space for the string (need + 1 for the null terminator)
-
- if (*result_string == NULL) // On error, malloc returns NULL
- return error_return_value;
-
- // We know we have enough space, so we can use vsprintf safely
- return vsprintf(*result_string, format_string, arguments); // vsprintf returns the amount of characters that got printed. This is what we're supposed to return
-}
-
-#endif
--- a/src/Helpers/Vasprintf.h
+++ /dev/null
@@ -1,16 +1,0 @@
-#pragma once
-
-// If <stdio.h> defines asprintf for us, use its definition
-#ifdef _GNU_SOURCE
-
-#include <stdio.h>
-
-#else
-
-#include "../Attributes.h"
-#include <stdarg.h>
-#define vasprintf Portable_vasprintf
-
-ATTRIBUTE_FORMAT(printf, 2, 0) ATTRIBUTE_WARN_UNUSED_RESULT int vasprintf(char **resultString, const char *formatString, va_list arguments);
-
-#endif
--- a/src/Main.cpp
+++ b/src/Main.cpp
@@ -24,11 +24,9 @@
#include "Resource.h"
#include "Sound.h"
#include "Triangle.h"
-#include "Helpers/Asprintf.h"
-#include "Helpers/Strdup.h"
-char *gModulePath;
-char *gDataPath;
+std::string gModulePath;
+std::string gDataPath;
BOOL bFullscreen;
BOOL gbUseJoystick = FALSE;
@@ -84,24 +82,6 @@
}
}
-static int main_out_backend_deinit(int return_value)
-{
- Backend_Deinit();
- return return_value;
-}
-
-static int main_out_free_module_path(int return_value)
-{
- free(gModulePath);
- return main_out_backend_deinit(return_value);
-}
-
-static int main_out_free_data_path(int return_value)
-{
- free(gDataPath);
- return main_out_free_module_path(return_value);
-}
-
// TODO - Inaccurate stack frame
int main(int argc, char *argv[])
{
@@ -116,18 +96,13 @@
if (!Backend_GetBasePath(&gModulePath))
{
// Fall back on argv[0] if the backend cannot provide a path
- gModulePath = strdup(argv[0]);
- if (!gModulePath)
- {
- Backend_PrintError("Could not get executable path (not enough memory)");
- return main_out_backend_deinit(EXIT_FAILURE);
- }
+ gModulePath = argv[0];
- for (size_t i = strlen(gModulePath);; --i)
+ for (size_t i = gModulePath.length();; --i)
{
if (i == 0 || gModulePath[i] == '\\' || gModulePath[i] == '/')
{
- gModulePath[i] = '\0';
+ gModulePath.resize(i);
break;
}
}
@@ -134,8 +109,7 @@
}
// Get path of the data folder
- if (asprintf(&gDataPath, "%s/data", gModulePath) < 0)
- return main_out_free_module_path(EXIT_FAILURE);
+ gDataPath = gModulePath + "/data";
CONFIG conf;
if (!LoadConfigData(&conf))
@@ -248,12 +222,12 @@
if (conf.display_mode == 1)
{
if (!StartDirectDraw(lpWindowName, windowWidth, windowHeight, 0))
- return main_out_free_data_path(EXIT_FAILURE);
+ return EXIT_FAILURE;
}
else
{
if (!StartDirectDraw(lpWindowName, windowWidth, windowHeight, 1))
- return main_out_free_data_path(EXIT_FAILURE);
+ return EXIT_FAILURE;
}
#else
// Doesn't handle StartDirectDraw failing
@@ -274,7 +248,7 @@
#ifdef FIX_BUGS
if (!StartDirectDraw(lpWindowName, windowWidth, windowHeight, 2))
- return main_out_free_data_path(EXIT_FAILURE);
+ return EXIT_FAILURE;
#else
// Doesn't handle StartDirectDraw failing
StartDirectDraw(lpWindowName, windowWidth, windowHeight, 2);
@@ -342,7 +316,7 @@
// Draw to screen
if (!Flip_SystemTask())
- return main_out_free_data_path(EXIT_SUCCESS);
+ return EXIT_SUCCESS;
// Initialize sound
InitDirectSound();
@@ -366,7 +340,7 @@
EndDirectSound();
EndDirectDraw();
- return main_out_free_data_path(EXIT_SUCCESS);
+ return EXIT_SUCCESS;
}
void InactiveWindow(void)
--- a/src/Main.h
+++ b/src/Main.h
@@ -1,9 +1,10 @@
#pragma once
#include "WindowsWrapper.h"
+#include <string>
-extern char *gModulePath;
-extern char *gDataPath;
+extern std::string gModulePath;
+extern std::string gDataPath;
extern BOOL bFullscreen;
extern BOOL gbUseJoystick;
--- a/src/Map.cpp
+++ b/src/Map.cpp
@@ -12,7 +12,6 @@
#include "File.h"
#include "Main.h"
#include "NpChar.h"
-#include "Helpers/FopenFormatted.h"
#define PXM_BUFFER_SIZE 0x4B000
@@ -28,11 +27,10 @@
BOOL LoadMapData2(const char *path_map)
{
- FILE *fp;
char check[3];
// Open file
- fp = fopenFormatted("rb", "%s/%s", gDataPath, path_map);
+ FILE *fp = fopen((gDataPath + '/' + path_map).c_str(), "rb");
if (fp == NULL)
return FALSE;
@@ -65,10 +63,8 @@
BOOL LoadAttributeData(const char *path_atrb)
{
- FILE *fp;
-
// Open file
- fp = fopenFormatted("rb", "%s/%s", gDataPath, path_atrb);
+ FILE *fp = fopen((gDataPath + '/' + path_atrb).c_str(), "rb");
if (fp == NULL)
return FALSE;
--- a/src/MycParam.cpp
+++ b/src/MycParam.cpp
@@ -16,8 +16,6 @@
#include "Sound.h"
#include "TextScr.h"
#include "ValueView.h"
-#include "Helpers/Asprintf.h"
-#include "Helpers/FopenFormatted.h"
ARMS_LEVEL gArmsLevelTable[14] =
{
@@ -437,8 +435,6 @@
int i;
unsigned char p[4];
REC rec;
- FILE *fp;
- char *path;
// Quit if player doesn't have the Nikumaru Counter
if (!(gMC.equip & EQUIP_NIKUMARU_COUNTER))
@@ -445,10 +441,9 @@
return TRUE;
// Get last time
- if (asprintf(&path, "%s/290.rec", gModulePath) < 0)
- return FALSE;
+ std::string path = gModulePath + "/290.rec";
- fp = fopen(path, "rb");
+ FILE *fp = fopen(path.c_str(), "rb");
if (fp != NULL)
{
// Read data
@@ -488,7 +483,7 @@
rec.counter[i] = p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
}
- fp = fopen(path, "wb");
+ fp = fopen(path.c_str(), "wb");
if (fp == NULL)
return FALSE;
@@ -510,10 +505,9 @@
int i;
unsigned char p[4];
REC rec;
- FILE *fp;
// Open file
- fp = fopenFormatted("rb", "%s/290.rec", gModulePath);
+ FILE *fp = fopen((gModulePath + "/290.rec").c_str(), "rb");
if (fp == NULL)
return 0;
--- a/src/NpChar.cpp
+++ b/src/NpChar.cpp
@@ -17,7 +17,6 @@
#include "NpcTbl.h"
#include "Sound.h"
#include "ValueView.h"
-#include "Helpers/FopenFormatted.h"
NPCHAR gNPC[NPC_MAX];
int gCurlyShoot_wait;
@@ -55,12 +54,11 @@
BOOL LoadEvent(const char *path_event)
{
int i, n;
- FILE *fp;
int count;
char code[4];
EVENT eve;
- fp = fopenFormatted("rb", "%s/%s", gDataPath, path_event);
+ FILE *fp = fopen((gDataPath + '/' + path_event).c_str(), "rb");
if (fp == NULL)
return FALSE;
--- a/src/Profile.cpp
+++ b/src/Profile.cpp
@@ -22,7 +22,6 @@
#include "Stage.h"
#include "Star.h"
#include "ValueView.h"
-#include "Helpers/FopenFormatted.h"
const char* const gDefaultName = "Profile.dat";
const char* const gProfileCode = "Do041220";
@@ -29,7 +28,7 @@
BOOL IsProfile(void)
{
- FILE *file = fopenFormatted("rb", "%s/%s", gModulePath, gDefaultName);
+ FILE *file = fopen((gModulePath + '/' + gDefaultName).c_str(), "rb");
if (file == NULL)
return FALSE;
@@ -42,7 +41,7 @@
const char *FLAG = "FLAG";
// Open file
- FILE *fp = fopenFormatted("wb", "%s/%s", gModulePath, ((name != NULL) ? name : gDefaultName));
+ FILE *fp = fopen((gModulePath + '/' + ((name != NULL) ? name : gDefaultName)).c_str(), "wb");
if (fp == NULL)
return FALSE;
@@ -111,14 +110,14 @@
BOOL LoadProfile(const char *name)
{
- FILE *fp;
PROFILE profile;
// Open file
+ FILE *fp;
if (name != NULL)
- fp = fopenFormatted("rb", "%s", name);
+ fp = fopen(name, "rb");
else
- fp = fopenFormatted("rb", "%s/%s", gModulePath, gDefaultName);
+ fp = fopen((gModulePath + '/' + gDefaultName).c_str(), "rb");
if (fp == NULL)
return FALSE;
--- a/src/SelStage.cpp
+++ b/src/SelStage.cpp
@@ -161,9 +161,7 @@
gSelectedStage = 0;
BackupSurface(SURFACE_ID_SCREEN_GRAB, &grcFull);
- char *old_script_path = GetTextScriptPath();
- if (!old_script_path)
- return enum_ESCRETURN_exit;
+ std::string old_script_path = GetTextScriptPath();
LoadTextScript2("StageSelect.tsc");
gStageSelectTitleY = (WINDOW_HEIGHT / 2) - 66;
StartTextScript(gPermitStage[gSelectedStage].index + 1000);
@@ -177,11 +175,9 @@
switch (Call_Escape())
{
case enum_ESCRETURN_exit:
- free(old_script_path);
return enum_ESCRETURN_exit;
case enum_ESCRETURN_restart:
- free(old_script_path);
return enum_ESCRETURN_restart;
}
}
@@ -191,11 +187,9 @@
switch (TextScriptProc())
{
case enum_ESCRETURN_exit:
- free(old_script_path);
return enum_ESCRETURN_exit;
case enum_ESCRETURN_restart:
- free(old_script_path);
return enum_ESCRETURN_restart;
}
@@ -216,8 +210,7 @@
else if (gKeyTrg & gKeyCancel)
{
StopTextScript();
- LoadTextScript_Stage(old_script_path);
- free(old_script_path);
+ LoadTextScript_Stage(old_script_path.c_str());
*p_event = 0;
return enum_ESCRETURN_continue;
}
@@ -225,14 +218,10 @@
PutFramePerSecound();
if (!Flip_SystemTask())
- {
- free(old_script_path);
return enum_ESCRETURN_exit;
- }
}
- LoadTextScript_Stage(old_script_path);
- free(old_script_path);
+ LoadTextScript_Stage(old_script_path.c_str());
*p_event = gPermitStage[gSelectedStage].event;
return enum_ESCRETURN_continue;
}
--- a/src/Stage.cpp
+++ b/src/Stage.cpp
@@ -138,56 +138,41 @@
bError = FALSE;
- // Get path
- char path_dir[sizeof("Stage")] = "Stage"; // Can only be either "Stage" or "Npc", and sizeof("Stage") > sizeof("Npc")
- /*
- * The size of path should be size of the format string (without the operands) + size of first operand + size of second operand - 2 (no null terminator needed for the first two operands, and sizeof will include the size for the null terminators here)
- * The size of the format string is sizeof("/.pxa") because that's the biggest raw format (omitting the operands) used here
- * Size of first operand is sizeof(path_dir) because that's always the first operand
- * Size of second operand is sizeof(gTMT[no].parts) because all the operands used here (gTMT[no].parts, gTMT[no].map, gTMT[no].npc, gTMT[no].boss) are of the same size
- * sprintf(path, "%s", gTMT[no].back) is irrelevant here because sizeof(gTMT[no].back) is the same as the size of all the operands discussed for the size of the second operand, so gTMT[no].back always fits into the size allocated for the second operand alone
- */
- char path[sizeof("/.pxa") + sizeof(path_dir) + sizeof(gTMT[no].parts) - 2];
-
// Load tileset
- sprintf(path, "%s/Prt%s", path_dir, gTMT[no].parts);
- if (!ReloadBitmap_File(path, SURFACE_ID_LEVEL_TILESET))
+ std::string path = std::string{"Stage/Prt"} + gTMT[no].parts;
+ if (!ReloadBitmap_File(path.c_str(), SURFACE_ID_LEVEL_TILESET))
bError = TRUE;
- sprintf(path, "%s/%s.pxa", path_dir, gTMT[no].parts);
- if (!LoadAttributeData(path))
+ path = std::string{"Stage/"} + gTMT[no].parts + ".pxa";
+ if (!LoadAttributeData(path.c_str()))
bError = TRUE;
// Load tilemap
- sprintf(path, "%s/%s.pxm", path_dir, gTMT[no].map);
- if (!LoadMapData2(path))
+ path = std::string{"Stage/"} + gTMT[no].map + ".pxm";
+ if (!LoadMapData2(path.c_str()))
bError = TRUE;
// Load NPCs
- sprintf(path, "%s/%s.pxe", path_dir, gTMT[no].map);
- if (!LoadEvent(path))
+ path = std::string{"Stage/"} + gTMT[no].map + ".pxe";
+ if (!LoadEvent(path.c_str()))
bError = TRUE;
// Load script
- sprintf(path, "%s/%s.tsc", path_dir, gTMT[no].map);
- if (!LoadTextScript_Stage(path))
+ path = std::string{"Stage/"} + gTMT[no].map + ".tsc";
+ if (!LoadTextScript_Stage(path.c_str()))
bError = TRUE;
// Load background
- sprintf(path, "%s", gTMT[no].back);
- if (!InitBack(path, gTMT[no].bkType))
+ if (!InitBack(gTMT[no].back, gTMT[no].bkType))
bError = TRUE;
- // Get path
- strcpy(path_dir, "Npc");
-
// Load NPC sprite sheets
- sprintf(path, "%s/Npc%s", path_dir, gTMT[no].npc);
- if (!ReloadBitmap_File(path, SURFACE_ID_LEVEL_SPRITESET_1))
+ path = std::string{"Npc/Npc"} + gTMT[no].npc;
+ if (!ReloadBitmap_File(path.c_str(), SURFACE_ID_LEVEL_SPRITESET_1))
bError = TRUE;
- sprintf(path, "%s/Npc%s", path_dir, gTMT[no].boss);
- if (!ReloadBitmap_File(path, SURFACE_ID_LEVEL_SPRITESET_2))
+ path = std::string{"Npc/Npc"} + gTMT[no].boss;
+ if (!ReloadBitmap_File(path.c_str(), SURFACE_ID_LEVEL_SPRITESET_2))
bError = TRUE;
if (bError)
--- a/src/TextScr.cpp
+++ b/src/TextScr.cpp
@@ -33,8 +33,6 @@
#include "SelStage.h"
#include "Sound.h"
#include "Stage.h"
-#include "Helpers/Asprintf.h"
-#include "Helpers/Strdup.h"
// This limits the size of a .tsc script to 0x5000 bytes (the game will crash above this)
#define TSC_BUFFER_SIZE 0x5000
@@ -126,22 +124,16 @@
BOOL LoadTextScript2(const char *name)
{
FILE *fp;
- char *path;
// Get path
- if (asprintf(&path, "%s/%s", gDataPath, name) < 0)
- return FALSE;
+ std::string path = gDataPath + '/' + name;
- gTS.size = GetFileSizeLong(path);
+ gTS.size = GetFileSizeLong(path.c_str());
if (gTS.size == -1)
- {
- free(path);
return FALSE;
- }
// Open file
- fp = fopen(path, "rb");
- free(path);
+ fp = fopen(path.c_str(), "rb");
if (fp == NULL)
return FALSE;
@@ -151,9 +143,7 @@
fclose(fp);
// Set path
- if (gTS.path != NULL)
- free(gTS.path);
- gTS.path = strdup(name);
+ gTS.path = name;
// Decrypt data
EncryptionBinaryData2((unsigned char*)gTS.data, gTS.size);
@@ -169,19 +159,12 @@
long body_size;
// Open Head.tsc
- char *path;
- if (asprintf(&path, "%s/%s", gDataPath, "Head.tsc") < 0)
- return FALSE;
-
- head_size = GetFileSizeLong(path);
+ std::string path = gDataPath + "/Head.tsc";
+ head_size = GetFileSizeLong(path.c_str());
if (head_size == -1)
- {
- free(path);
return FALSE;
- }
- fp = fopen(path, "rb");
- free(path);
+ fp = fopen(path.c_str(), "rb");
if (fp == NULL)
return FALSE;
@@ -192,18 +175,12 @@
fclose(fp);
// Open stage's .tsc
- if (asprintf(&path, "%s/%s", gDataPath, name) < 0)
- return FALSE;
-
- body_size = GetFileSizeLong(path);
+ path = gDataPath + '/' + name;
+ body_size = GetFileSizeLong(path.c_str());
if (body_size == -1)
- {
- free(path);
return FALSE;
- }
- fp = fopen(path, "rb");
- free(path);
+ fp = fopen(path.c_str(), "rb");
if (fp == NULL)
return FALSE;
@@ -215,17 +192,15 @@
// Set parameters
gTS.size = head_size + body_size;
- if (gTS.path != NULL)
- free(gTS.path);
- gTS.path = strdup(name);
+ gTS.path = name;
return TRUE;
}
// Get current path
-char *GetTextScriptPath()
+std::string GetTextScriptPath()
{
- return strdup(gTS.path);
+ return gTS.path;
}
// Get 4 digit number from TSC data
--- a/src/TextScr.h
+++ b/src/TextScr.h
@@ -1,11 +1,12 @@
#pragma once
#include "WindowsWrapper.h"
+#include <string>
typedef struct TEXT_SCRIPT
{
// Path (reload when exit teleporter menu/inventory)
- char *path;
+ std::string path;
// Script buffer
long size;
@@ -62,7 +63,7 @@
void EncryptionBinaryData2(unsigned char *pData, long size);
BOOL LoadTextScript2(const char *name);
BOOL LoadTextScript_Stage(const char *name);
-char *GetTextScriptPath();
+std::string GetTextScriptPath();
BOOL StartTextScript(int no);
void StopTextScript(void);
void PutTextScript(void);