14#include <condition_variable>
33constexpr toml::spec
version = toml::spec::v(1, 0, 0);
44 std::optional<std::string> datafiles_path = create_folder ?
48 if (!datafiles_path.has_value()) {
51 datafiles_path->append(
SEP_STR);
53 return std::move(*datafiles_path);
58 for (
const toml::error_info &error : errors) {
59 std::string msg = toml::format_error(error);
60 fprintf(stderr,
"%s\n", msg.c_str());
66 static toml::value value = []() -> toml::value {
68title = "Saved UI State Settings"
71["temp.window.dimensions"]
72_default = [100.0, 900.0, 200.0, 800.0]
73PREFERENCES = [100.0, 940.0, 350.0, 900.0]
74FILE_BROWSER = [100.0, 1160.0, 350.0, 950.0]
75IMAGE_EDITOR = [50.0, 1360.0, 50.0, 830.0]
76GRAPH_EDITOR = [50.0, 950.0, 200.0, 780.0]
77INFO = [100.0, 1000.0, 300.0, 880.0]
78OUTLINER = [100.0, 550.0, 350.0, 800.0]
79PROJECT = [100.0, 800.0, 500.0, 850.0]
88 toml::result default_result = toml::try_parse_str(default_str, version);
89 if (!default_result.is_ok()) {
93 return default_result.unwrap();
113 if (!path.empty() &&
BLI_exists(path.c_str())) {
117 std::string content =
data ? std::string(
data, size) : std::string();
119 toml::result file_result = toml::try_parse_str(std::move(content),
version);
120 if (file_result.is_ok()) {
175 if (current.is_empty()) {
188 std::string toml_as_string = toml::format(current,
version);
193 FILE *file_handle =
BLI_fopen(path.c_str(),
"w");
194 if (file_handle ==
nullptr) {
197 fputs(toml_as_string.c_str(), file_handle);
203 const std::string §ion_name,
204 const std::string &item_key)
206 if (!root.is_table()) {
211 const toml::table &root_table = root.as_table();
213 if (section_name.empty()) {
215 toml::table::const_iterator root_iter = root_table.find(item_key);
216 if (root_iter != root_table.end()) {
218 return &root_iter->second;
223 toml::table::const_iterator section_iter = root_table.find(section_name);
224 if (section_iter == root_table.end()) {
228 const toml::value §ion_value = section_iter->second;
229 if (!section_value.is_table()) {
235 const toml::table §ion_table = section_value.as_table();
236 toml::table::const_iterator
result = section_table.find(item_key);
237 if (
result != section_table.end()) {
244template<
typename T>
T Section::get(
const std::string &item_key)
const
250 const T default_value{};
255 return toml::get_or(*
v, default_value);
262 return toml::get_or(*
v, default_value);
266 if (!section_.empty()) {
268 return toml::get_or(*
v, default_value);
273 if (!section_.empty()) {
274 if (
const toml::value *
v =
recents_find_in(defaults, section_,
"_default")) {
275 return toml::get_or(*
v, default_value);
280 return default_value;
289 if (!value.is_array()) {
293 bool has_tables =
false;
294 for (toml::value &item : value.as_array()) {
295 if (item.is_table()) {
296 item.as_table_fmt().fmt = toml::table_format::oneline;
301 value.as_array_fmt().fmt = toml::array_format::multiline;
305template<
typename T>
void Section::set(
const std::string &item_key,
const T &value)
311 toml::value *table = ¤t;
312 if (!section_.empty()) {
313 table = ¤t[section_];
315 if (!table->is_table()) {
316 *table = toml::table{};
321 else if (table->as_table_fmt().fmt == toml::table_format::implicit) {
322 table->as_table_fmt().fmt = toml::table_format::multiline;
326 toml::value &item = (*table)[item_key];
336 if (!current.is_table()) {
339 toml::table &root_table = current.as_table();
340 if (section_.empty()) {
341 root_table.erase(item_key);
344 toml::table::iterator section_iter = root_table.find(section_);
345 if (section_iter == root_table.end()) {
348 toml::value §ion_value = section_iter->second;
349 if (!section_value.is_table()) {
352 toml::table §ion_table = section_value.as_table();
353 section_table.erase(item_key);
361 if (section_.empty() || !current.is_table()) {
364 toml::table &root_table = current.as_table();
365 root_table.erase(section_);
402 const std::vector<int> &value);
406 const std::vector<double> &value);
410 const std::vector<float> &value);
412template std::vector<std::map<std::string, std::string>>
Section::get<
413 std::vector<std::map<std::string, std::string>>>(
const std::string &item_key)
const;
415 const std::string &item_key,
const std::vector<std::map<std::string, std::string>> &value);
#define BLENDER_RECENTS_FILE
File and directory operations.
Read Blender memory allocation and freeing.
T get(const std::string &item_key) const
void set(const std::string &item_key, const T &value)
void remove(const std::string &item_key)
int size(const AssetLibraryReference *library_reference)
static std::string recents_file_path(const bool create_folder=false)
constexpr toml::spec version
static Mutex recents_init_mutex
static void recents_init_impl()
static std::once_flag recents_init_once
static std::condition_variable_any recents_init_cv
static const toml::value & defaults_get()
static const toml::value * recents_find_in(const toml::value &root, const std::string §ion_name, const std::string &item_key)
static toml::value & current_get()
static void recents_print_errors(const std::vector< toml::error_info > &errors)
static Mutex recents_mutex
static std::atomic< bool > recents_ready
static void recents_format_inline_tables(toml::value &value)
unsigned __int64 uint64_t
std::optional< std::string > BKE_appdir_folder_id_create(int folder_id, const char *subfolder) ATTR_WARN_UNUSED_RESULT
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
ATTR_WARN_UNUSED_RESULT const BMVert * v
char * BLI_file_read_text_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size)
std::optional< std::string > BKE_appdir_folder_id(int folder_id, const char *subfolder) ATTR_WARN_UNUSED_RESULT
BMesh const char void * data