26#include "RNA_prototypes.hh"
37 if (this->strings_.contains(name)) {
40 if (this->filepaths_.contains(name)) {
43 if (this->integers_.contains(name)) {
46 if (this->floats_.contains(name)) {
54 if (this->strings_.remove(name)) {
57 if (this->filepaths_.remove(name)) {
60 if (this->integers_.remove(name)) {
63 if (this->floats_.remove(name)) {
74 this->strings_.add_new(name, value);
83 this->filepaths_.add_new(name, value);
92 this->integers_.add_new(name, value);
101 this->floats_.add_new(name, value);
107 const std::string *value = this->strings_.lookup_ptr(name);
108 if (value ==
nullptr) {
116 const std::string *value = this->filepaths_.lookup_ptr(name);
117 if (value ==
nullptr) {
125 const int64_t *value = this->integers_.lookup_ptr(name);
126 if (value ==
nullptr) {
134 const double *value = this->floats_.lookup_ptr(name);
135 if (value ==
nullptr) {
148 if (file_name[0] ==
'\0') {
152 if (file_name_end == file_name) {
179 if (!success || dir_path[0] ==
'\0') {
233 if (ptr ==
nullptr || prop ==
nullptr ||
286 const bNode *bnode =
reinterpret_cast<const bNode *
>(node_rna_ptr->data);
300 for (
const std::unique_ptr<bke::ProjectVariable> &var :
project.variables) {
301 switch (var->type_get()) {
304 variables.
add_filepath(var->name_get(), var->value_string_get());
307 variables.
add_string(var->name_get(), var->value_string_get());
313 variables.
add_integer(var->name_get(), var->value_int_get());
317 variables.
add_float(var->name_get(), var->value_float_get());
324 const ID *path_owner_id,
349 "blend_name_lib", lib_blend_file_path,
StringRef(
DATA_(
"Unsaved")));
387 const bNode &owning_node)
395#define FORMAT_BUFFER_SIZE FILE_MAX
399enum class FormatSpecifierType {
418struct FormatSpecifier {
419 FormatSpecifierType type = FormatSpecifierType::NONE;
423 std::optional<uint8_t> integer_digit_count;
424 std::optional<uint8_t> fractional_digit_count;
438 VARIABLE_SYNTAX_ERROR,
441 UNESCAPED_CURLY_BRACE_ERROR,
448 TokenType type = TokenType::VARIABLE_EXPRESSION;
452 IndexRange byte_range;
458 StringRef variable_name;
464 FormatSpecifier format;
481 BLI_assert(format.type != FormatSpecifierType::SYNTAX_ERROR);
483 r_output_string[0] =
'\0';
484 int output_length = 0;
486 switch (format.type) {
487 case FormatSpecifierType::NONE: {
490 r_output_string[output_length] =
'\0';
494 case FormatSpecifierType::INTEGER: {
495 BLI_assert(format.integer_digit_count.has_value());
497 output_length = fmt::format_to_n(r_output_string,
501 *format.integer_digit_count)
503 r_output_string[output_length] =
'\0';
507 case FormatSpecifierType::FLOAT: {
512 BLI_assert(format.fractional_digit_count.has_value());
513 BLI_assert(*format.fractional_digit_count > 0);
515 if (format.integer_digit_count.has_value()) {
517 output_length = fmt::format_to_n(r_output_string,
521 *format.integer_digit_count)
529 r_output_string[output_length] =
'.';
532 for (
int i = 0; i < *format.fractional_digit_count && i < (
FORMAT_BUFFER_SIZE - 1); i++) {
533 r_output_string[output_length] =
'0';
537 r_output_string[output_length] =
'\0';
542 case FormatSpecifierType::SYNTAX_ERROR: {
545 "Format specifiers with invalid syntax should have been rejected before getting here.");
550 return output_length;
562 const double float_value,
565 BLI_assert(format.type != FormatSpecifierType::SYNTAX_ERROR);
567 r_output_string[0] =
'\0';
568 int output_length = 0;
570 switch (format.type) {
571 case FormatSpecifierType::NONE: {
578 r_output_string[output_length] =
'\0';
582 if (
StringRef(r_output_string).find_first_not_of(
"-0123456789") == std::string::npos) {
583 r_output_string[output_length] =
'.';
584 r_output_string[output_length + 1] =
'0';
585 r_output_string[output_length + 2] =
'\0';
591 case FormatSpecifierType::INTEGER: {
596 case FormatSpecifierType::FLOAT: {
597 BLI_assert(format.fractional_digit_count.has_value());
598 BLI_assert(*format.fractional_digit_count > 0);
600 if (format.integer_digit_count.has_value()) {
603 output_length = fmt::format_to_n(r_output_string,
607 *format.integer_digit_count +
608 *format.fractional_digit_count + 1,
609 *format.fractional_digit_count)
611 r_output_string[output_length] =
'\0';
615 output_length = fmt::format_to_n(r_output_string,
619 *format.fractional_digit_count)
621 r_output_string[output_length] =
'\0';
627 case FormatSpecifierType::SYNTAX_ERROR: {
630 "Format specifiers with invalid syntax should have been rejected before getting here.");
635 return output_length;
647 FormatSpecifier format = {};
651 format.type = FormatSpecifierType::SYNTAX_ERROR;
657 format.integer_digit_count = format_specifier.
size();
659 format.type = FormatSpecifierType::INTEGER;
666 const bool found_dot = dot_index != std::string::npos;
667 const bool only_one_dot = dot_index == dot_index_last;
668 if (format_specifier.
find_first_not_of(
".#") == std::string::npos && found_dot && only_one_dot) {
675 format.type = FormatSpecifierType::SYNTAX_ERROR;
680 format.integer_digit_count = left.
size();
683 format.fractional_digit_count = right.
size();
685 format.type = FormatSpecifierType::FLOAT;
689 format.type = FormatSpecifierType::SYNTAX_ERROR;
711 int format_specifier_split = -1;
714 for (
int byte_index = from_char; byte_index < path.
size(); byte_index++) {
716 if (start == -1 && (byte_index + 1) < path.
size() && path[byte_index] ==
'{' &&
717 path[byte_index + 1] ==
'{')
720 token.type = TokenType::LEFT_CURLY_BRACE;
730 if (start == -1 && (byte_index + 1) < path.
size() && path[byte_index] ==
'}' &&
731 path[byte_index + 1] ==
'}')
733 token.type = TokenType::RIGHT_CURLY_BRACE;
740 if (start == -1 && path[byte_index] ==
'}') {
741 token.type = TokenType::UNESCAPED_CURLY_BRACE_ERROR;
747 if (path[byte_index] ==
'{') {
750 token.type = TokenType::VARIABLE_SYNTAX_ERROR;
755 format_specifier_split = -1;
766 if (path[byte_index] ==
':') {
767 if (format_specifier_split == -1) {
771 format_specifier_split = byte_index;
777 if (path[byte_index] ==
'}') {
778 end = byte_index + 1;
790 token.type = TokenType::VARIABLE_SYNTAX_ERROR;
797 if (format_specifier_split == -1) {
799 token.variable_name = path.
substr(start + 1, (end - 1) - (start + 1));
803 token.variable_name = path.
substr(start + 1, format_specifier_split - (start + 1));
805 path.
substr(format_specifier_split + 1, (end - 1) - (format_specifier_split + 1)));
806 if (token.format.type == FormatSpecifierType::SYNTAX_ERROR) {
807 token.type = TokenType::VARIABLE_SYNTAX_ERROR;
815namespace bke::path_templates {
818 size_t starting_offset)
820 while (
const std::optional<Token> token =
next_token(text, starting_offset)) {
821 switch (token->type) {
822 case TokenType::LEFT_CURLY_BRACE:
823 case TokenType::RIGHT_CURLY_BRACE: {
824 starting_offset = token->byte_range.one_after_last();
828 case TokenType::VARIABLE_EXPRESSION: {
835 case TokenType::VARIABLE_SYNTAX_ERROR:
836 case TokenType::UNESCAPED_CURLY_BRACE_ERROR: {
853 for (
int bytes_read = 0; bytes_read < path.
size();) {
854 const std::optional<Token> token =
next_token(path, bytes_read);
856 if (!token.has_value()) {
860 bytes_read = token->byte_range.one_after_last();
871 switch (token.type) {
872 case TokenType::VARIABLE_SYNTAX_ERROR: {
873 if (token.format.type == FormatSpecifierType::SYNTAX_ERROR) {
874 return {{ErrorType::FORMAT_SPECIFIER, token.byte_range}};
876 return {{ErrorType::VARIABLE_SYNTAX, token.byte_range}};
879 case TokenType::UNESCAPED_CURLY_BRACE_ERROR: {
880 return {{ErrorType::UNESCAPED_CURLY_BRACE, token.byte_range}};
884 case TokenType::VARIABLE_EXPRESSION:
885 case TokenType::LEFT_CURLY_BRACE:
886 case TokenType::RIGHT_CURLY_BRACE:
902 escaped.reserve(text.
size());
904 for (
const char c : text) {
905 escaped.push_back(c);
906 if (c ==
'{' || c ==
'}') {
907 escaped.push_back(c);
944 r_out_path->append(in_path);
956 for (
const Token &token : tokens) {
965 switch (token.type) {
967 case TokenType::VARIABLE_SYNTAX_ERROR:
968 case TokenType::UNESCAPED_CURLY_BRACE_ERROR: {
974 case TokenType::LEFT_CURLY_BRACE: {
978 case TokenType::RIGHT_CURLY_BRACE: {
984 case TokenType::VARIABLE_EXPRESSION: {
985 if (std::optional<StringRefNull> string_value = template_variables.
get_string(
986 token.variable_name))
988 if (token.format.type != FormatSpecifierType::NONE) {
990 errors.
append({ErrorType::FORMAT_SPECIFIER, token.byte_range});
993 STRNCPY(replacement_string, string_value->c_str());
998 if (std::optional<StringRefNull> path_value = template_variables.
get_filepath(
999 token.variable_name))
1001 if (token.format.type != FormatSpecifierType::NONE) {
1003 errors.
append({ErrorType::FORMAT_SPECIFIER, token.byte_range});
1006 STRNCPY(replacement_string, path_value->c_str());
1010 if (std::optional<int64_t> integer_value = template_variables.
get_integer(
1011 token.variable_name))
1018 if (std::optional<double> float_value = template_variables.
get_float(token.variable_name))
1026 errors.
append({ErrorType::UNKNOWN_VARIABLE, token.byte_range});
1033 r_out_path->append(in_path.
substr(in_head, token.byte_range.start() - in_head));
1036 r_out_path->append(
StringRef(replacement_string));
1038 in_head = token.byte_range.one_after_last();
1043 r_out_path->append(in_path.
substr(in_head));
1061 std::string evaluated_path;
1066 BLI_strncpy(path, evaluated_path.c_str(), path_maxncpy);
1078 std::string evaluated_path;
1085 const int buffer_size =
math::min(
int(evaluated_path.size()) + 1, path_maxncpy);
1087 BLI_strncpy(buffer, evaluated_path.c_str(), buffer_size);
1099 switch (error.
type) {
1100 case ErrorType::UNESCAPED_CURLY_BRACE: {
1101 return std::string(
"Unescaped curly brace '") + subpath +
"'.";
1104 case ErrorType::VARIABLE_SYNTAX: {
1105 return std::string(
"Invalid or incomplete template expression '") + subpath +
"'.";
1108 case ErrorType::FORMAT_SPECIFIER: {
1109 return std::string(
"Invalid format specifier in template expression '") + subpath +
"'.";
1112 case ErrorType::UNKNOWN_VARIABLE: {
1113 return std::string(
"Unknown variable referenced in template expression '") + subpath +
"'.";
1118 return "Unknown error.";
1128 std::string error_message =
"Parse errors in path '" + path +
"':";
1129 for (
const Error &error : errors) {
1133 BKE_report(reports, report_type, error_message.c_str());
1140 if (format.type == FormatSpecifierType::SYNTAX_ERROR) {
1141 return std::nullopt;
1152 if (format.type == FormatSpecifierType::SYNTAX_ERROR) {
1153 return std::nullopt;
Functions and classes for evaluating template expressions in filepaths.
#define ID_BLEND_PATH_FROM_GLOBAL(_id)
Enumerations for DNA_ID.h.
#define ARRAY_SET_ITEMS(...)
#define BLI_assert_msg(a, msg)
constexpr int64_t size() const
static constexpr IndexRange from_begin_end(const int64_t begin, const int64_t end)
constexpr int64_t start() const
constexpr bool is_empty() const
constexpr const char * c_str() const
constexpr StringRefNull()
void append(const T &value)
std::optional< int64_t > get_integer(StringRef name) const
bool add_filename_only(StringRef var_name, StringRefNull full_path, StringRef fallback)
bool add_float(StringRef name, double value)
bool add_path_up_to_file(StringRef var_name, StringRefNull full_path, StringRef fallback)
bool remove(StringRef name)
std::optional< double > get_float(StringRef name) const
std::optional< StringRefNull > get_filepath(StringRef name) const
std::optional< StringRefNull > get_string(StringRef name) const
bool add_integer(StringRef name, int64_t value)
bool contains(StringRef name) const
bool add_string(StringRef name, StringRef value)
bool add_filepath(StringRef name, StringRef value)
constexpr int64_t find_first_of(StringRef chars, int64_t pos=0) const
constexpr const char * end() const
constexpr bool is_empty() const
constexpr int64_t size() const
constexpr int64_t find_first_not_of(StringRef chars, int64_t pos=0) const
void copy_unsafe(char *dst) const
constexpr StringRef substr(int64_t start, int64_t size) const
constexpr int64_t find_last_of(StringRef chars, int64_t pos=INT64_MAX) const
std::optional< ExpressionInfo > next_template_variable_expression(StringRef text, size_t starting_offset)
bool operator==(const Error &left, const Error &right)
T min(const T &a, const T &b)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
const char * BLI_path_extension_or_end(const char *filepath) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL
char * STRNCPY(char(&dst)[N], const char *src)
static int format_int_to_string(const FormatSpecifier &format, const int64_t integer_value, char r_output_string[FORMAT_BUFFER_SIZE])
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void BKE_add_template_variables_general(bke::path_templates::VariableMap &variables, const ID *path_owner_id, const bke::BlenderProject *project)
bool BKE_path_contains_template_syntax(StringRef path)
Vector< bke::path_templates::Error > BKE_path_apply_template_alloc(char **path, int path_maxncpy, const bke::path_templates::VariableMap &template_variables)
std::optional< AncestorPointerRNA > RNA_struct_search_closest_ancestor_by_type(const PointerRNA *ptr, const StructRNA *srna)
void BKE_report_path_template_errors(ReportList *reports, eReportType report_type, StringRef path, Span< bke::path_templates::Error > errors)
static FormatSpecifier parse_format_specifier(StringRef format_specifier)
const char * BKE_main_blendfile_path_from_global()
static std::optional< Token > next_token(StringRef path, const int from_char)
auto BKE_blender_project_read_callback(const Main *bmain, Fn lambda)
static Vector< Token > parse_template(StringRef path)
@ PROP_VARIABLES_RENDER_OUTPUT
void BKE_render_resolution(const RenderData *r, const bool use_crop, int *r_width, int *r_height)
std::string BKE_path_template_error_to_string(const bke::path_templates::Error &error, StringRef path)
void BKE_add_template_variables_for_render_path(bke::path_templates::VariableMap &variables, const Scene &scene)
void BKE_add_template_variables_for_node(bke::path_templates::VariableMap &variables, const bNode &owning_node)
Vector< bke::path_templates::Error > BKE_path_apply_template(char *path, int path_maxncpy, const bke::path_templates::VariableMap &template_variables)
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
static int format_float_to_string(const FormatSpecifier &format, const double float_value, char r_output_string[FORMAT_BUFFER_SIZE])
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
static std::optional< Error > token_to_syntax_error(const Token &token)
PropertyPathTemplateType RNA_property_path_template_type(PropertyRNA *prop)
std::string BKE_path_template_escape(StringRef text)
void BKE_report(ReportList *reports, eReportType type, const char *message)
std::optional< bke::path_templates::VariableMap > BKE_build_template_variables_for_prop(const bContext *C, PointerRNA *ptr, PropertyRNA *prop)
int RNA_property_flag(PropertyRNA *prop)
void BKE_add_template_variables_for_project(bke::path_templates::VariableMap &variables, const bke::BlenderProject &project)
bool BLI_path_parent_dir(char *path) ATTR_NONNULL(1)
static Vector< Error > eval_template(std::string *r_out_path, StringRef in_path, const VariableMap &template_variables)
Main * CTX_data_main(const bContext *C)
Vector< bke::path_templates::Error > BKE_path_validate_template(StringRef path, const bke::path_templates::VariableMap &template_variables)
ID_Type GS(const char *name)
@ PROP_PATH_SUPPORTS_TEMPLATES
std::optional< std::string > BKE_path_template_format_float(StringRef format_specifier, double value)
Scene * CTX_data_scene(const bContext *C)
bool BLI_path_make_safe_filename(char *filename) ATTR_NONNULL(1)
std::optional< std::string > BKE_path_template_format_int(StringRef format_specifier, int64_t value)
#define FORMAT_BUFFER_SIZE