1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-21 14:02:59 +01:00

Refactor/Improve String and Path Handling

This commit is contained in:
LRFLEW
2016-09-25 21:24:29 -05:00
parent 211a0d7863
commit e5ff7412e4
94 changed files with 1038 additions and 989 deletions

View File

@@ -644,7 +644,7 @@ namespace ThemeManager
void GetThemePath(utf8 * buffer, size_t bufferSize)
{
platform_get_user_directory(buffer, "themes");
platform_get_user_directory(buffer, "themes", bufferSize);
}
}

View File

@@ -102,7 +102,7 @@ void chat_draw(rct_drawpixelinfo * dpi)
continue;
}
safe_strcpy(lineBuffer, chat_history_get(i), CHAT_INPUT_SIZE + 10);
safe_strcpy(lineBuffer, chat_history_get(i), sizeof(lineBuffer));
int lineHeight = chat_string_wrapped_get_height((void*)&lineCh, _chatWidth - 10);
_chatTop -= (lineHeight + 5);
@@ -134,7 +134,7 @@ void chat_draw(rct_drawpixelinfo * dpi)
break;
}
safe_strcpy(lineBuffer, chat_history_get(i), CHAT_INPUT_SIZE + 10);
safe_strcpy(lineBuffer, chat_history_get(i), sizeof(lineBuffer));
stringHeight = chat_history_draw_string(dpi, (void*) &lineCh, x, y, _chatWidth - 10) + 5;
gfx_set_dirty_blocks(x, y - stringHeight, x + _chatWidth, y + 20);
@@ -220,7 +220,7 @@ int chat_history_draw_string(rct_drawpixelinfo *dpi, void *args, int x, int y, i
gfx_draw_string(dpi, "", 255, dpi->x, dpi->y);
char *buffer = gCommonStringFormatBuffer;
format_string(buffer, STR_STRING, args);
format_string(buffer, 256, STR_STRING, args);
gCurrentFontSpriteBase = FONT_SPRITE_BASE_MEDIUM;
gfx_wrap_string(buffer, width, &numLines, &fontSpriteBase);
@@ -251,7 +251,7 @@ int chat_string_wrapped_get_height(void *args, int width)
gCurrentFontSpriteBase = FONT_SPRITE_BASE_MEDIUM;
char *buffer = gCommonStringFormatBuffer;
format_string(buffer, STR_STRING, args);
format_string(buffer, 256, STR_STRING, args);
gCurrentFontSpriteBase = FONT_SPRITE_BASE_MEDIUM;
gfx_wrap_string(buffer, width, &numLines, &fontSpriteBase);

View File

@@ -205,7 +205,7 @@ void console_draw(rct_drawpixelinfo *dpi)
size_t lineLength = min(sizeof(lineBuffer) - (size_t)utf8_get_codepoint_length(FORMAT_WHITE), (size_t)(nextLine - ch));
lineCh = lineBuffer;
lineCh = utf8_write_codepoint(lineCh, FORMAT_WHITE);
strncpy(lineCh, ch, lineLength);
memcpy(lineCh, ch, lineLength);
lineCh[lineLength] = 0;
gfx_draw_string(dpi, lineBuffer, 100, x, y); //Value 100 outlines the letters
@@ -226,7 +226,7 @@ void console_draw(rct_drawpixelinfo *dpi)
// Draw current line
lineCh = lineBuffer;
lineCh = utf8_write_codepoint(lineCh, FORMAT_WHITE);
strcpy(lineCh, _consoleCurrentLine);
safe_strcpy(lineCh, _consoleCurrentLine, sizeof(lineBuffer) - (lineCh - lineBuffer));
gfx_draw_string(dpi, lineBuffer, 255, x, y);
// Draw caret
@@ -301,8 +301,9 @@ void console_write(const utf8 *src)
if (charactersToWrite > charactersRemainingInBuffer) {
memmove(_consoleBuffer, _consoleBuffer + bufferShift, CONSOLE_BUFFER_SIZE - bufferShift);
_consoleBufferPointer -= bufferShift;
charactersRemainingInBuffer = CONSOLE_BUFFER_SIZE - (_consoleBufferPointer - _consoleBuffer) - 1;
}
strcpy(_consoleBufferPointer, src);
safe_strcpy(_consoleBufferPointer, src, charactersRemainingInBuffer);
_consoleBufferPointer += charactersToWrite;
console_update_scroll();
}
@@ -315,14 +316,14 @@ void console_writeline(const utf8 *src)
void console_writeline_error(const utf8 *src)
{
strcpy(_consoleErrorBuffer + 1, src);
safe_strcpy(_consoleErrorBuffer + 1, src, CONSOLE_BUFFER_2_SIZE - 1);
_consoleErrorBuffer[0] = FORMAT_RED;
console_writeline(_consoleErrorBuffer);
}
void console_writeline_warning(const utf8 *src)
{
strcpy(_consoleErrorBuffer + 1, src);
safe_strcpy(_consoleErrorBuffer + 1, src, CONSOLE_BUFFER_2_SIZE - 1);
_consoleErrorBuffer[0] = FORMAT_YELLOW;
console_writeline(_consoleErrorBuffer);
}
@@ -331,7 +332,7 @@ void console_printf(const utf8 *format, ...)
{
va_list list;
va_start(list, format);
vsprintf(_consolePrintfBuffer, format, list);
vsnprintf(_consolePrintfBuffer, sizeof(_consolePrintfBuffer), format, list);
va_end(list);
console_writeline(_consolePrintfBuffer);
}
@@ -449,7 +450,7 @@ static int cc_rides(const utf8 **argv, int argc)
int i;
FOR_ALL_RIDES(i, ride) {
char name[128];
format_string(name, ride->name, &ride->name_arguments);
format_string(name, 128, ride->name, &ride->name_arguments);
console_printf("rides %03d type: %02u subtype %03u name %s", i, ride->type, ride->subtype, name);
}
} else if (strcmp(argv[0], "set") == 0) {
@@ -515,7 +516,7 @@ static int cc_staff(const utf8 **argv, int argc)
int i;
FOR_ALL_STAFF(i, peep) {
char name[128];
format_string(name, peep->name_string_idx, &peep->id);
format_string(name, 128, peep->name_string_idx, &peep->id);
console_printf("staff id %03d type: %02u energy %03u name %s", i, peep->staff_type, peep->energy, name);
}
} else if (strcmp(argv[0], "set") == 0) {
@@ -1178,7 +1179,7 @@ void console_execute_silent(const utf8 *src)
utf8 output[128];
utf8 *dst = output;
dst = utf8_write_codepoint(dst, FORMAT_TOPAZ);
strcpy(dst, "Unknown command. Type help to list available commands.");
safe_strcpy(dst, "Unknown command. Type help to list available commands.", sizeof(output) - (dst - output));
console_writeline(output);
}
}

View File

@@ -26,6 +26,7 @@
#include "../platform/platform.h"
#include "../ride/track_paint.h"
#include "../title.h"
#include "../util/util.h"
#include "keyboard_shortcut.h"
#include "viewport.h"
#include "widget.h"
@@ -92,33 +93,34 @@ void keyboard_shortcut_handle_command(int shortcutIndex)
}
}
void keyboard_shortcut_format_string(char *buffer, uint16 shortcutKey)
void keyboard_shortcut_format_string(char *buffer, size_t size, uint16 shortcutKey)
{
char formatBuffer[256];
if (size == 0) return;
*buffer = 0;
if (shortcutKey == SHORTCUT_UNDEFINED) return;
if (shortcutKey & 0x100) {
format_string(formatBuffer, STR_SHIFT_PLUS, NULL);
strcat(buffer, formatBuffer);
format_string(formatBuffer, 256, STR_SHIFT_PLUS, NULL);
safe_strcat(buffer, formatBuffer, size);
}
if (shortcutKey & 0x200) {
format_string(formatBuffer, STR_CTRL_PLUS, NULL);
strcat(buffer, formatBuffer);
format_string(formatBuffer, 256, STR_CTRL_PLUS, NULL);
safe_strcat(buffer, formatBuffer, size);
}
if (shortcutKey & 0x400) {
#ifdef __MACOSX__
format_string(formatBuffer, STR_OPTION_PLUS, NULL);
format_string(formatBuffer, 256, STR_OPTION_PLUS, NULL);
#else
format_string(formatBuffer, STR_ALT_PLUS, NULL);
format_string(formatBuffer, 256, STR_ALT_PLUS, NULL);
#endif
strcat(buffer, formatBuffer);
safe_strcat(buffer, formatBuffer, size);
}
if (shortcutKey & 0x800) {
format_string(formatBuffer, STR_CMD_PLUS, NULL);
strcat(buffer, formatBuffer);
format_string(formatBuffer, 256, STR_CMD_PLUS, NULL);
safe_strcat(buffer, formatBuffer, size);
}
strcat(buffer, SDL_GetScancodeName(shortcutKey & 0xFF));
safe_strcat(buffer, SDL_GetScancodeName(shortcutKey & 0xFF), size);
}
#pragma region Shortcut Commands

View File

@@ -25,6 +25,6 @@ extern uint8 gKeyboardShortcutChangeId;
void keyboard_shortcut_set(int key);
void keyboard_shortcut_handle(int key);
void keyboard_shortcut_handle_command(int shortcutIndex);
void keyboard_shortcut_format_string(char *buffer, uint16 shortcutKey);
void keyboard_shortcut_format_string(char *buffer, size_t size, uint16 shortcutKey);
#endif

View File

@@ -67,18 +67,18 @@ static void screenshot_get_rendered_palette(rct_palette* palette) {
}
}
static int screenshot_get_next_path(char *path)
static int screenshot_get_next_path(char *path, size_t size)
{
char screenshotPath[MAX_PATH];
platform_get_user_directory(screenshotPath, "screenshot");
platform_get_user_directory(screenshotPath, "screenshot", sizeof(screenshotPath));
if (!platform_ensure_directory_exists(screenshotPath)) {
log_error("Unable to save screenshots in OpenRCT2 screenshot directory.\n");
return -1;
}
char park_name[128] = { 0 };
format_string(park_name, gParkName, &gParkNameArgs);
char park_name[128];
format_string(park_name, 128, gParkName, &gParkNameArgs);
// retrieve current time
rct2_date currentDate;
@@ -87,7 +87,7 @@ static int screenshot_get_next_path(char *path)
platform_get_time_local(&currentTime);
// Glue together path and filename
sprintf(path, "%s%s %d-%02d-%02d %02d-%02d-%02d.png", screenshotPath, park_name, currentDate.year, currentDate.month, currentDate.day, currentTime.hour, currentTime.minute, currentTime.second);
snprintf(path, size, "%s%s %d-%02d-%02d %02d-%02d-%02d.png", screenshotPath, park_name, currentDate.year, currentDate.month, currentDate.day, currentTime.hour, currentTime.minute, currentTime.second);
if (!platform_file_exists(path)) {
return 0; // path ok
@@ -102,7 +102,7 @@ static int screenshot_get_next_path(char *path)
int i;
for (i = 1; i < 1000; i++) {
// Glue together path and filename
sprintf(path, "%s%s %d-%02d-%02d %02d-%02d-%02d (%d).png", screenshotPath, park_name, currentDate.year, currentDate.month, currentDate.day, currentTime.hour, currentTime.minute, currentTime.second, i);
snprintf(path, size, "%s%s %d-%02d-%02d %02d-%02d-%02d (%d).png", screenshotPath, park_name, currentDate.year, currentDate.month, currentDate.day, currentTime.hour, currentTime.minute, currentTime.second, i);
if (!platform_file_exists(path)) {
return i;
@@ -118,7 +118,7 @@ int screenshot_dump_png(rct_drawpixelinfo *dpi)
// Get a free screenshot path
int index;
char path[MAX_PATH] = "";
if ((index = screenshot_get_next_path(path)) == -1) {
if ((index = screenshot_get_next_path(path, MAX_PATH)) == -1) {
return -1;
}
@@ -137,7 +137,7 @@ int screenshot_dump_png_32bpp(sint32 width, sint32 height, const void *pixels)
// Get a free screenshot path
int index;
char path[MAX_PATH] = "";
if ((index = screenshot_get_next_path(path)) == -1) {
if ((index = screenshot_get_next_path(path, MAX_PATH)) == -1) {
return -1;
}
@@ -222,7 +222,7 @@ void screenshot_giant()
// Get a free screenshot path
char path[MAX_PATH];
int index;
if ((index = screenshot_get_next_path(path)) == -1) {
if ((index = screenshot_get_next_path(path, MAX_PATH)) == -1) {
log_error("Giant screenshot failed, unable to find a suitable destination path.");
window_error_open(STR_SCREENSHOT_FAILED, STR_NONE);
return;
@@ -236,9 +236,8 @@ void screenshot_giant()
free(dpi.bits);
// Show user that screenshot saved successfully
rct_string_id stringId = STR_PLACEHOLDER;
strcpy((char*)language_get_string(stringId), path_get_filename(path));
set_format_arg(0, rct_string_id, stringId);
set_format_arg(0, rct_string_id, STR_STRING);
set_format_arg(2, char *, path_get_filename(path));
window_error_open(STR_SCREENSHOT_SAVED_AS, STR_NONE);
}

View File

@@ -42,10 +42,10 @@ bool title_sequence_name_exists(const char *name)
bool title_sequence_save_exists(int preset, const char *name)
{
utf8 newName[MAX_PATH];
char *extension = (char*)path_get_extension(name);
const char *extension = path_get_extension(name);
safe_strcpy(newName, name, MAX_PATH);
if (_stricmp(extension, ".sv6") != 0 && _stricmp(extension, ".sc6") != 0)
strcat(newName, ".sv6");
path_append_extension(newName, ".sv6", MAX_PATH);
for (int i = 0; i < gConfigTitleSequences.presets[preset].num_saves; i++) {
if (_stricmp(gConfigTitleSequences.presets[preset].saves[i], newName) == 0)
return true;
@@ -102,8 +102,8 @@ void title_sequence_create_preset(const char *name)
// Create the folder
utf8 path[MAX_PATH];
platform_get_user_directory(path, "title sequences");
strcat(path, gConfigTitleSequences.presets[preset].name);
platform_get_user_directory(path, "title sequences", sizeof(path));
safe_strcat_path(path, gConfigTitleSequences.presets[preset].name, MAX_PATH);
platform_file_delete(path);
platform_ensure_directory_exists(path);
@@ -141,28 +141,25 @@ void title_sequence_duplicate_preset(int duplicate, const char *name)
// Create the folder
utf8 path[MAX_PATH], srcPath[MAX_PATH];
platform_get_user_directory(path, "title sequences");
strcat(path, gConfigTitleSequences.presets[preset].name);
platform_get_user_directory(path, "title sequences", sizeof(path));
safe_strcat_path(path, gConfigTitleSequences.presets[preset].name, MAX_PATH);
platform_file_delete(path);
platform_ensure_directory_exists(path);
// Copy the saves
char separator = platform_get_path_separator();
for (int i = 0; i < gConfigTitleSequences.presets[preset].num_saves; i++) {
if (gConfigTitleSequences.presets[duplicate].path[0]) {
safe_strcpy(srcPath, gConfigTitleSequences.presets[duplicate].path, MAX_PATH);
strcat(srcPath, gConfigTitleSequences.presets[duplicate].saves[i]);
safe_strcat_path(srcPath, gConfigTitleSequences.presets[duplicate].saves[i], MAX_PATH);
}
else {
platform_get_user_directory(srcPath, "title sequences");
strcat(srcPath, gConfigTitleSequences.presets[duplicate].name);
strncat(srcPath, &separator, 1);
strcat(srcPath, gConfigTitleSequences.presets[duplicate].saves[i]);
platform_get_user_directory(srcPath, "title sequences", sizeof(srcPath));
safe_strcat_path(srcPath, gConfigTitleSequences.presets[duplicate].name, MAX_PATH);
safe_strcat_path(srcPath, gConfigTitleSequences.presets[duplicate].saves[i], MAX_PATH);
}
platform_get_user_directory(path, "title sequences");
strcat(path, gConfigTitleSequences.presets[preset].name);
strncat(path, &separator, 1);
strcat(path, gConfigTitleSequences.presets[preset].saves[i]);
platform_get_user_directory(path, "title sequences", sizeof(path));
safe_strcat_path(path, gConfigTitleSequences.presets[preset].name, MAX_PATH);
safe_strcat_path(path, gConfigTitleSequences.presets[preset].saves[i], MAX_PATH);
platform_file_copy(srcPath, path, false);
}
@@ -181,8 +178,8 @@ void title_sequence_delete_preset(int preset)
if (preset >= TITLE_SEQUENCE_DEFAULT_PRESETS && preset < gConfigTitleSequences.num_presets) {
// Delete the folder
utf8 path[MAX_PATH];
platform_get_user_directory(path, "title sequences");
strcat(path, gConfigTitleSequences.presets[preset].name);
platform_get_user_directory(path, "title sequences", sizeof(path));
safe_strcat_path(path, gConfigTitleSequences.presets[preset].name, MAX_PATH);
if (!platform_directory_delete(path)) {
log_error("Failed to delete directory: \"%s\"", path);
}
@@ -209,10 +206,10 @@ void title_sequence_rename_preset(int preset, const char *newName)
if (preset >= TITLE_SEQUENCE_DEFAULT_PRESETS && preset < gConfigTitleSequences.num_presets && filename_valid_characters(newName) && !title_sequence_name_exists(newName)) {
// Rename the folder
utf8 src[MAX_PATH], dest[MAX_PATH];
platform_get_user_directory(src, "title sequences");
platform_get_user_directory(dest, "title sequences");
strcat(src, gConfigTitleSequences.presets[preset].name);
strcat(dest, newName);
platform_get_user_directory(src, "title sequences", sizeof(src));
platform_get_user_directory(dest, "title sequences", sizeof(dest));
safe_strcat_path(src, gConfigTitleSequences.presets[preset].name, sizeof(src));
safe_strcat_path(dest, newName, sizeof(dest));
platform_file_move(src, dest);
safe_strcpy(gConfigTitleSequences.presets[preset].name, newName, TITLE_SEQUENCE_NAME_SIZE);
@@ -228,20 +225,18 @@ void title_sequence_rename_preset(int preset, const char *newName)
void title_sequence_add_save(int preset, const char *path, const char *newName)
{
utf8 newPath[MAX_PATH];
char *extension = (char*)path_get_extension(newName);
const char *extension = path_get_extension(newName);
safe_strcpy(newPath, newName, MAX_PATH);
if (_stricmp(extension, ".sv6") != 0 && _stricmp(extension, ".sc6") != 0)
strcat(newPath, ".sv6");
path_append_extension(newPath, ".sv6", MAX_PATH);
if (preset >= TITLE_SEQUENCE_DEFAULT_PRESETS && preset < gConfigTitleSequences.num_presets && filename_valid_characters(newPath) && !title_sequence_save_exists(preset, newPath) && platform_file_exists(path)) {
// Copy the save file
char separator = platform_get_path_separator();
platform_get_user_directory(newPath, "title sequences");
strcat(newPath, gConfigTitleSequences.presets[preset].name);
strncat(newPath, &separator, 1);
strcat(newPath, newName);
platform_get_user_directory(newPath, "title sequences", sizeof(newPath));
safe_strcat_path(newPath, gConfigTitleSequences.presets[preset].name, sizeof(newPath));
safe_strcat_path(newPath, newName, sizeof(newPath));
// Add the appropriate extension if needed
if (_stricmp(extension, ".sv6") != 0 && _stricmp(extension, ".sc6") != 0)
strcat(newPath, ".sv6");
path_append_extension(newPath, ".sv6", sizeof(newPath));
platform_file_copy(path, newPath, false);
gConfigTitleSequences.presets[preset].num_saves++;
@@ -250,7 +245,7 @@ void title_sequence_add_save(int preset, const char *path, const char *newName)
safe_strcpy(gConfigTitleSequences.presets[preset].saves[gConfigTitleSequences.presets[preset].num_saves - 1], newName, TITLE_SEQUENCE_MAX_SAVE_LENGTH);
// Add the appropriate extension if needed
if (_stricmp(extension, ".sv6") != 0 && _stricmp(extension, ".sc6") != 0)
strcat(gConfigTitleSequences.presets[preset].saves[gConfigTitleSequences.presets[preset].num_saves - 1], ".sv6");
path_append_extension(gConfigTitleSequences.presets[preset].saves[gConfigTitleSequences.presets[preset].num_saves - 1], ".sv6", TITLE_SEQUENCE_MAX_SAVE_LENGTH);
}
}
@@ -258,12 +253,10 @@ void title_sequence_remove_save(int preset, int index)
{
if (preset >= TITLE_SEQUENCE_DEFAULT_PRESETS && preset < gConfigTitleSequences.num_presets && index >= 0 && index < gConfigTitleSequences.presets[preset].num_saves) {
// Delete the save file
char separator = platform_get_path_separator();
utf8 path[MAX_PATH];
platform_get_user_directory(path, "title sequences");
strcat(path, gConfigTitleSequences.presets[preset].name);
strncat(path, &separator, 1);
strcat(path, gConfigTitleSequences.presets[preset].saves[index]);
platform_get_user_directory(path, "title sequences", sizeof(path));
safe_strcat_path(path, gConfigTitleSequences.presets[preset].name, sizeof(path));
safe_strcat_path(path, gConfigTitleSequences.presets[preset].saves[index], sizeof(path));
platform_file_delete(path);
// Remove all references to this save in the commands and decrement save indices
@@ -291,26 +284,23 @@ void title_sequence_rename_save(int preset, int index, const char *newName)
filename_valid_characters(newName) && !title_sequence_save_exists(preset, newName)) {
// Rename the save file
char separator = platform_get_path_separator();
utf8 src[MAX_PATH], dest[MAX_PATH];
platform_get_user_directory(src, "title sequences");
platform_get_user_directory(dest, "title sequences");
strcat(src, gConfigTitleSequences.presets[preset].name);
strcat(dest, gConfigTitleSequences.presets[preset].name);
strncat(src, &separator, 1);
strncat(dest, &separator, 1);
strcat(src, gConfigTitleSequences.presets[preset].saves[index]);
strcat(dest, newName);
platform_get_user_directory(src, "title sequences", sizeof(src));
platform_get_user_directory(dest, "title sequences", sizeof(dest));
safe_strcat_path(src, gConfigTitleSequences.presets[preset].name, sizeof(src));
safe_strcat_path(dest, gConfigTitleSequences.presets[preset].name, sizeof(dest));
safe_strcat_path(src, gConfigTitleSequences.presets[preset].saves[index], sizeof(src));
safe_strcat_path(dest, newName, sizeof(dest));
// Add the appropriate extension if needed
char *extension = (char*)path_get_extension(newName);
const char *extension = path_get_extension(newName);
if (_stricmp(extension, ".sv6") != 0 && _stricmp(extension, ".sc6") != 0)
strcat(dest, ".sv6");
path_append_extension(dest, ".sv6", sizeof(dest));
platform_file_move(src, dest);
safe_strcpy(gConfigTitleSequences.presets[preset].saves[index], newName, TITLE_SEQUENCE_MAX_SAVE_LENGTH);
// Add the appropriate extension if needed
if (_stricmp(extension, ".sv6") != 0 && _stricmp(extension, ".sc6") != 0)
strcat(gConfigTitleSequences.presets[preset].saves[index], ".sv6");
path_append_extension(gConfigTitleSequences.presets[preset].saves[index], ".sv6", TITLE_SEQUENCE_MAX_SAVE_LENGTH);
title_sequence_save_preset_script(preset);
}
}

View File

@@ -2470,7 +2470,7 @@ void window_start_textbox(rct_window *call_w, int call_widget, rct_string_id exi
// Enter in the the text input buffer any existing
// text.
if (existing_text != STR_NONE)
format_string(gTextBoxInput, existing_text, &existing_args);
format_string(gTextBoxInput, 512, existing_text, &existing_args);
// In order to prevent strings that exceed the maxLength
// from crashing the game.