Blender V5.0
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
5#include "BKE_appdir.hh"
6
7#include "DNA_userdef_types.h"
8
9#include "UI_string_search.hh"
10
11#include "BLI_fileops.h"
12#include "BLI_fileops.hh"
13#include "BLI_map.hh"
14#include "BLI_path_utils.hh"
15
17
18using blender::string_search::RecentCache;
19
28
30{
32 static RecentCacheStorage storage;
33 return storage;
34}
35
36void add_recent_search(const StringRef chosen_str)
37{
39 storage.cache.logical_time_by_str.add_overwrite(chosen_str, storage.logical_clock);
40 storage.logical_clock++;
41}
42
44{
46 return nullptr;
47 }
49 return &storage.cache;
50}
51
52static std::optional<std::string> get_recent_searches_file_path()
53{
54 const std::optional<std::string> user_config_dir = BKE_appdir_folder_id_create(
55 BLENDER_USER_CONFIG, nullptr);
56 if (!user_config_dir.has_value()) {
57 return std::nullopt;
58 }
59 char filepath[FILE_MAX];
61 filepath, sizeof(filepath), user_config_dir->c_str(), BLENDER_RECENT_SEARCHES_FILE);
62 return std::string(filepath);
63}
64
66{
68 return;
69 }
70
71 const std::optional<std::string> path = get_recent_searches_file_path();
72 if (!path) {
73 return;
74 }
75
77 /* Avoid creating an empty file. */
78 if (storage.cache.logical_time_by_str.is_empty() && !BLI_exists((*path).c_str())) {
79 return;
80 }
81
83 for (const auto item : storage.cache.logical_time_by_str.items()) {
84 values.append({item.value, item.key});
85 }
86 std::sort(values.begin(), values.end());
87
88 fstream file(*path, std::ios::out);
89 for (const auto &item : values) {
90 file.write(item.second.data(), item.second.size());
91 file.write("\n", 1);
92 }
93}
94
96{
98 return;
99 }
100
101 const std::optional<std::string> path = get_recent_searches_file_path();
102 if (!path) {
103 return;
104 }
105
107 storage.logical_clock = 0;
109
110 fstream file(*path);
111 std::string line;
112 while (std::getline(file, line)) {
114 storage.logical_clock++;
115 }
116}
117
118} // namespace blender::ui::string_search
@ BLENDER_USER_CONFIG
#define BLENDER_RECENT_SEARCHES_FILE
std::optional< std::string > BKE_appdir_folder_id_create(int folder_id, const char *subfolder) ATTR_WARN_UNUSED_RESULT
Definition appdir.cc:781
#define BLI_assert(a)
Definition BLI_assert.h:46
File and directory operations.
int BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:360
File and directory operations.
#define FILE_MAX
#define BLI_path_join(...)
@ USER_FLAG_RECENT_SEARCHES_DISABLE
#define U
void clear()
Definition BLI_map.hh:1038
bool add_overwrite(const Key &key, const Value &value)
Definition BLI_map.hh:325
bool is_empty() const
Definition BLI_map.hh:986
ItemIterator items() const &
Definition BLI_map.hh:902
void append(const T &value)
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()
Map< std::string, int > logical_time_by_str