Blender V4.3
path_util.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4#include "IO_path_util.hh"
5
6#include "BLI_fileops.h"
7#include "BLI_path_utils.hh"
8#include "BLI_string.h"
9
10namespace blender::io {
11
12std::string path_reference(StringRefNull filepath,
13 StringRefNull base_src,
14 StringRefNull base_dst,
16 Set<std::pair<std::string, std::string>> *copy_set)
17{
18 const bool is_relative = BLI_path_is_rel(filepath.c_str());
19 char filepath_abs[PATH_MAX];
20 STRNCPY(filepath_abs, filepath.c_str());
21 BLI_path_abs(filepath_abs, base_src.c_str());
22 BLI_path_normalize(filepath_abs);
23
24 /* Figure out final mode to be used. */
25 if (mode == PATH_REFERENCE_MATCH) {
27 }
28 else if (mode == PATH_REFERENCE_AUTO) {
29 mode = BLI_path_contains(base_dst.c_str(), filepath_abs) ? PATH_REFERENCE_RELATIVE :
31 }
32 else if (mode == PATH_REFERENCE_COPY) {
33 char filepath_cpy[PATH_MAX];
34 BLI_path_join(filepath_cpy, PATH_MAX, base_dst.c_str(), BLI_path_basename(filepath_abs));
35 copy_set->add(std::make_pair(filepath_abs, filepath_cpy));
36 STRNCPY(filepath_abs, filepath_cpy);
38 }
39
40 /* Now we know the final path mode. */
41 if (mode == PATH_REFERENCE_ABSOLUTE) {
42 return filepath_abs;
43 }
44 if (mode == PATH_REFERENCE_RELATIVE) {
45 char rel_path[PATH_MAX];
46 STRNCPY(rel_path, filepath_abs);
47 BLI_path_rel(rel_path, base_dst.c_str());
48 /* Can't always find relative path (e.g. between different drives). */
49 if (!BLI_path_is_rel(rel_path)) {
50 return filepath_abs;
51 }
52 return rel_path + 2; /* Skip blender's internal "//" prefix. */
53 }
54 if (mode == PATH_REFERENCE_STRIP) {
55 return BLI_path_basename(filepath_abs);
56 }
57 BLI_assert_msg(false, "Invalid path reference mode");
58 return filepath_abs;
59}
60
61void path_reference_copy(const Set<std::pair<std::string, std::string>> &copy_set)
62{
63 for (const auto &copy : copy_set) {
64 const char *src = copy.first.c_str();
65 const char *dst = copy.second.c_str();
66 if (!BLI_exists(src)) {
67 fprintf(stderr, "Missing source file '%s', not copying\n", src);
68 continue;
69 }
70 if (0 == BLI_path_cmp_normalized(src, dst)) {
71 continue; /* Source and destination are the same. */
72 }
74 fprintf(stderr, "Can't make directory for '%s', not copying\n", dst);
75 continue;
76 }
77 if (BLI_copy(src, dst) != 0) {
78 fprintf(stderr, "Can't copy '%s' to '%s'\n", src, dst);
79 continue;
80 }
81 }
82}
83
84} // namespace blender::io
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:57
File and directory operations.
int BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:350
int BLI_copy(const char *path_src, const char *path_dst) ATTR_NONNULL()
#define PATH_MAX
Definition BLI_fileops.h:30
bool BLI_file_ensure_parent_dir_exists(const char *filepath) ATTR_NONNULL(1)
Definition fileops_c.cc:429
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
int BLI_path_normalize(char *path) ATTR_NONNULL(1)
bool BLI_path_contains(const char *container_path, const char *containee_path) ATTR_NONNULL(1
#define BLI_path_join(...)
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
int BLI_path_cmp_normalized(const char *p1, const char *p2) ATTR_NONNULL(1
bool void BLI_path_rel(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1)
#define STRNCPY(dst, src)
Definition BLI_string.h:593
ePathReferenceMode
@ PATH_REFERENCE_AUTO
@ PATH_REFERENCE_RELATIVE
@ PATH_REFERENCE_COPY
@ PATH_REFERENCE_MATCH
@ PATH_REFERENCE_ABSOLUTE
@ PATH_REFERENCE_STRIP
constexpr const char * c_str() const
std::string path_reference(StringRefNull filepath, StringRefNull base_src, StringRefNull base_dst, ePathReferenceMode mode, Set< std::pair< std::string, std::string > > *copy_set)
Definition path_util.cc:12
void path_reference_copy(const Set< std::pair< std::string, std::string > > &copy_set)
Definition path_util.cc:61
static void copy(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node)