| 1 |
/* |
| 2 |
* Portions of this file are copyright Rebirth contributors and licensed as |
| 3 |
* described in COPYING.txt. |
| 4 |
* Portions of this file are copyright Parallax Software and licensed |
| 5 |
* according to the Parallax license below. |
| 6 |
* See COPYING.txt for license details. |
| 7 |
|
| 8 |
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX |
| 9 |
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO |
| 10 |
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A |
| 11 |
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS |
| 12 |
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS |
| 13 |
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE |
| 14 |
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE |
| 15 |
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS |
| 16 |
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. |
| 17 |
COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* |
| 21 |
* |
| 22 |
* string manipulation utility code |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <ctype.h> |
| 29 |
#include <string.h> |
| 30 |
#include <stdarg.h> |
| 31 |
|
| 32 |
#include "u_mem.h" |
| 33 |
#include "dxxerror.h" |
| 34 |
#include "strutil.h" |
| 35 |
#include "inferno.h" |
| 36 |
|
| 37 |
#include "compiler-range_for.h" |
| 38 |
#include "d_zip.h" |
| 39 |
|
| 40 |
namespace dcx { |
| 41 |
|
| 42 |
#ifdef macintosh |
| 43 |
void snprintf(char *out_string, int size, char * format, ... ) |
| 44 |
{ |
| 45 |
va_list args; |
| 46 |
char buf[1024]; |
| 47 |
|
| 48 |
va_start(args, format ); |
| 49 |
vsprintf(buf,format,args); |
| 50 |
va_end(args); |
| 51 |
|
| 52 |
// Hack! Don't know any other [simple] way to do this, but I doubt it would ever exceed 1024 long. |
| 53 |
Assert(strlen(buf) < 1024); |
| 54 |
Assert(size < 1024); |
| 55 |
|
| 56 |
strncpy(out_string, buf, size); |
| 57 |
} |
| 58 |
#endif // macintosh |
| 59 |
|
| 60 |
// string compare without regard to case |
| 61 |
|
| 62 |
#ifndef DXX_HAVE_STRCASECMP |
| 63 |
int d_stricmp( const char *s1, const char *s2 ) |
| 64 |
{ |
| 65 |
for (;; ++s1, ++s2) |
| 66 |
{ |
| 67 |
auto u1 = toupper(static_cast<unsigned>(*s1)); |
| 68 |
auto u2 = toupper(static_cast<unsigned>(*s2)); |
| 69 |
if (u1 != u2) |
| 70 |
return (u1 > u2) ? 1 : -1; |
| 71 |
if (!u1) |
| 72 |
return u1; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
int d_strnicmp(const char *s1, const char *s2, uint_fast32_t n) |
| 77 |
{ |
| 78 |
for (; n; ++s1, ++s2, --n) |
| 79 |
{ |
| 80 |
auto u1 = toupper(static_cast<unsigned>(*s1)); |
| 81 |
auto u2 = toupper(static_cast<unsigned>(*s2)); |
| 82 |
if (u1 != u2) |
| 83 |
return (u1 > u2) ? 1 : -1; |
| 84 |
if (!u1) |
| 85 |
return u1; |
| 86 |
} |
| 87 |
return 0; |
| 88 |
} |
| 89 |
#endif |
| 90 |
|
| 91 |
void d_strlwr( char *s1 ) |
| 92 |
{ |
| 93 |
while( *s1 ) { |
| 94 |
*s1 = tolower(*s1); |
| 95 |
s1++; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
#if DXX_USE_EDITOR |
| 100 |
void d_strupr(std::array<char, PATH_MAX> &out, const std::array<char, PATH_MAX> &in) |
| 101 |
{ |
| 102 |
for (auto &&[i, o] : zip(in, out)) |
| 103 |
{ |
| 104 |
o = std::toupper(static_cast<unsigned char>(i)); |
| 105 |
if (!o) |
| 106 |
break; |
| 107 |
} |
| 108 |
} |
| 109 |
#endif |
| 110 |
|
| 111 |
std::unique_ptr<char[]> (d_strdup)(const char *str) |
| 112 |
{ |
| 113 |
const auto len = strlen(str) + 1; |
| 114 |
std::unique_ptr<char[]> newstr(new char[len]); |
| 115 |
memcpy(newstr.get(), str, len); |
| 116 |
return newstr; |
| 117 |
} |
| 118 |
|
| 119 |
//give a filename a new extension, won't append if strlen(dest) > 8 chars. |
| 120 |
bool change_filename_extension(const std::span<char> dest, const char *const src, const std::span<const char, 4> ext) |
| 121 |
{ |
| 122 |
const char *const p = strrchr(src, '.'); |
| 123 |
const std::size_t src_dist_to_last_dot = p ? std::distance(src, p) : strlen(src); |
| 124 |
if (src_dist_to_last_dot + 1 + ext.size() > dest.size()) |
| 125 |
{ |
| 126 |
dest.front() = 0; |
| 127 |
return false; // a non-opened file is better than a bad memory access |
| 128 |
} |
| 129 |
std::snprintf(dest.data(), dest.size(), "%.*s.%s", static_cast<int>(src_dist_to_last_dot), src, ext.data()); |
| 130 |
return true; |
| 131 |
} |
| 132 |
|
| 133 |
splitpath_t d_splitpath(const char *name) |
| 134 |
{ |
| 135 |
const char *s, *p; |
| 136 |
|
| 137 |
p = name; |
| 138 |
s = strchr(p, ':'); |
| 139 |
if ( s != NULL ) { |
| 140 |
p = s+1; |
| 141 |
} |
| 142 |
s = strrchr(p, '\\'); |
| 143 |
if ( s != NULL) { |
| 144 |
p = s+1; |
| 145 |
} |
| 146 |
|
| 147 |
s = strchr(p, '.'); |
| 148 |
if (s != NULL) { |
| 149 |
return {p, s}; |
| 150 |
} else |
| 151 |
return {}; |
| 152 |
} |
| 153 |
|
| 154 |
int string_array_sort_func(const void *v0, const void *v1) |
| 155 |
{ |
| 156 |
const auto e0 = reinterpret_cast<const char *const *>(v0); |
| 157 |
const auto e1 = reinterpret_cast<const char *const *>(v1); |
| 158 |
return d_stricmp(*e0, *e1); |
| 159 |
} |
| 160 |
|
| 161 |
void string_array_t::add(const char *s) |
| 162 |
{ |
| 163 |
const auto insert_string = [this, s]{ |
| 164 |
auto &b = this->buffer; |
| 165 |
b.insert(b.end(), s, s + strlen(s) + 1); |
| 166 |
}; |
| 167 |
if (buffer.empty()) |
| 168 |
{ |
| 169 |
insert_string(); |
| 170 |
ptr.emplace_back(&buffer.front()); |
| 171 |
return; |
| 172 |
} |
| 173 |
const char *ob = &buffer.front(); |
| 174 |
ptr.emplace_back(1 + &buffer.back()); |
| 175 |
insert_string(); |
| 176 |
if (auto d = &buffer.front() - ob) |
| 177 |
{ |
| 178 |
// Update all the pointers in the pointer list |
| 179 |
range_for (auto &i, ptr) |
| 180 |
i += d; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
void string_array_t::tidy(std::size_t offset) |
| 185 |
{ |
| 186 |
// Sort by name, starting at offset |
| 187 |
auto b = std::next(ptr.begin(), offset); |
| 188 |
auto e = ptr.end(); |
| 189 |
#ifdef __linux__ |
| 190 |
#define comp strcmp |
| 191 |
#else |
| 192 |
#define comp d_stricmp |
| 193 |
#endif |
| 194 |
std::sort(b, e, [](const char *sa, const char *sb) { return d_stricmp(sa, sb) < 0; }); |
| 195 |
|
| 196 |
// Remove duplicates |
| 197 |
// Can't do this before reallocating, otherwise it makes a mess of things (the strings in the buffer aren't ordered) |
| 198 |
ptr.erase(std::unique(b, e, [=](const char *sa, const char *sb) { return comp(sa, sb) == 0; }), e); |
| 199 |
#undef comp |
| 200 |
} |
| 201 |
|
| 202 |
} |