1 /*
2  * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
3  * It is copyright by its individual contributors, as recorded in the
4  * project's Git history.  See COPYING.txt at the top level for license
5  * terms and a link to the Git history.
6  */
7 #pragma once
8 
9 #ifdef __cplusplus
10 #include <cstddef>
11 #include <cstdint>
12 #include <cstring>
13 #include "dxxsconf.h"
14 #include "dsx-ns.h"
15 #include <array>
16 #include <vector>
17 #if DXX_USE_EDITOR
18 #include <limits.h>	/* PATH_MAX */
19 #endif
20 
21 namespace dcx {
22 
23 #if defined(macintosh)
24 #define snprintf macintosh_snprintf
25 extern void snprintf(char *out_string, int size, const char * format, ... );
26 #endif
27 
28 #ifdef DXX_HAVE_STRCASECMP
29 #define d_stricmp strcasecmp
d_strnicmp(const char * s1,const char * s2,size_t n)30 static inline int d_strnicmp(const char *s1, const char *s2, size_t n)
31 {
32 	return strncasecmp(s1, s2, n);
33 }
34 #else
35 __attribute_nonnull()
36 extern int d_stricmp( const char *s1, const char *s2 );
37 __attribute_nonnull()
38 int d_strnicmp(const char *s1, const char *s2, uint_fast32_t n);
39 #endif
40 extern void d_strlwr( char *s1 );
41 #ifdef DEBUG_MEMORY_ALLOCATIONS
42 char *d_strdup(const char *str, const char *, const char *, unsigned) __attribute_malloc();
43 #define d_strdup(str)	(d_strdup(str, #str, __FILE__,__LINE__))
44 #else
45 #include <cstring>
46 #define d_strdup strdup
47 #endif
48 
49 #if DXX_USE_EDITOR
50 void d_strupr(std::array<char, PATH_MAX> &out, const std::array<char, PATH_MAX> &in);
51 #endif
52 
53 template <std::size_t N>
d_strnicmp(const char * s1,const char (& s2)[N])54 static inline int d_strnicmp(const char *s1, const char (&s2)[N])
55 {
56 	return d_strnicmp(s1, s2, N - 1);
57 }
58 
59 struct splitpath_t
60 {
61 	const char *drive_start, *drive_end, *path_start, *path_end, *base_start, *base_end, *ext_start;
62 };
63 
64 // remove extension from filename, doesn't work with paths.
65 void removeext(const char *filename, std::array<char, 20> &out);
66 
67 //give a filename a new extension, doesn't work with paths with no extension already there
68 extern void change_filename_extension( char *dest, const char *src, const char *new_ext );
69 
70 // split an MS-DOS path into drive, directory path, filename without the extension (base) and extension.
71 // if it's just a filename with no directory specified, this function will get 'base' and 'ext'
72 void d_splitpath(const char *name, struct splitpath_t *path);
73 
74 class string_array_t
75 {
76 	typedef std::vector<const char *> ptr_t;
77 	std::vector<char> buffer;
78 	ptr_t ptr;
79 public:
pointer()80 	ptr_t &pointer() { return ptr; }
clear()81 	void clear()
82 	{
83 		ptr.clear();
84 		buffer.clear();
85 	}
86 	void add(const char *);
87 	void tidy(std::size_t offset);
88 };
89 
90 int string_array_sort_func(const void *v0, const void *v1);
91 
92 /* Compute the number of digits required to represent MaximumValue in
93  * the given Base.  The result does not account for any characters other
94  * than the required digits.
95  */
96 template <std::size_t MaximumValue, std::size_t Base = 10>
97 constexpr std::size_t number_to_text_length = (MaximumValue < Base) ? 1 : 1 + number_to_text_length<MaximumValue / Base, Base>;
98 static_assert(number_to_text_length<1> == 1, "");
99 static_assert(number_to_text_length<9> == 1, "");
100 static_assert(number_to_text_length<10> == 2, "");
101 static_assert(number_to_text_length<255, 16> == 2, "");
102 static_assert(number_to_text_length<256, 16> == 3, "");
103 
104 }
105 
106 #endif
107