Blender V5.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.hh"
7#include "BLI_path_utils.hh"
8#include "BLI_string.hh"
9
10#include "CLG_log.h"
11
12namespace blender {
13
14static CLG_LogRef LOG = {"io.common"};
15
16namespace io {
17
18std::string path_reference(StringRefNull filepath,
19 StringRefNull base_src,
20 StringRefNull base_dst,
22 Set<std::pair<std::string, std::string>> *copy_set)
23{
24 const bool is_relative = BLI_path_is_rel(filepath.c_str());
25 char filepath_abs[FILE_MAX];
26 STRNCPY(filepath_abs, filepath.c_str());
27 BLI_path_abs(filepath_abs, base_src.c_str());
28 BLI_path_normalize(filepath_abs);
29
30 /* Figure out final mode to be used. */
31 if (mode == PATH_REFERENCE_MATCH) {
33 }
34 else if (mode == PATH_REFERENCE_AUTO) {
35 mode = BLI_path_contains(base_dst.c_str(), filepath_abs) ? PATH_REFERENCE_RELATIVE :
37 }
38 else if (mode == PATH_REFERENCE_COPY) {
39 char filepath_cpy[FILE_MAX];
40 BLI_path_join(filepath_cpy, FILE_MAX, base_dst.c_str(), BLI_path_basename(filepath_abs));
41 copy_set->add(std::make_pair(filepath_abs, filepath_cpy));
42 STRNCPY(filepath_abs, filepath_cpy);
44 }
45
46 /* Now we know the final path mode. */
47 if (mode == PATH_REFERENCE_ABSOLUTE) {
48 return filepath_abs;
49 }
50 if (mode == PATH_REFERENCE_RELATIVE) {
51 const bool walk_up = true;
52 char rel_path[FILE_MAX];
53 /* Can't always find relative path (e.g. between different drives). */
54 if (!BLI_path_relative_to(filepath_abs, base_dst.c_str(), walk_up, rel_path, sizeof(rel_path)))
55 {
56 return filepath_abs;
57 }
58 return rel_path;
59 }
60 if (mode == PATH_REFERENCE_STRIP) {
61 return BLI_path_basename(filepath_abs);
62 }
63 BLI_assert_msg(false, "Invalid path reference mode");
64 return filepath_abs;
65}
66
67void path_reference_copy(const Set<std::pair<std::string, std::string>> &copy_set)
68{
69 for (const auto &copy : copy_set) {
70 const char *src = copy.first.c_str();
71 const char *dst = copy.second.c_str();
72 if (!BLI_exists(src)) {
73 CLOG_WARN(&LOG, "Missing source file '%s', not copying", src);
74 continue;
75 }
76 if (0 == BLI_path_cmp_normalized(src, dst)) {
77 continue; /* Source and destination are the same. */
78 }
80 CLOG_WARN(&LOG, "Can't make directory for '%s', not copying", dst);
81 continue;
82 }
83 if (BLI_copy(src, dst) != 0) {
84 CLOG_WARN(&LOG, "Can't copy '%s' to '%s'", src, dst);
85 continue;
86 }
87 }
88}
89
90} // namespace io
91} // namespace blender
File and directory operations.
#define FILE_MAX
#define BLI_path_join(...)
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
#define BLI_assert_msg(a, msg)
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:18
void path_reference_copy(const Set< std::pair< std::string, std::string > > &copy_set)
Definition path_util.cc:67
BMesh Iterators.
char * STRNCPY(char(&dst)[N], const char *src)
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:404
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
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
int BLI_path_cmp_normalized(const char *p1, const char *p2) ATTR_NONNULL(1
int BLI_path_normalize(char *path) ATTR_NONNULL(1)
int BLI_copy(const char *path_src, const char *path_dst) ATTR_NONNULL()
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
bool BLI_path_contains(const char *container_path, const char *containee_path) ATTR_NONNULL(1
static CLG_LogRef LOG
Definition logging.cpp:12
bool BLI_file_ensure_parent_dir_exists(const char *filepath) ATTR_NONNULL(1)
Definition fileops_c.cc:454