| 1 |
/** |
| 2 |
* Encapsulate path and file names. |
| 3 |
* |
| 4 |
* Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved |
| 5 |
* Authors: Walter Bright, https://www.digitalmars.com |
| 6 |
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 7 |
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/root/filename.d, root/_filename.d) |
| 8 |
* Documentation: https://dlang.org/phobos/dmd_root_filename.html |
| 9 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/root/filename.d |
| 10 |
*/ |
| 11 |
|
| 12 |
module dmd.root.filename; |
| 13 |
|
| 14 |
import core.stdc.ctype; |
| 15 |
import core.stdc.errno; |
| 16 |
import core.stdc.stdio; |
| 17 |
import core.stdc.string; |
| 18 |
|
| 19 |
import dmd.common.file; |
| 20 |
import dmd.common.outbuffer; |
| 21 |
|
| 22 |
import dmd.root.array; |
| 23 |
import dmd.root.file; |
| 24 |
import dmd.root.port; |
| 25 |
import dmd.root.rmem; |
| 26 |
import dmd.root.string; |
| 27 |
|
| 28 |
version (Posix) |
| 29 |
{ |
| 30 |
import core.sys.posix.stdlib; |
| 31 |
import core.sys.posix.sys.stat; |
| 32 |
import core.sys.posix.unistd : getcwd; |
| 33 |
} |
| 34 |
|
| 35 |
version (Windows) |
| 36 |
{ |
| 37 |
import core.sys.windows.winbase; |
| 38 |
import core.sys.windows.windef; |
| 39 |
import core.sys.windows.winnls; |
| 40 |
|
| 41 |
import dmd.common.smallbuffer : extendedPathThen; |
| 42 |
|
| 43 |
extern (Windows) DWORD GetFullPathNameW(LPCWSTR, DWORD, LPWSTR, LPWSTR*) nothrow @nogc; |
| 44 |
extern (Windows) void SetLastError(DWORD) nothrow @nogc; |
| 45 |
extern (C) char* _getcwd(char* buffer, size_t maxlen) nothrow; |
| 46 |
alias getcwd = _getcwd; |
| 47 |
} |
| 48 |
|
| 49 |
version (CRuntime_Glibc) |
| 50 |
{ |
| 51 |
extern (C) char* canonicalize_file_name(const char*) nothrow; |
| 52 |
} |
| 53 |
|
| 54 |
alias Strings = Array!(const(char)*); |
| 55 |
|
| 56 |
|
| 57 |
// Check whether character is a directory separator |
| 58 |
bool isDirSeparator(char c) pure nothrow @nogc @safe |
| 59 |
{ |
| 60 |
version (Windows) |
| 61 |
{ |
| 62 |
return c == '\\' || c == '/'; |
| 63 |
} |
| 64 |
else version (Posix) |
| 65 |
{ |
| 66 |
return c == '/'; |
| 67 |
} |
| 68 |
else |
| 69 |
{ |
| 70 |
assert(0); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/*********************************************************** |
| 75 |
* Encapsulate path and file names. |
| 76 |
*/ |
| 77 |
struct FileName |
| 78 |
{ |
| 79 |
nothrow: |
| 80 |
private const(char)[] str; |
| 81 |
|
| 82 |
/// |
| 83 |
extern (D) this(const char[] str) pure |
| 84 |
{ |
| 85 |
this.str = str.xarraydup; |
| 86 |
} |
| 87 |
|
| 88 |
/// |
| 89 |
extern (C++) static FileName create(const(char)* name) pure |
| 90 |
{ |
| 91 |
return FileName(name.toDString); |
| 92 |
} |
| 93 |
|
| 94 |
/// Compare two name according to the platform's rules (case sensitive or not) |
| 95 |
extern (C++) static bool equals(const(char)* name1, const(char)* name2) pure @nogc |
| 96 |
{ |
| 97 |
return equals(name1.toDString, name2.toDString); |
| 98 |
} |
| 99 |
|
| 100 |
/// Ditto |
| 101 |
extern (D) static bool equals(const char[] name1, const char[] name2) pure @nogc |
| 102 |
{ |
| 103 |
if (name1.length != name2.length) |
| 104 |
return false; |
| 105 |
|
| 106 |
version (Windows) |
| 107 |
{ |
| 108 |
return name1.ptr == name2.ptr || |
| 109 |
Port.memicmp(name1.ptr, name2.ptr, name1.length) == 0; |
| 110 |
} |
| 111 |
else |
| 112 |
{ |
| 113 |
return name1 == name2; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/************************************ |
| 118 |
* Determine if path is absolute. |
| 119 |
* Params: |
| 120 |
* name = path |
| 121 |
* Returns: |
| 122 |
* true if absolute path name. |
| 123 |
*/ |
| 124 |
extern (C++) static bool absolute(const(char)* name) pure @nogc |
| 125 |
{ |
| 126 |
return absolute(name.toDString); |
| 127 |
} |
| 128 |
|
| 129 |
/// Ditto |
| 130 |
extern (D) static bool absolute(const char[] name) pure @nogc @safe |
| 131 |
{ |
| 132 |
if (!name.length) |
| 133 |
return false; |
| 134 |
|
| 135 |
version (Windows) |
| 136 |
{ |
| 137 |
return isDirSeparator(name[0]) |
| 138 |
|| (name.length >= 2 && name[1] == ':'); |
| 139 |
} |
| 140 |
else version (Posix) |
| 141 |
{ |
| 142 |
return isDirSeparator(name[0]); |
| 143 |
} |
| 144 |
else |
| 145 |
{ |
| 146 |
assert(0); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
unittest |
| 151 |
{ |
| 152 |
assert(absolute("/"[]) == true); |
| 153 |
assert(absolute(""[]) == false); |
| 154 |
|
| 155 |
version (Windows) |
| 156 |
{ |
| 157 |
assert(absolute(r"\"[]) == true); |
| 158 |
assert(absolute(r"\\"[]) == true); |
| 159 |
assert(absolute(r"c:"[]) == true); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
Return the given name as an absolute path |
| 165 |
|
| 166 |
Params: |
| 167 |
name = path |
| 168 |
base = the absolute base to prefix name with if it is relative |
| 169 |
|
| 170 |
Returns: name as an absolute path relative to base |
| 171 |
*/ |
| 172 |
extern (C++) static const(char)* toAbsolute(const(char)* name, const(char)* base = null) |
| 173 |
{ |
| 174 |
const name_ = name.toDString(); |
| 175 |
const base_ = base ? base.toDString() : getcwd(null, 0).toDString(); |
| 176 |
return absolute(name_) ? name : combine(base_, name_).ptr; |
| 177 |
} |
| 178 |
|
| 179 |
/******************************** |
| 180 |
* Determine file name extension as slice of input. |
| 181 |
* Params: |
| 182 |
* str = file name |
| 183 |
* Returns: |
| 184 |
* filename extension (read-only). |
| 185 |
* Points past '.' of extension. |
| 186 |
* If there isn't one, return null. |
| 187 |
*/ |
| 188 |
extern (C++) static const(char)* ext(const(char)* str) pure @nogc |
| 189 |
{ |
| 190 |
return ext(str.toDString).ptr; |
| 191 |
} |
| 192 |
|
| 193 |
/// Ditto |
| 194 |
extern (D) static const(char)[] ext(const char[] str) nothrow pure @safe @nogc |
| 195 |
{ |
| 196 |
foreach_reverse (idx, char e; str) |
| 197 |
{ |
| 198 |
switch (e) |
| 199 |
{ |
| 200 |
case '.': |
| 201 |
return str[idx + 1 .. $]; |
| 202 |
version (Posix) |
| 203 |
{ |
| 204 |
case '/': |
| 205 |
return null; |
| 206 |
} |
| 207 |
version (Windows) |
| 208 |
{ |
| 209 |
case '\\': |
| 210 |
case ':': |
| 211 |
case '/': |
| 212 |
return null; |
| 213 |
} |
| 214 |
default: |
| 215 |
continue; |
| 216 |
} |
| 217 |
} |
| 218 |
return null; |
| 219 |
} |
| 220 |
|
| 221 |
unittest |
| 222 |
{ |
| 223 |
assert(ext("/foo/bar/dmd.conf"[]) == "conf"); |
| 224 |
assert(ext("object.o"[]) == "o"); |
| 225 |
assert(ext("/foo/bar/dmd"[]) == null); |
| 226 |
assert(ext(".objdir.o/object"[]) == null); |
| 227 |
assert(ext([]) == null); |
| 228 |
} |
| 229 |
|
| 230 |
extern (C++) const(char)* ext() const pure @nogc |
| 231 |
{ |
| 232 |
return ext(str).ptr; |
| 233 |
} |
| 234 |
|
| 235 |
/******************************** |
| 236 |
* Return file name without extension. |
| 237 |
* |
| 238 |
* TODO: |
| 239 |
* Once slice are used everywhere and `\0` is not assumed, |
| 240 |
* this can be turned into a simple slicing. |
| 241 |
* |
| 242 |
* Params: |
| 243 |
* str = file name |
| 244 |
* |
| 245 |
* Returns: |
| 246 |
* mem.xmalloc'd filename with extension removed. |
| 247 |
*/ |
| 248 |
extern (C++) static const(char)* removeExt(const(char)* str) |
| 249 |
{ |
| 250 |
return removeExt(str.toDString).ptr; |
| 251 |
} |
| 252 |
|
| 253 |
/// Ditto |
| 254 |
extern (D) static const(char)[] removeExt(const char[] str) |
| 255 |
{ |
| 256 |
auto e = ext(str); |
| 257 |
if (e.length) |
| 258 |
{ |
| 259 |
const len = (str.length - e.length) - 1; // -1 for the dot |
| 260 |
char* n = cast(char*)mem.xmalloc(len + 1); |
| 261 |
memcpy(n, str.ptr, len); |
| 262 |
n[len] = 0; |
| 263 |
return n[0 .. len]; |
| 264 |
} |
| 265 |
return mem.xstrdup(str.ptr)[0 .. str.length]; |
| 266 |
} |
| 267 |
|
| 268 |
unittest |
| 269 |
{ |
| 270 |
assert(removeExt("/foo/bar/object.d"[]) == "/foo/bar/object"); |
| 271 |
assert(removeExt("/foo/bar/frontend.di"[]) == "/foo/bar/frontend"); |
| 272 |
} |
| 273 |
|
| 274 |
/******************************** |
| 275 |
* Slice of file name without extension. |
| 276 |
* Params: |
| 277 |
* filename = file name |
| 278 |
* Returns: |
| 279 |
* the slice |
| 280 |
*/ |
| 281 |
extern (D) static const(char)[] sansExt(const char[] filename) @safe |
| 282 |
{ |
| 283 |
auto e = ext(filename); |
| 284 |
size_t length = e.length; |
| 285 |
return filename[0 .. filename.length - (length ? length + 1 : 0)]; // +1 for . |
| 286 |
} |
| 287 |
|
| 288 |
/******************************** |
| 289 |
* Return filename name excluding path (read-only). |
| 290 |
*/ |
| 291 |
extern (C++) static const(char)* name(const(char)* str) pure @nogc |
| 292 |
{ |
| 293 |
return name(str.toDString).ptr; |
| 294 |
} |
| 295 |
|
| 296 |
/// Ditto |
| 297 |
extern (D) static const(char)[] name(const char[] str) pure @nogc @safe |
| 298 |
{ |
| 299 |
foreach_reverse (idx, char e; str) |
| 300 |
{ |
| 301 |
switch (e) |
| 302 |
{ |
| 303 |
version (Posix) |
| 304 |
{ |
| 305 |
case '/': |
| 306 |
return str[idx + 1 .. $]; |
| 307 |
} |
| 308 |
version (Windows) |
| 309 |
{ |
| 310 |
case '/': |
| 311 |
case '\\': |
| 312 |
return str[idx + 1 .. $]; |
| 313 |
case ':': |
| 314 |
/* The ':' is a drive letter only if it is the second |
| 315 |
* character or the last character, |
| 316 |
* otherwise it is an ADS (Alternate Data Stream) separator. |
| 317 |
* Consider ADS separators as part of the file name. |
| 318 |
*/ |
| 319 |
if (idx == 1 || idx == str.length - 1) |
| 320 |
return str[idx + 1 .. $]; |
| 321 |
break; |
| 322 |
} |
| 323 |
default: |
| 324 |
break; |
| 325 |
} |
| 326 |
} |
| 327 |
return str; |
| 328 |
} |
| 329 |
|
| 330 |
extern (C++) const(char)* name() const pure @nogc |
| 331 |
{ |
| 332 |
return name(str).ptr; |
| 333 |
} |
| 334 |
|
| 335 |
unittest |
| 336 |
{ |
| 337 |
assert(name("/foo/bar/object.d"[]) == "object.d"); |
| 338 |
assert(name("/foo/bar/frontend.di"[]) == "frontend.di"); |
| 339 |
} |
| 340 |
|
| 341 |
/************************************** |
| 342 |
* Return path portion of str. |
| 343 |
* returned string is newly allocated |
| 344 |
* Path does not include trailing path separator. |
| 345 |
*/ |
| 346 |
extern (C++) static const(char)* path(const(char)* str) |
| 347 |
{ |
| 348 |
return path(str.toDString).ptr; |
| 349 |
} |
| 350 |
|
| 351 |
/// Ditto |
| 352 |
extern (D) static const(char)[] path(const char[] str) |
| 353 |
{ |
| 354 |
const n = name(str); |
| 355 |
bool hasTrailingSlash; |
| 356 |
if (n.length < str.length) |
| 357 |
{ |
| 358 |
if (isDirSeparator(str[$ - n.length - 1])) |
| 359 |
hasTrailingSlash = true; |
| 360 |
} |
| 361 |
const pathlen = str.length - n.length - (hasTrailingSlash ? 1 : 0); |
| 362 |
char* path = cast(char*)mem.xmalloc(pathlen + 1); |
| 363 |
memcpy(path, str.ptr, pathlen); |
| 364 |
path[pathlen] = 0; |
| 365 |
return path[0 .. pathlen]; |
| 366 |
} |
| 367 |
|
| 368 |
unittest |
| 369 |
{ |
| 370 |
assert(path("/foo/bar"[]) == "/foo"); |
| 371 |
assert(path("foo"[]) == ""); |
| 372 |
} |
| 373 |
|
| 374 |
/************************************** |
| 375 |
* Replace filename portion of path. |
| 376 |
*/ |
| 377 |
extern (D) static const(char)[] replaceName(const char[] path, const char[] name) |
| 378 |
{ |
| 379 |
if (absolute(name)) |
| 380 |
return name; |
| 381 |
auto n = FileName.name(path); |
| 382 |
if (n == path) |
| 383 |
return name; |
| 384 |
return combine(path[0 .. $ - n.length], name); |
| 385 |
} |
| 386 |
|
| 387 |
/** |
| 388 |
Combine a `path` and a file `name` |
| 389 |
|
| 390 |
Params: |
| 391 |
path = Path to append to |
| 392 |
name = Name to append to path |
| 393 |
|
| 394 |
Returns: |
| 395 |
The `\0` terminated string which is the combination of `path` and `name` |
| 396 |
and a valid path. |
| 397 |
*/ |
| 398 |
extern (C++) static const(char)* combine(const(char)* path, const(char)* name) |
| 399 |
{ |
| 400 |
if (!path) |
| 401 |
return name; |
| 402 |
return combine(path.toDString, name.toDString).ptr; |
| 403 |
} |
| 404 |
|
| 405 |
/// Ditto |
| 406 |
extern(D) static const(char)[] combine(const char[] path, const char[] name) |
| 407 |
{ |
| 408 |
return !path.length ? name : buildPath(path, name); |
| 409 |
} |
| 410 |
|
| 411 |
unittest |
| 412 |
{ |
| 413 |
version (Windows) |
| 414 |
assert(combine("foo"[], "bar"[]) == "foo\\bar"); |
| 415 |
else |
| 416 |
assert(combine("foo"[], "bar"[]) == "foo/bar"); |
| 417 |
assert(combine("foo/"[], "bar"[]) == "foo/bar"); |
| 418 |
} |
| 419 |
|
| 420 |
static const(char)[] buildPath(const char[][] fragments...) |
| 421 |
{ |
| 422 |
size_t size; |
| 423 |
foreach (f; fragments) |
| 424 |
size += f.length ? f.length + 1 : 0; |
| 425 |
if (size == 0) |
| 426 |
size = 1; |
| 427 |
|
| 428 |
char* p = cast(char*) mem.xmalloc_noscan(size); |
| 429 |
size_t length; |
| 430 |
foreach (f; fragments) |
| 431 |
{ |
| 432 |
if (!f.length) |
| 433 |
continue; |
| 434 |
|
| 435 |
p[length .. length + f.length] = f; |
| 436 |
length += f.length; |
| 437 |
|
| 438 |
const last = p[length - 1]; |
| 439 |
version (Posix) |
| 440 |
{ |
| 441 |
if (!isDirSeparator(last)) |
| 442 |
p[length++] = '/'; |
| 443 |
} |
| 444 |
else version (Windows) |
| 445 |
{ |
| 446 |
if (!isDirSeparator(last) && last != ':') |
| 447 |
p[length++] = '\\'; |
| 448 |
} |
| 449 |
else |
| 450 |
assert(0); |
| 451 |
} |
| 452 |
|
| 453 |
// overwrite last slash with null terminator |
| 454 |
p[length ? --length : 0] = 0; |
| 455 |
|
| 456 |
return p[0 .. length]; |
| 457 |
} |
| 458 |
|
| 459 |
unittest |
| 460 |
{ |
| 461 |
assert(buildPath() == ""); |
| 462 |
assert(buildPath("foo") == "foo"); |
| 463 |
assert(buildPath("foo", null) == "foo"); |
| 464 |
assert(buildPath(null, "foo") == "foo"); |
| 465 |
version (Windows) |
| 466 |
assert(buildPath("C:", r"a\", "bb/", "ccc", "d") == r"C:a\bb/ccc\d"); |
| 467 |
else |
| 468 |
assert(buildPath("a/", "bb", "ccc") == "a/bb/ccc"); |
| 469 |
} |
| 470 |
|
| 471 |
// Split a path and append the results to `array` |
| 472 |
extern (C++) static void appendSplitPath(const(char)* path, ref Strings array) |
| 473 |
{ |
| 474 |
int sink(const(char)* p) nothrow |
| 475 |
{ |
| 476 |
array.push(p); |
| 477 |
return 0; |
| 478 |
} |
| 479 |
splitPath(&sink, path); |
| 480 |
} |
| 481 |
|
| 482 |
/**** |
| 483 |
* Split path (such as that returned by `getenv("PATH")`) into pieces, each piece is mem.xmalloc'd |
| 484 |
* Handle double quotes and ~. |
| 485 |
* Pass the pieces to sink() |
| 486 |
* Params: |
| 487 |
* sink = send the path pieces here, end when sink() returns !=0 |
| 488 |
* path = the path to split up. |
| 489 |
*/ |
| 490 |
static void splitPath(int delegate(const(char)*) nothrow sink, const(char)* path) |
| 491 |
{ |
| 492 |
if (!path) |
| 493 |
return; |
| 494 |
|
| 495 |
auto p = path; |
| 496 |
OutBuffer buf; |
| 497 |
char c; |
| 498 |
do |
| 499 |
{ |
| 500 |
const(char)* home; |
| 501 |
bool instring = false; |
| 502 |
while (isspace(*p)) // skip leading whitespace |
| 503 |
++p; |
| 504 |
buf.reserve(8); // guess size of piece |
| 505 |
for (;; ++p) |
| 506 |
{ |
| 507 |
c = *p; |
| 508 |
switch (c) |
| 509 |
{ |
| 510 |
case '"': |
| 511 |
instring ^= false; // toggle inside/outside of string |
| 512 |
continue; |
| 513 |
|
| 514 |
version (OSX) |
| 515 |
{ |
| 516 |
case ',': |
| 517 |
} |
| 518 |
version (Windows) |
| 519 |
{ |
| 520 |
case ';': |
| 521 |
} |
| 522 |
version (Posix) |
| 523 |
{ |
| 524 |
case ':': |
| 525 |
} |
| 526 |
p++; // ; cannot appear as part of a |
| 527 |
break; // path, quotes won't protect it |
| 528 |
|
| 529 |
case 0x1A: // ^Z means end of file |
| 530 |
case 0: |
| 531 |
break; |
| 532 |
|
| 533 |
case '\r': |
| 534 |
continue; // ignore carriage returns |
| 535 |
|
| 536 |
version (Posix) |
| 537 |
{ |
| 538 |
case '~': |
| 539 |
if (!home) |
| 540 |
home = getenv("HOME"); |
| 541 |
// Expand ~ only if it is prefixing the rest of the path. |
| 542 |
if (!buf.length && p[1] == '/' && home) |
| 543 |
buf.writestring(home); |
| 544 |
else |
| 545 |
buf.writeByte('~'); |
| 546 |
continue; |
| 547 |
} |
| 548 |
|
| 549 |
version (none) |
| 550 |
{ |
| 551 |
case ' ': |
| 552 |
case '\t': // tabs in filenames? |
| 553 |
if (!instring) // if not in string |
| 554 |
break; // treat as end of path |
| 555 |
} |
| 556 |
default: |
| 557 |
buf.writeByte(c); |
| 558 |
continue; |
| 559 |
} |
| 560 |
break; |
| 561 |
} |
| 562 |
if (buf.length) // if path is not empty |
| 563 |
{ |
| 564 |
if (sink(buf.extractChars())) |
| 565 |
break; |
| 566 |
} |
| 567 |
} while (c); |
| 568 |
} |
| 569 |
|
| 570 |
/** |
| 571 |
* Add the extension `ext` to `name`, regardless of the content of `name` |
| 572 |
* |
| 573 |
* Params: |
| 574 |
* name = Path to append the extension to |
| 575 |
* ext = Extension to add (should not include '.') |
| 576 |
* |
| 577 |
* Returns: |
| 578 |
* A newly allocated string (free with `FileName.free`) |
| 579 |
*/ |
| 580 |
extern(D) static char[] addExt(const char[] name, const char[] ext) pure |
| 581 |
{ |
| 582 |
const len = name.length + ext.length + 2; |
| 583 |
auto s = cast(char*)mem.xmalloc(len); |
| 584 |
s[0 .. name.length] = name[]; |
| 585 |
s[name.length] = '.'; |
| 586 |
s[name.length + 1 .. len - 1] = ext[]; |
| 587 |
s[len - 1] = '\0'; |
| 588 |
return s[0 .. len - 1]; |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
/*************************** |
| 593 |
* Free returned value with FileName::free() |
| 594 |
*/ |
| 595 |
extern (C++) static const(char)* defaultExt(const(char)* name, const(char)* ext) pure |
| 596 |
{ |
| 597 |
return defaultExt(name.toDString, ext.toDString).ptr; |
| 598 |
} |
| 599 |
|
| 600 |
/// Ditto |
| 601 |
extern (D) static const(char)[] defaultExt(const char[] name, const char[] ext) pure |
| 602 |
{ |
| 603 |
auto e = FileName.ext(name); |
| 604 |
if (e.length) // it already has an extension |
| 605 |
return name.xarraydup; |
| 606 |
return addExt(name, ext); |
| 607 |
} |
| 608 |
|
| 609 |
unittest |
| 610 |
{ |
| 611 |
assert(defaultExt("/foo/object.d"[], "d") == "/foo/object.d"); |
| 612 |
assert(defaultExt("/foo/object"[], "d") == "/foo/object.d"); |
| 613 |
assert(defaultExt("/foo/bar.d"[], "o") == "/foo/bar.d"); |
| 614 |
} |
| 615 |
|
| 616 |
/*************************** |
| 617 |
* Free returned value with FileName::free() |
| 618 |
*/ |
| 619 |
extern (C++) static const(char)* forceExt(const(char)* name, const(char)* ext) pure |
| 620 |
{ |
| 621 |
return forceExt(name.toDString, ext.toDString).ptr; |
| 622 |
} |
| 623 |
|
| 624 |
/// Ditto |
| 625 |
extern (D) static const(char)[] forceExt(const char[] name, const char[] ext) pure |
| 626 |
{ |
| 627 |
if (auto e = FileName.ext(name)) |
| 628 |
return addExt(name[0 .. $ - e.length - 1], ext); |
| 629 |
return defaultExt(name, ext); // doesn't have one |
| 630 |
} |
| 631 |
|
| 632 |
unittest |
| 633 |
{ |
| 634 |
assert(forceExt("/foo/object.d"[], "d") == "/foo/object.d"); |
| 635 |
assert(forceExt("/foo/object"[], "d") == "/foo/object.d"); |
| 636 |
assert(forceExt("/foo/bar.d"[], "o") == "/foo/bar.o"); |
| 637 |
} |
| 638 |
|
| 639 |
/// Returns: |
| 640 |
/// `true` if `name`'s extension is `ext` |
| 641 |
extern (C++) static bool equalsExt(const(char)* name, const(char)* ext) pure @nogc |
| 642 |
{ |
| 643 |
return equalsExt(name.toDString, ext.toDString); |
| 644 |
} |
| 645 |
|
| 646 |
/// Ditto |
| 647 |
extern (D) static bool equalsExt(const char[] name, const char[] ext) pure @nogc |
| 648 |
{ |
| 649 |
auto e = FileName.ext(name); |
| 650 |
if (!e.length && !ext.length) |
| 651 |
return true; |
| 652 |
if (!e.length || !ext.length) |
| 653 |
return false; |
| 654 |
return FileName.equals(e, ext); |
| 655 |
} |
| 656 |
|
| 657 |
unittest |
| 658 |
{ |
| 659 |
assert(!equalsExt("foo.bar"[], "d")); |
| 660 |
assert(equalsExt("foo.bar"[], "bar")); |
| 661 |
assert(equalsExt("object.d"[], "d")); |
| 662 |
assert(!equalsExt("object"[], "d")); |
| 663 |
} |
| 664 |
|
| 665 |
/****************************** |
| 666 |
* Return !=0 if extensions match. |
| 667 |
*/ |
| 668 |
extern (C++) bool equalsExt(const(char)* ext) const pure @nogc |
| 669 |
{ |
| 670 |
return equalsExt(str, ext.toDString()); |
| 671 |
} |
| 672 |
|
| 673 |
/************************************* |
| 674 |
* Search paths for file. |
| 675 |
* Params: |
| 676 |
* path = array of path strings |
| 677 |
* name = file to look for |
| 678 |
* cwd = true means search current directory before searching path |
| 679 |
* Returns: |
| 680 |
* if found, filename combined with path, otherwise null |
| 681 |
*/ |
| 682 |
extern (C++) static const(char)* searchPath(const ref Strings path, const(char)* name, bool cwd) |
| 683 |
{ |
| 684 |
return searchPath(path[], name.toDString, cwd).ptr; |
| 685 |
} |
| 686 |
|
| 687 |
extern (D) static const(char)[] searchPath(const char*[] path, const char[] name, bool cwd) |
| 688 |
{ |
| 689 |
if (absolute(name)) |
| 690 |
{ |
| 691 |
return exists(name) ? name : null; |
| 692 |
} |
| 693 |
if (cwd) |
| 694 |
{ |
| 695 |
if (exists(name)) |
| 696 |
return name; |
| 697 |
} |
| 698 |
foreach (p; path) |
| 699 |
{ |
| 700 |
auto n = combine(p.toDString, name); |
| 701 |
if (exists(n)) |
| 702 |
return n; |
| 703 |
//combine might return name |
| 704 |
if (n.ptr != name.ptr) |
| 705 |
{ |
| 706 |
mem.xfree(cast(void*)n.ptr); |
| 707 |
} |
| 708 |
} |
| 709 |
return null; |
| 710 |
} |
| 711 |
|
| 712 |
extern (D) static const(char)[] searchPath(const char* path, const char[] name, bool cwd) |
| 713 |
{ |
| 714 |
if (absolute(name)) |
| 715 |
{ |
| 716 |
return exists(name) ? name : null; |
| 717 |
} |
| 718 |
if (cwd) |
| 719 |
{ |
| 720 |
if (exists(name)) |
| 721 |
return name; |
| 722 |
} |
| 723 |
if (path && *path) |
| 724 |
{ |
| 725 |
const(char)[] result; |
| 726 |
|
| 727 |
int sink(const(char)* p) nothrow |
| 728 |
{ |
| 729 |
auto n = combine(p.toDString, name); |
| 730 |
mem.xfree(cast(void*)p); |
| 731 |
if (exists(n)) |
| 732 |
{ |
| 733 |
result = n; |
| 734 |
return 1; // done with splitPath() call |
| 735 |
} |
| 736 |
return 0; |
| 737 |
} |
| 738 |
|
| 739 |
splitPath(&sink, path); |
| 740 |
return result; |
| 741 |
} |
| 742 |
return null; |
| 743 |
} |
| 744 |
|
| 745 |
/************************************ |
| 746 |
* Determine if path contains reserved character. |
| 747 |
* Params: |
| 748 |
* name = path |
| 749 |
* Returns: |
| 750 |
* index of the first reserved character in path if found, size_t.max otherwise |
| 751 |
*/ |
| 752 |
extern (D) static size_t findReservedChar(const char[] name) pure @nogc @safe |
| 753 |
{ |
| 754 |
version (Windows) |
| 755 |
{ |
| 756 |
// According to https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions |
| 757 |
// the following characters are not allowed in path: < > : " | ? * |
| 758 |
foreach (idx; 0 .. name.length) |
| 759 |
{ |
| 760 |
char c = name[idx]; |
| 761 |
if (c == '<' || c == '>' || c == ':' || c == '"' || c == '|' || c == '?' || c == '*') |
| 762 |
{ |
| 763 |
return idx; |
| 764 |
} |
| 765 |
} |
| 766 |
return size_t.max; |
| 767 |
} |
| 768 |
else |
| 769 |
{ |
| 770 |
return size_t.max; |
| 771 |
} |
| 772 |
} |
| 773 |
unittest |
| 774 |
{ |
| 775 |
assert(findReservedChar(r"") == size_t.max); |
| 776 |
assert(findReservedChar(r" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.-_=+()") == size_t.max); |
| 777 |
|
| 778 |
version (Windows) |
| 779 |
{ |
| 780 |
assert(findReservedChar(` < `) == 1); |
| 781 |
assert(findReservedChar(` >`) == 1); |
| 782 |
assert(findReservedChar(`: `) == 0); |
| 783 |
assert(findReservedChar(`"`) == 0); |
| 784 |
assert(findReservedChar(`|`) == 0); |
| 785 |
assert(findReservedChar(`?`) == 0); |
| 786 |
assert(findReservedChar(`*`) == 0); |
| 787 |
} |
| 788 |
else |
| 789 |
{ |
| 790 |
assert(findReservedChar(`<>:"|?*`) == size_t.max); |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
/************************************ |
| 795 |
* Determine if path has a reference to parent directory. |
| 796 |
* Params: |
| 797 |
* name = path |
| 798 |
* Returns: |
| 799 |
* true if path contains '..' reference to parent directory |
| 800 |
*/ |
| 801 |
extern (D) static bool refersToParentDir(const char[] name) pure @nogc @safe |
| 802 |
{ |
| 803 |
size_t s = 0; |
| 804 |
foreach (i; 0 .. name.length) |
| 805 |
{ |
| 806 |
if (isDirSeparator(name[i])) |
| 807 |
{ |
| 808 |
if (name[s..i] == "..") |
| 809 |
return true; |
| 810 |
s = i + 1; |
| 811 |
} |
| 812 |
} |
| 813 |
if (name[s..$] == "..") |
| 814 |
return true; |
| 815 |
|
| 816 |
return false; |
| 817 |
} |
| 818 |
unittest |
| 819 |
{ |
| 820 |
assert(!refersToParentDir(r"")); |
| 821 |
assert(!refersToParentDir(r"foo")); |
| 822 |
assert(!refersToParentDir(r"foo..")); |
| 823 |
assert(!refersToParentDir(r"foo..boo")); |
| 824 |
assert(!refersToParentDir(r"foo/..boo")); |
| 825 |
assert(!refersToParentDir(r"foo../boo")); |
| 826 |
assert(refersToParentDir(r"..")); |
| 827 |
assert(refersToParentDir(r"../")); |
| 828 |
assert(refersToParentDir(r"foo/..")); |
| 829 |
assert(refersToParentDir(r"foo/../")); |
| 830 |
assert(refersToParentDir(r"foo/../../boo")); |
| 831 |
|
| 832 |
version (Windows) |
| 833 |
{ |
| 834 |
// Backslash as directory separator |
| 835 |
assert(!refersToParentDir(r"foo\..boo")); |
| 836 |
assert(!refersToParentDir(r"foo..\boo")); |
| 837 |
assert(refersToParentDir(r"..\")); |
| 838 |
assert(refersToParentDir(r"foo\..")); |
| 839 |
assert(refersToParentDir(r"foo\..\")); |
| 840 |
assert(refersToParentDir(r"foo\..\..\boo")); |
| 841 |
} |
| 842 |
} |
| 843 |
|
| 844 |
|
| 845 |
/** |
| 846 |
Check if the file the `path` points to exists |
| 847 |
|
| 848 |
Returns: |
| 849 |
0 if it does not exists |
| 850 |
1 if it exists and is not a directory |
| 851 |
2 if it exists and is a directory |
| 852 |
*/ |
| 853 |
extern (C++) static int exists(const(char)* name) |
| 854 |
{ |
| 855 |
return exists(name.toDString); |
| 856 |
} |
| 857 |
|
| 858 |
/// Ditto |
| 859 |
extern (D) static int exists(const char[] name) |
| 860 |
{ |
| 861 |
if (!name.length) |
| 862 |
return 0; |
| 863 |
//static int count; printf("count: %d %.*s\n", ++count, cast(int)name.length, name.ptr); |
| 864 |
version (Posix) |
| 865 |
{ |
| 866 |
stat_t st; |
| 867 |
if (name.toCStringThen!((v) => stat(v.ptr, &st)) < 0) |
| 868 |
return 0; |
| 869 |
if (S_ISDIR(st.st_mode)) |
| 870 |
return 2; |
| 871 |
return 1; |
| 872 |
} |
| 873 |
else version (Windows) |
| 874 |
{ |
| 875 |
return name.extendedPathThen!((wname) |
| 876 |
{ |
| 877 |
const dw = GetFileAttributesW(&wname[0]); |
| 878 |
if (dw == -1) |
| 879 |
return 0; |
| 880 |
if (dw & FILE_ATTRIBUTE_DIRECTORY) |
| 881 |
return 2; |
| 882 |
return 1; |
| 883 |
}); |
| 884 |
} |
| 885 |
else |
| 886 |
{ |
| 887 |
assert(0); |
| 888 |
} |
| 889 |
} |
| 890 |
|
| 891 |
/** |
| 892 |
Ensure that the provided path exists |
| 893 |
|
| 894 |
Accepts a path to either a file or a directory. |
| 895 |
In the former case, the basepath (path to the containing directory) |
| 896 |
will be checked for existence, and created if it does not exists. |
| 897 |
In the later case, the directory pointed to will be checked for existence |
| 898 |
and created if needed. |
| 899 |
|
| 900 |
Params: |
| 901 |
path = a path to a file or a directory |
| 902 |
|
| 903 |
Returns: |
| 904 |
`true` if the directory exists or was successfully created |
| 905 |
*/ |
| 906 |
extern (D) static bool ensurePathExists(const char[] path) |
| 907 |
{ |
| 908 |
//printf("FileName::ensurePathExists(%s)\n", path ? path : ""); |
| 909 |
if (!path.length) |
| 910 |
return true; |
| 911 |
if (exists(path)) |
| 912 |
return true; |
| 913 |
|
| 914 |
// We were provided with a file name |
| 915 |
// We need to call ourselves recursively to ensure parent dir exist |
| 916 |
const char[] p = FileName.path(path); |
| 917 |
if (p.length) |
| 918 |
{ |
| 919 |
version (Windows) |
| 920 |
{ |
| 921 |
// Note: Windows filename comparison should be case-insensitive, |
| 922 |
// however p is a subslice of path so we don't need it |
| 923 |
if (path.length == p.length || |
| 924 |
(path.length > 2 && path[1] == ':' && path[2 .. $] == p)) |
| 925 |
{ |
| 926 |
mem.xfree(cast(void*)p.ptr); |
| 927 |
return true; |
| 928 |
} |
| 929 |
} |
| 930 |
const r = ensurePathExists(p); |
| 931 |
mem.xfree(cast(void*)p); |
| 932 |
|
| 933 |
if (!r) |
| 934 |
return r; |
| 935 |
} |
| 936 |
|
| 937 |
version (Windows) |
| 938 |
const r = _mkdir(path); |
| 939 |
version (Posix) |
| 940 |
{ |
| 941 |
errno = 0; |
| 942 |
const r = path.toCStringThen!((pathCS) => mkdir(pathCS.ptr, (7 << 6) | (7 << 3) | 7)); |
| 943 |
} |
| 944 |
|
| 945 |
if (r == 0) |
| 946 |
return true; |
| 947 |
|
| 948 |
// Don't error out if another instance of dmd just created |
| 949 |
// this directory |
| 950 |
version (Windows) |
| 951 |
{ |
| 952 |
import core.sys.windows.winerror : ERROR_ALREADY_EXISTS; |
| 953 |
if (GetLastError() == ERROR_ALREADY_EXISTS) |
| 954 |
return true; |
| 955 |
} |
| 956 |
version (Posix) |
| 957 |
{ |
| 958 |
if (errno == EEXIST) |
| 959 |
return true; |
| 960 |
} |
| 961 |
|
| 962 |
return false; |
| 963 |
} |
| 964 |
|
| 965 |
///ditto |
| 966 |
extern (C++) static bool ensurePathExists(const(char)* path) |
| 967 |
{ |
| 968 |
return ensurePathExists(path.toDString); |
| 969 |
} |
| 970 |
|
| 971 |
/****************************************** |
| 972 |
* Return canonical version of name. |
| 973 |
* This code is high risk. |
| 974 |
*/ |
| 975 |
extern (C++) static const(char)* canonicalName(const(char)* name) |
| 976 |
{ |
| 977 |
return canonicalName(name.toDString).ptr; |
| 978 |
} |
| 979 |
|
| 980 |
/// Ditto |
| 981 |
extern (D) static const(char)[] canonicalName(const char[] name) |
| 982 |
{ |
| 983 |
version (Posix) |
| 984 |
{ |
| 985 |
import core.stdc.limits; // PATH_MAX |
| 986 |
import core.sys.posix.unistd; // _PC_PATH_MAX |
| 987 |
|
| 988 |
// Older versions of druntime don't have PATH_MAX defined. |
| 989 |
// i.e: dmd __VERSION__ < 2085, gdc __VERSION__ < 2076. |
| 990 |
static if (!__traits(compiles, PATH_MAX)) |
| 991 |
{ |
| 992 |
version (DragonFlyBSD) |
| 993 |
enum PATH_MAX = 1024; |
| 994 |
else version (FreeBSD) |
| 995 |
enum PATH_MAX = 1024; |
| 996 |
else version (linux) |
| 997 |
enum PATH_MAX = 4096; |
| 998 |
else version (NetBSD) |
| 999 |
enum PATH_MAX = 1024; |
| 1000 |
else version (OpenBSD) |
| 1001 |
enum PATH_MAX = 1024; |
| 1002 |
else version (OSX) |
| 1003 |
enum PATH_MAX = 1024; |
| 1004 |
else version (Solaris) |
| 1005 |
enum PATH_MAX = 1024; |
| 1006 |
} |
| 1007 |
|
| 1008 |
// Have realpath(), passing a NULL destination pointer may return an |
| 1009 |
// internally malloc'd buffer, however it is implementation defined |
| 1010 |
// as to what happens, so cannot rely on it. |
| 1011 |
static if (__traits(compiles, PATH_MAX)) |
| 1012 |
{ |
| 1013 |
// Have compile time limit on filesystem path, use it with realpath. |
| 1014 |
char[PATH_MAX] buf = void; |
| 1015 |
auto path = name.toCStringThen!((n) => realpath(n.ptr, buf.ptr)); |
| 1016 |
if (path !is null) |
| 1017 |
return xarraydup(path.toDString); |
| 1018 |
} |
| 1019 |
else static if (__traits(compiles, canonicalize_file_name)) |
| 1020 |
{ |
| 1021 |
// Have canonicalize_file_name, which malloc's memory. |
| 1022 |
// We need a dmd.root.rmem allocation though. |
| 1023 |
auto path = name.toCStringThen!((n) => canonicalize_file_name(n.ptr)); |
| 1024 |
scope(exit) .free(path); |
| 1025 |
if (path !is null) |
| 1026 |
return xarraydup(path.toDString); |
| 1027 |
} |
| 1028 |
else static if (__traits(compiles, _PC_PATH_MAX)) |
| 1029 |
{ |
| 1030 |
// Panic! Query the OS for the buffer limit. |
| 1031 |
auto path_max = pathconf("/", _PC_PATH_MAX); |
| 1032 |
if (path_max > 0) |
| 1033 |
{ |
| 1034 |
char *buf = cast(char*)mem.xmalloc(path_max); |
| 1035 |
scope(exit) mem.xfree(buf); |
| 1036 |
auto path = name.toCStringThen!((n) => realpath(n.ptr, buf)); |
| 1037 |
if (path !is null) |
| 1038 |
return xarraydup(path.toDString); |
| 1039 |
} |
| 1040 |
} |
| 1041 |
// Give up trying to support this platform, just duplicate the filename |
| 1042 |
// unless there is nothing to copy from. |
| 1043 |
if (!name.length) |
| 1044 |
return null; |
| 1045 |
return xarraydup(name); |
| 1046 |
} |
| 1047 |
else version (Windows) |
| 1048 |
{ |
| 1049 |
// Convert to wstring first since otherwise the Win32 APIs have a character limit |
| 1050 |
return name.toWStringzThen!((wname) |
| 1051 |
{ |
| 1052 |
/* Apparently, there is no good way to do this on Windows. |
| 1053 |
* GetFullPathName isn't it, but use it anyway. |
| 1054 |
*/ |
| 1055 |
// First find out how long the buffer has to be, incl. terminating null. |
| 1056 |
const capacity = GetFullPathNameW(&wname[0], 0, null, null); |
| 1057 |
if (!capacity) return null; |
| 1058 |
auto buffer = cast(wchar*) mem.xmalloc_noscan(capacity * wchar.sizeof); |
| 1059 |
scope(exit) mem.xfree(buffer); |
| 1060 |
|
| 1061 |
// Actually get the full path name. If the buffer is large enough, |
| 1062 |
// the returned length does NOT include the terminating null... |
| 1063 |
const length = GetFullPathNameW(&wname[0], capacity, buffer, null /*filePart*/); |
| 1064 |
assert(length == capacity - 1); |
| 1065 |
|
| 1066 |
return toNarrowStringz(buffer[0 .. length]); |
| 1067 |
}); |
| 1068 |
} |
| 1069 |
else |
| 1070 |
{ |
| 1071 |
assert(0); |
| 1072 |
} |
| 1073 |
} |
| 1074 |
|
| 1075 |
unittest |
| 1076 |
{ |
| 1077 |
string filename = "foo.bar"; |
| 1078 |
const path = canonicalName(filename); |
| 1079 |
scope(exit) free(path.ptr); |
| 1080 |
assert(path.length >= filename.length); |
| 1081 |
assert(path[$ - filename.length .. $] == filename); |
| 1082 |
} |
| 1083 |
|
| 1084 |
/******************************** |
| 1085 |
* Free memory allocated by FileName routines |
| 1086 |
*/ |
| 1087 |
extern (C++) static void free(const(char)* str) pure |
| 1088 |
{ |
| 1089 |
if (str) |
| 1090 |
{ |
| 1091 |
assert(str[0] != cast(char)0xAB); |
| 1092 |
memset(cast(void*)str, 0xAB, strlen(str) + 1); // stomp |
| 1093 |
} |
| 1094 |
mem.xfree(cast(void*)str); |
| 1095 |
} |
| 1096 |
|
| 1097 |
extern (C++) const(char)* toChars() const pure nothrow @nogc @trusted |
| 1098 |
{ |
| 1099 |
// Since we can return an empty slice (but '\0' terminated), |
| 1100 |
// we don't do bounds check (as `&str[0]` does) |
| 1101 |
return str.ptr; |
| 1102 |
} |
| 1103 |
|
| 1104 |
const(char)[] toString() const pure nothrow @nogc @safe |
| 1105 |
{ |
| 1106 |
return str; |
| 1107 |
} |
| 1108 |
|
| 1109 |
bool opCast(T)() const pure nothrow @nogc @safe |
| 1110 |
if (is(T == bool)) |
| 1111 |
{ |
| 1112 |
return str.ptr !is null; |
| 1113 |
} |
| 1114 |
} |
| 1115 |
|
| 1116 |
version(Windows) |
| 1117 |
{ |
| 1118 |
/**************************************************************** |
| 1119 |
* The code before used the POSIX function `mkdir` on Windows. That |
| 1120 |
* function is now deprecated and fails with long paths, so instead |
| 1121 |
* we use the newer `CreateDirectoryW`. |
| 1122 |
* |
| 1123 |
* `CreateDirectoryW` is the unicode version of the generic macro |
| 1124 |
* `CreateDirectory`. `CreateDirectoryA` has a file path |
| 1125 |
* limitation of 248 characters, `mkdir` fails with less and might |
| 1126 |
* fail due to the number of consecutive `..`s in the |
| 1127 |
* path. `CreateDirectoryW` also normally has a 248 character |
| 1128 |
* limit, unless the path is absolute and starts with `\\?\`. Note |
| 1129 |
* that this is different from starting with the almost identical |
| 1130 |
* `\\?`. |
| 1131 |
* |
| 1132 |
* Params: |
| 1133 |
* path = The path to create. |
| 1134 |
* |
| 1135 |
* Returns: |
| 1136 |
* 0 on success, 1 on failure. |
| 1137 |
* |
| 1138 |
* References: |
| 1139 |
* https://msdn.microsoft.com/en-us/library/windows/desktop/aa363855(v=vs.85).aspx |
| 1140 |
*/ |
| 1141 |
private int _mkdir(const char[] path) nothrow |
| 1142 |
{ |
| 1143 |
const createRet = path.extendedPathThen!( |
| 1144 |
p => CreateDirectoryW(&p[0], null /*securityAttributes*/)); |
| 1145 |
// different conventions for CreateDirectory and mkdir |
| 1146 |
return createRet == 0 ? 1 : 0; |
| 1147 |
} |
| 1148 |
|
| 1149 |
/********************************** |
| 1150 |
* Converts a UTF-16 string to a (null-terminated) narrow string. |
| 1151 |
* Returns: |
| 1152 |
* If `buffer` is specified and the result fits, a slice of that buffer, |
| 1153 |
* otherwise a new buffer which can be released via `mem.xfree()`. |
| 1154 |
* Nulls are propagated, i.e., if `wide` is null, the returned slice is |
| 1155 |
* null too. |
| 1156 |
*/ |
| 1157 |
char[] toNarrowStringz(const(wchar)[] wide, char[] buffer = null) nothrow |
| 1158 |
{ |
| 1159 |
import dmd.common.file : CodePage; |
| 1160 |
|
| 1161 |
if (wide is null) |
| 1162 |
return null; |
| 1163 |
|
| 1164 |
const requiredLength = WideCharToMultiByte(CodePage, 0, wide.ptr, cast(int) wide.length, buffer.ptr, cast(int) buffer.length, null, null); |
| 1165 |
if (requiredLength < buffer.length) |
| 1166 |
{ |
| 1167 |
buffer[requiredLength] = 0; |
| 1168 |
return buffer[0 .. requiredLength]; |
| 1169 |
} |
| 1170 |
|
| 1171 |
char* newBuffer = cast(char*) mem.xmalloc_noscan(requiredLength + 1); |
| 1172 |
const length = WideCharToMultiByte(CodePage, 0, wide.ptr, cast(int) wide.length, newBuffer, requiredLength, null, null); |
| 1173 |
assert(length == requiredLength); |
| 1174 |
newBuffer[length] = 0; |
| 1175 |
return newBuffer[0 .. length]; |
| 1176 |
} |
| 1177 |
|
| 1178 |
/********************************** |
| 1179 |
* Converts a slice of UTF-8 characters to an array of wchar that's null |
| 1180 |
* terminated so it can be passed to Win32 APIs then calls the supplied |
| 1181 |
* function on it. |
| 1182 |
* |
| 1183 |
* Params: |
| 1184 |
* str = The string to convert. |
| 1185 |
* |
| 1186 |
* Returns: |
| 1187 |
* The result of calling F on the UTF16 version of str. |
| 1188 |
*/ |
| 1189 |
private auto toWStringzThen(alias F)(const char[] str) nothrow |
| 1190 |
{ |
| 1191 |
import dmd.common.smallbuffer : SmallBuffer, toWStringz; |
| 1192 |
|
| 1193 |
if (!str.length) return F(""w.ptr); |
| 1194 |
|
| 1195 |
wchar[1024] support = void; |
| 1196 |
auto buf = SmallBuffer!wchar(support.length, support); |
| 1197 |
wchar[] wide = toWStringz(str, buf); |
| 1198 |
scope(exit) wide.ptr != buf.ptr && mem.xfree(wide.ptr); |
| 1199 |
|
| 1200 |
return F(wide); |
| 1201 |
} |
| 1202 |
} |