Blender V5.3
interface_string_search.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 "BKE_appdir.hh"
10
11#include "DNA_userdef_types.h"
12
13#include "UI_string_search.hh"
14
15#include "BLI_fileops.hh"
16#include "BLI_map.hh"
17#include "BLI_path_utils.hh"
18
20
21using blender::string_search::RecentCache;
22
31
38
39void add_recent_search(const StringRef chosen_str)
40{
42 storage.cache.logical_time_by_str.add_overwrite(chosen_str, storage.logical_clock);
43 storage.logical_clock++;
44}
45
47{
49 return nullptr;
50 }
52 return &storage.cache;
53}
54
55static std::optional<std::string> get_recent_searches_file_path()
56{
57 const std::optional<std::string> user_config_dir = BKE_appdir_folder_id_create(
58 BLENDER_USER_CONFIG, nullptr);
59 if (!user_config_dir.has_value()) {
60 return std::nullopt;
61 }
62 char filepath[FILE_MAX];
64 filepath, sizeof(filepath), user_config_dir->c_str(), BLENDER_RECENT_SEARCHES_FILE);
65 return std::string(filepath);
66}
67
69{
71 return;
72 }
73
74 const std::optional<std::string> path = get_recent_searches_file_path();
75 if (!path) {
76 return;
77 }
78
80 /* Avoid creating an empty file. */
81 if (storage.cache.logical_time_by_str.is_empty() && !BLI_exists((*path).c_str())) {
82 return;
83 }
84
86 for (const auto item : storage.cache.logical_time_by_str.items()) {
87 values.append({item.value, item.key});
88 }
89 std::ranges::sort(values);
90
91 fstream file(*path, std::ios::out);
92 for (const auto &item : values) {
93 file.write(item.second.data(), item.second.size());
94 file.write("\n", 1);
95 }
96}
97
99{
101 return;
102 }
103
104 const std::optional<std::string> path = get_recent_searches_file_path();
105 if (!path) {
106 return;
107 }
108
110 storage.logical_clock = 0;
111 storage.cache.logical_time_by_str.clear();
112
113 fstream file(*path);
114 std::string line;
115 while (std::getline(file, line)) {
116 storage.cache.logical_time_by_str.add_overwrite(line, storage.logical_clock);
117 storage.logical_clock++;
118 }
119}
120
121} // namespace blender::ui::string_search
#define BLENDER_RECENT_SEARCHES_FILE
File and directory operations.
#define FILE_MAX
#define BLI_path_join(...)
void append(const T &value)
#define storage(slot, qualifiers)
static RecentCacheStorage & get_recent_cache_storage()
void add_recent_search(StringRef chosen_str)
static std::optional< std::string > get_recent_searches_file_path()
const blender::string_search::RecentCache * get_recent_cache_or_null()
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
std::optional< std::string > BKE_appdir_folder_id_create(int folder_id, const char *subfolder) ATTR_WARN_UNUSED_RESULT
Definition appdir.cc:822
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:404
@ USER_FLAG_RECENT_SEARCHES_DISABLE
@ BLENDER_USER_CONFIG