Convert all slashes to native format when initializing an asset library. This might convert slashes that are valid parts of the file name, but this just leads to an error about a not found asset library, which is better than crashing. This is a typical tradeoff when dealing with cross platform paths.
53 lines
1.3 KiB
C++
53 lines
1.3 KiB
C++
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
|
*
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
/** \file
|
|
* \ingroup asset_system
|
|
*/
|
|
|
|
#include "BLI_fileops.h"
|
|
#include "BLI_path_utils.hh"
|
|
#include "BLI_string.h"
|
|
|
|
#include "utils.hh"
|
|
|
|
namespace blender::asset_system::utils {
|
|
|
|
std::string normalize_directory_path(StringRef directory)
|
|
{
|
|
if (directory.is_empty()) {
|
|
return "";
|
|
}
|
|
|
|
char dir_normalized[PATH_MAX];
|
|
BLI_strncpy(dir_normalized,
|
|
directory.data(),
|
|
/* + 1 for null terminator. */
|
|
std::min(directory.size() + 1, int64_t(sizeof(dir_normalized))));
|
|
BLI_path_slash_native(dir_normalized);
|
|
BLI_path_normalize_dir(dir_normalized, sizeof(dir_normalized));
|
|
return std::string(dir_normalized);
|
|
}
|
|
|
|
std::string normalize_path(StringRefNull path, int64_t max_len)
|
|
{
|
|
const int64_t len = (max_len == StringRef::not_found) ? path.size() :
|
|
std::min(max_len, path.size());
|
|
|
|
char *buf = BLI_strdupn(path.c_str(), len);
|
|
BLI_path_slash_native(buf);
|
|
BLI_path_normalize(buf);
|
|
|
|
std::string normalized_path = buf;
|
|
MEM_freeN(buf);
|
|
|
|
if (len != path.size()) {
|
|
normalized_path = normalized_path + path.substr(len);
|
|
}
|
|
|
|
return normalized_path;
|
|
}
|
|
|
|
} // namespace blender::asset_system::utils
|