- removed all Doom specific dependencies from cmdlib.cpp/h.

This meant moving CleanseString and ParseHex elsewhere and removing the I_Error call from ScanDirectory.
This commit is contained in:
Christoph Oelckers 2019-08-20 20:10:54 +02:00
parent 38fec546a7
commit 3cfda930ea
10 changed files with 100 additions and 106 deletions

View file

@ -70,6 +70,12 @@ enum
TELEFRAG_DAMAGE = 1000000 TELEFRAG_DAMAGE = 1000000
}; };
inline int Tics2Seconds(int tics)
{
return tics / TICRATE;
}
typedef float skill_t; typedef float skill_t;

View file

@ -1745,3 +1745,33 @@ void V_ClearFonts()
AlternativeSmallFont = OriginalSmallFont = CurrentConsoleFont = NewSmallFont = NewConsoleFont = SmallFont = SmallFont2 = BigFont = ConFont = IntermissionFont = nullptr; AlternativeSmallFont = OriginalSmallFont = CurrentConsoleFont = NewSmallFont = NewConsoleFont = SmallFont = SmallFont2 = BigFont = ConFont = IntermissionFont = nullptr;
} }
//==========================================================================
//
// CleanseString
//
// Does some mild sanity checking on a string: If it ends with an incomplete
// color escape, the escape is removed.
//
//==========================================================================
char* CleanseString(char* str)
{
char* escape = strrchr(str, TEXTCOLOR_ESCAPE);
if (escape != NULL)
{
if (escape[1] == '\0')
{
*escape = '\0';
}
else if (escape[1] == '[')
{
char* close = strchr(escape + 2, ']');
if (close == NULL)
{
*escape = '\0';
}
}
}
return str;
}

View file

@ -190,6 +190,7 @@ PalEntry V_LogColorFromColorRange (EColorRange range);
EColorRange V_ParseFontColor (const uint8_t *&color_value, int normalcolor, int boldcolor); EColorRange V_ParseFontColor (const uint8_t *&color_value, int normalcolor, int boldcolor);
FFont *V_GetFont(const char *fontname, const char *fontlumpname = nullptr); FFont *V_GetFont(const char *fontname, const char *fontlumpname = nullptr);
void V_InitFontColors(); void V_InitFontColors();
char* CleanseString(char* str);
#endif //__V_FONT_H__ #endif //__V_FONT_H__

View file

@ -1151,13 +1151,9 @@ UNSAFE_CCMD(clearnodecache)
FString path = M_GetCachePath(false); FString path = M_GetCachePath(false);
path += "/"; path += "/";
try if (!ScanDirectory(list, path))
{ {
ScanDirectory(list, path); Printf("Unable to scan node cache directory %s\n", path);
}
catch (CRecoverableError &err)
{
Printf("%s\n", err.GetMessage());
return; return;
} }

View file

@ -6,17 +6,6 @@
typedef uint32_t BITFIELD; typedef uint32_t BITFIELD;
typedef int INTBOOL; typedef int INTBOOL;
#if !defined(GUID_DEFINED)
#define GUID_DEFINED
typedef struct _GUID
{
uint32_t Data1;
uint16_t Data2;
uint16_t Data3;
uint8_t Data4[8];
} GUID;
#endif
// //
// fixed point, 32bit as 16.16. // fixed point, 32bit as 16.16.
// //

View file

@ -34,11 +34,7 @@
#include <fts.h> #include <fts.h>
#endif #endif
#endif #endif
#include "doomtype.h"
#include "cmdlib.h" #include "cmdlib.h"
#include "doomerrors.h"
#include "v_text.h"
#include "sc_man.h"
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -299,40 +295,6 @@ FString ExtractFileBase (const char *path, bool include_extension)
} }
//==========================================================================
//
// ParseHex
//
//==========================================================================
int ParseHex (const char *hex, FScriptPosition *sc)
{
const char *str;
int num;
num = 0;
str = hex;
while (*str)
{
num <<= 4;
if (*str >= '0' && *str <= '9')
num += *str-'0';
else if (*str >= 'a' && *str <= 'f')
num += 10 + *str-'a';
else if (*str >= 'A' && *str <= 'F')
num += 10 + *str-'A';
else {
if (!sc) Printf ("Bad hex number: %s\n",hex);
else sc->Message(MSG_WARNING, "Bad hex number: %s", hex);
return 0;
}
str++;
}
return num;
}
//========================================================================== //==========================================================================
// //
// IsNum // IsNum
@ -408,7 +370,7 @@ bool CheckWildcards (const char *pattern, const char *text)
void FormatGUID (char *buffer, size_t buffsize, const GUID &guid) void FormatGUID (char *buffer, size_t buffsize, const GUID &guid)
{ {
mysnprintf (buffer, buffsize, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}", snprintf (buffer, buffsize, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
(uint32_t)guid.Data1, guid.Data2, guid.Data3, (uint32_t)guid.Data1, guid.Data2, guid.Data3,
guid.Data4[0], guid.Data4[1], guid.Data4[0], guid.Data4[1],
guid.Data4[2], guid.Data4[3], guid.Data4[2], guid.Data4[3],
@ -463,7 +425,7 @@ void DoCreatePath(const char *fn)
return; return;
} }
char path[PATH_MAX]; char path[_MAX_PATH];
_makepath_s(path, sizeof path, drive, dir, nullptr, nullptr); _makepath_s(path, sizeof path, drive, dir, nullptr, nullptr);
if ('\0' == *path) if ('\0' == *path)
@ -746,36 +708,6 @@ FString strbin1 (const char *start)
return result; return result;
} }
//==========================================================================
//
// CleanseString
//
// Does some mild sanity checking on a string: If it ends with an incomplete
// color escape, the escape is removed.
//
//==========================================================================
char *CleanseString(char *str)
{
char *escape = strrchr(str, TEXTCOLOR_ESCAPE);
if (escape != NULL)
{
if (escape[1] == '\0')
{
*escape = '\0';
}
else if (escape[1] == '[')
{
char *close = strchr(escape + 2, ']');
if (close == NULL)
{
*escape = '\0';
}
}
}
return str;
}
//========================================================================== //==========================================================================
// //
// ExpandEnvVars // ExpandEnvVars
@ -904,7 +836,7 @@ FString NicePath(const char *path)
// //
//========================================================================== //==========================================================================
void ScanDirectory(TArray<FFileList> &list, const char *dirpath) bool ScanDirectory(TArray<FFileList> &list, const char *dirpath)
{ {
struct _finddata_t fileinfo; struct _finddata_t fileinfo;
intptr_t handle; intptr_t handle;
@ -914,7 +846,7 @@ void ScanDirectory(TArray<FFileList> &list, const char *dirpath)
if ((handle = _findfirst(dirmatch, &fileinfo)) == -1) if ((handle = _findfirst(dirmatch, &fileinfo)) == -1)
{ {
I_Error("Could not scan '%s': %s\n", dirpath, strerror(errno)); return false;
} }
else else
{ {
@ -954,6 +886,7 @@ void ScanDirectory(TArray<FFileList> &list, const char *dirpath)
while (_findnext(handle, &fileinfo) == 0); while (_findnext(handle, &fileinfo) == 0);
_findclose(handle); _findclose(handle);
} }
return true;
} }
#elif defined(__sun) || defined(__linux__) #elif defined(__sun) || defined(__linux__)
@ -967,11 +900,11 @@ void ScanDirectory(TArray<FFileList> &list, const char *dirpath)
// //
//========================================================================== //==========================================================================
void ScanDirectory(TArray<FFileList> &list, const char *dirpath) bool ScanDirectory(TArray<FFileList> &list, const char *dirpath)
{ {
DIR *directory = opendir(dirpath); DIR *directory = opendir(dirpath);
if(directory == NULL) if(directory == NULL)
return; return false;
struct dirent *file; struct dirent *file;
while((file = readdir(directory)) != NULL) while((file = readdir(directory)) != NULL)
@ -993,6 +926,7 @@ void ScanDirectory(TArray<FFileList> &list, const char *dirpath)
} }
closedir(directory); closedir(directory);
return true;
} }
#else #else
@ -1004,7 +938,7 @@ void ScanDirectory(TArray<FFileList> &list, const char *dirpath)
// //
//========================================================================== //==========================================================================
void ScanDirectory(TArray<FFileList> &list, const char *dirpath) bool ScanDirectory(TArray<FFileList> &list, const char *dirpath)
{ {
char * const argv[] = {new char[strlen(dirpath)+1], NULL }; char * const argv[] = {new char[strlen(dirpath)+1], NULL };
memcpy(argv[0], dirpath, strlen(dirpath)+1); memcpy(argv[0], dirpath, strlen(dirpath)+1);
@ -1014,9 +948,8 @@ void ScanDirectory(TArray<FFileList> &list, const char *dirpath)
fts = fts_open(argv, FTS_LOGICAL, NULL); fts = fts_open(argv, FTS_LOGICAL, NULL);
if (fts == NULL) if (fts == NULL)
{ {
I_Error("Failed to start directory traversal: %s\n", strerror(errno));
delete[] argv[0]; delete[] argv[0];
return; return false;
} }
while ((ent = fts_read(fts)) != NULL) while ((ent = fts_read(fts)) != NULL)
{ {
@ -1042,6 +975,7 @@ void ScanDirectory(TArray<FFileList> &list, const char *dirpath)
} }
fts_close(fts); fts_close(fts);
delete[] argv[0]; delete[] argv[0];
return true;
} }
#endif #endif

View file

@ -4,16 +4,25 @@
#define __CMDLIB__ #define __CMDLIB__
#include "doomtype.h"
#include "doomdef.h"
#include "m_fixed.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <errno.h> #include <errno.h>
#include <ctype.h> #include <ctype.h>
#include <stdarg.h> #include <stdarg.h>
#include "zstring.h"
#if !defined(GUID_DEFINED)
#define GUID_DEFINED
typedef struct _GUID
{
uint32_t Data1;
uint16_t Data2;
uint16_t Data3;
uint8_t Data4[8];
} GUID;
#endif
// the dec offsetof macro doesnt work very well... // the dec offsetof macro doesnt work very well...
#define myoffsetof(type,identifier) ((size_t)&((type *)alignof(type))->identifier - alignof(type)) #define myoffsetof(type,identifier) ((size_t)&((type *)alignof(type))->identifier - alignof(type))
@ -33,7 +42,6 @@ FString ExtractFilePath (const char *path);
FString ExtractFileBase (const char *path, bool keep_extension=false); FString ExtractFileBase (const char *path, bool keep_extension=false);
struct FScriptPosition; struct FScriptPosition;
int ParseHex(const char *str, FScriptPosition *sc = nullptr);
bool IsNum (const char *str); // [RH] added bool IsNum (const char *str); // [RH] added
char *copystring(const char *s); char *copystring(const char *s);
@ -47,7 +55,6 @@ const char *myasctime ();
int strbin (char *str); int strbin (char *str);
FString strbin1 (const char *start); FString strbin1 (const char *start);
char *CleanseString (char *str);
void CreatePath(const char * fn); void CreatePath(const char * fn);
@ -60,14 +67,9 @@ struct FFileList
bool isDirectory; bool isDirectory;
}; };
void ScanDirectory(TArray<FFileList> &list, const char *dirpath); bool ScanDirectory(TArray<FFileList> &list, const char *dirpath);
bool IsAbsPath(const char*); bool IsAbsPath(const char*);
inline int Tics2Seconds(int tics)
{
return tics / TICRATE;
}
#endif #endif

View file

@ -1259,4 +1259,37 @@ void FScriptPosition::Message (int severity, const char *message, ...) const
color, type, FileName.GetChars(), ScriptLine, color, composed.GetChars()); color, type, FileName.GetChars(), ScriptLine, color, composed.GetChars());
} }
//==========================================================================
//
// ParseHex
//
//==========================================================================
int ParseHex(const char* hex, FScriptPosition* sc)
{
const char* str;
int num;
num = 0;
str = hex;
while (*str)
{
num <<= 4;
if (*str >= '0' && *str <= '9')
num += *str - '0';
else if (*str >= 'a' && *str <= 'f')
num += 10 + *str - 'a';
else if (*str >= 'A' && *str <= 'F')
num += 10 + *str - 'A';
else {
sc->Message(MSG_WARNING, "Bad hex number: %s", hex);
return 0;
}
str++;
}
return num;
}

View file

@ -185,5 +185,7 @@ struct FScriptPosition
} }
}; };
int ParseHex(const char* hex, FScriptPosition* sc);
#endif //__SC_MAN_H__ #endif //__SC_MAN_H__

View file

@ -38,6 +38,7 @@
#include <shlobj.h> #include <shlobj.h>
#include "cmdlib.h" #include "cmdlib.h"
#include "doomtype.h"
#include "m_misc.h" #include "m_misc.h"
#include "version.h" // for GAMENAME #include "version.h" // for GAMENAME