From 4ffe445572877e59cae459f039e401984e3d11ce Mon Sep 17 00:00:00 2001 From: Max Wash Date: Tue, 22 Apr 2025 15:21:30 +0100 Subject: [PATCH] mie: add strict-naming support to mie_name_map in strict mode, the hint is taken as the required name. if a value already exists with the given name, the operation fails. --- mie/func.c | 2 +- mie/include/mie/name.h | 7 ++++++- mie/module.c | 3 ++- mie/name.c | 9 ++++++++- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/mie/func.c b/mie/func.c index 9a8fdc4..5587895 100644 --- a/mie/func.c +++ b/mie/func.c @@ -84,7 +84,7 @@ struct mie_value *mie_func_generate_value_name( struct mie_func *func, struct mie_value *val, const char *hint) { struct mie_name *name - = mie_name_map_put(func->f_names, &val->v_name, hint); + = mie_name_map_put(func->f_names, &val->v_name, hint, 0); return b_unbox(struct mie_value, name, v_name); } diff --git a/mie/include/mie/name.h b/mie/include/mie/name.h index 6faeba9..0e81084 100644 --- a/mie/include/mie/name.h +++ b/mie/include/mie/name.h @@ -4,6 +4,10 @@ #include #include +enum mie_name_map_flags { + MIE_NAME_MAP_STRICT = 0x01u, +}; + enum mie_name_map_entry_type { MIE_NAME_MAP_E_NONE = 0, MIE_NAME_MAP_E_NAME, @@ -38,6 +42,7 @@ extern struct mie_name_map *mie_name_map_create(void); extern void mie_name_map_destroy(struct mie_name_map *map); extern struct mie_name *mie_name_map_put( - struct mie_name_map *map, struct mie_name *entry, const char *hint); + struct mie_name_map *map, struct mie_name *entry, const char *hint, + enum mie_name_map_flags flags); #endif diff --git a/mie/module.c b/mie/module.c index 6c896aa..7d47f39 100644 --- a/mie/module.c +++ b/mie/module.c @@ -106,7 +106,8 @@ enum b_status mie_module_put_data( struct mie_value *mie_module_generate_value_name( struct mie_module *mod, struct mie_value *val, const char *hint) { - struct mie_name *name = mie_name_map_put(mod->m_names, &val->v_name, hint); + struct mie_name *name + = mie_name_map_put(mod->m_names, &val->v_name, hint, 0); return b_unbox(struct mie_value, name, v_name); } diff --git a/mie/name.c b/mie/name.c index 01fa134..7e9199b 100644 --- a/mie/name.c +++ b/mie/name.c @@ -123,7 +123,8 @@ static b_status put_name(struct mie_name_map *map, struct mie_name *name) } struct mie_name *mie_name_map_put( - struct mie_name_map *map, struct mie_name *entry, const char *hint) + struct mie_name_map *map, struct mie_name *entry, const char *hint, + enum mie_name_map_flags flags) { memset(entry, 0x0, sizeof *entry); entry->n_base.e_type = MIE_NAME_MAP_E_NAME; @@ -149,6 +150,12 @@ struct mie_name *mie_name_map_put( } } + if (flags & MIE_NAME_MAP_STRICT) { + /* the caller insists that `hint` be used as the name. + * such a name already exists, so we can't help them */ + return NULL; + } + /* that name already exists, use a suffix to make the name unique. * alternately, no hint was specified, so it's up to us to generate the name */ b_rope dot = B_ROPE_CHAR('.');