From 6ef93ba5144e2c8b757c745a5c7360aff67b14b3 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 11 Apr 2020 23:39:40 +0200 Subject: [PATCH] - match cmdlib.cpp/.h with GZDoom. --- source/build/include/build.h | 1 + source/build/include/compat.h | 3 - source/build/src/compat.cpp | 98 +--------- source/core/console/c_bind.cpp | 1 + source/core/console/c_con.cpp | 1 + source/core/filesystem/resourcefile.cpp | 1 + source/core/gamecontrol.cpp | 2 - source/core/input/m_joy.cpp | 1 + source/core/menu/optionmenuitems.h | 1 + source/core/rendering/r_videoscale.cpp | 1 + source/core/searchpaths.cpp | 1 - source/core/sound/s_environment.cpp | 1 + source/core/utility/basics.h | 5 - source/core/utility/cmdlib.cpp | 220 +++++++++++++++++++++-- source/core/utility/cmdlib.h | 7 + source/core/utility/files_decompress.cpp | 31 +--- source/duke3d/src/game.h | 1 + source/duke3d/src/gamedef.cpp | 2 - source/duke3d/src/network.cpp | 3 - source/duke3d/src/premap.cpp | 2 - source/platform/win32/i_crash.cpp | 1 + source/platform/win32/i_input.cpp | 1 + source/platform/win32/i_rawps2.cpp | 1 + source/platform/win32/i_specialpaths.cpp | 1 - source/platform/win32/i_system.cpp | 1 + source/platform/win32/i_xinput.cpp | 4 +- source/platform/win32/st_start.cpp | 1 + source/platform/win32/win32basevideo.cpp | 1 + source/rr/src/game.h | 1 + source/rr/src/gamedef.cpp | 2 - source/rr/src/net.cpp | 3 - source/rr/src/premap.cpp | 2 - 32 files changed, 227 insertions(+), 175 deletions(-) diff --git a/source/build/include/build.h b/source/build/include/build.h index 31a881fc0..ab62f35f2 100644 --- a/source/build/include/build.h +++ b/source/build/include/build.h @@ -28,6 +28,7 @@ static_assert('\xff' == 255, "Char must be unsigned!"); #include "textures.h" #include "c_cvars.h" +#include "cmdlib.h" typedef int64_t coord_t; diff --git a/source/build/include/compat.h b/source/build/include/compat.h index 36ac8e7f0..137d15a87 100644 --- a/source/build/include/compat.h +++ b/source/build/include/compat.h @@ -984,11 +984,8 @@ static inline void append_ext_UNSAFE(char *outbuf, const char *ext) ////////// Paths ////////// -int32_t Bcorrectfilename(char *filename, int32_t removefn); - ////////// String manipulation ////////// -char *Bstrtoken(char *s, const char *delim, char **ptrptr, int chop); char *Bstrtolower(char *str); diff --git a/source/build/src/compat.cpp b/source/build/src/compat.cpp index 496d9a62a..20a361393 100644 --- a/source/build/src/compat.cpp +++ b/source/build/src/compat.cpp @@ -52,105 +52,9 @@ void set_memerr_handler(void(*handlerfunc)(int32_t, const char *, const char *)) } -int32_t Bcorrectfilename(char *filename, int32_t removefn) -{ - char *fn = Xstrdup(filename); - char *tokarr[64], *first, *next = NULL; - - for (first=fn; *first; first++) - { -#ifdef _WIN32 - if (*first == '\\') *first = '/'; -#endif - } - - int leadslash = (*fn == '/'); - int trailslash = (first>fn && first[-1] == '/'); - int ntok = 0; - - first = fn; - do - { - char *token = Bstrtoken(first, "/", &next, 1); - first = NULL; - if (!token) break; - else if (token[0] == 0) continue; - else if (token[0] == '.' && token[1] == 0) continue; - else if (token[0] == '.' && token[1] == '.' && token[2] == 0) ntok = max(0,ntok-1); - else tokarr[ntok++] = token; - } - while (1); - - if (!trailslash && removefn) { ntok = max(0,ntok-1); trailslash = 1; } - if (ntok == 0 && trailslash && leadslash) trailslash = 0; - - first = filename; - if (leadslash) *(first++) = '/'; - for (int i=0; i0) *(first++) = '/'; - for (char *token=tokarr[i]; *token; token++) - *(first++) = *token; - } - if (trailslash) *(first++) = '/'; - *(first++) = 0; - - Xfree(fn); - return 0; -} - - -char *Bstrtoken(char *s, const char *delim, char **ptrptr, int chop) -{ - if (!ptrptr) - return NULL; - - char *p = s ? s : *ptrptr; - - if (!p) - return NULL; - - while (*p != 0 && Bstrchr(delim, *p)) p++; - - if (*p == 0) - { - *ptrptr = NULL; - return NULL; - } - - char * const start = p; - - while (*p != 0 && !Bstrchr(delim, *p)) p++; - - if (*p == 0) - *ptrptr = NULL; - else - { - if (chop) - *(p++) = 0; - *ptrptr = p; - } - - return start; -} char *Bstrtolower(char *str) { - if (!str) - return NULL; - - int len = strlen(str); - - if (len <= 0) - return str; - - int i = 0; - - do - { - *(str + i) = tolower(*(str + i)); - i++; - } while (--len); - + if (str) for (int i = 0; str[i]; i++) str[i] = tolower(str[i]); return str; } diff --git a/source/core/console/c_bind.cpp b/source/core/console/c_bind.cpp index a95251978..b0eb1c4a0 100644 --- a/source/core/console/c_bind.cpp +++ b/source/core/console/c_bind.cpp @@ -35,6 +35,7 @@ #include //#include "doomtype.h" +#include "cmdlib.h" #include "keydef.h" #include "c_commandline.h" #include "c_bind.h" diff --git a/source/core/console/c_con.cpp b/source/core/console/c_con.cpp index a7db2650a..273eca022 100644 --- a/source/core/console/c_con.cpp +++ b/source/core/console/c_con.cpp @@ -38,6 +38,7 @@ #include "zstring.h" #include "c_bind.h" #include "gamecontrol.h" +#include "cmdlib.h" //============================================================================= // diff --git a/source/core/filesystem/resourcefile.cpp b/source/core/filesystem/resourcefile.cpp index ea64d6665..344dde414 100644 --- a/source/core/filesystem/resourcefile.cpp +++ b/source/core/filesystem/resourcefile.cpp @@ -39,6 +39,7 @@ #include "name.h" #include "m_swap.h" #include "gamecontrol.h" +#include "cmdlib.h" //========================================================================== // diff --git a/source/core/gamecontrol.cpp b/source/core/gamecontrol.cpp index 99bc5ae6d..f5aebfc88 100644 --- a/source/core/gamecontrol.cpp +++ b/source/core/gamecontrol.cpp @@ -75,8 +75,6 @@ MapRecord userMapRecord; // stand-in for the user map. FStartupInfo RazeStartupInfo; FMemArena dump; // this is for memory blocks than cannot be deallocated without some huge effort. Put them in here so that they do not register on shutdown. -FString progdir; - void C_CON_SetAliases(); InputState inputState; void SetClipshapes(); diff --git a/source/core/input/m_joy.cpp b/source/core/input/m_joy.cpp index 054bceca7..cd0fb56e0 100644 --- a/source/core/input/m_joy.cpp +++ b/source/core/input/m_joy.cpp @@ -37,6 +37,7 @@ #include "m_joy.h" #include "gameconfigfile.h" #include "d_event.h" +#include "cmdlib.h" // MACROS ------------------------------------------------------------------ diff --git a/source/core/menu/optionmenuitems.h b/source/core/menu/optionmenuitems.h index ca3a99c4a..32b4b9609 100644 --- a/source/core/menu/optionmenuitems.h +++ b/source/core/menu/optionmenuitems.h @@ -35,6 +35,7 @@ #include "v_draw.h" #include "gstrings.h" #include "v_font.h" +#include "cmdlib.h" void M_DrawConText (int color, int x, int y, const char *str); diff --git a/source/core/rendering/r_videoscale.cpp b/source/core/rendering/r_videoscale.cpp index 031334f62..812d4d2c7 100644 --- a/source/core/rendering/r_videoscale.cpp +++ b/source/core/rendering/r_videoscale.cpp @@ -36,6 +36,7 @@ #include "v_video.h" #include "templates.h" #include "r_videoscale.h" +#include "cmdlib.h" #include "console/c_console.h" #include "menu/menu.h" diff --git a/source/core/searchpaths.cpp b/source/core/searchpaths.cpp index d7f15cbed..9587d63ae 100644 --- a/source/core/searchpaths.cpp +++ b/source/core/searchpaths.cpp @@ -581,7 +581,6 @@ TArray ParseAllGrpInfos(TArray& filelist) { TArray groups; TMap CRCMap; - extern FString progdir; // This opens the base resource only for reading the grpinfo from it which we need before setting up the game state. std::unique_ptr engine_res; FString baseres = progdir + ENGINERES_FILE; diff --git a/source/core/sound/s_environment.cpp b/source/core/sound/s_environment.cpp index ff0b6260d..9bc3cf4d7 100644 --- a/source/core/sound/s_environment.cpp +++ b/source/core/sound/s_environment.cpp @@ -35,6 +35,7 @@ #include "s_soundinternal.h" #include "sc_man.h" #include "templates.h" +#include "cmdlib.h" FReverbField ReverbFields[] = diff --git a/source/core/utility/basics.h b/source/core/utility/basics.h index bc0d76fa4..2f1ba5966 100644 --- a/source/core/utility/basics.h +++ b/source/core/utility/basics.h @@ -19,11 +19,6 @@ #define GCCNOWARN #endif -template -char(&_ArraySizeHelper(T(&array)[N]))[N]; - -#define countof( array ) (sizeof( _ArraySizeHelper( array ) )) - using INTBOOL = int; #endif diff --git a/source/core/utility/cmdlib.cpp b/source/core/utility/cmdlib.cpp index ab7d7f92f..6588dd479 100644 --- a/source/core/utility/cmdlib.cpp +++ b/source/core/utility/cmdlib.cpp @@ -34,27 +34,24 @@ */ -#ifdef _WIN32 -#include -#include -#else -#include -#include -#include -#include -#if !defined(__sun) -#include -#endif -#endif #include "cmdlib.h" -#include "compat.h" +#include "i_system.h" #include #include #include +/* +progdir will hold the path up to the game directory, including the slash -extern FString progdir; + f:\quake\ + /raid/quake/ + +gamedir will hold progdir + the game directory (id1, id2, etc) + + */ + +FString progdir; //========================================================================== // @@ -75,7 +72,78 @@ static inline bool IsSeperator (int c) return false; } - //========================================================================== +//========================================================================== +// +// FixPathSeperator +// +// Convert backslashes to forward slashes. +// +//========================================================================== + +void FixPathSeperator (char *path) +{ + while (*path) + { + if (*path == '\\') + *path = '/'; + path++; + } +} + +//========================================================================== +// +// copystring +// +// Replacement for strdup that uses new instead of malloc. +// +//========================================================================== + +char *copystring (const char *s) +{ + char *b; + if (s) + { + size_t len = strlen (s) + 1; + b = new char[len]; + memcpy (b, s, len); + } + else + { + b = new char[1]; + b[0] = '\0'; + } + return b; +} + +//========================================================================== +// +// ReplaceString +// +// Do not use in new code. +// +//========================================================================== + +void ReplaceString (char **ptr, const char *str) +{ + if (*ptr) + { + if (*ptr == str) + return; + delete[] *ptr; + } + *ptr = copystring (str); +} + +/* +============================================================================= + + MISC FUNCTIONS + +============================================================================= +*/ + + +//========================================================================== // // FileExists // @@ -821,6 +889,68 @@ FString NicePath(const char *path) } +//========================================================================== +// +// ScanDirectory +// +//========================================================================== + +bool ScanDirectory(TArray &list, const char *dirpath) +{ + findstate_t find; + FString dirmatch; + + dirmatch << dirpath << "*"; + + auto handle = I_FindFirst(dirmatch.GetChars(), &find); + if (handle == ((void*)(-1))) + { + return false; + } + else + { + do + { + auto attr = I_FindAttr(&find); + if (attr & FA_HIDDEN) + { + // Skip hidden files and directories. (Prevents SVN bookkeeping + // info from being included.) + continue; + } + auto fn = I_FindName(&find); + + if (attr & FA_DIREC) + { + if (fn[0] == '.' && + (fn[1] == '\0' || + (fn[1] == '.' && fn[2] == '\0'))) + { + // Do not record . and .. directories. + continue; + } + + FFileList* fl = &list[list.Reserve(1)]; + fl->Filename << dirpath << fn; + fl->isDirectory = true; + FString newdir = fl->Filename; + newdir << "/"; + ScanDirectory(list, newdir); + } + else + { + FFileList* fl = &list[list.Reserve(1)]; + fl->Filename << dirpath << fn; + fl->isDirectory = false; + } + } + while (I_FindNext(handle, &find) == 0); + I_FindClose(handle); + } + return true; +} + + //========================================================================== // // @@ -843,10 +973,62 @@ bool IsAbsPath(const char *name) // //========================================================================== -void NormalizeFileName(FString &str) +void NormalizeFileName(FString& str) { - auto strp = str.LockBuffer(); - Bcorrectfilename(strp, false); - str.UnlockBuffer(); + FixPathSeperator(str); + auto splits = str.Split("/"); + for (unsigned i = 1; i < splits.Size(); i++) + { + if (splits[i].Compare(".") == 0) + { + splits.Delete(i); + i--; + } + + if (splits[i].Compare("..") == 0 && splits[i - 1].Compare("..") != 0) + { + splits.Delete(i); + splits.Delete(i - 1); + i -= 2; + if (i < 1) i = 1; + } + } + str = splits[0]; + for (unsigned i = 1; i < splits.Size(); i++) + { + str << "/" << splits[i]; + } } +//========================================================================== +// +// +// +//========================================================================== + +FString M_ZLibError(int zerr) +{ + if (zerr >= 0) + { + return "OK"; + } + else if (zerr < -6) + { + FString out; + out.Format("%d", zerr); + return out; + } + else + { + static const char* errs[6] = + { + "Errno", + "Stream Error", + "Data Error", + "Memory Error", + "Buffer Error", + "Version Error" + }; + return errs[-zerr - 1]; + } +} diff --git a/source/core/utility/cmdlib.h b/source/core/utility/cmdlib.h index 00c7168e3..75cb5a012 100644 --- a/source/core/utility/cmdlib.h +++ b/source/core/utility/cmdlib.h @@ -10,6 +10,7 @@ #include #include #include +#include #include "zstring.h" #if !defined(GUID_DEFINED) @@ -38,6 +39,7 @@ bool GetFileInfo(const char* pathname, size_t* size, time_t* time); extern FString progdir; +void FixPathSeperator (char *path); static void inline FixPathSeperator (FString &path) { path.ReplaceChars('\\', '/'); } void DefaultExtension (FString &path, const char *extension); @@ -50,6 +52,9 @@ FString StripExtension(const char* path); struct FScriptPosition; bool IsNum (const char *str); // [RH] added +char *copystring(const char *s); +void ReplaceString (char **ptr, const char *str); + bool CheckWildcards (const char *pattern, const char *text); void FormatGUID (char *buffer, size_t buffsize, const GUID &guid); @@ -70,7 +75,9 @@ struct FFileList bool isDirectory; }; +bool ScanDirectory(TArray &list, const char *dirpath); bool IsAbsPath(const char*); +FString M_ZLibError(int zerrnum); inline int32_t Scale(int32_t a, int32_t b, int32_t c) { diff --git a/source/core/utility/files_decompress.cpp b/source/core/utility/files_decompress.cpp index 5cdf69af1..8617f575b 100644 --- a/source/core/utility/files_decompress.cpp +++ b/source/core/utility/files_decompress.cpp @@ -43,6 +43,7 @@ #include "files.h" #include "templates.h" #include "zstring.h" +#include "cmdlib.h" //========================================================================== // @@ -89,36 +90,6 @@ void DecompressorBase::SetOwnsReader() File = &OwnedFile; } -// -// M_ZlibError -// -FString M_ZLibError(int zerr) -{ - if (zerr >= 0) - { - return "OK"; - } - else if (zerr < -6) - { - FString out; - out.Format("%d", zerr); - return out; - } - else - { - static const char* errs[6] = - { - "Errno", - "Stream Error", - "Data Error", - "Memory Error", - "Buffer Error", - "Version Error" - }; - return errs[-zerr - 1]; - } -} - //========================================================================== // // DecompressorZ diff --git a/source/duke3d/src/game.h b/source/duke3d/src/game.h index 237240a62..6fd0f39fe 100644 --- a/source/duke3d/src/game.h +++ b/source/duke3d/src/game.h @@ -35,6 +35,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "network.h" #include "menu/menu.h" #include "palette.h" +#include "cmdlib.h" BEGIN_DUKE_NS diff --git a/source/duke3d/src/gamedef.cpp b/source/duke3d/src/gamedef.cpp index b9afe13e3..05b4abea9 100644 --- a/source/duke3d/src/gamedef.cpp +++ b/source/duke3d/src/gamedef.cpp @@ -5214,8 +5214,6 @@ repeatcase: } tempbuf[i+1] = '\0'; - Bcorrectfilename(tempbuf,0); - mapList[j * MAXLEVELS + k].SetFileName(tempbuf); C_SkipComments(); diff --git a/source/duke3d/src/network.cpp b/source/duke3d/src/network.cpp index 7d2331124..3d6474f8b 100644 --- a/source/duke3d/src/network.cpp +++ b/source/duke3d/src/network.cpp @@ -2003,7 +2003,6 @@ static void Net_ReceiveUserMapName(uint8_t *pbuf, int32_t packbufleng) Bstrcpy(boardfilename, (char *)pbuf + 1); boardfilename[packbufleng - 1] = 0; - Bcorrectfilename(boardfilename, 0); if (boardfilename[0] != 0) { if (fileSystem.FileExists(boardfilename)) @@ -4854,8 +4853,6 @@ void Net_SendUserMapName(void) packbuf[0] = PACKET_USER_MAP; - Bcorrectfilename(boardfilename, 0); - // user map name is sent with a NUL at the end int32_t j = Bstrlen(boardfilename) + 1; diff --git a/source/duke3d/src/premap.cpp b/source/duke3d/src/premap.cpp index 020d70aad..f7202e0ac 100644 --- a/source/duke3d/src/premap.cpp +++ b/source/duke3d/src/premap.cpp @@ -1731,8 +1731,6 @@ int G_EnterLevel(int gameMode) if (Menu_HaveUserMap()) { - Bcorrectfilename(boardfilename, 0); - int levelNum = G_FindLevelByFile(boardfilename); if (levelNum != -1) diff --git a/source/platform/win32/i_crash.cpp b/source/platform/win32/i_crash.cpp index 6fa397f53..90c0658f1 100644 --- a/source/platform/win32/i_crash.cpp +++ b/source/platform/win32/i_crash.cpp @@ -63,6 +63,7 @@ #include "basics.h" #include "zstring.h" #include "printf.h" +#include "cmdlib.h" #include #include diff --git a/source/platform/win32/i_input.cpp b/source/platform/win32/i_input.cpp index c8faffa83..711eddd21 100644 --- a/source/platform/win32/i_input.cpp +++ b/source/platform/win32/i_input.cpp @@ -86,6 +86,7 @@ #include "menu.h" #include "c_buttons.h" #include "gamecontrol.h" +#include "cmdlib.h" // Compensate for w32api's lack #ifndef GET_XBUTTON_WPARAM diff --git a/source/platform/win32/i_rawps2.cpp b/source/platform/win32/i_rawps2.cpp index c09a2eba8..fb86205db 100644 --- a/source/platform/win32/i_rawps2.cpp +++ b/source/platform/win32/i_rawps2.cpp @@ -42,6 +42,7 @@ #include "gameconfigfile.h" #include "m_argv.h" #include "keydef.h" +#include "cmdlib.h" // MACROS ------------------------------------------------------------------ diff --git a/source/platform/win32/i_specialpaths.cpp b/source/platform/win32/i_specialpaths.cpp index 44167f20e..dce6f2acb 100644 --- a/source/platform/win32/i_specialpaths.cpp +++ b/source/platform/win32/i_specialpaths.cpp @@ -45,7 +45,6 @@ #include "version.h" // for GAMENAME // Stuff that needs to be set up later. -extern FString progdir; static bool batchrun; // Vanilla MinGW does not have folder ids diff --git a/source/platform/win32/i_system.cpp b/source/platform/win32/i_system.cpp index 291886093..6ba79b813 100644 --- a/source/platform/win32/i_system.cpp +++ b/source/platform/win32/i_system.cpp @@ -79,6 +79,7 @@ #include "v_font.h" #include "i_system.h" #include "textures/bitmap.h" +#include "cmdlib.h" extern bool batchrun; // MACROS ------------------------------------------------------------------ diff --git a/source/platform/win32/i_xinput.cpp b/source/platform/win32/i_xinput.cpp index 8dac28373..cdcd66a08 100644 --- a/source/platform/win32/i_xinput.cpp +++ b/source/platform/win32/i_xinput.cpp @@ -437,9 +437,7 @@ void FXInputController::SetDefaultConfig() FString FXInputController::GetIdentifier() { - char id[16]; - snprintf(id, countof(id), "XI:%d", Index); - return id; + return FStringf("XI:%d", Index); } //========================================================================== diff --git a/source/platform/win32/st_start.cpp b/source/platform/win32/st_start.cpp index dff57fdcc..50e9d2539 100644 --- a/source/platform/win32/st_start.cpp +++ b/source/platform/win32/st_start.cpp @@ -47,6 +47,7 @@ #include "m_argv.h" #include "s_music.h" #include "printf.h" +#include "cmdlib.h" // MACROS ------------------------------------------------------------------ diff --git a/source/platform/win32/win32basevideo.cpp b/source/platform/win32/win32basevideo.cpp index dab8c49eb..c400ad4a7 100644 --- a/source/platform/win32/win32basevideo.cpp +++ b/source/platform/win32/win32basevideo.cpp @@ -48,6 +48,7 @@ #include "m_argv.h" #include "printf.h" #include "win32basevideo.h" +#include "cmdlib.h" #include "gl/system/gl_framebuffer.h" diff --git a/source/rr/src/game.h b/source/rr/src/game.h index 7d014b5c5..017fa63ed 100644 --- a/source/rr/src/game.h +++ b/source/rr/src/game.h @@ -32,6 +32,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "net.h" #include "mmulti.h" #include "palette.h" +#include "cmdlib.h" BEGIN_RR_NS diff --git a/source/rr/src/gamedef.cpp b/source/rr/src/gamedef.cpp index 7f0ee1a5b..403959bfd 100644 --- a/source/rr/src/gamedef.cpp +++ b/source/rr/src/gamedef.cpp @@ -2293,8 +2293,6 @@ ifvar: } tempbuf[i+1] = '\0'; - Bcorrectfilename(tempbuf,0); - mapList[j *MAXLEVELS+k].SetFileName(tempbuf); C_SkipComments(); diff --git a/source/rr/src/net.cpp b/source/rr/src/net.cpp index fdf0ccc72..35c50681f 100644 --- a/source/rr/src/net.cpp +++ b/source/rr/src/net.cpp @@ -3158,8 +3158,6 @@ void Net_SendUserMapName(void) packbuf[0] = PACKET_USER_MAP; - Bcorrectfilename(boardfilename,0); - // user map name is sent with a NUL at the end j = Bstrlen(boardfilename)+1; Bmemcpy(&packbuf[1], boardfilename, j); @@ -3181,7 +3179,6 @@ void Net_ReceiveUserMapName(uint8_t *pbuf, int32_t packbufleng) { Bstrcpy(boardfilename,(char *)pbuf+1); boardfilename[packbufleng-1] = 0; - Bcorrectfilename(boardfilename,0); if (boardfilename[0] != 0) { if (fileSystem.FileExists(boardfilename)) diff --git a/source/rr/src/premap.cpp b/source/rr/src/premap.cpp index d216c2e32..16f97ff93 100644 --- a/source/rr/src/premap.cpp +++ b/source/rr/src/premap.cpp @@ -2296,8 +2296,6 @@ int G_EnterLevel(int gameMode) if (Menu_HaveUserMap()) { - Bcorrectfilename(boardfilename,0); - int levelNum = G_FindLevelByFile(boardfilename); if (levelNum != MAXLEVELS*MAXVOLUMES)