forked from bradharding/doomretro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm_misc.c
1160 lines (908 loc) · 27.4 KB
/
m_misc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
==============================================================================
DOOM Retro
The classic, refined DOOM source port. For Windows PC.
==============================================================================
Copyright © 1993-2024 by id Software LLC, a ZeniMax Media company.
Copyright © 2013-2024 by Brad Harding <mailto:[email protected]>.
This file is a part of DOOM Retro.
DOOM Retro 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.
DOOM Retro 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 DOOM Retro. If not, see <https://www.gnu.org/licenses/>.
DOOM is a registered trademark of id Software LLC, a ZeniMax Media
company, in the US and/or other countries, and is used without
permission. All other trademarks are the property of their respective
holders. DOOM Retro is in no way affiliated with nor endorsed by
id Software.
==============================================================================
*/
#if defined(_WIN32)
#pragma warning( disable : 4091 )
#include <Windows.h>
#include <io.h>
#include <sys/types.h>
#include <sys/stat.h>
#if defined(_MSC_VER)
#include <direct.h>
#endif
#else
#include <sys/stat.h>
#include <sys/types.h>
#include <pwd.h>
#endif
#include <ctype.h>
#include <math.h>
#include <stdarg.h>
#include <string.h>
#include "c_console.h"
#include "i_system.h"
#include "m_config.h"
#include "m_misc.h"
#include "version.h"
#include "w_file.h"
#if defined(__APPLE__)
#import <Cocoa/Cocoa.h>
#include <dirent.h>
#include <libgen.h>
#include <mach-o/dyld.h>
#include <errno.h>
#elif defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
#include <sys/sysctl.h>
#include <dirent.h>
#include <errno.h>
#include <libgen.h>
#include <unistd.h>
#elif defined(__linux__) || defined(__HAIKU__) || defined(__sun)
#include <dirent.h>
#include <errno.h>
#include <libgen.h>
#include <unistd.h>
#if defined(__HAIKU__)
#include <FindDirectory.h>
#endif
#endif
const char *daynames[7] =
{
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
};
const char *monthnames[12] =
{
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
// Create a directory
void M_MakeDirectory(const char *path)
{
#if defined(_WIN32)
mkdir(path);
#else
mkdir(path, 0755);
#endif
}
// Check if a file exists
bool M_FileExists(const char *filename)
{
FILE *fstream = fopen(filename, "r");
if (fstream)
{
fclose(fstream);
return true;
}
return false;
}
#if !defined(_WIN32) && !defined(__APPLE__)
bool file_exists_get_path(const char *basedir, const char *filename, char **retpath)
{
*retpath = M_StringJoin(basedir, DIR_SEPARATOR_S, filename, NULL);
if (M_FileExists(*retpath))
return true;
free(*retpath);
*retpath = NULL;
return false;
}
// Check if a file exists by probing for common case variation of its filename.
// Returns a newly allocated string that the caller is responsible for freeing.
char *M_FileCaseExists(const char *path)
{
char *basedir;
char *filename;
char *retpath;
char *tmpfilename = NULL;
char *pos;
// actual path
if (M_FileExists(path))
return M_StringDuplicate(path);
if ((pos = strrchr(path, DIR_SEPARATOR)))
{
basedir = M_SubString(path, 0, pos - path);
filename = M_StringDuplicate(pos + 1);
}
else
{
basedir = ".";
filename = M_StringDuplicate(path);
}
// lowercase filename, e.g. doom2.wad
if (file_exists_get_path(basedir, lowercase(filename), &retpath))
goto cleanup;
// uppercase filename, e.g. DOOM2.WAD
tmpfilename = uppercase(filename);
if (file_exists_get_path(basedir, tmpfilename, &retpath))
goto cleanup;
// uppercase basename with lowercase extension, e.g. DOOM2.wad
if ((pos = strrchr(tmpfilename, '.')) && tmpfilename[strlen(tmpfilename) - 1] != '.')
{
lowercase(pos + 1);
if(file_exists_get_path(basedir, tmpfilename, &retpath))
goto cleanup;
}
// lowercase filename with uppercase first letter, e.g. Doom2.wad
if (strlen(tmpfilename) > 1)
{
lowercase(tmpfilename + 1);
if (file_exists_get_path(basedir, tmpfilename, &retpath))
goto cleanup;
}
cleanup:
free(filename);
if (tmpfilename)
free(tmpfilename);
if (strcmp(basedir, "."))
free(basedir);
return retpath;
}
#endif
// Check if a folder exists
bool M_FolderExists(const char *folder)
{
struct stat status;
return (!stat(folder, &status) && (status.st_mode & S_IFDIR));
}
// Safe string copy function that works like OpenBSD's strlcpy().
void M_StringCopy(char *dest, const char *src, const size_t dest_size)
{
if (dest_size >= 1)
{
dest[dest_size - 1] = '\0';
if (dest_size > 1)
strncpy(dest, src, dest_size - 1);
}
}
char *M_ExtractFolder(const char *path)
{
char *pos;
char *folder;
if (!*path)
return "";
folder = M_StringDuplicate(path);
if ((pos = strrchr(folder, DIR_SEPARATOR)))
*pos = '\0';
return folder;
}
char *M_GetAppDataFolder(void)
{
char *executablefolder = M_GetExecutableFolder();
#if defined(_WIN32)
return executablefolder;
#else
// On Linux and macOS, if ../share/doomretro doesn't exist then we're dealing with
// a portable installation, and we write doomretro.cfg to the executable directory.
char *resourcefolder = M_StringJoin(executablefolder,
DIR_SEPARATOR_S ".." DIR_SEPARATOR_S "share" DIR_SEPARATOR_S DOOMRETRO, NULL);
DIR *resourcedir = opendir(resourcefolder);
free(resourcefolder);
if (resourcedir)
{
#if defined(__APPLE__)
// On macOS, store generated application files in ~/Library/Application Support/DOOM Retro.
NSFileManager *manager = [NSFileManager defaultManager];
NSURL *baseAppSupportURL = [manager URLsForDirectory : NSApplicationSupportDirectory
inDomains : NSUserDomainMask].firstObject;
NSURL *appSupportURL = [baseAppSupportURL URLByAppendingPathComponent : @DOOMRETRO_NAME];
closedir(resourcedir);
return (char *)appSupportURL.fileSystemRepresentation;
#else
// On Linux, store generated application files in /home/<username>/.config/doomretro
char *buffer = getenv("HOME");
if (!buffer)
buffer = getpwuid(getuid())->pw_dir;
closedir(resourcedir);
free(executablefolder);
#if defined(__HAIKU__)
return M_StringJoin(buffer, DIR_SEPARATOR_S "config" DIR_SEPARATOR_S "settings" DIR_SEPARATOR_S DOOMRETRO, NULL);
#else
return M_StringJoin(buffer, DIR_SEPARATOR_S ".config" DIR_SEPARATOR_S DOOMRETRO, NULL);
#endif
#endif
}
else
return executablefolder;
#endif
}
char *M_GetResourceFolder(void)
{
char *executablefolder = M_GetExecutableFolder();
#if !defined(_WIN32)
// On Linux and macOS, first assume that the executable is in ../bin and
// try to load resources from ../share/doomretro.
char *resourcefolder = M_StringJoin(executablefolder,
DIR_SEPARATOR_S ".." DIR_SEPARATOR_S "share" DIR_SEPARATOR_S DOOMRETRO, NULL);
DIR *resourcedir = opendir(resourcefolder);
if (resourcedir)
{
closedir(resourcedir);
free(executablefolder);
return resourcefolder;
}
#if defined(__APPLE__)
// On macOS, load resources from the Contents/Resources folder within the application bundle
// if ../share/doomretro is not available.
NSURL *resourceURL = [NSBundle mainBundle].resourceURL;
return (char *)resourceURL.fileSystemRepresentation;
#else
free(resourcefolder);
// And on Linux, fall back to the same folder as the executable.
return executablefolder;
#endif
#else
// On Windows, load resources from the same folder as the executable.
return executablefolder;
#endif
}
char *M_GetExecutableFolder(void)
{
#if defined(_WIN32)
char *folder = malloc(MAX_PATH);
if (folder)
{
char *pos;
GetModuleFileName(NULL, folder, MAX_PATH);
if ((pos = strrchr(folder, DIR_SEPARATOR)))
*pos = '\0';
}
return folder;
#elif defined(__linux__) || defined(__NetBSD__) || defined(__sun)
char exe[MAX_PATH];
#if defined(__linux__)
ssize_t len = readlink("/proc/self/exe", exe, MAX_PATH - 1);
#elif defined(__NetBSD__)
ssize_t len = readlink("/proc/curproc/exe", exe, MAX_PATH - 1);
#elif defined(__sun)
ssize_t len = readlink("/proc/self/path/a.out", exe, MAX_PATH - 1);
#endif
if (len == -1)
{
strcpy(exe, ".");
return M_StringDuplicate(exe);
}
else
{
exe[len] = '\0';
return M_StringDuplicate(dirname(exe));
}
#elif defined(__FreeBSD__) || defined(__DragonFly__)
char exe[MAX_PATH];
size_t len = MAX_PATH;
int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
if (!sysctl(mib, 4, exe, &len, NULL, 0))
{
exe[len] = '\0';
return M_StringDuplicate(dirname(exe));
}
else
{
strcpy(exe, ".");
return M_StringDuplicate(exe);
}
#elif defined(__APPLE__)
char exe[MAX_PATH];
uint32_t len = MAX_PATH;
if (_NSGetExecutablePath(exe, &len))
{
strcpy(exe, ".");
return M_StringDuplicate(exe);
}
return M_StringDuplicate(dirname(exe));
#elif defined(__HAIKU__)
char exe[MAX_PATH];
exe[0] = '\0';
if (find_path(B_APP_IMAGE_SYMBOL, B_FIND_PATH_IMAGE_PATH, NULL, exe, MAX_PATH) == B_OK)
return M_StringDuplicate(dirname(exe));
strcpy(exe, ".");
return M_StringDuplicate(exe);
#else
char *folder = malloc(2);
strcpy(folder, ".");
return folder;
#endif
}
char *M_TempFile(char *s)
{
char *tempdir;
#if defined(_WIN32)
tempdir = getenv("TEMP");
if (!tempdir)
tempdir = ".";
#else
tempdir = "/tmp";
#endif
return M_StringJoin(tempdir, DIR_SEPARATOR_S, s, NULL);
}
// Return a newly-malloced string with all the strings given as arguments
// concatenated together.
char *M_StringJoin(const char *s, ...)
{
char *result;
const char *v;
va_list args;
size_t result_len = strlen(s) + 1;
va_start(args, s);
while (true)
{
if (!(v = va_arg(args, const char *)))
break;
result_len += strlen(v);
}
va_end(args);
if (!(result = malloc(result_len)))
return NULL;
M_StringCopy(result, s, result_len);
va_start(args, s);
while (true)
{
if (!(v = va_arg(args, const char *)))
break;
strncat(result, v, result_len - strlen(result) - 1);
}
va_end(args);
return result;
}
bool M_StrToInt(const char *str, int *result)
{
return (sscanf(str, " 0x%2x", (unsigned int *)result) == 1
|| sscanf(str, " 0X%2x", (unsigned int *)result) == 1
|| sscanf(str, " 0%3o", (unsigned int *)result) == 1
|| sscanf(str, " %12d", result) == 1);
}
// Case-insensitive version of strstr()
const char *M_StrCaseStr(const char *haystack, const char *needle)
{
const int haystack_len = (int)strlen(haystack);
const int needle_len = (int)strlen(needle);
int len;
if (haystack_len < needle_len)
return NULL;
len = haystack_len - needle_len;
for (int i = 0; i <= len; i++)
if (!strncasecmp(haystack + i, needle, needle_len))
return (haystack + i);
return NULL;
}
#if !defined(stristr)
static char *stristr(char *ch1, const char *ch2)
{
char *chN1 = M_StringDuplicate(ch1);
char *chN2 = M_StringDuplicate(ch2);
char *chRet = NULL;
if (chN1 && chN2)
{
char *chNdx = chN1;
while (*chNdx)
{
*chNdx = (char)tolower(*chNdx);
chNdx++;
}
chNdx = chN2;
while (*chNdx)
{
*chNdx = (char)tolower(*chNdx);
chNdx++;
}
if ((chNdx = strstr(chN1, chN2)))
chRet = ch1 + (chNdx - chN1);
}
free(chN1);
free(chN2);
return chRet;
}
#endif
// String replace function.
char *M_StringReplaceFirst(char *haystack, const char *needle, const char *replacement)
{
static char buffer[4096];
char *p;
if (!(p = stristr(haystack, (char *)needle)))
return haystack;
strncpy(buffer, haystack, p - haystack);
buffer[p - haystack] = '\0';
sprintf(buffer + (p - haystack), "%s%s", replacement, p + strlen(needle));
return buffer;
}
#if !defined(strrstr)
char *strrstr(const char *haystack, const char *needle)
{
char *r = NULL;
if (!needle[0])
return (char *)haystack + strlen(haystack);
while (true)
{
char *p = strstr(haystack, needle);
if (!p)
return r;
r = p;
haystack = p + 1;
}
}
#endif
char *M_StringReplaceLast(char *haystack, const char *needle, const char *replacement)
{
static char buffer[4096];
char *p;
if (!(p = strrstr(haystack, (char *)needle)))
return haystack;
strncpy(buffer, haystack, p - haystack);
buffer[p - haystack] = '\0';
sprintf(buffer + (p - haystack), "%s%s", replacement, p + strlen(needle));
return buffer;
}
void M_StringReplaceAll(char *haystack, const char *needle, const char *replacement, bool usecase)
{
char buffer[1024] = "";
char *insert_point = &buffer[0];
char *temp = haystack;
const int needle_len = (int)strlen(needle);
const int repl_len = (int)strlen(replacement);
while (true)
{
char *p = (usecase ? strstr(temp, (char *)needle) : stristr(temp, (char *)needle));
if (!p)
{
strcpy(insert_point, temp);
break;
}
memcpy(insert_point, temp, p - temp);
insert_point += p - temp;
memcpy(insert_point, replacement, repl_len);
insert_point += repl_len;
temp = p + needle_len;
}
strcpy(haystack, buffer);
}
// Safe version of strdup() that checks the string was successfully allocated.
char *M_StringDuplicate(const char *orig)
{
char *result = strdup(orig);
if (!result)
I_Error("Failed to duplicate string.");
return result;
}
// Returns true if str1 and str2 are the same.
// (Case-insensitive, return value reverse of strcasecmp() to avoid confusion.
bool M_StringCompare(const char *str1, const char *str2)
{
return !strcasecmp(str1, str2);
}
// Returns true if string begins with the specified prefix.
bool M_StringStartsWith(const char *s, const char *prefix)
{
const size_t len = strlen(prefix);
return (strlen(s) >= len && !strncasecmp(s, prefix, len));
}
// Returns true if string ends with the specified suffix.
bool M_StringEndsWith(const char *s, const char *suffix)
{
const size_t len1 = strlen(s);
const size_t len2 = strlen(suffix);
return (len1 >= len2 && M_StringCompare(s + len1 - len2, suffix));
}
// Safe, portable vsnprintf().
void M_vsnprintf(char *buf, int buf_len, const char *s, va_list args)
{
if (buf_len >= 1)
{
// Windows (and other OSes?) have a vsnprintf() that doesn't always
// append a trailing \0. So we must do it, and write into a buffer
// that is one byte shorter; otherwise this function is unsafe.
const int result = vsnprintf(buf, buf_len, s, args);
// If truncated, change the final char in the buffer to a \0.
// A negative result indicates a truncated buffer on Windows.
if (result < 0 || result >= buf_len)
buf[buf_len - 1] = '\0';
}
}
// Safe, portable snprintf().
void M_snprintf(char *buf, int buf_len, const char *s, ...)
{
va_list args;
va_start(args, s);
M_vsnprintf(buf, buf_len, s, args);
va_end(args);
}
#if !defined(strndup)
char *strndup(const char *s, size_t n)
{
const size_t len = strnlen(s, n);
char *new = malloc(len + 1);
if (!new)
return NULL;
new[len] = '\0';
return (char *)memcpy(new, s, len);
}
#endif
char *M_SubString(const char *str, size_t begin, size_t len)
{
const size_t length = strlen(str);
if (!length || length < begin || length < begin + len)
return 0;
return strndup(str + begin, len);
}
char *uppercase(const char *str)
{
char *newstr;
char *p = newstr = M_StringDuplicate(str);
while ((*p = toupper(*p)))
p++;
return newstr;
}
char *lowercase(char *str)
{
for (char *p = str; *p; p++)
*p = tolower(*p);
return str;
}
char *titlecase(const char *str)
{
char *newstr = M_StringDuplicate(str);
const int len = (int)strlen(newstr);
if (len > 0)
{
newstr[0] = toupper(newstr[0]);
if (len > 1)
for (int i = 1; i < len; i++)
if ((newstr[i - 1] != '\'' || (i >= 2 && newstr[i - 2] == ' '))
&& !isalnum((unsigned char)newstr[i - 1])
&& isalnum((unsigned char)newstr[i]))
newstr[i] = toupper(newstr[i]);
}
return newstr;
}
char *sentencecase(const char *str)
{
char *newstr = M_StringDuplicate(str);
if (newstr[0] != '\0')
newstr[0] = toupper(newstr[0]);
return newstr;
}
bool isuppercase(const char *str)
{
const int len = (int)strlen(str);
for (int i = 0; i < len; i++)
if (islower(str[i]))
return false;
return true;
}
bool islowercase(const char *str)
{
const int len = (int)strlen(str);
for (int i = 0; i < len; i++)
if (isupper(str[i]))
return false;
return true;
}
char *commify(int64_t value)
{
char result[64];
M_snprintf(result, sizeof(result), "%" PRIi64, value);
if (value <= -1000 || value >= 1000)
{
char *pt;
size_t n;
for (pt = result; *pt && *pt != '.'; pt++);
n = result + sizeof(result) - pt;
while (true)
if ((pt -= 3) > result)
{
memmove(pt + 1, pt, n);
*pt = ',';
n += 4;
}
else
break;
}
return M_StringDuplicate(result);
}
char *commifystat(uint64_t value)
{
char result[64];
M_snprintf(result, sizeof(result), "%" PRIu64, value);
if (value >= 1000)
{
char *pt;
size_t n;
for (pt = result; *pt && *pt != '.'; pt++);
n = result + sizeof(result) - pt;
while (true)
if ((pt -= 3) > result)
{
memmove(pt + 1, pt, n);
*pt = ',';
n += 4;
}
else
break;
}
return M_StringDuplicate(result);
}
char *uncommify(const char *input)
{
char *p;
if (!*input)
return "";
if ((p = malloc(strlen(input) + 1)))
{
char *p2 = p;
while (*input != '\0')
if (*input != ',' || *(input + 1) == '\0')
*p2++ = *input++;
else
input++;
*p2 = '\0';
}
return p;
}
bool wildcard(char *input, char *pattern)
{
if (!*pattern || M_StringCompare(input, pattern))
return true;
for (int i = 0; pattern[i] != '\0'; i++)
{
if (pattern[i] == '?')
continue;
else if (pattern[i] == '*')
{
for (int j = i; input[j] != '\0'; j++)
if (wildcard(input + j, pattern + i + 1))
return true;
return false;
}
else if (pattern[i] != input[i])
return false;
}
return false;
}
int gcd(int a, int b)
{
return (!b ? a : gcd(b, a % b));
}
int numspaces(const char *str)
{
int result = 0;
const int len = (int)strlen(str);
for (int i = 0; i < len; i++)
result += (str[i] == ' ');
return result;
}
char *removespaces(const char *input)
{
char *p;
if (!*input)
return "";
if ((p = malloc(strlen(input) + 1)))
{
char *p2 = p;
while (*input != '\0')
if (!isspace((unsigned char)*input))
*p2++ = *input++;
else
input++;
*p2 = '\0';
}
return p;
}
char *removenonalpha(const char *input)
{
char *p;
if (!*input)
return "";
if ((p = malloc(strlen(input) + 1)))
{
char *p2 = p;
while (*input != '\0')
if (isalnum((unsigned char)*input))
*p2++ = *input++;
else
input++;
*p2 = '\0';
}
return p;
}
char *trimwhitespace(char *input)
{
char *end;
while (isspace((unsigned char)*input))
input++;
if (!*input)
return input;
end = input + strlen(input) - 1;
while (end > input && isspace((unsigned char)*end))
end--;
*(end + 1) = '\0';
return input;
}
char *makevalidfilename(const char *input)
{
char *newstr = M_StringDuplicate(input);
const int len = (int)strlen(newstr);
for (int i = 0; i < len; i++)
if (strchr("\\/:?\"<>|", newstr[i]))
newstr[i] = ' ';
return newstr;
}
char *leafname(char *path)
{
char cc;
char *ptr = path;
do
{
cc = *ptr++;
if (cc == '\\' || cc == '/')
path = ptr;
} while (cc);
return path;
}
char *removeext(const char *file)
{
char *newstr = M_StringDuplicate(file);
char *lastdot = strrchr(newstr, '.');
*lastdot = '\0';
return newstr;
}
bool isvowel(const char ch)
{
return !!strchr("aeiouAEIOU", ch);
}
bool ispunctuation(const char ch)
{
return !!strchr(".!?", ch);
}
bool isbreak(const char ch)