Blender V4.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
9#include "BLI_fileops.h"
10#include "BLI_path_utils.hh"
11#include "BLI_string.h"
12
13#include "utils.hh"
14
16
17std::string normalize_directory_path(StringRef directory)
18{
19 if (directory.is_empty()) {
20 return "";
21 }
22
23 char dir_normalized[PATH_MAX];
24 BLI_strncpy(dir_normalized,
25 directory.data(),
26 /* + 1 for null terminator. */
27 std::min(directory.size() + 1, int64_t(sizeof(dir_normalized))));
28 BLI_path_normalize_dir(dir_normalized, sizeof(dir_normalized));
29 return std::string(dir_normalized);
30}
31
32std::string normalize_path(StringRefNull path, int64_t max_len)
33{
34 const int64_t len = (max_len == StringRef::not_found) ? path.size() :
35 std::min(max_len, path.size());
36
37 char *buf = BLI_strdupn(path.c_str(), len);
40
41 std::string normalized_path = buf;
42 MEM_freeN(buf);
43
44 if (len != path.size()) {
45 normalized_path = normalized_path + path.substr(len);
46 }
47
48 return normalized_path;
49}
50
51} // namespace blender::asset_system::utils
File and directory operations.
#define PATH_MAX
Definition BLI_fileops.h:30
int BLI_path_normalize_dir(char *dir, size_t dir_maxncpy) ATTR_NONNULL(1)
void BLI_path_slash_native(char *path) ATTR_NONNULL(1)
int BLI_path_normalize(char *path) ATTR_NONNULL(1)
char * BLI_strdupn(const char *str, size_t len) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition string.c:29
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
static constexpr int64_t not_found
constexpr bool is_empty() const
constexpr StringRef substr(int64_t start, int64_t size) const
constexpr int64_t size() const
constexpr const char * data() const
constexpr const char * c_str() const
int len
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
std::string normalize_path(StringRefNull path, int64_t max_len)
std::string normalize_directory_path(StringRef directory)
__int64 int64_t
Definition stdint.h:89