|
@@ -49,6 +49,10 @@
|
|
|
* (empty) - element of list at position index (starting from zero) at the time of initialization
|
|
* (empty) - element of list at position index (starting from zero) at the time of initialization
|
|
|
*
|
|
*
|
|
|
* Synopsis: list::shift()
|
|
* Synopsis: list::shift()
|
|
|
|
|
+ *
|
|
|
|
|
+ * Synopsis: list::contains(value)
|
|
|
|
|
+ * Variables:
|
|
|
|
|
+ * (empty) - "true" if list contains value, "false" if not
|
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
#include <stdlib.h>
|
|
@@ -89,6 +93,11 @@ struct shift_instance {
|
|
|
NCDModuleInst *i;
|
|
NCDModuleInst *i;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
+struct contains_instance {
|
|
|
|
|
+ NCDModuleInst *i;
|
|
|
|
|
+ int contains;
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
static void func_new_list (NCDModuleInst *i)
|
|
static void func_new_list (NCDModuleInst *i)
|
|
|
{
|
|
{
|
|
|
// allocate instance
|
|
// allocate instance
|
|
@@ -527,6 +536,78 @@ static void shift_func_die (void *vo)
|
|
|
NCDModuleInst_Backend_Dead(i);
|
|
NCDModuleInst_Backend_Dead(i);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+static void contains_func_new (NCDModuleInst *i)
|
|
|
|
|
+{
|
|
|
|
|
+ // allocate instance
|
|
|
|
|
+ struct contains_instance *o = malloc(sizeof(*o));
|
|
|
|
|
+ if (!o) {
|
|
|
|
|
+ ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
|
|
|
|
|
+ goto fail0;
|
|
|
|
|
+ }
|
|
|
|
|
+ NCDModuleInst_Backend_SetUser(i, o);
|
|
|
|
|
+
|
|
|
|
|
+ // init arguments
|
|
|
|
|
+ o->i = i;
|
|
|
|
|
+
|
|
|
|
|
+ // read arguments
|
|
|
|
|
+ NCDValue *value_arg;
|
|
|
|
|
+ if (!NCDValue_ListRead(i->args, 1, &value_arg)) {
|
|
|
|
|
+ ModuleLog(o->i, BLOG_ERROR, "wrong arity");
|
|
|
|
|
+ goto fail1;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // get method object
|
|
|
|
|
+ struct instance *mo = i->method_object->inst_user;
|
|
|
|
|
+
|
|
|
|
|
+ // search
|
|
|
|
|
+ o->contains = 0;
|
|
|
|
|
+ for (NCDValue *v = NCDValue_ListFirst(&mo->list); v; v = NCDValue_ListNext(&mo->list, v)) {
|
|
|
|
|
+ if (NCDValue_Compare(v, value_arg) == 0) {
|
|
|
|
|
+ o->contains = 1;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // signal up
|
|
|
|
|
+ NCDModuleInst_Backend_Up(o->i);
|
|
|
|
|
+ return;
|
|
|
|
|
+
|
|
|
|
|
+fail1:
|
|
|
|
|
+ free(o);
|
|
|
|
|
+fail0:
|
|
|
|
|
+ NCDModuleInst_Backend_SetError(i);
|
|
|
|
|
+ NCDModuleInst_Backend_Dead(i);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void contains_func_die (void *vo)
|
|
|
|
|
+{
|
|
|
|
|
+ struct contains_instance *o = vo;
|
|
|
|
|
+ NCDModuleInst *i = o->i;
|
|
|
|
|
+
|
|
|
|
|
+ // free instance
|
|
|
|
|
+ free(o);
|
|
|
|
|
+
|
|
|
|
|
+ NCDModuleInst_Backend_Dead(i);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int contains_func_getvar (void *vo, const char *name, NCDValue *out)
|
|
|
|
|
+{
|
|
|
|
|
+ struct contains_instance *o = vo;
|
|
|
|
|
+
|
|
|
|
|
+ if (!strcmp(name, "")) {
|
|
|
|
|
+ const char *value = (o->contains ? "true" : "false");
|
|
|
|
|
+
|
|
|
|
|
+ if (!NCDValue_InitString(out, value)) {
|
|
|
|
|
+ ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
static const struct NCDModule modules[] = {
|
|
static const struct NCDModule modules[] = {
|
|
|
{
|
|
{
|
|
|
.type = "list",
|
|
.type = "list",
|
|
@@ -561,6 +642,11 @@ static const struct NCDModule modules[] = {
|
|
|
.type = "list::shift",
|
|
.type = "list::shift",
|
|
|
.func_new = shift_func_new,
|
|
.func_new = shift_func_new,
|
|
|
.func_die = shift_func_die
|
|
.func_die = shift_func_die
|
|
|
|
|
+ }, {
|
|
|
|
|
+ .type = "list::contains",
|
|
|
|
|
+ .func_new = contains_func_new,
|
|
|
|
|
+ .func_die = contains_func_die,
|
|
|
|
|
+ .func_getvar = contains_func_getvar
|
|
|
}, {
|
|
}, {
|
|
|
.type = NULL
|
|
.type = NULL
|
|
|
}
|
|
}
|