common: implement ident and selector manipulation

This commit is contained in:
2024-12-13 12:25:40 +00:00
parent 24d443b818
commit 0a8d913fdd
5 changed files with 201 additions and 7 deletions

View File

@@ -0,0 +1,90 @@
#include <ivy/selector.h>
#include <blue/core/queue.h>
#include <blue/object/string.h>
#include <stdlib.h>
#include <string.h>
enum ivy_status ivy_selector_create(struct ivy_selector **sel)
{
struct ivy_selector *out = malloc(sizeof *out);
if (!out) {
return IVY_ERR_NO_MEMORY;
}
memset(out, 0x0, sizeof *out);
*sel = out;
return IVY_OK;
}
void ivy_selector_destroy(struct ivy_selector *sel)
{
if (sel->sel_name) {
free(sel->sel_name);
}
b_queue_iterator it = {0};
b_queue_iterator_begin(&sel->sel_args, &it);
while (b_queue_iterator_is_valid(&it)) {
struct ivy_selector_arg *arg = b_unbox(struct ivy_selector_arg, it.entry, arg_entry);
b_queue_iterator_erase(&it);
if (arg->arg_label) {
free(arg->arg_label);
}
if (arg->arg_name) {
free(arg->arg_name);
}
free(arg);
}
free(sel);
}
void ivy_selector_set_recipient(
struct ivy_selector *sel, enum ivy_selector_recipient r)
{
sel->sel_recipient = r;
}
enum ivy_status ivy_selector_set_name(struct ivy_selector *sel, const char *name)
{
sel->sel_name = b_strdup(name);
return sel->sel_name ? IVY_OK : IVY_ERR_NO_MEMORY;
}
enum ivy_status ivy_selector_add_arg(
struct ivy_selector *sel, const char *label, const char *name)
{
struct ivy_selector_arg *arg = malloc(sizeof *arg);
if (!arg) {
return IVY_ERR_NO_MEMORY;
}
memset(arg, 0x0, sizeof *arg);
if (label) {
arg->arg_label = b_strdup(label);
if (!arg->arg_label) {
free(arg);
return IVY_ERR_NO_MEMORY;
}
}
if (name) {
arg->arg_name = b_strdup(name);
if (!arg->arg_name) {
free(arg->arg_label);
free(arg);
return IVY_ERR_NO_MEMORY;
}
}
b_queue_push_back(&sel->sel_args, &arg->arg_entry);
return IVY_OK;
}