From 37ea3e1d5833764cd29a75725d3ab9d6c8119061 Mon Sep 17 00:00:00 2001 From: Yamagi Burmeister Date: Mon, 5 Feb 2018 07:43:26 +0100 Subject: [PATCH] Introduce a wrapper Q_fopen() and replace fopen() with it. On Unix platforms unicode is implemented through UTF-8 which is transparent for applications. But on Windows a UTF-16 dialect is used which needs alteration at application side. This wrapper is another step to unicode support on Windows, now we can replace fopen() by a function that converts our internal UTF-8 pathes to Windows UTF-16 dialect. This is a noop for Unix platforms. The Windows build is broken, the compiler errors out in shared.h. This will be fixed in a later commit. Caveats: * fopen() calls in 3rd party code (std_* and unzip) are not replaced. This may become a problem. We need to check that. * In the Unix specific code fopen() isn't replaced since it's not necessayry. --- src/backends/generic/vid.c | 2 +- src/client/cl_console.c | 2 +- src/client/cl_download.c | 4 ++-- src/client/cl_keyboard.c | 4 ++-- src/client/cl_main.c | 4 ++-- src/common/clientserver.c | 4 ++-- src/common/cvar.c | 2 +- src/common/filesystem.c | 8 ++++---- src/common/frame.c | 2 +- src/common/header/shared.h | 10 ++++++++++ src/game/g_svcmds.c | 2 +- src/game/savegame/savegame.c | 8 ++++---- src/server/sv_cmd.c | 2 +- src/server/sv_init.c | 2 +- src/server/sv_save.c | 10 +++++----- 15 files changed, 38 insertions(+), 28 deletions(-) diff --git a/src/backends/generic/vid.c b/src/backends/generic/vid.c index 6b0fe386..21c04df9 100644 --- a/src/backends/generic/vid.c +++ b/src/backends/generic/vid.c @@ -284,7 +284,7 @@ void VID_WriteScreenshot( int width, int height, int comp, const void* data ) { FILE *f; Com_sprintf(checkname, sizeof(checkname), "%s/scrnshot/q2_%04d.%s", gameDir, i, supportedFormats[format]); - f = fopen(checkname, "rb"); + f = Q_fopen(checkname, "rb"); if (!f) { diff --git a/src/client/cl_console.c b/src/client/cl_console.c index 277e1d5a..c9bfabd2 100644 --- a/src/client/cl_console.c +++ b/src/client/cl_console.c @@ -165,7 +165,7 @@ Con_Dump_f(void) Com_Printf("Dumped console text to %s.\n", name); FS_CreatePath(name); - f = fopen(name, "w"); + f = Q_fopen(name, "w"); if (!f) { diff --git a/src/client/cl_download.c b/src/client/cl_download.c index e4891c70..9561f3f6 100644 --- a/src/client/cl_download.c +++ b/src/client/cl_download.c @@ -477,7 +477,7 @@ CL_CheckOrDownloadFile(char *filename) not opened yet */ CL_DownloadFileName(name, sizeof(name), cls.downloadtempname); - fp = fopen(name, "r+b"); + fp = Q_fopen(name, "r+b"); if (fp) { @@ -587,7 +587,7 @@ CL_ParseDownload(void) FS_CreatePath(name); - cls.download = fopen(name, "wb"); + cls.download = Q_fopen(name, "wb"); if (!cls.download) { diff --git a/src/client/cl_keyboard.c b/src/client/cl_keyboard.c index 3e612b3b..87ac5156 100644 --- a/src/client/cl_keyboard.c +++ b/src/client/cl_keyboard.c @@ -837,7 +837,7 @@ Key_WriteConsoleHistory() Com_sprintf(path, sizeof(path), "%sconsole_history.txt", Sys_GetHomeDir()); } - FILE* f = fopen(path, "w"); + FILE* f = Q_fopen(path, "w"); if(f==NULL) { @@ -884,7 +884,7 @@ Key_ReadConsoleHistory() Com_sprintf(path, sizeof(path), "%sconsole_history.txt", Sys_GetHomeDir()); } - FILE* f = fopen(path, "r"); + FILE* f = Q_fopen(path, "r"); if(f==NULL) { Com_DPrintf("Opening console history %s for reading failed!\n", path); diff --git a/src/client/cl_main.c b/src/client/cl_main.c index df2f5fcb..0cb3902e 100644 --- a/src/client/cl_main.c +++ b/src/client/cl_main.c @@ -178,7 +178,7 @@ CL_Record_f(void) Com_Printf("recording to %s.\n", name); FS_CreatePath(name); - cls.demofile = fopen(name, "wb"); + cls.demofile = Q_fopen(name, "wb"); if (!cls.demofile) { @@ -604,7 +604,7 @@ CL_WriteConfiguration(void) Com_sprintf(path, sizeof(path), "%s/config.cfg", FS_Gamedir()); - f = fopen(path, "w"); + f = Q_fopen(path, "w"); if (!f) { diff --git a/src/common/clientserver.c b/src/common/clientserver.c index d8389954..3309e195 100644 --- a/src/common/clientserver.c +++ b/src/common/clientserver.c @@ -144,12 +144,12 @@ Com_VPrintf(int print_level, const char *fmt, va_list argptr) if (logfile_active->value > 2) { - logfile = fopen(name, "a"); + logfile = Q_fopen(name, "a"); } else { - logfile = fopen(name, "w"); + logfile = Q_fopen(name, "w"); } } diff --git a/src/common/cvar.c b/src/common/cvar.c index f86cf018..9f124cdc 100644 --- a/src/common/cvar.c +++ b/src/common/cvar.c @@ -501,7 +501,7 @@ Cvar_WriteVariables(char *path) char buffer[1024]; FILE *f; - f = fopen(path, "a"); + f = Q_fopen(path, "a"); for (var = cvar_vars; var; var = var->next) { diff --git a/src/common/filesystem.c b/src/common/filesystem.c index 771c8686..52b6babb 100644 --- a/src/common/filesystem.c +++ b/src/common/filesystem.c @@ -389,7 +389,7 @@ FS_FOpenFile(const char *name, fileHandle_t *f, qboolean gamedir_only) { /* PAK */ file_from_pak = true; - handle->file = fopen(pack->name, "rb"); + handle->file = Q_fopen(pack->name, "rb"); if (handle->file) { @@ -428,12 +428,12 @@ FS_FOpenFile(const char *name, fileHandle_t *f, qboolean gamedir_only) /* Search in a directory tree. */ Com_sprintf(path, sizeof(path), "%s/%s", search->path, handle->name); - handle->file = fopen(path, "rb"); + handle->file = Q_fopen(path, "rb"); if (!handle->file) { Q_strlwr(path); - handle->file = fopen(path, "rb"); + handle->file = Q_fopen(path, "rb"); } if (!handle->file) @@ -668,7 +668,7 @@ FS_LoadPAK(const char *packPath) dpackheader_t header; /* PAK file header. */ dpackfile_t info[MAX_FILES_IN_PACK]; /* PAK info. */ - handle = fopen(packPath, "rb"); + handle = Q_fopen(packPath, "rb"); if (handle == NULL) { diff --git a/src/common/frame.c b/src/common/frame.c index 5f19a278..297c3bf8 100644 --- a/src/common/frame.c +++ b/src/common/frame.c @@ -342,7 +342,7 @@ Qcommon_Frame(int msec) log_stats_file = 0; } - log_stats_file = fopen("stats.log", "w"); + log_stats_file = Q_fopen("stats.log", "w"); if (log_stats_file) { diff --git a/src/common/header/shared.h b/src/common/header/shared.h index d12ab68b..8fa6b232 100644 --- a/src/common/header/shared.h +++ b/src/common/header/shared.h @@ -254,6 +254,16 @@ int Q_strlcat(char *dst, const char *src, int size); /* ============================================= */ +/* Unicode wrappers around fopen(). */ + +#ifdef _WIN32 +#error "Not implemented yet" +#else +#define Q_fopen(file, mode) fopen(file, mode) +#endif + +/* ============================================= */ + short BigShort(short l); short LittleShort(short l); int BigLong(int l); diff --git a/src/game/g_svcmds.c b/src/game/g_svcmds.c index 45d221cb..b39507d3 100644 --- a/src/game/g_svcmds.c +++ b/src/game/g_svcmds.c @@ -293,7 +293,7 @@ SVCmd_WriteIP_f(void) gi.cprintf(NULL, PRINT_HIGH, "Writing %s.\n", name); - f = fopen(name, "wb"); + f = Q_fopen(name, "wb"); if (!f) { diff --git a/src/game/savegame/savegame.c b/src/game/savegame/savegame.c index c9c2fc2c..e668db70 100644 --- a/src/game/savegame/savegame.c +++ b/src/game/savegame/savegame.c @@ -772,7 +772,7 @@ WriteGame(const char *filename, qboolean autosave) SaveClientData(); } - f = fopen(filename, "wb"); + f = Q_fopen(filename, "wb"); if (!f) { @@ -824,7 +824,7 @@ ReadGame(const char *filename) gi.FreeTags(TAG_GAME); - f = fopen(filename, "rb"); + f = Q_fopen(filename, "rb"); if (!f) { @@ -980,7 +980,7 @@ WriteLevel(const char *filename) edict_t *ent; FILE *f; - f = fopen(filename, "wb"); + f = Q_fopen(filename, "wb"); if (!f) { @@ -1071,7 +1071,7 @@ ReadLevel(const char *filename) int i; edict_t *ent; - f = fopen(filename, "rb"); + f = Q_fopen(filename, "rb"); if (!f) { diff --git a/src/server/sv_cmd.c b/src/server/sv_cmd.c index dd62499c..38a5750e 100644 --- a/src/server/sv_cmd.c +++ b/src/server/sv_cmd.c @@ -509,7 +509,7 @@ SV_ServerRecord_f(void) Com_Printf("recording to %s.\n", name); FS_CreatePath(name); - svs.demofile = fopen(name, "wb"); + svs.demofile = Q_fopen(name, "wb"); if (!svs.demofile) { diff --git a/src/server/sv_init.c b/src/server/sv_init.c index b83b29fb..f40b1ac9 100644 --- a/src/server/sv_init.c +++ b/src/server/sv_init.c @@ -141,7 +141,7 @@ SV_CheckForSavegame(void) Com_sprintf(name, sizeof(name), "%s/save/current/%s.sav", FS_Gamedir(), sv.name); - f = fopen(name, "rb"); + f = Q_fopen(name, "rb"); if (!f) { diff --git a/src/server/sv_save.c b/src/server/sv_save.c index 004efb44..8ed6170a 100644 --- a/src/server/sv_save.c +++ b/src/server/sv_save.c @@ -80,14 +80,14 @@ CopyFile(char *src, char *dst) Com_DPrintf("CopyFile (%s, %s)\n", src, dst); - f1 = fopen(src, "rb"); + f1 = Q_fopen(src, "rb"); if (!f1) { return; } - f2 = fopen(dst, "wb"); + f2 = Q_fopen(dst, "wb"); if (!f2) { @@ -168,7 +168,7 @@ SV_WriteLevelFile(void) Com_sprintf(name, sizeof(name), "%s/save/current/%s.sv2", FS_Gamedir(), sv.name); - f = fopen(name, "wb"); + f = Q_fopen(name, "wb"); if (!f) { @@ -224,7 +224,7 @@ SV_WriteServerFile(qboolean autosave) Com_DPrintf("SV_WriteServerFile(%s)\n", autosave ? "true" : "false"); Com_sprintf(name, sizeof(name), "%s/save/current/server.ssv", FS_Gamedir()); - f = fopen(name, "wb"); + f = Q_fopen(name, "wb"); if (!f) { @@ -367,7 +367,7 @@ SV_Loadgame_f(void) /* make sure the server.ssv file exists */ Com_sprintf(name, sizeof(name), "%s/save/%s/server.ssv", FS_Gamedir(), Cmd_Argv(1)); - f = fopen(name, "rb"); + f = Q_fopen(name, "rb"); if (!f) {