Blender V5.3
path_utils.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9
10#include <algorithm> /* For `min/max`. */
11#include <cctype>
12#include <cstdlib>
13#include <cstring>
14
15#include "BLI_assert.hh"
16#include "BLI_fileops.hh"
17#include "BLI_fnmatch.hh"
18#include "BLI_path_utils.hh"
19#include "BLI_string.hh"
20#include "BLI_string_utf8.hh"
21#include "BLI_string_utils.hh"
22#include "BLI_utildefines.hh"
23
24#ifdef WIN32
25# include "utf_winfunc.hh"
26# include "utfconv.hh"
27# include <io.h>
28# ifdef _WIN32_IE
29# undef _WIN32_IE
30# endif
31# define _WIN32_IE 0x0501
32# include "BLI_alloca.hh"
33# include "BLI_winstuff.hh"
34# include <shlobj.h>
35# include <windows.h>
36#else
37# include <unistd.h>
38#endif /* WIN32 */
39
40#include "MEM_guardedalloc.h"
41
42namespace blender {
43
44/* Declarations. */
45
46static int BLI_path_unc_prefix_len(const char *path);
47
48#ifdef WIN32
49static bool BLI_path_is_abs_win32(const char *path);
50static int BLI_path_win32_prefix_len(const char *path);
51#endif /* WIN32 */
52
59#define FILENAME_FRAME_CHARS_MAX FILE_MAX
60
61int BLI_path_sequence_decode(const char *path,
62 char *head,
63 const size_t head_maxncpy,
64 char *tail,
65 const size_t tail_maxncpy,
66 ushort *r_digits_len)
67{
68 if (head) {
69 BLI_string_debug_size(head, head_maxncpy);
70 }
71 if (tail) {
72 BLI_string_debug_size(tail, tail_maxncpy);
73 }
74
75 uint nums = 0, nume = 0;
76 int i;
77 bool found_digit = false;
78 const char *const lslash = BLI_path_slash_rfind(path);
79 const char *const extension = BLI_path_extension_or_end(lslash ? lslash : path);
80 const uint lslash_len = lslash != nullptr ? int(lslash - path) : 0;
81 const uint name_end = uint(extension - path);
82
83 for (i = name_end - 1; i >= int(lslash_len); i--) {
84 if (isdigit(path[i])) {
85 if (found_digit) {
86 nums = i;
87 }
88 else {
89 nume = i;
90 nums = i;
91 found_digit = true;
92 }
93 }
94 else {
95 if (found_digit) {
96 break;
97 }
98 }
99 }
100
101 if (found_digit) {
102 const long long int ret = strtoll(&(path[nums]), nullptr, 10);
103 if (ret >= INT_MIN && ret <= INT_MAX) {
104 if (tail) {
105 BLI_strncpy(tail, &path[nume + 1], tail_maxncpy);
106 }
107 if (head) {
108 BLI_strncpy(head, path, std::min<size_t>(head_maxncpy, nums + 1));
109 }
110 if (r_digits_len) {
111 *r_digits_len = nume - nums + 1;
112 }
113 return int(ret);
114 }
115 }
116
117 if (tail) {
118 BLI_strncpy(tail, path + name_end, tail_maxncpy);
119 }
120 if (head) {
121 /* Name_end points to last character of head,
122 * make it +1 so null-terminator is nicely placed. */
123 BLI_strncpy(head, path, std::min<size_t>(head_maxncpy, name_end + 1));
124 }
125 if (r_digits_len) {
126 *r_digits_len = 0;
127 }
128 return 0;
129}
130
132 const size_t path_maxncpy,
133 const char *head,
134 const char *tail,
135 ushort numlen,
136 int pic)
137{
138 BLI_string_debug_size(path, path_maxncpy);
139
140 /* FIXME: MAX_ID_NAME & FILE_MAXFILE
141 *
142 * As this function directly works on a full file path (typically a FILE_MAX long char buffer),
143 * and does not perform any check on the filename part of the path, it can easily generate final
144 * paths containing a filename longer than the max supported length (FILE_MAXFILE).
145 */
146 BLI_snprintf(path, path_maxncpy, "%s%.*d%s", head, numlen, std::max(0, pic), tail);
147}
148
153static int path_normalize_impl(char *path, bool check_blend_relative_prefix)
154{
155 const char *path_orig = path;
156 int path_len = strlen(path);
157
158 /*
159 * Skip absolute prefix.
160 * ---------------------
161 */
162 if (check_blend_relative_prefix && (path[0] == '/' && path[1] == '/')) {
163 path = path + 2; /* Leave the initial `//` untouched. */
164 path_len -= 2;
165
166 /* Strip leading slashes, as they will interfere with the absolute/relative check
167 * (besides being redundant). */
168 int i = 0;
169 while (path[i] == SEP) {
170 i++;
171 }
172
173 if (i != 0) {
174 memmove(path, path + i, (path_len - i) + 1);
175 path_len -= i;
176 }
177 BLI_assert(path_len == strlen(path));
178 }
179
180#ifdef WIN32
181 /* Skip to the first slash of the drive or UNC path,
182 * so additional slashes are treated as doubles. */
183 if (path_orig == path) {
184 int path_unc_len = BLI_path_unc_prefix_len(path);
185 if (path_unc_len) {
186 path_unc_len -= 1;
187 BLI_assert(path_unc_len > 0 && path[path_unc_len] == SEP);
188 path += path_unc_len;
189 path_len -= path_unc_len;
190 }
191 else if (BLI_path_is_win32_drive(path)) { /* Check for `C:` (2 characters only). */
192 path += 2;
193 path_len -= 2;
194 }
195 }
196#endif /* WIN32 */
197 /* Works on WIN32 as well, because the drive component is skipped. */
198 const bool is_relative = path[0] && (path[0] != SEP);
199
200 /*
201 * Strip redundant path components.
202 * --------------------------------
203 */
204
205 /* NOTE(@ideasman42):
206 * `memmove(start, eind, strlen(eind) + 1);`
207 * is the same as
208 * `BLI_strncpy(start, eind, ...);`
209 * except string-copy should not be used because there is overlap,
210 * so use `memmove` 's slightly more obscure syntax. */
211
212 /* Inline replacement:
213 * - `/./` -> `/`.
214 * - `//` -> `/`.
215 * Performed until no more replacements can be made. */
216 if (path_len > 1) {
217 for (int i = path_len - 1; i > 0; i--) {
218 /* Calculate the redundant slash span (if any). */
219 if (path[i] == SEP) {
220 const int i_end = i;
221 do {
222 /* Stepping over elements assumes 'i' references a separator. */
223 BLI_assert(path[i] == SEP);
224 if (path[i - 1] == SEP) {
225 i -= 1; /* Found `//`, replace with `/`. */
226 }
227 else if (i >= 2 && path[i - 1] == '.' && path[i - 2] == SEP) {
228 i -= 2; /* Found `/./`, replace with `/`. */
229 }
230 else {
231 break;
232 }
233 } while (i > 0);
234
235 if (i < i_end) {
236 memmove(path + i, path + i_end, (path_len - i_end) + 1);
237 path_len -= i_end - i;
238 BLI_assert(strlen(path) == path_len);
239 }
240 }
241 }
242 }
243 /* Remove redundant `./` prefix as it's redundant & complicates collapsing directories. */
244 if (is_relative) {
245 if ((path_len > 2) && (path[0] == '.') && (path[1] == SEP)) {
246 memmove(path, path + 2, (path_len - 2) + 1);
247 path_len -= 2;
248 }
249 }
250
251 /*
252 * Collapse Parent Directories.
253 * ----------------------------
254 *
255 * Example: `<parent>/<child>/../` -> `<parent>/`
256 *
257 * Notes:
258 * - Leading `../` are skipped as they cannot be collapsed (see `start_base`).
259 * - Multiple parent directories are handled at once to reduce number of `memmove` calls.
260 */
261
262#define IS_PARENT_DIR(p) ((p)[0] == '.' && (p)[1] == '.' && ELEM((p)[2], SEP, '\0'))
263
264 /* First non prefix path component. */
265 char *path_first_non_slash_part = path;
266 while (*path_first_non_slash_part && *path_first_non_slash_part == SEP) {
267 path_first_non_slash_part++;
268 }
269
270 /* Maintain a pointer to the end of leading `..` component.
271 * Skip leading parent directories because logically they cannot be collapsed. */
272 char *start_base = path_first_non_slash_part;
273 while (IS_PARENT_DIR(start_base)) {
274 start_base += 3;
275 }
276
277 /* It's possible the entire path is made of up `../`,
278 * in this case there is nothing to do. */
279 if (start_base < path + path_len) {
280 /* Step over directories, always starting out on the character after the slash. */
281 char *start = start_base;
282 char *start_temp;
283 while ((start_temp = strstr(start, SEP_STR ".." SEP_STR)) ||
284 /* Check if the string ends with `/..` & assign when found, else nullptr. */
285 (start_temp = ((start <= &path[path_len - 3]) &&
286 STREQ(&path[path_len - 3], SEP_STR "..")) ?
287 &path[path_len - 3] :
288 nullptr))
289 {
290 start = start_temp + 1; /* Skip the `/`. */
291 BLI_assert(start_base != start);
292
293 /* Step `end_all` forwards (over all `..`). */
294 char *end_all = start;
295 do {
296 BLI_assert(IS_PARENT_DIR(end_all));
297 end_all += 3;
298 BLI_assert(end_all <= path + path_len + 1);
299 } while (IS_PARENT_DIR(end_all));
300
301 /* Step `start` backwards (until `end` meets `end_all` or `start` meets `start_base`). */
302 char *end = start;
303 do {
304 BLI_assert(start_base < start);
305 BLI_assert(*(start - 1) == SEP);
306 /* Step `start` backwards one. */
307 do {
308 start--;
309 } while (start_base < start && *(start - 1) != SEP);
310 BLI_assert(*start != SEP); /* Ensure the loop ran at least once. */
311 BLI_assert(!IS_PARENT_DIR(start)); /* Clamping by `start_base` prevents this. */
312 end += 3;
313 } while ((start != start_base) && (end < end_all));
314
315 if (end > path + path_len) {
316 BLI_assert(*(end - 1) == '\0');
317 end--;
318 end_all--;
319 }
320 BLI_assert(start < end && start >= start_base);
321 const size_t start_len = path_len - (end - path);
322 memmove(start, end, start_len + 1);
323 path_len -= end - start;
324 BLI_assert(strlen(path) == path_len);
325 /* Other `..` directories may have been moved to the front, step `start_base` past them. */
326 if (start == start_base && (end != end_all)) [[unlikely]] {
327 start_base += (end_all - end);
328 start = (start_base < path + path_len) ? start_base : start_base - 1;
329 }
330 }
331 }
332
333 BLI_assert(strlen(path) == path_len);
334 /* Characters before the `start_base` must *only* be `../../../` (multiples of 3). */
335 BLI_assert((start_base - path_first_non_slash_part) % 3 == 0);
336 /* All `..` ahead of `start_base` were collapsed (including trailing `/..`). */
337 BLI_assert(!(start_base < path + path_len) ||
338 (!strstr(start_base, SEP_STR ".." SEP_STR) &&
339 !(path_len >= 3 && STREQ(&path[path_len - 3], SEP_STR ".."))));
340
341 /*
342 * Final Prefix Cleanup.
343 * ---------------------
344 */
345 if (is_relative) {
346 if (path_len == 0 && (path == path_orig)) {
347 path[0] = '.';
348 path[1] = '\0';
349 path_len = 1;
350 }
351 }
352 else {
353 /* Support for odd paths: eg `/../home/me` --> `/home/me`
354 * this is a valid path in blender but we can't handle this the usual way below
355 * simply strip this prefix then evaluate the path as usual.
356 * Python's `os.path.normpath()` does this. */
357 if (start_base != path_first_non_slash_part) {
358 char *start = start_base > path + path_len ? start_base - 1 : start_base;
359 /* As long as `start` is set correctly, it should never begin with `../`
360 * as these directories are expected to be skipped. */
361 BLI_assert(!IS_PARENT_DIR(start));
362 const size_t start_len = path_len - (start - path);
363 BLI_assert(strlen(start) == start_len);
364 memmove(path_first_non_slash_part, start, start_len + 1);
365 path_len -= start - path_first_non_slash_part;
366 BLI_assert(strlen(path) == path_len);
367 }
368 }
369
370 BLI_assert(strlen(path) == path_len);
371
372#undef IS_PARENT_DIR
373
374 return (path - path_orig) + path_len;
375}
376
377int BLI_path_normalize(char *path)
378{
379 return path_normalize_impl(path, true);
380}
381
383{
384 return path_normalize_impl(path, false);
385}
386
387int BLI_path_normalize_dir(char *dir, size_t dir_maxncpy)
388{
389 /* Would just create an unexpected "/" path, just early exit entirely. */
390 if (dir[0] == '\0') {
391 return 0;
392 }
393
394 int dir_len = BLI_path_normalize(dir);
395 return BLI_path_slash_ensure_ex(dir, dir_maxncpy, dir_len);
396}
397
398int BLI_path_canonicalize_native(char *path, int path_maxncpy)
399{
400 BLI_path_abs_from_cwd(path, path_maxncpy);
401 /* As these are system level paths, only convert slashes
402 * if the alternate direction is accepted as a slash. */
405 }
406 int path_len = BLI_path_normalize_native(path);
407 /* Strip trailing slash but don't strip `/` away to nothing. */
408 if (path_len > 1 && path[path_len - 1] == SEP) {
409#ifdef WIN32
410 /* Don't strip `C:\` -> `C:` as this is no longer a valid directory. */
411 if (BLI_path_win32_prefix_len(path) + 1 < path_len)
412#endif
413 {
414 path_len -= 1;
415 path[path_len] = '\0';
416 }
417 }
418 return path_len;
419}
420
421bool BLI_path_make_safe_filename_ex(char *filename, bool allow_tokens)
422{
423#define INVALID_CHARS \
424 "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" \
425 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" \
426 "/\\?*:|\""
427#define INVALID_TOKENS "<>"
428
429 const char *invalid = allow_tokens ? INVALID_CHARS : INVALID_CHARS INVALID_TOKENS;
430
431#undef INVALID_CHARS
432#undef INVALID_TOKENS
433
434 char *fn;
435 bool changed = false;
436
437 if (*filename == '\0') {
438 return changed;
439 }
440
441 for (fn = filename; *fn && (fn = strpbrk(fn, invalid)); fn++) {
442 *fn = '_';
443 changed = true;
444 }
445
446 /* Forbid only dots. */
447 for (fn = filename; *fn == '.'; fn++) {
448 /* Pass. */
449 }
450 if (*fn == '\0') {
451 *filename = '_';
452 changed = true;
453 }
454
455#ifdef WIN32
456 {
457 const char *invalid_names[] = {
458 "con", "prn", "aux", "null", "com1", "com2", "com3", "com4",
459 "com5", "com6", "com7", "com8", "com9", "lpt1", "lpt2", "lpt3",
460 "lpt4", "lpt5", "lpt6", "lpt7", "lpt8", "lpt9", nullptr,
461 };
462 const size_t len = strlen(filename);
463 char *filename_lower = BLI_strdupn(filename, len);
464 const char **iname;
465
466 /* Forbid trailing dot (trailing space has already been replaced above). */
467 if (filename[len - 1] == '.') {
468 filename[len - 1] = '_';
469 changed = true;
470 }
471
472 /* Check for forbidden names - not we have to check all combination
473 * of upper and lower cases, hence the usage of filename_lower
474 * (more efficient than using #BLI_strcasestr repeatedly). */
475 BLI_str_tolower_ascii(filename_lower, len);
476 for (iname = invalid_names; *iname; iname++) {
477 if (strstr(filename_lower, *iname) == filename_lower) {
478 const size_t iname_len = strlen(*iname);
479 /* Only invalid if the whole name is made of the invalid chunk, or it has an
480 * (assumed extension) dot just after. This means it will also catch *valid*
481 * names like `aux.foo.bar`, but should be good enough for us! */
482 if ((iname_len == len) || (filename_lower[iname_len] == '.')) {
483 *filename = '_';
484 changed = true;
485 break;
486 }
487 }
488 }
489
490 MEM_delete(filename_lower);
491 }
492#endif
493
494 return changed;
495}
496
501
502bool BLI_path_make_safe(char *path)
503{
504 /* Simply apply #BLI_path_make_safe_filename() over each component of the path.
505 * Luckily enough, same *safe* rules applies to file & directory names. */
506 char *curr_slash, *curr_path = path;
507 bool changed = false;
508 bool skip_first = false;
509
510#ifdef WIN32
511 if (BLI_path_is_abs_win32(path)) {
512 /* Do not make safe `C:` in `C:\foo\bar`. */
513 skip_first = true;
514 }
515#endif
516
517 for (curr_slash = const_cast<char *>(BLI_path_slash_find(curr_path)); curr_slash;
518 curr_slash = const_cast<char *>(BLI_path_slash_find(curr_path)))
519 {
520 const char backup = *curr_slash;
521 *curr_slash = '\0';
522 if (!skip_first && (*curr_path != '\0') && BLI_path_make_safe_filename(curr_path)) {
523 changed = true;
524 }
525 skip_first = false;
526 curr_path = curr_slash + 1;
527 *curr_slash = backup;
528 }
529 if (BLI_path_make_safe_filename(curr_path)) {
530 changed = true;
531 }
532
533 return changed;
534}
535
536bool BLI_path_is_rel(const char *path)
537{
538 return path[0] == '/' && path[1] == '/';
539}
540
541bool BLI_path_is_unc(const char *path)
542{
543 return path[0] == '\\' && path[1] == '\\';
544}
545
552static int BLI_path_unc_prefix_len(const char *path)
553{
554 if (BLI_path_is_unc(path)) {
555 if ((path[2] == '?') && (path[3] == '\\')) {
556 /* We assume long UNC path like `\\?\server\share\folder` etc. */
557 return 4;
558 }
559
560 return 2;
561 }
562
563 return 0;
564}
565
566#ifdef WIN32
567static int BLI_path_win32_prefix_len(const char *path)
568{
569 if (BLI_path_is_win32_drive(path)) {
570 return 2;
571 }
572 return BLI_path_unc_prefix_len(path);
573}
574#endif
575
576bool BLI_path_is_win32_drive(const char *path)
577{
578 return isalpha(path[0]) && (path[1] == ':');
579}
580
581bool BLI_path_is_win32_drive_only(const char *path)
582{
583 return isalpha(path[0]) && (path[1] == ':') && (path[2] == '\0');
584}
585
587{
588 return isalpha(path[0]) && (path[1] == ':') && ELEM(path[2], '\\', '/');
589}
590
591#if defined(WIN32)
592
601static bool BLI_path_is_abs_win32(const char *path)
602{
603 /* Don't use the `BLI_path_is_win32_drive_with_slash`
604 * since paths such as `C:` are valid on their own. */
605 return BLI_path_is_win32_drive(path) || BLI_path_is_unc(path);
606}
607
608static wchar_t *next_slash(wchar_t *path)
609{
610 wchar_t *slash = path;
611 while (*slash && *slash != L'\\') {
612 slash++;
613 }
614 return slash;
615}
616
617/* Adds a slash if the UNC path points to a share. */
618static void BLI_path_add_slash_to_share(wchar_t *uncpath)
619{
620 wchar_t *slash_after_server = next_slash(uncpath + 2);
621 if (*slash_after_server) {
622 wchar_t *slash_after_share = next_slash(slash_after_server + 1);
623 if (!(*slash_after_share)) {
624 slash_after_share[0] = L'\\';
625 slash_after_share[1] = L'\0';
626 }
627 }
628}
629
630static void BLI_path_unc_to_short(wchar_t *unc)
631{
632 int len = wcslen(unc);
633 /* Convert:
634 * - `\\?\UNC\server\share\folder\...` to `\\server\share\folder\...`
635 * - `\\?\C:\` to `C:\`
636 * - `\\?\C:\folder\...` to `C:\folder\...`
637 */
638 if ((len > 3) && (unc[0] == L'\\') && (unc[1] == L'\\') && (unc[2] == L'?') &&
639 ELEM(unc[3], L'\\', L'/'))
640 {
641 /* The result is always shorter, shift the text in place (+1 for the terminator). */
642 if ((len > 5) && (unc[5] == L':')) {
643 memmove(unc, unc + 4, (len - 4 + 1) * sizeof(*unc));
644 }
645 else if ((len > 7) && (wcsncmp(&unc[4], L"UNC", 3) == 0) && ELEM(unc[7], L'\\', L'/')) {
646 /* Keep the leading `\\`. */
647 memmove(unc + 2, unc + 8, (len - 8 + 1) * sizeof(*unc));
648 }
649 }
650}
651
652void BLI_path_normalize_unc(char *path, int path_maxncpy)
653{
654 wchar_t *tmp_16 = alloc_utf16_from_8(path, 1);
655 BLI_path_normalize_unc_16(tmp_16);
656 conv_utf_16_to_8(tmp_16, path, path_maxncpy);
657 free(tmp_16);
658}
659
660void BLI_path_normalize_unc_16(wchar_t *path_16)
661{
662 BLI_path_unc_to_short(path_16);
663 BLI_path_add_slash_to_share(path_16);
664}
665#endif
666
676static bool path_relative_to_impl(char *path,
677 const size_t path_maxncpy,
678 const char *basepath,
679 const bool walk_up,
680 const bool blend_relative)
681{
682 BLI_string_debug_size_after_nil(path, path_maxncpy);
683 /* A `basepath` starting with `//` will be made relative multiple times. */
684 BLI_assert_msg(!BLI_path_is_rel(basepath), "The 'basepath' cannot start with '//'!");
685
686 const char *base_dir_end;
687 char base_path[FILE_MAX];
688
689 /* If path is already relative, bail out. */
690 if (BLI_path_is_rel(path)) {
691 return false;
692 }
693
694 /* Also bail out if relative path is not set. */
695 if (basepath[0] == '\0') {
696 return false;
697 }
698
699 STRNCPY(base_path, basepath);
700
701#ifdef WIN32
702 if (BLI_strnlen(path, 3) > 2) {
703 bool is_unc = BLI_path_is_unc(path);
704
705 /* Ensure paths are both UNC paths or are both drives. */
706 if (BLI_path_is_unc(base_path) != is_unc) {
707 return false;
708 }
709
710 /* Ensure both UNC paths are on the same share. Windows server and share names
711 * are case-insensitive, and either slash may be used. */
712 if (is_unc) {
713 int num_slashes = 0;
714 for (int offset = 0; base_path[offset] && num_slashes < 4; offset++) {
715 const char base_ch = base_path[offset];
716 const char path_ch = path[offset];
717
718 if (ELEM(base_ch, '\\', '/')) {
719 num_slashes++;
720 /* The path is the share itself. */
721 if (path_ch == '\0' && num_slashes == 4) {
722 break;
723 }
724 if (!ELEM(path_ch, '\\', '/')) {
725 return false;
726 }
727 }
728 else if (tolower(base_ch) != tolower(path_ch)) {
729 return false;
730 }
731 }
732 }
733 else if ((base_path[1] == ':' && path[1] == ':') &&
734 (tolower(base_path[0]) != tolower(path[0])))
735 {
736 return false;
737 }
738 }
739#endif
740
741 /* #BLI_path_normalize requires native slashes. */
744
745 /* Removes `/./` and `/../` which confuse the following slash counting. */
746 BLI_path_normalize(path);
747 BLI_path_normalize(base_path);
748
749 /* Use forward slashes as assumed by the comparison below. */
752
753 /* The last slash in the path indicates where the path part ends. */
754 base_dir_end = BLI_path_slash_rfind(base_path);
755
756 if (base_dir_end) {
757 /* Only use the directory part of the base path, including the trailing slash. */
758 base_path[base_dir_end - base_path + 1] = '\0';
759
760 /* Find the prefix of the filename that is equal for both filenames.
761 * Only entire components between slashes match. */
762 int base_offset = 0;
763 int path_offset = 0;
764 int i = 0;
765#ifdef WIN32
766 while ((base_path[i] != '\0') && tolower(base_path[i]) == tolower(path[i])) {
767#else
768 while ((base_path[i] != '\0') && base_path[i] == path[i]) {
769#endif
770 i++;
771 if (base_path[i - 1] == '/') {
772 base_offset = path_offset = i;
773 }
774 }
775
776 if ((path[i] == '\0') && (base_path[i] == '/')) {
777 /* The path is the base directory itself, or one of its parents. */
778 base_offset = i + 1;
779 path_offset = i;
780 }
781
782 if (base_offset == 0) {
783 /* No common root. */
784 return false;
785 }
786
787 char result[FILE_MAX] = "//";
788 char *result_iter = result + (blend_relative ? 2 : 0);
789
790 /* Walk up one directory for every component left in the base directory. */
791 for (const char *base_iter = base_path + base_offset; *base_iter; base_iter++) {
792 if (*base_iter == '/') {
793 if (!walk_up) {
794 return false;
795 }
796 result_iter += BLI_strncpy_rlen(
797 result_iter, "../", sizeof(result) - (result_iter - result));
798 }
799 }
800
801 result_iter += BLI_strncpy_rlen(
802 result_iter, path + path_offset, sizeof(result) - (result_iter - result));
803 UNUSED_VARS(result_iter);
804
805#ifdef WIN32
806 BLI_string_replace_char(result + (blend_relative ? 2 : 0), '/', '\\');
807#endif
808 BLI_strncpy(path, result, path_maxncpy);
809 return true;
810 }
811
812 return false;
813}
814
815void BLI_path_rel(char path[FILE_MAX], const char *basepath)
816{
817 const bool walk_up = true;
818 const bool blend_relative = true;
819
820#ifdef WIN32
821 if (BLI_strnlen(basepath, 3) > 2 && !BLI_path_is_abs_win32(basepath)) {
822 /* Fix missing volume name in relative base,
823 * can happen with old `recent-files.txt` files. */
824 char base_path[FILE_MAX];
826 char *base_path_after_root = &base_path[2];
827 if (!ELEM(basepath[0], '\\', '/')) {
828 base_path_after_root++;
829 }
831 base_path_after_root, basepath, sizeof(base_path) - (base_path_after_root - base_path));
832 path_relative_to_impl(path, FILE_MAX, base_path, walk_up, blend_relative);
833 return;
834 }
835#endif
836
837 path_relative_to_impl(path, FILE_MAX, basepath, walk_up, blend_relative);
838}
839
840bool BLI_path_suffix(char *path, size_t path_maxncpy, const char *suffix, const char *sep)
841{
842 BLI_string_debug_size_after_nil(path, path_maxncpy);
843
844 const size_t suffix_len = strlen(suffix);
845 const size_t sep_len = strlen(sep);
846 char *extension = const_cast<char *>(BLI_path_extension_or_end(path));
847 const size_t extension_len = strlen(extension);
848 const size_t path_end = extension - path;
849 const size_t path_len = path_end + extension_len;
850 if (path_len + sep_len + suffix_len >= path_maxncpy) {
851 return false;
852 }
853
854 if (extension_len) {
855 memmove(extension + (sep_len + suffix_len), extension, extension_len);
856 }
857 char *c = path + path_end;
858 if (sep_len) {
859 memcpy(c, sep, sep_len);
860 c += sep_len;
861 }
862 if (suffix_len) {
863 memcpy(c, suffix, suffix_len);
864 c += suffix_len;
865 }
866 c += extension_len;
867 *c = '\0';
868 return true;
869}
870
871const char *BLI_path_parent_dir_end(const char *path, size_t path_len)
872{
873 const char *path_end = path + path_len - 1;
874 const char *p = path_end;
875 while (p >= path) {
877 break;
878 }
879 p--;
880 }
881 while (p > path) {
882 if (BLI_path_slash_is_native_compat(*(p - 1))) {
883 p -= 1; /* Skip `/`. */
884 }
885 else if ((p - 1 > path) && (*(p - 1) == '.') && BLI_path_slash_is_native_compat(*(p - 2))) {
886 p -= 2; /* Skip `/.` (actually `/./` but the last slash was already skipped) */
887 }
888 else {
889 break;
890 }
891 }
892 if ((p > path) && (p != path_end)) {
893 return p;
894 }
895 return nullptr;
896}
897
898bool BLI_path_parent_dir(char *path)
899{
900 /* Use #BLI_path_name_at_index instead of checking if the strings ends with `parent_dir`
901 * to ensure the logic isn't confused by:
902 * - Directory names that happen to end with `..`.
903 * - When `path` is empty, the contents will be `../`
904 * which would cause checking for a tailing `/../` fail.
905 * Extracting the span of the final directory avoids both these issues. */
906 int tail_ofs = 0, tail_len = 0;
907 if (!BLI_path_name_at_index(path, -1, &tail_ofs, &tail_len)) {
908 return false;
909 }
910 if (tail_len == 1) {
911 /* Last path is `.`, as normalize should remove this, it's safe to assume failure.
912 * This happens when the input a single period (possibly with slashes before or after). */
913 if (path[tail_ofs] == '.') {
914 return false;
915 }
916 }
917
918 /* Input paths should already be normalized if `..` is part of the path. */
919 BLI_assert(!((tail_len == 2) && (path[tail_ofs] == '.') && (path[tail_ofs + 1] == '.')));
920 path[tail_ofs] = '\0';
921 return true;
922}
923
925{
926 bool valid_path = true;
927
928 /* Loop as long as cur path is not a dir, and we can get a parent path. */
929 while ((BLI_access(path, R_OK) != 0) && (valid_path = BLI_path_parent_dir(path))) {
930 /* Pass. */
931 }
932 return (valid_path && path[0]);
933}
934
945static bool path_frame_chars_find_range(const char *path, int *r_char_start, int *r_char_end)
946{
947 uint ch_sta, ch_end, i;
948 /* Insert current frame: `file###` -> `file001`. */
949 ch_sta = ch_end = 0;
950 for (i = 0; path[i] != '\0'; i++) {
951 if (ELEM(path[i], '\\', '/')) {
952 ch_end = 0; /* This is a directory name, don't use any hashes we found. */
953 }
954 else if (path[i] == '#') {
955 ch_sta = i;
956 ch_end = ch_sta + 1;
957 while (path[ch_end] == '#') {
958 ch_end++;
959 }
960 i = ch_end - 1; /* Keep searching. */
961
962 /* Don't break, there may be a slash after this that invalidates the previous #'s. */
963 }
964 }
965
966 if (ch_end) {
967 *r_char_start = ch_sta;
968 *r_char_end = ch_end;
969 return true;
970 }
971
972 *r_char_start = -1;
973 *r_char_end = -1;
974 return false;
975}
976
983static bool ensure_digits(char *path, const size_t path_maxncpy, int digits)
984{
985 char *file = const_cast<char *>(BLI_path_basename(path));
986 if (strrchr(file, '#') == nullptr) {
987 int len = strlen(file);
988 if (size_t(file - path) + size_t(len + digits) >= path_maxncpy) {
989 return false;
990 }
991
992 while (digits--) {
993 file[len++] = '#';
994 }
995 file[len] = '\0';
996 }
997 return true;
998}
999
1000bool BLI_path_frame(char *path, size_t path_maxncpy, int frame, int digits)
1001{
1002 BLI_string_debug_size_after_nil(path, path_maxncpy);
1003
1004 int ch_sta, ch_end;
1005
1006 if (digits && !ensure_digits(path, path_maxncpy, digits)) {
1007 return false;
1008 }
1009
1010 if (path_frame_chars_find_range(path, &ch_sta, &ch_end)) {
1011 char frame_str[FILENAME_FRAME_CHARS_MAX + 1]; /* One for null. */
1012 const int ch_span = std::min(ch_end - ch_sta, FILENAME_FRAME_CHARS_MAX);
1013 SNPRINTF(frame_str, "%.*d", ch_span, frame);
1014 BLI_string_replace_range(path, path_maxncpy, ch_sta, ch_end, frame_str);
1015 return true;
1016 }
1017 return false;
1018}
1019
1020bool BLI_path_frame_range(char *path, size_t path_maxncpy, int sta, int end, int digits)
1021{
1022 BLI_string_debug_size_after_nil(path, path_maxncpy);
1023
1024 int ch_sta, ch_end;
1025
1026 if (digits && !ensure_digits(path, path_maxncpy, digits)) {
1027 return false;
1028 }
1029
1030 if (path_frame_chars_find_range(path, &ch_sta, &ch_end)) {
1031 char frame_str[(FILENAME_FRAME_CHARS_MAX * 2) + 1 + 1]; /* One for null, one for the '-' */
1032 const int ch_span = std::min(ch_end - ch_sta, FILENAME_FRAME_CHARS_MAX);
1033 SNPRINTF(frame_str, "%.*d-%.*d", ch_span, sta, ch_span, end);
1034 BLI_string_replace_range(path, path_maxncpy, ch_sta, ch_end, frame_str);
1035 return true;
1036 }
1037 return false;
1038}
1039
1040bool BLI_path_frame_get(const char *path, int *r_frame, int *r_digits_len)
1041{
1042 if (*path == '\0') {
1043 return false;
1044 }
1045
1046 *r_digits_len = 0;
1047
1048 const char *file = BLI_path_basename(path);
1049 const char *file_ext = BLI_path_extension_or_end(file);
1050 const char *c = file_ext;
1051
1052 /* Find start of number (if there is one). */
1053 int digits_len = 0;
1054 while (c-- != file && isdigit(*c)) {
1055 digits_len++;
1056 }
1057 c++;
1058
1059 if (digits_len == 0) {
1060 return false;
1061 }
1062
1063 *r_digits_len = digits_len;
1064
1065 /* Only consider numbers inside the range of valid frame-numbers (ints). */
1066 /* No need to trim the string, `strtoll` ignores non-digits. */
1067 const long long num = strtoll(c, nullptr, 10);
1068 if (num > INT_MAX) {
1069 return false;
1070 }
1071
1072 *r_frame = int(num);
1073 return true;
1074}
1075
1076void BLI_path_frame_strip(char *path, char *r_ext, const size_t ext_maxncpy)
1077{
1078 BLI_string_debug_size(r_ext, ext_maxncpy);
1079 *r_ext = '\0';
1080 if (*path == '\0') {
1081 return;
1082 }
1083
1084 char *file = const_cast<char *>(BLI_path_basename(path));
1085 char *file_ext = const_cast<char *>(BLI_path_extension_or_end(file));
1086 BLI_strncpy(r_ext, file_ext, ext_maxncpy);
1087
1088 char *c = file_ext;
1089
1090 /* Find start of number (if there is one). */
1091 int digits_len = 0;
1092 while (c-- != file && isdigit(*c)) {
1093 digits_len++;
1094 }
1095 c++;
1096
1097 /* No need to trim the string, `strtoll` ignores non-digits. */
1098 const long long num = strtoll(c, nullptr, 10);
1099 if (num <= INT_MAX) {
1100 /* Replace the number with the suffix. */
1101 while (digits_len--) {
1102 *c++ = '#';
1103 }
1104 }
1105 else {
1106 /* Don't strip numbers outside the range of valid frame-numbers (ints).
1107 * So go back to where we were before finding the start of the number. */
1108 c += digits_len;
1109 }
1110
1111 /* Terminate the string. */
1112 *c = '\0';
1113}
1114
1115bool BLI_path_frame_check_chars(const char *path)
1116{
1117 int ch_sta_dummy, ch_end_dummy;
1118 return path_frame_chars_find_range(path, &ch_sta_dummy, &ch_end_dummy);
1119}
1120
1121void BLI_path_to_display_name(char *display_name, int display_name_maxncpy, const char *name)
1122{
1123 BLI_string_debug_size(display_name, display_name_maxncpy);
1124
1125 /* Strip leading underscores and spaces. */
1126 int strip_offset = 0;
1127 while (ELEM(name[strip_offset], '_', ' ')) {
1128 strip_offset++;
1129 }
1130
1131 int display_name_len = BLI_strncpy_rlen(display_name, name + strip_offset, display_name_maxncpy);
1132
1133 /* Replace underscores with spaces. */
1134 BLI_string_replace_char(display_name, '_', ' ');
1135
1136 /* In rare cases file paths may not contain valid UTF8 sequences.
1137 * In this case show *something*, since it's possible stripping would remove all text.
1138 * Use a dummy character as a hint the character could not be shown. */
1139 BLI_str_utf8_invalid_substitute(display_name, display_name_len, '_');
1140
1141 BLI_path_extension_strip(display_name);
1142
1143 /* Test if string has any upper case characters. */
1144 bool all_lower = true;
1145 for (int i = 0; display_name[i]; i++) {
1146 if (isupper(display_name[i])) {
1147 all_lower = false;
1148 break;
1149 }
1150 }
1151
1152 if (all_lower) {
1153 /* For full lowercase string, use title case. */
1154 bool prevspace = true;
1155 for (int i = 0; display_name[i]; i++) {
1156 if (prevspace) {
1157 display_name[i] = toupper(display_name[i]);
1158 }
1159
1160 prevspace = isspace(display_name[i]);
1161 }
1162 }
1163}
1164
1165bool BLI_path_abs(char path[FILE_MAX], const char *basepath)
1166{
1168 /* A `basepath` starting with `//` will be made absolute multiple times. */
1169 BLI_assert_msg(!BLI_path_is_rel(basepath), "The 'basepath' cannot start with '//'!");
1170
1171 const bool wasrelative = BLI_path_is_rel(path);
1172 char tmp[FILE_MAX];
1173 char base[FILE_MAX];
1174#ifdef WIN32
1175
1176 /* Without this, an empty string converts to: `C:\` */
1177 if (*path == '\0') {
1178 return wasrelative;
1179 }
1180
1181 /* We are checking here if we have an absolute path that is not in the current `.blend` file
1182 * as a lib main - we are basically checking for the case that a UNIX root `/` is passed. */
1183 if (!wasrelative && !BLI_path_is_abs_win32(path)) {
1184 const size_t root_dir_len = 3;
1185 char *p = path;
1187 BLI_assert(strlen(tmp) == root_dir_len);
1188
1189 /* Step over the slashes at the beginning of the path. */
1190 p = (char *)BLI_path_slash_skip(p);
1191 BLI_strncpy(tmp + root_dir_len, p, sizeof(tmp) - root_dir_len);
1192 }
1193 else {
1194 STRNCPY(tmp, path);
1195 }
1196#else
1197 STRNCPY(tmp, path);
1198
1199 /* Check for loading a MS-Windows path on a POSIX system
1200 * in this case, there is no use in trying `C:/` since it
1201 * will never exist on a Unix system.
1202 *
1203 * Add a `/` prefix and lowercase the drive-letter, remove the `:`.
1204 * `C:\foo.JPG` -> `/c/foo.JPG` */
1205
1207 tmp[1] = tolower(tmp[0]); /* Replace `:` with drive-letter. */
1208 tmp[0] = '/';
1209 /* `\` the slash will be converted later. */
1210 }
1211
1212#endif
1213
1214 /* NOTE(@jesterKing): push slashes into unix mode - strings entering this part are
1215 * potentially messed up: having both back- and forward slashes.
1216 * Here we push into one conform direction, and at the end we
1217 * push them into the system specific dir. This ensures uniformity
1218 * of paths and solving some problems (and prevent potential future ones).
1219 *
1220 * NOTE(@elubie): For UNC paths the first characters containing the UNC prefix
1221 * shouldn't be switched as we need to distinguish them from
1222 * paths relative to the `.blend` file. */
1223 BLI_string_replace_char(tmp + BLI_path_unc_prefix_len(tmp), '\\', '/');
1224
1225 /* Paths starting with `//` will get the blend file as their base,
1226 * this isn't standard in any OS but is used in blender all over the place. */
1227 if (wasrelative) {
1228 const char *lslash;
1229 STRNCPY(base, basepath);
1230
1231 /* File component is ignored, so don't bother with the trailing slash. */
1232 BLI_path_normalize(base);
1233 lslash = BLI_path_slash_rfind(base);
1234 BLI_string_replace_char(base + BLI_path_unc_prefix_len(base), '\\', '/');
1235
1236 if (lslash) {
1237 /* Length up to and including last `/`. */
1238 const int baselen = int(lslash - base) + 1;
1239 /* Use path for temp storage here, we copy back over it right away. */
1240 BLI_strncpy(path, tmp + 2, FILE_MAX); /* Strip `//` prefix. */
1241
1242 memcpy(tmp, base, baselen); /* Prefix with base up to last `/`. */
1243 BLI_strncpy(tmp + baselen, path, sizeof(tmp) - baselen); /* Append path after `//`. */
1244 BLI_strncpy(path, tmp, FILE_MAX); /* Return as result. */
1245 }
1246 else {
1247 /* Base doesn't seem to be a directory, ignore it and just strip `//` prefix on path.
1248 * Skip any extra slashes too, otherwise `///` would become an absolute path. */
1249 BLI_strncpy(path, BLI_path_slash_skip(tmp + 2), FILE_MAX);
1250 }
1251 }
1252 else {
1253 /* Base ignored. */
1254 BLI_strncpy(path, tmp, FILE_MAX);
1255 }
1256
1257#ifdef WIN32
1258 /* NOTE(@jesterking): Skip first two chars, which in case of absolute path will
1259 * be `drive:/blabla` and in case of `relpath` `//blabla/`.
1260 * So `relpath` `//` will be retained, rest will be nice and shiny WIN32 backward slashes. */
1261 BLI_string_replace_char(path + 2, '/', '\\');
1262#endif
1263
1264 /* Ensure this is after correcting for path switch. */
1265 BLI_path_normalize(path);
1266
1267 return wasrelative;
1268}
1269
1270bool BLI_path_is_abs_from_cwd(const char *path)
1271{
1272 bool is_abs = false;
1273
1274#ifdef WIN32
1275 if (BLI_path_is_abs_win32(path)) {
1276 is_abs = true;
1277 }
1278#else
1279 if (path[0] == '/') {
1280 is_abs = true;
1281 }
1282#endif
1283 return is_abs;
1284}
1285
1286bool BLI_path_abs_from_cwd(char *path, const size_t path_maxncpy)
1287{
1288 BLI_string_debug_size_after_nil(path, path_maxncpy);
1289
1290 if (!BLI_path_is_abs_from_cwd(path)) {
1291 char cwd[PATH_MAX];
1292 /* In case the full path to the blend isn't used. */
1293 if (BLI_current_working_dir(cwd, sizeof(cwd))) {
1294 char origpath[PATH_MAX];
1295 STRNCPY(origpath, path);
1296 BLI_path_join(path, path_maxncpy, cwd, origpath);
1297 }
1298 else {
1299 printf("Could not get the current working directory - $PWD for an unknown reason.\n");
1300 }
1301 return true;
1302 }
1303
1304 return false;
1305}
1306
1307#ifdef _WIN32
1313bool BLI_path_program_extensions_add_win32(char *program_name, const size_t program_name_maxncpy)
1314{
1315 bool retval = false;
1316 int type;
1317
1318 type = BLI_file_stat_mode(program_name);
1319 if ((type == 0) || S_ISDIR(type)) {
1320 /* Typically 3-5, ".EXE", ".BAT"... etc. */
1321 const int ext_max = 12;
1322 const char *ext = BLI_getenv("PATHEXT");
1323 if (ext) {
1324 const int program_name_len = strlen(program_name);
1325 char *filename = static_cast<char *>(alloca(program_name_len + ext_max));
1326 char *filename_ext;
1327 const char *ext_next;
1328
1329 /* Null terminated in the loop. */
1330 memcpy(filename, program_name, program_name_len);
1331 filename_ext = filename + program_name_len;
1332
1333 do {
1334 int ext_len;
1335 ext_next = strchr(ext, ';');
1336 ext_len = ext_next ? ((ext_next++) - ext) : strlen(ext);
1337
1338 if (ext_len < ext_max) [[likely]] {
1339 memcpy(filename_ext, ext, ext_len);
1340 filename_ext[ext_len] = '\0';
1341
1343 if (type && (!S_ISDIR(type))) {
1344 retval = true;
1345 BLI_strncpy(program_name, filename, program_name_maxncpy);
1346 break;
1347 }
1348 }
1349 } while ((ext = ext_next));
1350 }
1351 }
1352 else {
1353 retval = true;
1354 }
1355
1356 return retval;
1357}
1358#endif /* WIN32 */
1359
1360bool BLI_path_program_search(char *program_filepath,
1361 const size_t program_filepath_maxncpy,
1362 const char *program_name)
1363{
1364 BLI_string_debug_size(program_filepath, program_filepath_maxncpy);
1365
1366 const char *path;
1367 bool retval = false;
1368
1369#ifdef _WIN32
1370 const char separator = ';';
1371#else
1372 const char separator = ':';
1373#endif
1374
1375 path = BLI_getenv("PATH");
1376 if (path) {
1377 char filepath_test[PATH_MAX];
1378 const char *temp;
1379
1380 do {
1381 temp = strchr(path, separator);
1382 if (temp) {
1383 memcpy(filepath_test, path, temp - path);
1384 filepath_test[temp - path] = 0;
1385 path = temp + 1;
1386 }
1387 else {
1388 STRNCPY(filepath_test, path);
1389 }
1390
1391 BLI_path_append(filepath_test, program_filepath_maxncpy, program_name);
1392 if (
1393#ifdef _WIN32
1394 BLI_path_program_extensions_add_win32(filepath_test, sizeof(filepath_test))
1395#else
1396 BLI_exists(filepath_test)
1397#endif
1398 )
1399 {
1400 BLI_strncpy(program_filepath, filepath_test, program_filepath_maxncpy);
1401 retval = true;
1402 break;
1403 }
1404 } while (temp);
1405 }
1406
1407 if (retval == false) {
1408 *program_filepath = '\0';
1409 }
1410
1411 return retval;
1412}
1413
1414void BLI_setenv(const char *env, const char *val)
1415{
1416#if (defined(_WIN32) || defined(_WIN64))
1417 /* MS-Windows. */
1418 uputenv(env, val);
1419#else
1420 /* Linux/macOS/BSD */
1421 if (val) {
1422 setenv(env, val, 1);
1423 }
1424 else {
1425 unsetenv(env);
1426 }
1427#endif
1428}
1429
1430void BLI_setenv_if_new(const char *env, const char *val)
1431{
1432 if (BLI_getenv(env) == nullptr) {
1433 BLI_setenv(env, val);
1434 }
1435}
1436
1437const char *BLI_getenv(const char *env)
1438{
1439#ifdef _MSC_VER
1440 const char *result = nullptr;
1441 /* 32767 is the maximum size of the environment variable on windows,
1442 * reserve one more character for the zero terminator. */
1443 static wchar_t buffer[32768];
1444 wchar_t *env_16 = alloc_utf16_from_8(env, 0);
1445 if (env_16) {
1446 /* Skip zero for both empty strings and when the environment variable isn't found. */
1447 if (GetEnvironmentVariableW(env_16, buffer, ARRAY_SIZE(buffer))) {
1448 char *res_utf8 = alloc_utf_8_from_16(buffer, 0);
1449 /* Make sure the result is valid, and will fit into our temporary storage buffer. */
1450 if (res_utf8) {
1451 if (strlen(res_utf8) + 1 < sizeof(buffer)) {
1452 /* We are re-using the utf16 buffer here, since allocating a second static buffer to
1453 * contain the UTF8 version to return would be wasteful. */
1454 memcpy(buffer, res_utf8, strlen(res_utf8) + 1);
1455 result = (const char *)buffer;
1456 }
1457 free(res_utf8);
1458 }
1459 }
1460 free(env_16);
1461 }
1462 return result;
1463#else
1464 return getenv(env);
1465#endif
1466}
1467
1468static bool path_extension_check_ex(const char *path,
1469 const size_t path_len,
1470 const char *ext,
1471 const size_t ext_len)
1472{
1473 BLI_assert(strlen(path) == path_len);
1474 BLI_assert(strlen(ext) == ext_len);
1475
1476 return (((path_len == 0 || ext_len == 0 || ext_len >= path_len) == 0) &&
1477 (BLI_strcasecmp(ext, path + path_len - ext_len) == 0));
1478}
1479
1480bool BLI_path_extension_check(const char *path, const char *ext)
1481{
1482 return path_extension_check_ex(path, strlen(path), ext, strlen(ext));
1483}
1484
1485bool BLI_path_extension_check_n(const char *path, ...)
1486{
1487 const size_t path_len = strlen(path);
1488
1489 va_list args;
1490 const char *ext;
1491 bool ret = false;
1492
1493 va_start(args, path);
1494
1495 while ((ext = static_cast<const char *>(va_arg(args, void *)))) {
1496 if (path_extension_check_ex(path, path_len, ext, strlen(ext))) {
1497 ret = true;
1498 break;
1499 }
1500 }
1501
1502 va_end(args);
1503
1504 return ret;
1505}
1506
1507bool BLI_path_extension_check_array(const char *path, const char **ext_array)
1508{
1509 const size_t path_len = strlen(path);
1510 int i = 0;
1511
1512 while (ext_array[i]) {
1513 if (path_extension_check_ex(path, path_len, ext_array[i], strlen(ext_array[i]))) {
1514 return true;
1515 }
1516
1517 i++;
1518 }
1519 return false;
1520}
1521
1522bool BLI_path_extension_check_glob(const char *path, const char *ext_fnmatch)
1523{
1524 const char *ext_step = ext_fnmatch;
1525 char pattern[16];
1526
1527 while (ext_step[0]) {
1528 const char *ext_next;
1529 size_t len_ext;
1530
1531 if ((ext_next = strchr(ext_step, ';'))) {
1532 len_ext = ext_next - ext_step + 1;
1533 BLI_strncpy(pattern, ext_step, (len_ext > sizeof(pattern)) ? sizeof(pattern) : len_ext);
1534 }
1535 else {
1536 len_ext = STRNCPY_RLEN(pattern, ext_step);
1537 }
1538
1539 if (fnmatch(pattern, path, FNM_CASEFOLD) == 0) {
1540 return true;
1541 }
1542 ext_step += len_ext;
1543 }
1544
1545 return false;
1546}
1547
1549{
1550 bool only_wildcards = false;
1551
1552 for (size_t i = strlen(ext_fnmatch); i-- > 0;) {
1553 if (ext_fnmatch[i] == ';') {
1554 /* Group separator, we truncate here if we only had wildcards so far.
1555 * Otherwise, all is sound and fine. */
1556 if (only_wildcards) {
1557 ext_fnmatch[i] = '\0';
1558 return true;
1559 }
1560 return false;
1561 }
1562 if (!ELEM(ext_fnmatch[i], '?', '*')) {
1563 /* Non-wildcard char, we can break here and consider the pattern valid. */
1564 return false;
1565 }
1566 /* So far, only wildcards in last group of the pattern. */
1567 only_wildcards = true;
1568 }
1569 /* Only one group in the pattern, so even if its only made of wildcard(s),
1570 * it is assumed valid. */
1571 return false;
1572}
1573
1574bool BLI_path_extension_replace(char *path, size_t path_maxncpy, const char *ext)
1575{
1576 BLI_string_debug_size_after_nil(path, path_maxncpy);
1577
1578 char *path_ext = const_cast<char *>(BLI_path_extension_or_end(path));
1579 const size_t ext_len = strlen(ext);
1580 if ((path_ext - path) + ext_len >= path_maxncpy) {
1581 return false;
1582 }
1583
1584 memcpy(path_ext, ext, ext_len + 1);
1585 return true;
1586}
1587
1589{
1590 char *path_ext = const_cast<char *>(BLI_path_extension(path));
1591 if (path_ext == nullptr) {
1592 return false;
1593 }
1594 *path_ext = '\0';
1595 return true;
1596}
1597
1598bool BLI_path_extension_ensure(char *path, size_t path_maxncpy, const char *ext)
1599{
1600 BLI_string_debug_size_after_nil(path, path_maxncpy);
1601
1602 /* First check the extension is already there.
1603 * If `path_ext` is the end of the string this is simply checking if `ext` is also empty. */
1604 const char *path_ext = BLI_path_extension_or_end(path);
1605 if (STREQ(path_ext, ext)) {
1606 return true;
1607 }
1608
1609 const size_t path_len = strlen(path);
1610 const size_t ext_len = strlen(ext);
1611 int64_t a;
1612
1613 for (a = path_len - 1; a >= 0; a--) {
1614 if (path[a] == '.') {
1615 path[a] = '\0';
1616 }
1617 else {
1618 break;
1619 }
1620 }
1621 a++;
1622
1623 if (a + ext_len >= path_maxncpy) {
1624 return false;
1625 }
1626
1627 memcpy(path + a, ext, ext_len + 1);
1628 return true;
1629}
1630
1631bool BLI_path_filename_ensure(char *filepath, size_t filepath_maxncpy, const char *filename)
1632{
1633 BLI_string_debug_size_after_nil(filepath, filepath_maxncpy);
1634 char *c = const_cast<char *>(BLI_path_basename(filepath));
1635 const size_t filename_size = strlen(filename) + 1;
1636 if (filename_size <= filepath_maxncpy - (c - filepath)) {
1637 memcpy(c, filename, filename_size);
1638 return true;
1639 }
1640 return false;
1641}
1642
1643void BLI_path_split_dir_file(const char *filepath,
1644 char *dir,
1645 const size_t dir_maxncpy,
1646 char *file,
1647 const size_t file_maxncpy)
1648{
1649 BLI_string_debug_size(dir, dir_maxncpy);
1650 BLI_string_debug_size(file, file_maxncpy);
1651
1652 const char *basename = BLI_path_basename(filepath);
1653 if (basename != filepath) {
1654 const size_t dir_size = (basename - filepath) + 1;
1655 BLI_strncpy(dir, filepath, std::min(dir_maxncpy, dir_size));
1656 }
1657 else {
1658 dir[0] = '\0';
1659 }
1660 BLI_strncpy(file, basename, file_maxncpy);
1661}
1662
1663void BLI_path_split_dir_part(const char *filepath, char *dir, const size_t dir_maxncpy)
1664{
1665 BLI_string_debug_size(dir, dir_maxncpy);
1666 const char *basename = BLI_path_basename(filepath);
1667 if (basename != filepath) {
1668 const size_t dir_size = (basename - filepath) + 1;
1669 BLI_strncpy(dir, filepath, std::min(dir_maxncpy, dir_size));
1670 }
1671 else {
1672 dir[0] = '\0';
1673 }
1674}
1675
1676void BLI_path_split_file_part(const char *filepath, char *file, const size_t file_maxncpy)
1677{
1678 BLI_string_debug_size(file, file_maxncpy);
1679 const char *basename = BLI_path_basename(filepath);
1680 BLI_strncpy(file, basename, file_maxncpy);
1681}
1682
1683const char *BLI_path_extension_or_end(const char *filepath)
1684{
1685 /* NOTE(@ideasman42): Skip the extension when there are no preceding non-extension characters in
1686 * the file name. This ignores extensions at the beginning of a string or directly after a slash.
1687 * Only using trailing extension characters has the advantage that stripping the extension
1688 * never leads to a blank string (which can't be used as a file path).
1689 * Matches Python's `os.path.splitext`. */
1690 const char *ext = nullptr;
1691 bool has_non_ext = false;
1692 const char *c = filepath;
1693 for (; *c; c++) {
1694 switch (*c) {
1695 case '.': {
1696 if (has_non_ext) {
1697 ext = c;
1698 }
1699 break;
1700 }
1701 case SEP:
1702 case ALTSEP: {
1703 ext = nullptr;
1704 has_non_ext = false;
1705 break;
1706 }
1707 default: {
1708 has_non_ext = true;
1709 break;
1710 }
1711 }
1712 }
1713 if (ext) {
1714 return ext;
1715 }
1716 BLI_assert(*c == '\0');
1717 return c;
1718}
1719
1720const char *BLI_path_extension(const char *filepath)
1721{
1722 const char *ext = BLI_path_extension_or_end(filepath);
1723 return *ext ? ext : nullptr;
1724}
1725
1726size_t BLI_path_append(char *__restrict dst, const size_t dst_maxncpy, const char *__restrict file)
1727{
1728 /* Slash ensure uses #BLI_string_debug_size */
1729 int dst_len = BLI_path_slash_ensure(dst, dst_maxncpy);
1730 if (dst_len + 1 < dst_maxncpy) {
1731 dst_len += BLI_strncpy_rlen(dst + dst_len, file, dst_maxncpy - dst_len);
1732 }
1733 return dst_len;
1734}
1735
1736size_t BLI_path_append_dir(char *__restrict dst,
1737 const size_t dst_maxncpy,
1738 const char *__restrict dir)
1739{
1740 size_t dst_len = BLI_path_append(dst, dst_maxncpy, dir);
1741 return BLI_path_slash_ensure_ex(dst, dst_maxncpy, dst_len);
1742}
1743
1744size_t BLI_path_join_array(char *__restrict dst,
1745 const size_t dst_maxncpy,
1746 const char *path_array[],
1747 const int path_array_num)
1748{
1749 BLI_assert(path_array_num > 0);
1750 BLI_string_debug_size(dst, dst_maxncpy);
1751
1752 if (dst_maxncpy == 0) [[unlikely]] {
1753 return 0;
1754 }
1755 const char *path = path_array[0];
1756
1757 const size_t dst_last = dst_maxncpy - 1;
1758 size_t ofs = BLI_strncpy_rlen(dst, path, dst_maxncpy);
1759
1760 if (ofs == dst_last) {
1761 return ofs;
1762 }
1763
1764#ifdef WIN32
1765 /* Special case `//` for relative paths, don't use separator #SEP
1766 * as this has a special meaning on both WIN32 & UNIX.
1767 * Without this check joining `"//", "path"`. results in `"//\path"`. */
1768 if (ofs != 0) {
1769 size_t i;
1770 for (i = 0; i < ofs; i++) {
1771 if (dst[i] != '/') {
1772 break;
1773 }
1774 }
1775 if (i == ofs) {
1776 /* All slashes, keep them as-is, and join the remaining path array. */
1777 return path_array_num > 1 ?
1778 ofs + BLI_path_join_array(
1779 dst + ofs, dst_maxncpy - ofs, &path_array[1], path_array_num - 1) :
1780 ofs;
1781 }
1782 }
1783#endif
1784
1785 /* Remove trailing slashes, unless there are *only* trailing slashes
1786 * (allow `//` or `//some_path` as the first argument). */
1787 bool has_trailing_slash = false;
1788 if (ofs != 0) {
1789 size_t len = ofs;
1790 while ((len != 0) && BLI_path_slash_is_native_compat(path[len - 1])) {
1791 len -= 1;
1792 }
1793
1794 if (len != 0) {
1795 ofs = len;
1796 }
1797 has_trailing_slash = (path[len] != '\0');
1798 }
1799
1800 for (int path_index = 1; path_index < path_array_num; path_index++) {
1801 path = path_array[path_index];
1802 has_trailing_slash = false;
1803 const char *path_init = path;
1804 while (BLI_path_slash_is_native_compat(path[0])) {
1805 path++;
1806 }
1807 size_t len = strlen(path);
1808 if (len != 0) {
1809 while ((len != 0) && BLI_path_slash_is_native_compat(path[len - 1])) {
1810 len -= 1;
1811 }
1812
1813 if (len != 0) {
1814 /* The very first path may have a slash at the end. */
1815 if (ofs && !BLI_path_slash_is_native_compat(dst[ofs - 1])) {
1816 dst[ofs++] = SEP;
1817 if (ofs == dst_last) {
1818 break;
1819 }
1820 }
1821 has_trailing_slash = (path[len] != '\0');
1822 if (ofs + len >= dst_last) {
1823 len = dst_last - ofs;
1824 }
1825 memcpy(&dst[ofs], path, len);
1826 ofs += len;
1827 if (ofs == dst_last) {
1828 break;
1829 }
1830 }
1831 }
1832 else {
1833 has_trailing_slash = (path_init != path);
1834 }
1835 }
1836
1837 if (has_trailing_slash) {
1838 if ((ofs != dst_last) && (ofs != 0) && !BLI_path_slash_is_native_compat(dst[ofs - 1])) {
1839 dst[ofs++] = SEP;
1840 }
1841 }
1842
1843 BLI_assert(ofs <= dst_last);
1844 dst[ofs] = '\0';
1845
1846 return ofs;
1847}
1848
1849const char *BLI_path_basename(const char *path)
1850{
1851 const char *const filename = BLI_path_slash_rfind(path);
1852 return filename ? filename + 1 : path;
1853}
1854
1855static bool path_name_at_index_forward(const char *__restrict path,
1856 const int index,
1857 int *__restrict r_offset,
1858 int *__restrict r_len)
1859{
1860 BLI_assert(index >= 0);
1861 int index_step = 0;
1862 int prev = -1;
1863 int i = 0;
1864 while (true) {
1865 const char c = path[i];
1866 if ((c == '\0') || BLI_path_slash_is_native_compat(c)) {
1867 if (prev + 1 != i) {
1868 prev += 1;
1869 /* Skip `/./` (behave as if they don't exist). */
1870 if (!((i - prev == 1) && (prev != 0) && (path[prev] == '.'))) {
1871 if (index_step == index) {
1872 *r_offset = prev;
1873 *r_len = i - prev;
1874 return true;
1875 }
1876 index_step += 1;
1877 }
1878 }
1879 if (c == '\0') {
1880 break;
1881 }
1882 prev = i;
1883 }
1884 i += 1;
1885 }
1886 return false;
1887}
1888
1889static bool path_name_at_index_backward(const char *__restrict path,
1890 const int index,
1891 int *__restrict r_offset,
1892 int *__restrict r_len)
1893{
1894 /* Negative number, reverse where -1 is the last element. */
1895 BLI_assert(index < 0);
1896 int index_step = -1;
1897 int prev = strlen(path);
1898 int i = prev - 1;
1899 while (true) {
1900 const char c = i >= 0 ? path[i] : '\0';
1901 if ((c == '\0') || BLI_path_slash_is_native_compat(c)) {
1902 if (prev - 1 != i) {
1903 i += 1;
1904 /* Skip `/./` (behave as if they don't exist). */
1905 if (!((prev - i == 1) && (i != 0) && (path[i] == '.'))) {
1906 if (index_step == index) {
1907 *r_offset = i;
1908 *r_len = prev - i;
1909 return true;
1910 }
1911 index_step -= 1;
1912 }
1913 }
1914 if (c == '\0') {
1915 break;
1916 }
1917 prev = i;
1918 }
1919 i -= 1;
1920 }
1921 return false;
1922}
1923
1924bool BLI_path_name_at_index(const char *__restrict path,
1925 const int index,
1926 int *__restrict r_offset,
1927 int *__restrict r_len)
1928{
1929 return (index >= 0) ? path_name_at_index_forward(path, index, r_offset, r_len) :
1930 path_name_at_index_backward(path, index, r_offset, r_len);
1931}
1932
1933bool BLI_path_contains(const char *container_path, const char *containee_path)
1934{
1935 if (container_path[0] == '\0') {
1936 return false;
1937 }
1938
1939 const bool walk_up = false;
1940 return BLI_path_relative_to(containee_path, container_path, walk_up, nullptr, 0);
1941}
1942
1943bool BLI_path_relative_to(const char *path,
1944 const char *base_dir,
1945 const bool walk_up,
1946 char *r_path_relative,
1947 const size_t r_path_relative_maxncpy)
1948{
1949 if (r_path_relative) {
1950 r_path_relative[0] = '\0';
1951 }
1952
1953 if (base_dir[0] == '\0') {
1954 BLI_assert_msg(0, "BLI_path_relative_to should not be called with an empty base directory");
1955 return false;
1956 }
1957
1958 /* Add a trailing slash, otherwise path_relative_to_impl assumes it's a file. */
1959 char base_dir_slash[FILE_MAX];
1960 STRNCPY(base_dir_slash, base_dir);
1961 BLI_path_slash_ensure(base_dir_slash, sizeof(base_dir_slash));
1962
1963 char path_rel[FILE_MAX];
1964 STRNCPY(path_rel, path);
1965
1966 const bool blend_relative = false;
1967 if (!path_relative_to_impl(path_rel, sizeof(path_rel), base_dir_slash, walk_up, blend_relative))
1968 {
1969 return false;
1970 }
1971
1972 if (r_path_relative) {
1973 BLI_strncpy(r_path_relative, path_rel, r_path_relative_maxncpy);
1974 }
1975 return true;
1976}
1977
1978const char *BLI_path_slash_find(const char *path)
1979{
1980 const char *const ffslash = strchr(path, '/');
1981 const char *const fbslash = strchr(path, '\\');
1982
1983 if (!ffslash) {
1984 return fbslash;
1985 }
1986 if (!fbslash) {
1987 return ffslash;
1988 }
1989
1990 return (ffslash < fbslash) ? ffslash : fbslash;
1991}
1992
1993const char *BLI_path_slash_rfind(const char *path)
1994{
1995 const char *const lfslash = strrchr(path, '/');
1996 const char *const lbslash = strrchr(path, '\\');
1997
1998 if (!lfslash) {
1999 return lbslash;
2000 }
2001 if (!lbslash) {
2002 return lfslash;
2003 }
2004
2005 return (lfslash > lbslash) ? lfslash : lbslash;
2006}
2007
2008int BLI_path_slash_ensure_ex(char *path, size_t path_maxncpy, size_t path_len)
2009{
2010 BLI_string_debug_size_after_nil(path, path_maxncpy);
2011 BLI_assert(strlen(path) == path_len);
2012 BLI_assert(path_len < path_maxncpy);
2013 if (path_len == 0 || !BLI_path_slash_is_native_compat(path[path_len - 1])) {
2014 /* Avoid unlikely buffer overflow. */
2015 if (path_len + 1 < path_maxncpy) {
2016 path[path_len++] = SEP;
2017 path[path_len] = '\0';
2018 }
2019 }
2020 return path_len;
2021}
2022
2023int BLI_path_slash_ensure(char *path, size_t path_maxncpy)
2024{
2025 return BLI_path_slash_ensure_ex(path, path_maxncpy, strlen(path));
2026}
2027
2029{
2030 int len = strlen(path);
2031 while (len) {
2032 if (BLI_path_slash_is_native_compat(path[len - 1])) {
2033 path[len - 1] = '\0';
2034 len--;
2035 }
2036 else {
2037 break;
2038 }
2039 }
2040}
2041
2042const char *BLI_path_slash_skip(const char *path)
2043{
2044 /* This accounts for a null byte too. */
2045 while (BLI_path_slash_is_native_compat(*path)) {
2046 path++;
2047 }
2048 return path;
2049}
2050
2052{
2053#ifdef WIN32
2054 if (path && BLI_strnlen(path, 3) > 2) {
2056 }
2057#else
2059#endif
2060}
2061
2063{
2064 if (SEP != '/') {
2065 BLI_string_replace_char(path, SEP, '/');
2066 }
2067}
2068
2069int BLI_path_cmp_normalized(const char *p1, const char *p2)
2070{
2071 BLI_assert_msg(!BLI_path_is_rel(p1) && !BLI_path_is_rel(p2), "Paths arguments must be absolute");
2072
2073 /* Normalize the paths so we can compare them. */
2074 char norm_p1_buf[256];
2075 char norm_p2_buf[256];
2076
2077 const size_t p1_size = strlen(p1) + 1;
2078 const size_t p2_size = strlen(p2) + 1;
2079
2080 char *norm_p1 = (p1_size <= sizeof(norm_p1_buf)) ? norm_p1_buf :
2081 MEM_new_array_zeroed<char>(p1_size, __func__);
2082 char *norm_p2 = (p2_size <= sizeof(norm_p2_buf)) ? norm_p2_buf :
2083 MEM_new_array_zeroed<char>(p2_size, __func__);
2084
2085 memcpy(norm_p1, p1, p1_size);
2086 memcpy(norm_p2, p2, p2_size);
2087
2088 BLI_path_slash_native(norm_p1);
2089 BLI_path_slash_native(norm_p2);
2090
2091 /* One of the paths ending with a slash does not make them different, strip both. */
2092 BLI_path_slash_rstrip(norm_p1);
2093 BLI_path_slash_rstrip(norm_p2);
2094
2095 BLI_path_normalize(norm_p1);
2096 BLI_path_normalize(norm_p2);
2097
2098 const int result = BLI_path_cmp(norm_p1, norm_p2);
2099
2100 if (norm_p1 != norm_p1_buf) {
2101 MEM_delete(norm_p1);
2102 }
2103 if (norm_p2 != norm_p2_buf) {
2104 MEM_delete(norm_p2);
2105 }
2106 return result;
2107}
2108
2109bool BLI_path_has_hidden_component(const char *path)
2110{
2111 bool component_start = true;
2112 char cur_char = path[0];
2113 char prev_char = '\0';
2114
2115 while (cur_char != '\0') {
2116 char next_char = path[1];
2117 /* If we're at a start of path component, current is '.'
2118 * and next one is not '.', end or separator: hidden. */
2119 if (component_start && cur_char == '.') {
2120 if (!ELEM(path[1], '.', '\0', '/', '\\')) {
2121 return true;
2122 }
2123 }
2124
2125 component_start = ELEM(cur_char, '/', '\\');
2126 /* Separator, and previous was tilde: hidden. */
2127 if (component_start && prev_char == '~') {
2128 return true;
2129 }
2130
2131 path++;
2132 prev_char = cur_char;
2133 cur_char = next_char;
2134 }
2135
2136 /* Was a tilde right at end of path: hidden. */
2137 if (prev_char == '~') {
2138 return true;
2139 }
2140
2141 /* Nothing was hidden. */
2142 return false;
2143}
2144
2145} // namespace blender
File and directory operations.
#define PATH_MAX
#define FILE_MAX
#define BLI_path_join(...)
#define BLI_path_cmp
#define STRNCPY_RLEN(dst, src)
#define SNPRINTF(dst, format,...)
#define BLI_string_debug_size_after_nil(str, str_maxncpy)
#define BLI_string_debug_size(str, str_maxncpy)
Compatibility-like things for windows.
#define S_ISDIR(x)
Read Blender memory allocation and freeing.
#define UNUSED_VARS(...)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define STREQ(a, b)
#define INT_MAX
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
#define L
void free(Subdiv *subdiv)
Definition subdiv.cc:192
std::string filename(const std::string_view &str, size_t pos)
BMesh Iterators.
bool bool BLI_path_extension_strip(char *path) ATTR_NONNULL(1)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
int char char int BLI_strcasecmp(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
const char * BLI_path_parent_dir_end(const char *path, size_t path_len) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
bool void bool BLI_path_frame_check_chars(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
bool BLI_path_is_win32_drive_with_slash(const char *path)
const char * BLI_path_extension_or_end(const char *filepath) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL
__int64 int64_t
Definition BKE_text.h:90
bool BLI_path_is_win32_drive(const char *path)
static int path_normalize_impl(char *path, bool check_blend_relative_prefix)
size_t BLI_snprintf(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
char * STRNCPY(char(&dst)[N], const char *src)
void BLI_path_slash_forward_from_native(char *path) ATTR_NONNULL(1)
unsigned int uint
Definition BKE_text.h:55
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
static bool path_extension_check_ex(const char *path, const size_t path_len, const char *ext, const size_t ext_len)
static bool path_relative_to_impl(char *path, const size_t path_maxncpy, const char *basepath, const bool walk_up, const bool blend_relative)
bool BLI_path_make_safe(char *path) ATTR_NONNULL(1)
constexpr char ALTSEP
int BLI_path_normalize_dir(char *dir, size_t dir_maxncpy) ATTR_NONNULL(1)
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:404
void BLI_string_replace_char(char *str, char src, char dst) ATTR_NONNULL(1)
BLI_INLINE bool BLI_path_slash_is_native_compat(const char ch)
bool bool BLI_path_suffix(char *path, size_t path_maxncpy, const char *suffix, const char *sep) ATTR_NONNULL(1
size_t size_t BLI_path_append_dir(char *__restrict dst, size_t dst_maxncpy, const char *__restrict dir) ATTR_NONNULL(1
const char * BLI_path_extension(const char *filepath) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
unsigned short ushort
Definition BKE_text.h:56
static bool ensure_digits(char *path, const size_t path_maxncpy, int digits)
bool BLI_path_frame(char *path, size_t path_maxncpy, int frame, int digits) ATTR_NONNULL(1)
size_t BLI_path_append(char *__restrict dst, size_t dst_maxncpy, const char *__restrict file) ATTR_NONNULL(1
static bool path_name_at_index_forward(const char *__restrict path, const int index, int *__restrict r_offset, int *__restrict r_len)
char * BLI_strdupn(const char *str, size_t len) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
int BLI_file_stat_mode(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:362
bool BLI_path_frame_get(const char *path, int *r_frame, int *r_digits_len) ATTR_NONNULL(1
void BLI_path_slash_native(char *path) ATTR_NONNULL(1)
static bool path_name_at_index_backward(const char *__restrict path, const int index, int *__restrict r_offset, int *__restrict r_len)
void BLI_setenv(const char *env, const char *val) ATTR_NONNULL(1)
bool BLI_path_relative_to(const char *path, const char *base_dir, bool walk_up, char *r_path_relative, size_t r_path_relative_maxncpy) ATTR_NONNULL(1
bool BLI_path_frame_range(char *path, size_t path_maxncpy, int sta, int end, int digits) ATTR_NONNULL(1)
size_t BLI_string_replace_range(char *string, size_t string_maxncpy, int src_beg, int src_end, const char *dst)
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
bool BLI_path_extension_ensure(char *path, size_t path_maxncpy, const char *ext) ATTR_NONNULL(1
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
static bool path_frame_chars_find_range(const char *path, int *r_char_start, int *r_char_end)
void BLI_path_slash_rstrip(char *path) ATTR_NONNULL(1)
void void BLI_path_split_dir_part(const char *filepath, char *dir, size_t dir_maxncpy) ATTR_NONNULL(1
bool BLI_path_is_unc(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
static int BLI_path_unc_prefix_len(const char *path)
int BLI_path_canonicalize_native(char *path, int path_maxncpy)
int BLI_str_utf8_invalid_substitute(char *str, size_t str_len, const char substitute) ATTR_NONNULL(1)
int BLI_path_cmp_normalized(const char *p1, const char *p2) ATTR_NONNULL(1
bool BLI_path_is_abs_from_cwd(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
int BLI_access(const char *filepath, int mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
bool BLI_path_program_search(char *program_filepath, size_t program_filepath_maxncpy, const char *program_name) ATTR_NONNULL(1
char char size_t BLI_strncpy_rlen(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
bool void BLI_path_rel(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1)
bool BLI_path_abs_from_cwd(char *path, size_t path_maxncpy) ATTR_NONNULL(1)
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
int char char int int int int size_t BLI_strnlen(const char *str, size_t maxlen) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
bool BLI_path_extension_glob_validate(char *ext_fnmatch) ATTR_NONNULL(1)
int BLI_path_normalize(char *path) ATTR_NONNULL(1)
bool BLI_path_is_win32_drive_only(const char *path)
ATTR_WARN_UNUSED_RESULT const size_t num
void BLI_setenv_if_new(const char *env, const char *val) ATTR_NONNULL(1)
int BLI_path_slash_ensure(char *path, size_t path_maxncpy) ATTR_NONNULL(1)
bool BLI_path_extension_replace(char *path, size_t path_maxncpy, const char *ext) ATTR_NONNULL(1
void BLI_str_tolower_ascii(char *str, size_t len) ATTR_NONNULL(1)
void BLI_path_to_display_name(char *display_name, int display_name_maxncpy, const char *name) ATTR_NONNULL(1
bool BLI_path_filename_ensure(char *filepath, size_t filepath_maxncpy, const char *filename) ATTR_NONNULL(1
int BLI_path_slash_ensure_ex(char *path, size_t path_maxncpy, const size_t path_len) ATTR_NONNULL(1)
bool BLI_path_name_at_index(const char *__restrict path, int index, int *__restrict r_offset, int *__restrict r_len) ATTR_NONNULL(1
bool BLI_path_parent_dir(char *path) ATTR_NONNULL(1)
void void void BLI_path_split_file_part(const char *filepath, char *file, size_t file_maxncpy) ATTR_NONNULL(1
bool BLI_path_extension_check_array(const char *path, const char **ext_array) ATTR_NONNULL(1
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
bool BLI_path_extension_check(const char *path, const char *ext) ATTR_NONNULL(1
bool BLI_path_contains(const char *container_path, const char *containee_path) ATTR_NONNULL(1
int BLI_path_sequence_decode(const char *path, char *head, size_t head_maxncpy, char *tail, size_t tail_maxncpy, unsigned short *r_digits_len)
Definition path_utils.cc:61
void BLI_path_split_dir_file(const char *filepath, char *dir, size_t dir_maxncpy, char *file, size_t file_maxncpy) ATTR_NONNULL(1
void BLI_path_sequence_encode(char *path, size_t path_maxncpy, const char *head, const char *tail, unsigned short numlen, int pic)
bool BLI_path_has_hidden_component(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
int BLI_path_normalize_native(char *path) ATTR_NONNULL(1)
bool BLI_path_extension_check_glob(const char *path, const char *ext_fnmatch) ATTR_NONNULL(1
bool void BLI_path_frame_strip(char *path, char *r_ext, size_t ext_maxncpy) ATTR_NONNULL(1
size_t BLI_path_join_array(char *__restrict dst, const size_t dst_maxncpy, const char *path_array[], const int path_array_num) ATTR_NONNULL(1
bool BLI_path_make_safe_filename(char *filename) ATTR_NONNULL(1)
constexpr char SEP
const char * BLI_path_slash_find(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
bool BLI_path_parent_dir_until_exists(char *path) ATTR_NONNULL(1)
char * BLI_current_working_dir(char *dir, size_t maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:83
bool BLI_path_extension_check_n(const char *path,...) ATTR_NONNULL(1) ATTR_SENTINEL(0)
const char * BLI_path_slash_rfind(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
const char * BLI_getenv(const char *env) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
bool BLI_path_make_safe_filename_ex(char *filename, bool allow_tokens) ATTR_NONNULL(1)
const char * BLI_path_slash_skip(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
void BLI_windows_get_default_root_dir(char root_dir[4])
void path_init(const string &path, const string &user_path)
Definition path.cpp:355
#define INVALID_CHARS
#define FILENAME_FRAME_CHARS_MAX
Definition path_utils.cc:59
#define IS_PARENT_DIR(p)
#define INVALID_TOKENS
#define SEP_STR
Definition unit.cc:41
int uputenv(const char *name, const char *value)
wchar_t * alloc_utf16_from_8(const char *in8, size_t add)
Definition utfconv.cc:294
char * alloc_utf_8_from_16(const wchar_t *in16, size_t add)
Definition utfconv.cc:282
int conv_utf_16_to_8(const wchar_t *in16, char *out8, size_t size8)
Definition utfconv.cc:116