| 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 |
#include <cstddef> |
| 10 |
#include <cstdint> |
| 11 |
#include <cstring> |
| 12 |
#include <span> |
| 13 |
#include "dxxsconf.h" |
| 14 |
#include "u_mem.h" |
| 15 |
#include "dsx-ns.h" |
| 16 |
#include <array> |
| 17 |
#include <vector> |
| 18 |
#if DXX_USE_EDITOR |
| 19 |
#include <limits.h> /* PATH_MAX */ |
| 20 |
#endif |
| 21 |
|
| 22 |
namespace dcx { |
| 23 |
|
| 24 |
#if defined(macintosh) |
| 25 |
#define snprintf macintosh_snprintf |
| 26 |
extern void snprintf(char *out_string, int size, const char * format, ... ); |
| 27 |
#endif |
| 28 |
|
| 29 |
#ifdef DXX_HAVE_STRCASECMP |
| 30 |
#define d_stricmp strcasecmp |
| 31 |
static inline int d_strnicmp(const char *s1, const char *s2, size_t n) |
| 32 |
{ |
| 33 |
return strncasecmp(s1, s2, n); |
| 34 |
} |
| 35 |
#else |
| 36 |
dxx_compiler_attribute_nonnull() |
| 37 |
extern int d_stricmp( const char *s1, const char *s2 ); |
| 38 |
dxx_compiler_attribute_nonnull() |
| 39 |
int d_strnicmp(const char *s1, const char *s2, uint_fast32_t n); |
| 40 |
#endif |
| 41 |
extern void d_strlwr( char *s1 ); |
| 42 |
std::unique_ptr<char[]> d_strdup(const char *str); |
| 43 |
|
| 44 |
#if DXX_USE_EDITOR |
| 45 |
void d_strupr(std::array<char, PATH_MAX> &out, const std::array<char, PATH_MAX> &in); |
| 46 |
#endif |
| 47 |
|
| 48 |
template <std::size_t N> |
| 49 |
static inline int d_strnicmp(const char *s1, const char (&s2)[N]) |
| 50 |
{ |
| 51 |
return d_strnicmp(s1, s2, N - 1); |
| 52 |
} |
| 53 |
|
| 54 |
struct splitpath_t |
| 55 |
{ |
| 56 |
const char *const base_start; |
| 57 |
const char *const base_end; |
| 58 |
}; |
| 59 |
|
| 60 |
//give a filename a new extension, doesn't work with paths with no extension already there |
| 61 |
[[nodiscard]] |
| 62 |
bool change_filename_extension(std::span<char> dest, const char *src, std::span<const char, 4> ext); |
| 63 |
|
| 64 |
/* Given an MS-DOS path, return pointers to the start of the basename and the |
| 65 |
* start of the extension. |
| 66 |
*/ |
| 67 |
splitpath_t d_splitpath(const char *name); |
| 68 |
|
| 69 |
class string_array_t |
| 70 |
{ |
| 71 |
typedef std::vector<const char *> ptr_t; |
| 72 |
std::vector<char> buffer; |
| 73 |
ptr_t ptr; |
| 74 |
public: |
| 75 |
ptr_t &pointer() { return ptr; } |
| 76 |
void clear() |
| 77 |
{ |
| 78 |
ptr.clear(); |
| 79 |
buffer.clear(); |
| 80 |
} |
| 81 |
void add(const char *); |
| 82 |
void tidy(std::size_t offset); |
| 83 |
}; |
| 84 |
|
| 85 |
int string_array_sort_func(const void *v0, const void *v1); |
| 86 |
|
| 87 |
/* Compute the number of digits required to represent MaximumValue in |
| 88 |
* the given Base. The result does not account for any characters other |
| 89 |
* than the required digits. |
| 90 |
*/ |
| 91 |
template <std::size_t MaximumValue, std::size_t Base = 10> |
| 92 |
constexpr std::size_t number_to_text_length = (MaximumValue < Base) ? 1 : 1 + number_to_text_length<MaximumValue / Base, Base>; |
| 93 |
static_assert(number_to_text_length<1> == 1, ""); |
| 94 |
static_assert(number_to_text_length<9> == 1, ""); |
| 95 |
static_assert(number_to_text_length<10> == 2, ""); |
| 96 |
static_assert(number_to_text_length<255, 16> == 2, ""); |
| 97 |
static_assert(number_to_text_length<256, 16> == 3, ""); |
| 98 |
|
| 99 |
} |