common: implement stringification of selectors
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
#ifndef IVY_SELECTOR_H_
|
#ifndef IVY_SELECTOR_H_
|
||||||
#define IVY_SELECTOR_H_
|
#define IVY_SELECTOR_H_
|
||||||
|
|
||||||
#include <ivy/status.h>
|
|
||||||
#include <blue/core/queue.h>
|
#include <blue/core/queue.h>
|
||||||
|
#include <ivy/status.h>
|
||||||
|
|
||||||
enum ivy_selector_recipient {
|
enum ivy_selector_recipient {
|
||||||
IVY_SEL_NONE = 0,
|
IVY_SEL_NONE = 0,
|
||||||
@@ -27,8 +27,12 @@ IVY_API void ivy_selector_destroy(struct ivy_selector *sel);
|
|||||||
|
|
||||||
IVY_API void ivy_selector_set_recipient(
|
IVY_API void ivy_selector_set_recipient(
|
||||||
struct ivy_selector *sel, enum ivy_selector_recipient r);
|
struct ivy_selector *sel, enum ivy_selector_recipient r);
|
||||||
IVY_API enum ivy_status ivy_selector_set_name(struct ivy_selector *sel, const char *name);
|
IVY_API enum ivy_status ivy_selector_set_name(
|
||||||
|
struct ivy_selector *sel, const char *name);
|
||||||
IVY_API enum ivy_status ivy_selector_add_arg(
|
IVY_API enum ivy_status ivy_selector_add_arg(
|
||||||
struct ivy_selector *sel, const char *label, const char *name);
|
struct ivy_selector *sel, const char *label, const char *name);
|
||||||
|
|
||||||
|
IVY_API size_t ivy_selector_to_string(
|
||||||
|
const struct ivy_selector *sel, char *out, size_t max);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include <ivy/selector.h>
|
|
||||||
#include <blue/core/queue.h>
|
#include <blue/core/queue.h>
|
||||||
|
#include <blue/core/stringstream.h>
|
||||||
#include <blue/object/string.h>
|
#include <blue/object/string.h>
|
||||||
|
#include <ivy/selector.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@@ -27,7 +28,8 @@ void ivy_selector_destroy(struct ivy_selector *sel)
|
|||||||
b_queue_iterator it = {0};
|
b_queue_iterator it = {0};
|
||||||
b_queue_iterator_begin(&sel->sel_args, &it);
|
b_queue_iterator_begin(&sel->sel_args, &it);
|
||||||
while (b_queue_iterator_is_valid(&it)) {
|
while (b_queue_iterator_is_valid(&it)) {
|
||||||
struct ivy_selector_arg *arg = b_unbox(struct ivy_selector_arg, it.entry, arg_entry);
|
struct ivy_selector_arg *arg
|
||||||
|
= b_unbox(struct ivy_selector_arg, it.entry, arg_entry);
|
||||||
b_queue_iterator_erase(&it);
|
b_queue_iterator_erase(&it);
|
||||||
|
|
||||||
if (arg->arg_label) {
|
if (arg->arg_label) {
|
||||||
@@ -88,3 +90,42 @@ enum ivy_status ivy_selector_add_arg(
|
|||||||
b_queue_push_back(&sel->sel_args, &arg->arg_entry);
|
b_queue_push_back(&sel->sel_args, &arg->arg_entry);
|
||||||
return IVY_OK;
|
return IVY_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t ivy_selector_to_string(const struct ivy_selector *sel, char *out, size_t max)
|
||||||
|
{
|
||||||
|
b_stringstream str;
|
||||||
|
b_stringstream_begin(&str, out, max);
|
||||||
|
|
||||||
|
switch (sel->sel_recipient) {
|
||||||
|
case IVY_SEL_OBJECT:
|
||||||
|
b_stringstream_add(&str, "-");
|
||||||
|
break;
|
||||||
|
case IVY_SEL_CLASS:
|
||||||
|
b_stringstream_add(&str, "+");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sel->sel_name) {
|
||||||
|
b_stringstream_add(&str, sel->sel_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sel->sel_name && !b_queue_empty(&sel->sel_args)) {
|
||||||
|
b_stringstream_add(&str, "(");
|
||||||
|
}
|
||||||
|
|
||||||
|
b_queue_iterator it;
|
||||||
|
b_queue_foreach (&it, &sel->sel_args) {
|
||||||
|
struct ivy_selector_arg *arg
|
||||||
|
= b_unbox(struct ivy_selector_arg, it.entry, arg_entry);
|
||||||
|
b_stringstream_addf(
|
||||||
|
&str, "%s:", arg->arg_label ? arg->arg_label : "_");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sel->sel_name && !b_queue_empty(&sel->sel_args)) {
|
||||||
|
b_stringstream_add(&str, ")");
|
||||||
|
}
|
||||||
|
|
||||||
|
return b_stringstream_get_length(&str);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user