Blender V5.3
file_handler.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#include <cstring>
10
11#include "BKE_file_handler.hh"
12
13#include "BLI_path_utils.hh"
14#include "BLI_string.hh"
15
16#include "BLT_translation.hh"
17
18namespace blender::bke {
19
25
30
32{
33 const auto *itr = std::find_if(file_handlers().begin(),
34 file_handlers().end(),
35 [idname](const std::unique_ptr<FileHandlerType> &file_handler) {
36 return idname == file_handler->idname;
37 });
38 if (itr != file_handlers().end()) {
39 return itr->get();
40 }
41 return nullptr;
42}
43
44void file_handler_add(std::unique_ptr<FileHandlerType> file_handler)
45{
46 BLI_assert(file_handler_find(file_handler->idname) == nullptr);
47
49 const char char_separator = ';';
50 const char *char_begin = file_handler->file_extensions_str;
51 const char *char_end = BLI_strchr_or_end(char_begin, char_separator);
52 while (char_begin[0]) {
53 if (char_end - char_begin > 1) {
54 std::string file_extension(char_begin, char_end - char_begin);
55 file_handler->file_extensions.append(file_extension);
56 }
57 char_begin = char_end[0] ? char_end + 1 : char_end;
58 char_end = BLI_strchr_or_end(char_begin, char_separator);
59 }
60
61 file_handlers_vector().append(std::move(file_handler));
62}
63
65{
66 file_handlers_vector().remove_if(
67 [file_handler](const std::unique_ptr<FileHandlerType> &test_file_handler) {
68 return test_file_handler.get() == file_handler;
69 });
70}
71
73 const Span<std::string> paths)
74{
75 Vector<std::string> path_extensions;
76 for (const std::string &path : paths) {
77 const char *extension = BLI_path_extension(path.c_str());
78 if (!extension) {
79 continue;
80 }
81 path_extensions.append_non_duplicates(extension);
82 }
83
85 for (const std::unique_ptr<FileHandlerType> &file_handler_ptr : file_handlers()) {
86 FileHandlerType &file_handler = *file_handler_ptr;
87 const auto &file_extensions = file_handler.file_extensions;
88 bool support_any_extension = false;
89 for (const std::string &extension : path_extensions) {
90 auto test_fn = [&extension](const std::string &test_extension) {
91 return BLI_strcaseeq(extension.c_str(), test_extension.c_str()) == 1;
92 };
93 support_any_extension = std::any_of(file_extensions.begin(), file_extensions.end(), test_fn);
94 if (support_any_extension) {
95 break;
96 }
97 }
98 if (!support_any_extension) {
99 continue;
100 }
101
102 if (!(file_handler.poll_drop && file_handler.poll_drop(C, &file_handler))) {
103 continue;
104 }
105
106 result.append(&file_handler);
107 }
108 return result;
109}
110
112{
113 Vector<int64_t> indices;
114
115 for (const int idx : paths.index_range()) {
116 const char *extension = BLI_path_extension(paths[idx].c_str());
117 if (!extension) {
118 continue;
119 }
120 auto test_fn = [extension](const std::string &test_extension) {
121 return BLI_strcaseeq(extension, test_extension.c_str()) == 1;
122 };
123
124 if (std::any_of(file_extensions.begin(), file_extensions.end(), test_fn)) {
125 indices.append(idx);
126 }
127 }
128 return indices;
129}
130
132{
133 /* Spaces are supported but do not allow all characters to be blank. */
134 const bool all_blank = name.is_empty() ||
135 std::all_of(name.begin(), name.end(), [](char c) { return c == ' '; });
136
138 STRNCPY(filename, all_blank ? DATA_("Untitled") : name.c_str());
140 sizeof(filename),
141 file_extensions.is_empty() ? "" : file_extensions.first().c_str());
143
144 return filename;
145}
146
147static std::string extensions_common_prefix(const Span<std::string> extensions)
148{
149 std::string prefix = extensions.first();
150 for (const std::string &extension : extensions.drop_front(1)) {
151 const int64_t max_len = std::min<int64_t>(prefix.size(), extension.size());
152 int64_t i = 0;
153 while (i < max_len && prefix[i] == extension[i]) {
154 i++;
155 }
156 prefix.resize(i);
157 }
158 return prefix;
159}
160
162{
163 const std::string prefix = extensions_common_prefix(extensions);
164 const int64_t prefix_letters = prefix.size() - (!prefix.empty() && prefix[0] == '.' ? 1 : 0);
165 return prefix_letters >= 3 && std::ranges::all_of(extensions, [&](const auto &extension) {
166 return extension.size() <= prefix.size() + 1;
167 });
168}
169
171{
172 if (file_extensions.is_empty()) {
173 return label;
174 }
175
176 std::string extensions;
177 const Span<std::string> all_extensions = file_extensions.as_span();
178 int64_t group_start = 0;
179 while (group_start < all_extensions.size()) {
180 int64_t group_size = 1;
181 while (group_start + group_size < all_extensions.size() &&
182 extensions_group_is_collapsible(all_extensions.slice(group_start, group_size + 1)))
183 {
184 group_size++;
185 }
186
187 const Span<std::string> group = all_extensions.slice(group_start, group_size);
188 if (!extensions.empty()) {
189 extensions += "/";
190 }
191 if (group.size() > 1) {
192 extensions += extensions_common_prefix(group) + "*";
193 }
194 else {
195 extensions += group.first();
196 }
197
198 group_start += group_size;
199 }
200
201 return std::string(CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, label)) + " (" + extensions +
202 ")";
203}
204
205std::optional<std::string> FileHandlerType::filter_glob_from_extensions() const
206{
207 if (file_extensions.is_empty()) {
208 return std::nullopt;
209 }
210
211 std::string filter_glob;
212 bool is_first = true;
213 for (StringRef extension : file_extensions) {
214 filter_glob += StringRef((is_first ? "" : ";")) +
215 ((!extension.is_empty() && extension[0] == '.') ? "*" : "*.") + extension;
216 is_first = false;
217 }
218 return filter_glob;
219}
220
221} // namespace blender::bke
#define FILE_MAXFILE
#define CTX_IFACE_(context, msgid)
#define BLT_I18NCONTEXT_OPERATOR_DEFAULT
#define DATA_(msgid)
#define C
Definition RandGen.cpp:29
constexpr Span drop_front(int64_t n) const
Definition BLI_span.hh:175
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr const T & first() const
Definition BLI_span.hh:319
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr const char * c_str() const
constexpr StringRef()
void append(const T &value)
void append_non_duplicates(const T &value)
constexpr const char * end() const
constexpr const char * begin() const
constexpr bool is_empty() const
Vector< FileHandlerType * > file_handlers_poll_file_drop(const bContext *C, const Span< std::string > paths)
FileHandlerType * file_handler_find(StringRef idname)
void file_handler_add(std::unique_ptr< FileHandlerType > file_handler)
static std::string extensions_common_prefix(const Span< std::string > extensions)
static Vector< std::unique_ptr< FileHandlerType > > & file_handlers_vector()
void file_handler_remove(FileHandlerType *file_handler)
static bool extensions_group_is_collapsible(const Span< std::string > extensions)
Span< std::unique_ptr< FileHandlerType > > file_handlers()
std::string filename(const std::string_view &str, size_t pos)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
char * STRNCPY(char(&dst)[N], const char *src)
const char * BLI_path_extension(const char *filepath) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
bool BLI_path_extension_ensure(char *path, size_t path_maxncpy, const char *ext) ATTR_NONNULL(1
iter begin(iter)
bool BLI_path_make_safe_filename(char *filename) ATTR_NONNULL(1)
int BLI_strcaseeq(const char *a, const char *b) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
char char size_t char const char * BLI_strchr_or_end(const char *str, char ch) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(1)
std::string get_default_filename(StringRefNull name)
Vector< std::string > file_extensions
Vector< int64_t > filter_supported_paths(const Span< std::string > paths) const
std::string label_with_extensions() const
std::optional< std::string > filter_glob_from_extensions() const
bool(*) poll_drop(const bContext *C, FileHandlerType *file_handle_type)