- added some utilities to cmdlib.cpp and changed its license to BSD, because no original Quake code is left here.

This commit is contained in:
Christoph Oelckers 2020-04-11 12:17:28 +02:00
parent 3113c6b69b
commit 58afc3d5b2
14 changed files with 126 additions and 86 deletions

View File

@ -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;

View File

@ -36,6 +36,7 @@
#include "m_joy.h"
#include "gameconfigfile.h"
#include "d_event.h"
#include "cmdlib.h"
// MACROS ------------------------------------------------------------------

View File

@ -32,6 +32,7 @@
#include "doomdata.h"
#include "r_defs.h"
#include "nodebuild.h"
#include "cmdlib.h"
struct MapData

View File

@ -6,6 +6,7 @@
#include "doomstat.h"
#include "doomdata.h"
#include "m_bbox.h"
#include "cmdlib.h"
extern int validcount;
struct FBlockNode;

View File

@ -3,6 +3,7 @@
#include <stddef.h>
#include <stdint.h>
#include "cmdlib.h"
typedef uint32_t BITFIELD;
typedef int INTBOOL;
@ -37,12 +38,6 @@ typedef uint32_t angle_t;
#define GCCNOWARN
#endif
template <typename T, size_t N>
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)))

View File

@ -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)

View File

@ -24,6 +24,10 @@ typedef struct _GUID
} GUID;
#endif
template <typename T, size_t N>
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<FFileList> &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

View File

@ -4,53 +4,6 @@
#include <stdlib.h>
#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

View File

@ -59,6 +59,7 @@
#include "resource.h"
#include "version.h"
#include "m_swap.h"
#include "cmdlib.h"
#include <time.h>
#include <zlib.h>

View File

@ -41,6 +41,7 @@
#include "templates.h"
#include "gameconfigfile.h"
#include "m_argv.h"
#include "cmdlib.h"
// MACROS ------------------------------------------------------------------

View File

@ -62,6 +62,7 @@
#include "hardware.h"
#include "doomerrors.h"
#include "cmdlib.h"
#include "version.h"
#include "m_misc.h"

View File

@ -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);
}
//==========================================================================

View File

@ -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"

View File

@ -50,6 +50,7 @@
#include "m_argv.h"
#include "doomerrors.h"
#include "win32basevideo.h"
#include "cmdlib.h"
#include "gl/system/gl_framebuffer.h"