Procházet zdrojové kódy

ncd: modules: list: add list::contains()

ambrop7 před 14 roky
rodič
revize
387bb131a5
1 změnil soubory, kde provedl 86 přidání a 0 odebrání
  1. 86 0
      ncd/modules/list.c

+ 86 - 0
ncd/modules/list.c

@@ -49,6 +49,10 @@
  *   (empty) - element of list at position index (starting from zero) at the time of initialization
  * 
  * Synopsis: list::shift()
+ * 
+ * Synopsis: list::contains(value)
+ * Variables:
+ *   (empty) - "true" if list contains value, "false" if not
  */
 
 #include <stdlib.h>
@@ -89,6 +93,11 @@ struct shift_instance {
     NCDModuleInst *i;
 };
 
+struct contains_instance {
+    NCDModuleInst *i;
+    int contains;
+};
+
 static void func_new_list (NCDModuleInst *i)
 {
     // allocate instance
@@ -527,6 +536,78 @@ static void shift_func_die (void *vo)
     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[] = {
     {
         .type = "list",
@@ -561,6 +642,11 @@ static const struct NCDModule modules[] = {
         .type = "list::shift",
         .func_new = shift_func_new,
         .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
     }