diff options
Diffstat (limited to 'src/m_misc.c')
-rw-r--r-- | src/m_misc.c | 151 |
1 files changed, 148 insertions, 3 deletions
diff --git a/src/m_misc.c b/src/m_misc.c index 31c87898..f3e11c36 100644 --- a/src/m_misc.c +++ b/src/m_misc.c @@ -2,6 +2,7 @@ //----------------------------------------------------------------------------- // // Copyright(C) 1993-1996 Id Software, Inc. +// Copyright(C) 1993-2008 Raven Software // Copyright(C) 2005 Simon Howard // // This program is free software; you can redistribute it and/or @@ -27,6 +28,7 @@ #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <ctype.h> #include <errno.h> @@ -42,10 +44,9 @@ #include <sys/types.h> #endif -#include "doomdef.h" -#include "doomstat.h" +#include "doomtype.h" -#include "deh_main.h" +#include "deh_str.h" #include "i_swap.h" #include "i_system.h" @@ -206,6 +207,150 @@ boolean M_StrToInt(const char *str, int *result) || sscanf(str, " %d", result) == 1; } +void M_ExtractFileBase(char *path, char *dest) +{ + char *src; + char *filename; + int length; + + src = path + strlen(path) - 1; + + // back up until a \ or the start + while (src != path && *(src - 1) != DIR_SEPARATOR) + { + src--; + } + + filename = src; + + // Copy up to eight characters + // Note: Vanilla Doom exits with an error if a filename is specified + // with a base of more than eight characters. To remove the 8.3 + // filename limit, instead we simply truncate the name. + + length = 0; + memset(dest, 0, 8); + + while (*src != '\0' && *src != '.') + { + if (length >= 8) + { + printf("Warning: Truncated '%s' lump name to '%.8s'.\n", + filename, dest); + break; + } + + dest[length++] = toupper((int)*src++); + } +} + +//--------------------------------------------------------------------------- +// +// PROC M_ForceUppercase +// +// Change string to uppercase. +// +//--------------------------------------------------------------------------- + +void M_ForceUppercase(char *text) +{ + char *p; + + for (p = text; *p != '\0'; ++p) + { + *p = toupper(*p); + } +} + +// +// M_StrCaseStr +// +// Case-insensitive version of strstr() +// + +char *M_StrCaseStr(char *haystack, char *needle) +{ + unsigned int haystack_len; + unsigned int needle_len; + unsigned int len; + unsigned int i; + + haystack_len = strlen(haystack); + needle_len = strlen(needle); + + if (haystack_len < needle_len) + { + return NULL; + } + + len = haystack_len - needle_len; + + for (i = 0; i <= len; ++i) + { + if (!strncasecmp(haystack + i, needle, needle_len)) + { + return haystack + i; + } + } + + return NULL; +} + +// +// String replace function. +// Returns a Z_Malloc()ed string. +// + +char *M_StringReplace(char *haystack, char *needle, char *replacement) +{ + char *result, *p, *dst; + size_t needle_len = strlen(needle); + int n; + + // Count number of occurrences of 'p': + + for (p = haystack, n = 0;; ++n) + { + p = strstr(p, needle); + + if (p == NULL) + { + break; + } + + p += needle_len; + } + + // Construct new string. + + result = Z_Malloc(strlen(haystack) + + (strlen(replacement) - needle_len) * n + + 1, + PU_STATIC, NULL); + + dst = result; + p = haystack; + + while (*p != '\0') + { + if (!strncmp(p, needle, needle_len)) + { + strcpy(dst, replacement); + dst += strlen(replacement); + p += needle_len; + } + else + { + *dst = *p; + ++dst; + ++p; + } + } + *dst = '\0'; + + return result; +} + #ifdef _WIN32 char *M_OEMToUTF8(const char *oem) |