From 58afc3d5b296f784b7878b1f47c00398d64e24f1 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 11 Apr 2020 12:17:28 +0200 Subject: [PATCH] - added some utilities to cmdlib.cpp and changed its license to BSD, because no original Quake code is left here. --- src/console/c_expr.cpp | 11 ++-- src/m_joy.cpp | 1 + src/p_setup.h | 1 + src/playsim/p_maputl.h | 1 + src/utility/basictypes.h | 7 +- src/utility/cmdlib.cpp | 123 ++++++++++++++++++++++++++++------- src/utility/cmdlib.h | 11 +++- src/utility/m_fixed.h | 47 ------------- src/win32/i_crash.cpp | 1 + src/win32/i_rawps2.cpp | 1 + src/win32/i_system.cpp | 1 + src/win32/i_xinput.cpp | 5 +- src/win32/st_start.cpp | 1 + src/win32/win32basevideo.cpp | 1 + 14 files changed, 126 insertions(+), 86 deletions(-) diff --git a/src/console/c_expr.cpp b/src/console/c_expr.cpp index cde8527e2..4a92eeec7 100644 --- a/src/console/c_expr.cpp +++ b/src/console/c_expr.cpp @@ -41,6 +41,7 @@ #include "c_dispatch.h" #include "c_cvars.h" #include "doomtype.h" +#include "cmdlib.h" // MACROS ------------------------------------------------------------------ @@ -82,7 +83,7 @@ bool IsFloat (const char *str); // PRIVATE FUNCTION PROTOTYPES --------------------------------------------- static FProduction *ParseExpression (FCommandLine &argv, int &parsept); -static const char *IsNum (const char *str); +static const char *CIsNum (const char *str); static FStringProd *NewStringProd (const char *str); static FStringProd *NewStringProd (size_t len); static FDoubleProd *NewDoubleProd (double val); @@ -257,13 +258,13 @@ bool IsFloat (const char *str) } else { - pt = IsNum (str); + pt = CIsNum (str); if (pt == NULL) return false; } if (*pt == '.') { - pt = IsNum (pt+1); + pt = CIsNum (pt+1); if (pt == NULL) return false; } @@ -272,7 +273,7 @@ bool IsFloat (const char *str) pt++; if (*pt == '+' || *pt == '-') pt++; - pt = IsNum (pt); + pt = CIsNum (pt); } return pt != NULL && *pt == 0; } @@ -283,7 +284,7 @@ bool IsFloat (const char *str) // //========================================================================== -static const char *IsNum (const char *str) +static const char *CIsNum (const char *str) { const char *start = str; diff --git a/src/m_joy.cpp b/src/m_joy.cpp index bd51a77df..151b3b4ed 100644 --- a/src/m_joy.cpp +++ b/src/m_joy.cpp @@ -36,6 +36,7 @@ #include "m_joy.h" #include "gameconfigfile.h" #include "d_event.h" +#include "cmdlib.h" // MACROS ------------------------------------------------------------------ diff --git a/src/p_setup.h b/src/p_setup.h index 0cae8124c..0af26d101 100644 --- a/src/p_setup.h +++ b/src/p_setup.h @@ -32,6 +32,7 @@ #include "doomdata.h" #include "r_defs.h" #include "nodebuild.h" +#include "cmdlib.h" struct MapData diff --git a/src/playsim/p_maputl.h b/src/playsim/p_maputl.h index e86d363cf..7bbf4f83a 100644 --- a/src/playsim/p_maputl.h +++ b/src/playsim/p_maputl.h @@ -6,6 +6,7 @@ #include "doomstat.h" #include "doomdata.h" #include "m_bbox.h" +#include "cmdlib.h" extern int validcount; struct FBlockNode; diff --git a/src/utility/basictypes.h b/src/utility/basictypes.h index 989b6a764..57e4bb5c5 100644 --- a/src/utility/basictypes.h +++ b/src/utility/basictypes.h @@ -3,6 +3,7 @@ #include #include +#include "cmdlib.h" typedef uint32_t BITFIELD; typedef int INTBOOL; @@ -37,12 +38,6 @@ typedef uint32_t angle_t; #define GCCNOWARN #endif -template -char(&_ArraySizeHelper(T(&array)[N]))[N]; - -#define countof( array ) (sizeof( _ArraySizeHelper( array ) )) - - #ifndef MAKE_ID #ifndef __BIG_ENDIAN__ #define MAKE_ID(a,b,c,d) ((uint32_t)((a)|((b)<<8)|((c)<<16)|((d)<<24))) diff --git a/src/utility/cmdlib.cpp b/src/utility/cmdlib.cpp index c571a23e6..6588dd479 100644 --- a/src/utility/cmdlib.cpp +++ b/src/utility/cmdlib.cpp @@ -1,26 +1,38 @@ -//----------------------------------------------------------------------------- -// -// Copyright 1996 id Software -// Copyright 1999-2016 Randy Heit -// Copyright 2002-2016 Christoph Oelckers -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see http://www.gnu.org/licenses/ -// -//----------------------------------------------------------------------------- -// +/* +** cmdlib.cpp +** Misc utilities (mostly file handling stuff) +** +**--------------------------------------------------------------------------- +** Copyright 1999-2016 Randy Heit +** Copyright 2019 Christoph Oelckers +** All rights reserved. +** +** Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions +** are met: +** +** 1. Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** 2. Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in the +** documentation and/or other materials provided with the distribution. +** 3. The name of the author may not be used to endorse or promote products +** derived from this software without specific prior written permission. +** +** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +**--------------------------------------------------------------------------- +** +*/ -// cmdlib.c (mostly borrowed from the Q2 source) #include "cmdlib.h" #include "i_system.h" @@ -53,7 +65,7 @@ static inline bool IsSeperator (int c) { if (c == '/') return true; -#ifdef WIN32 +#ifdef _WIN32 if (c == '\\' || c == ':') return true; #endif @@ -311,6 +323,33 @@ FString ExtractFileBase (const char *path, bool include_extension) return FString(); } +//========================================================================== +// +// StripExtension +// +// Returns the path with the extension removed +// +//========================================================================== + +FString StripExtension(const char* path) +{ + const char* src; + if (*path == 0) return ""; + + src = path + strlen(path) - 1; + + // + // back up until a . and abort on a \ + // + while (src != path && !IsSeperator(*(src - 1))) + { + if (*src == '.') return FString(path, src - path); + src--; + } + + return path; + +} //========================================================================== // @@ -928,9 +967,45 @@ bool IsAbsPath(const char *name) return 0; } +//========================================================================== // -// M_ZlibError // +// +//========================================================================== + +void NormalizeFileName(FString& str) +{ + 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) diff --git a/src/utility/cmdlib.h b/src/utility/cmdlib.h index 823b07fde..75cb5a012 100644 --- a/src/utility/cmdlib.h +++ b/src/utility/cmdlib.h @@ -24,6 +24,10 @@ typedef struct _GUID } GUID; #endif +template +char(&_ArraySizeHelper(T(&array)[N]))[N]; + +#define countof( array ) (sizeof( _ArraySizeHelper( array ) )) // the dec offsetof macro doesnt work very well... #define myoffsetof(type,identifier) ((size_t)&((type *)alignof(type))->identifier - alignof(type)) @@ -39,9 +43,11 @@ void FixPathSeperator (char *path); static void inline FixPathSeperator (FString &path) { path.ReplaceChars('\\', '/'); } void DefaultExtension (FString &path, const char *extension); +void NormalizeFileName(FString &str); FString ExtractFilePath (const char *path); FString ExtractFileBase (const char *path, bool keep_extension=false); +FString StripExtension(const char* path); struct FScriptPosition; bool IsNum (const char *str); // [RH] added @@ -71,8 +77,11 @@ struct FFileList 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) +{ + return (int32_t)(((int64_t)a * b) / c); +} #endif diff --git a/src/utility/m_fixed.h b/src/utility/m_fixed.h index 8bbb2dbf9..8f1eb72d0 100644 --- a/src/utility/m_fixed.h +++ b/src/utility/m_fixed.h @@ -4,53 +4,6 @@ #include #include "doomtype.h" -// Unfortunately, the Scale function still gets badly handled on 32 bit x86 platforms so it's the last remaining piece of inline assembly - -// GCC inlines -#if defined(__GNUC__) && defined(__i386__) && !defined(__clang__) && !defined(__PIC__) -#ifndef alloca -// MinGW does not seem to come with alloca defined. -#define alloca __builtin_alloca -#endif - -static inline int32_t Scale(int32_t a, int32_t b, int32_t c) -{ - int32_t result, dummy; - - asm volatile - ("imull %3\n\t" - "idivl %4" - : "=a,a,a,a,a,a" (result), - "=&d,&d,&d,&d,d,d" (dummy) - : "a,a,a,a,a,a" (a), - "m,r,m,r,d,d" (b), - "r,r,m,m,r,m" (c) - : "cc" - ); - - return result; -} - -// MSVC inlines -#elif defined(_MSC_VER) && defined(_M_IX86) -#pragma warning (disable: 4035) - -__forceinline int32_t Scale(int32_t a, int32_t b, int32_t c) -{ - __asm mov eax, a - __asm imul b - __asm idiv c -} - -#pragma warning (default: 4035) -#else - -static __forceinline int32_t Scale(int32_t a, int32_t b, int32_t c) -{ - return (int32_t)(((int64_t)a*b) / c); -} - -#endif // Modern compilers are smart enough to do these multiplications intelligently. __forceinline int32_t MulScale14(int32_t a, int32_t b) { return (int32_t)(((int64_t)a * b) >> 14); } // only used by R_DrawVoxel diff --git a/src/win32/i_crash.cpp b/src/win32/i_crash.cpp index aea027990..a8045070d 100644 --- a/src/win32/i_crash.cpp +++ b/src/win32/i_crash.cpp @@ -59,6 +59,7 @@ #include "resource.h" #include "version.h" #include "m_swap.h" +#include "cmdlib.h" #include #include diff --git a/src/win32/i_rawps2.cpp b/src/win32/i_rawps2.cpp index 0bf28efba..083188a53 100644 --- a/src/win32/i_rawps2.cpp +++ b/src/win32/i_rawps2.cpp @@ -41,6 +41,7 @@ #include "templates.h" #include "gameconfigfile.h" #include "m_argv.h" +#include "cmdlib.h" // MACROS ------------------------------------------------------------------ diff --git a/src/win32/i_system.cpp b/src/win32/i_system.cpp index 9361e7658..075909b7f 100644 --- a/src/win32/i_system.cpp +++ b/src/win32/i_system.cpp @@ -62,6 +62,7 @@ #include "hardware.h" #include "doomerrors.h" +#include "cmdlib.h" #include "version.h" #include "m_misc.h" diff --git a/src/win32/i_xinput.cpp b/src/win32/i_xinput.cpp index 63cfe294e..93ef75c01 100644 --- a/src/win32/i_xinput.cpp +++ b/src/win32/i_xinput.cpp @@ -43,6 +43,7 @@ #include "templates.h" #include "gameconfigfile.h" #include "m_argv.h" +#include "cmdlib.h" // MACROS ------------------------------------------------------------------ @@ -434,9 +435,7 @@ void FXInputController::SetDefaultConfig() FString FXInputController::GetIdentifier() { - char id[16]; - mysnprintf(id, countof(id), "XI:%d", Index); - return id; + return FStringf("XI:%d", Index); } //========================================================================== diff --git a/src/win32/st_start.cpp b/src/win32/st_start.cpp index 3e9a96047..e309c919d 100644 --- a/src/win32/st_start.cpp +++ b/src/win32/st_start.cpp @@ -40,6 +40,7 @@ #include "resource.h" #include "st_start.h" +#include "cmdlib.h" #include "templates.h" #include "i_system.h" #include "i_input.h" diff --git a/src/win32/win32basevideo.cpp b/src/win32/win32basevideo.cpp index cc37848f1..809571a8f 100644 --- a/src/win32/win32basevideo.cpp +++ b/src/win32/win32basevideo.cpp @@ -50,6 +50,7 @@ #include "m_argv.h" #include "doomerrors.h" #include "win32basevideo.h" +#include "cmdlib.h" #include "gl/system/gl_framebuffer.h"