Blender V5.3
asset_system/intern/utils.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
8
9/* For PATH_MAX (at least on Windows). */
10#include "BLI_fileops.hh" // IWYU pragma: keep
11#include "BLI_path_utils.hh"
12#include "BLI_string.hh"
13
14#include "BKE_global.hh"
15#include "BKE_path_templates.hh"
16
17#include "utils.hh"
18
20
22
23std::string resolve_directory_path(StringRef directory)
24{
25 if (directory.is_empty()) {
26 return "";
27 }
28 char dir_resolved[PATH_MAX];
29 BLI_strncpy(dir_resolved,
30 directory.data(),
31 /* + 1 for null terminator. */
32 std::min(directory.size() + 1, int64_t(sizeof(dir_resolved))));
33
34 path_templates::VariableMap template_variables;
36 BKE_add_template_variables_general(template_variables, nullptr, project);
38 dir_resolved, sizeof(dir_resolved), template_variables);
39 if (!variable_errors.is_empty()) {
40 /* Do nothing on errors as the "dir_normalized" variable should still contain the original
41 * string. The reset of the code should still gracefully treat this as an invalid path.
42 * However it might be nice to bubble up the exact errors in the future.
43 */
44 }
45 BLI_path_slash_native(dir_resolved);
46 BLI_path_normalize_dir(dir_resolved, sizeof(dir_resolved));
47 return std::string(dir_resolved);
48}
49
50std::string resolve_path(StringRefNull path, int64_t max_len)
51{
52 const int64_t len = (max_len == StringRef::not_found) ? path.size() :
53 std::min(max_len, path.size());
54
55 char buf[FILE_MAX];
56
57 /* A longer path does not fit the buffer. Leave it untouched, rather than splicing a
58 * truncated result back together with the tail below. */
59 if (len >= int64_t(sizeof(buf))) {
60 return path;
61 }
62
63 BLI_strncpy(buf, path.c_str(), len + 1);
64
65 path_templates::VariableMap template_variables;
67 BKE_add_template_variables_general(template_variables, nullptr, project);
69 buf, sizeof(buf), template_variables);
70 if (!variable_errors.is_empty()) {
71 /* Do nothing on errors as the "dir_normalized" variable should still contain the original
72 * string. The reset of the code should still gracefully treat this as an invalid path.
73 * However it might be nice to bubble up the exact errors in the future.
74 */
75 }
76
79
80 std::string resolved_path = buf;
81
82 if (len != path.size()) {
83 resolved_path = resolved_path + path.substr(len);
84 }
85
86 return resolved_path;
87}
88
89} // namespace blender::asset_system::utils
#define G_MAIN
Functions and classes for evaluating template expressions in filepaths.
File and directory operations.
#define PATH_MAX
#define FILE_MAX
constexpr const char * c_str() const
bool is_empty() const
static constexpr int64_t not_found
constexpr bool is_empty() const
constexpr int64_t size() const
constexpr StringRef substr(int64_t start, int64_t size) const
constexpr const char * data() const
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
std::string resolve_directory_path(StringRef directory)
std::string resolve_path(StringRefNull path, int64_t max_len)
__int64 int64_t
Definition BKE_text.h:90
void BKE_add_template_variables_general(bke::path_templates::VariableMap &variables, const ID *path_owner_id, const bke::BlenderProject *project)
int BLI_path_normalize_dir(char *dir, size_t dir_maxncpy) ATTR_NONNULL(1)
bke::BlenderProject * BKE_blender_project_get(const Main *bmain)
void BLI_path_slash_native(char *path) ATTR_NONNULL(1)
Vector< bke::path_templates::Error > BKE_path_apply_template(char *path, int path_maxncpy, const bke::path_templates::VariableMap &template_variables)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
int BLI_path_normalize(char *path) ATTR_NONNULL(1)