Skip to content

Commit

Permalink
- removed all Doom specific dependencies from cmdlib.cpp/h.
Browse files Browse the repository at this point in the history
This meant moving CleanseString and ParseHex elsewhere and removing the I_Error call from ScanDirectory.
  • Loading branch information
coelckers committed Aug 20, 2019
1 parent 38fec54 commit 3cfda93
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 106 deletions.
6 changes: 6 additions & 0 deletions src/doomdef.h
Expand Up @@ -70,6 +70,12 @@ enum
TELEFRAG_DAMAGE = 1000000
};

inline int Tics2Seconds(int tics)
{
return tics / TICRATE;
}



typedef float skill_t;

Expand Down
30 changes: 30 additions & 0 deletions src/gamedata/fonts/v_font.cpp
Expand Up @@ -1745,3 +1745,33 @@ void V_ClearFonts()
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;
}

1 change: 1 addition & 0 deletions src/gamedata/fonts/v_font.h
Expand Up @@ -190,6 +190,7 @@ PalEntry V_LogColorFromColorRange (EColorRange range);
EColorRange V_ParseFontColor (const uint8_t *&color_value, int normalcolor, int boldcolor);
FFont *V_GetFont(const char *fontname, const char *fontlumpname = nullptr);
void V_InitFontColors();
char* CleanseString(char* str);


#endif //__V_FONT_H__
8 changes: 2 additions & 6 deletions src/maploader/glnodes.cpp
Expand Up @@ -1151,13 +1151,9 @@ UNSAFE_CCMD(clearnodecache)
FString path = M_GetCachePath(false);
path += "/";

try
{
ScanDirectory(list, path);
}
catch (CRecoverableError &err)
if (!ScanDirectory(list, path))
{
Printf("%s\n", err.GetMessage());
Printf("Unable to scan node cache directory %s\n", path);
return;
}

Expand Down
11 changes: 0 additions & 11 deletions src/utility/basictypes.h
Expand Up @@ -6,17 +6,6 @@
typedef uint32_t BITFIELD;
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.
//
Expand Down
88 changes: 11 additions & 77 deletions src/utility/cmdlib.cpp
Expand Up @@ -34,11 +34,7 @@
#include <fts.h>
#endif
#endif
#include "doomtype.h"
#include "cmdlib.h"
#include "doomerrors.h"
#include "v_text.h"
#include "sc_man.h"

#include <sys/types.h>
#include <sys/stat.h>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -408,7 +370,7 @@ bool CheckWildcards (const char *pattern, const char *text)

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,
guid.Data4[0], guid.Data4[1],
guid.Data4[2], guid.Data4[3],
Expand Down Expand Up @@ -463,7 +425,7 @@ void DoCreatePath(const char *fn)
return;
}

char path[PATH_MAX];
char path[_MAX_PATH];
_makepath_s(path, sizeof path, drive, dir, nullptr, nullptr);

if ('\0' == *path)
Expand Down Expand Up @@ -746,36 +708,6 @@ FString strbin1 (const char *start)
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
Expand Down Expand Up @@ -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;
intptr_t handle;
Expand All @@ -914,7 +846,7 @@ void ScanDirectory(TArray<FFileList> &list, const char *dirpath)

if ((handle = _findfirst(dirmatch, &fileinfo)) == -1)
{
I_Error("Could not scan '%s': %s\n", dirpath, strerror(errno));
return false;
}
else
{
Expand Down Expand Up @@ -954,6 +886,7 @@ void ScanDirectory(TArray<FFileList> &list, const char *dirpath)
while (_findnext(handle, &fileinfo) == 0);
_findclose(handle);
}
return true;
}

#elif defined(__sun) || defined(__linux__)
Expand All @@ -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);
if(directory == NULL)
return;
return false;

struct dirent *file;
while((file = readdir(directory)) != NULL)
Expand All @@ -993,6 +926,7 @@ void ScanDirectory(TArray<FFileList> &list, const char *dirpath)
}

closedir(directory);
return true;
}

#else
Expand All @@ -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 };
memcpy(argv[0], dirpath, strlen(dirpath)+1);
Expand All @@ -1014,9 +948,8 @@ void ScanDirectory(TArray<FFileList> &list, const char *dirpath)
fts = fts_open(argv, FTS_LOGICAL, NULL);
if (fts == NULL)
{
I_Error("Failed to start directory traversal: %s\n", strerror(errno));
delete[] argv[0];
return;
return false;
}
while ((ent = fts_read(fts)) != NULL)
{
Expand All @@ -1042,6 +975,7 @@ void ScanDirectory(TArray<FFileList> &list, const char *dirpath)
}
fts_close(fts);
delete[] argv[0];
return true;
}
#endif

Expand Down
26 changes: 14 additions & 12 deletions src/utility/cmdlib.h
Expand Up @@ -4,16 +4,25 @@
#define __CMDLIB__


#include "doomtype.h"
#include "doomdef.h"
#include "m_fixed.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.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...
#define myoffsetof(type,identifier) ((size_t)&((type *)alignof(type))->identifier - alignof(type))
Expand All @@ -33,7 +42,6 @@ FString ExtractFilePath (const char *path);
FString ExtractFileBase (const char *path, bool keep_extension=false);

struct FScriptPosition;
int ParseHex(const char *str, FScriptPosition *sc = nullptr);
bool IsNum (const char *str); // [RH] added

char *copystring(const char *s);
Expand All @@ -47,7 +55,6 @@ const char *myasctime ();

int strbin (char *str);
FString strbin1 (const char *start);
char *CleanseString (char *str);

void CreatePath(const char * fn);

Expand All @@ -60,14 +67,9 @@ struct FFileList
bool isDirectory;
};

void ScanDirectory(TArray<FFileList> &list, const char *dirpath);
bool ScanDirectory(TArray<FFileList> &list, const char *dirpath);
bool IsAbsPath(const char*);


inline int Tics2Seconds(int tics)
{
return tics / TICRATE;
}


#endif
33 changes: 33 additions & 0 deletions src/utility/sc_man.cpp
Expand Up @@ -1259,4 +1259,37 @@ void FScriptPosition::Message (int severity, const char *message, ...) const
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;
}


2 changes: 2 additions & 0 deletions src/utility/sc_man.h
Expand Up @@ -185,5 +185,7 @@ struct FScriptPosition
}
};

int ParseHex(const char* hex, FScriptPosition* sc);


#endif //__SC_MAN_H__
1 change: 1 addition & 0 deletions src/win32/i_specialpaths.cpp
Expand Up @@ -38,6 +38,7 @@
#include <shlobj.h>

#include "cmdlib.h"
#include "doomtype.h"
#include "m_misc.h"
#include "version.h" // for GAMENAME

Expand Down

0 comments on commit 3cfda93

Please sign in to comment.