Blender V4.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
5#include <sstream>
6
7#include "BKE_appdir.hh"
8
9#include "DNA_userdef_types.h"
10
11#include "UI_string_search.hh"
12
13#include "BLI_fileops.h"
14#include "BLI_fileops.hh"
15#include "BLI_map.hh"
16#include "BLI_path_utils.hh"
17
19
21
30
32{
34 static RecentCacheStorage storage;
35 return storage;
36}
37
38void add_recent_search(const StringRef chosen_str)
39{
41 storage.cache.logical_time_by_str.add_overwrite(chosen_str, storage.logical_clock);
42 storage.logical_clock++;
43}
44
46{
48 return nullptr;
49 }
51 return &storage.cache;
52}
53
54static std::optional<std::string> get_recent_searches_file_path()
55{
56 const std::optional<std::string> user_config_dir = BKE_appdir_folder_id_create(
57 BLENDER_USER_CONFIG, nullptr);
58 if (!user_config_dir.has_value()) {
59 return std::nullopt;
60 }
61 char filepath[FILE_MAX];
63 filepath, sizeof(filepath), user_config_dir->c_str(), BLENDER_RECENT_SEARCHES_FILE);
64 return std::string(filepath);
65}
66
68{
70 return;
71 }
72
73 const std::optional<std::string> path = get_recent_searches_file_path();
74 if (!path) {
75 return;
76 }
77
79 /* Avoid creating an empty file. */
80 if (storage.cache.logical_time_by_str.is_empty() && !BLI_exists((*path).c_str())) {
81 return;
82 }
83
85 for (const auto item : storage.cache.logical_time_by_str.items()) {
86 values.append({item.value, item.key});
87 }
88 std::sort(values.begin(), values.end());
89
90 fstream file(*path, std::ios::out);
91 for (const auto &item : values) {
92 file.write(item.second.data(), item.second.size());
93 file.write("\n", 1);
94 }
95}
96
98{
100 return;
101 }
102
103 const std::optional<std::string> path = get_recent_searches_file_path();
104 if (!path) {
105 return;
106 }
107
109 storage.logical_clock = 0;
111
112 fstream file(*path);
113 std::string line;
114 while (std::getline(file, line)) {
116 storage.logical_clock++;
117 }
118}
119
120} // 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:764
#define BLI_assert(a)
Definition BLI_assert.h:50
File and directory operations.
int BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:350
File and directory operations.
#define FILE_MAX
#define BLI_path_join(...)
@ USER_FLAG_RECENT_SEARCHES_DISABLE
unsigned int U
Definition btGjkEpa3.h:78
void clear()
Definition BLI_map.hh:989
bool add_overwrite(const Key &key, const Value &value)
Definition BLI_map.hh:301
ItemIterator items() const
Definition BLI_map.hh:864
bool is_empty() const
Definition BLI_map.hh:937
void append(const T &value)
FILE * file
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