Преглед изворни кода

ncd/modules/list.c: add list::length, list::get

ambrop7 пре 15 година
родитељ
комит
bb30763a7b
1 измењених фајлова са 202 додато и 0 уклоњено
  1. 202 0
      ncd/modules/list.c

+ 202 - 0
ncd/modules/list.c

@@ -28,10 +28,20 @@
  *   (empty) - list containing elem1, ..., elemN
  * 
  * Synopsis: list::append(arg)
+ * 
+ * Synopsis: list::length()
+ * Variables:
+ *   (empty) - number of elements in list at the time of initialization
+ * 
+ * Synopsis: list::get(string index)
+ * Variables:
+ *   (empty) - element of list at position index (starting from zero) at the time of initialization
  */
 
 #include <stdlib.h>
 #include <string.h>
+#include <stdio.h>
+#include <inttypes.h>
 
 #include <ncd/NCDModule.h>
 
@@ -48,6 +58,16 @@ struct append_instance {
     NCDModuleInst *i;
 };
 
+struct length_instance {
+    NCDModuleInst *i;
+    uint64_t length;
+};
+
+struct get_instance {
+    NCDModuleInst *i;
+    NCDValue value;
+};
+
 static void func_new (NCDModuleInst *i)
 {
     // allocate instance
@@ -167,6 +187,178 @@ static void append_func_die (void *vo)
     NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
 }
 
+static void length_func_new (NCDModuleInst *i)
+{
+    // allocate instance
+    struct length_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;
+    
+    // check arguments
+    if (!NCDValue_ListRead(o->i->args, 0)) {
+        ModuleLog(o->i, BLOG_ERROR, "wrong arity");
+        goto fail1;
+    }
+    
+    // get method object
+    struct instance *mo = i->method_object->inst_user;
+    
+    // count elements
+    o->length = 0;
+    NCDValue *e = NCDValue_ListFirst(&mo->list);
+    while (e) {
+        if (o->length == UINT64_MAX) {
+            ModuleLog(o->i, BLOG_ERROR, "too many elements");
+            goto fail1;
+        }
+        o->length++;
+        e = NCDValue_ListNext(&mo->list, e);
+    }
+    
+    // signal up
+    NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
+    
+    return;
+    
+fail1:
+    free(o);
+fail0:
+    NCDModuleInst_Backend_SetError(i);
+    NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
+}
+
+static void length_func_die (void *vo)
+{
+    struct length_instance *o = vo;
+    NCDModuleInst *i = o->i;
+    
+    // free instance
+    free(o);
+    
+    NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
+}
+
+static int length_func_getvar (void *vo, const char *name, NCDValue *out)
+{
+    struct length_instance *o = vo;
+    
+    if (!strcmp(name, "")) {
+        char str[30];
+        snprintf(str, sizeof(str), "%"PRIu64, o->length);
+        
+        if (!NCDValue_InitString(out, str)) {
+            ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
+            return 0;
+        }
+        
+        return 1;
+    }
+    
+    return 0;
+}
+    
+static void get_func_new (NCDModuleInst *i)
+{
+    // allocate instance
+    struct get_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;
+    
+    // check arguments
+    NCDValue *index_arg;
+    if (!NCDValue_ListRead(o->i->args, 1, &index_arg)) {
+        ModuleLog(o->i, BLOG_ERROR, "wrong arity");
+        goto fail1;
+    }
+    if (NCDValue_Type(index_arg) != NCDVALUE_STRING) {
+        ModuleLog(o->i, BLOG_ERROR, "wrong type");
+        goto fail1;
+    }
+    uint64_t index;
+    if (sscanf(NCDValue_StringValue(index_arg), "%"SCNu64, &index) != 1) {
+        ModuleLog(o->i, BLOG_ERROR, "wrong value");
+        goto fail1;
+    }
+    
+    // get method object
+    struct instance *mo = i->method_object->inst_user;
+    
+    // find requested element
+    uint64_t pos = 0;
+    NCDValue *e = NCDValue_ListFirst(&mo->list);
+    while (e) {
+        if (pos == index) {
+            break;
+        }
+        pos++;
+        e = NCDValue_ListNext(&mo->list, e);
+    }
+    
+    if (!e) {
+        ModuleLog(o->i, BLOG_ERROR, "no element at index %"PRIu64, index);
+        goto fail1;
+    }
+    
+    // copy value
+    if (!NCDValue_InitCopy(&o->value, e)) {
+        ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
+        goto fail1;
+    }
+    
+    // signal up
+    NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
+    
+    return;
+    
+fail1:
+    free(o);
+fail0:
+    NCDModuleInst_Backend_SetError(i);
+    NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
+}
+
+static void get_func_die (void *vo)
+{
+    struct get_instance *o = vo;
+    NCDModuleInst *i = o->i;
+    
+    // free value
+    NCDValue_Free(&o->value);
+    
+    // free instance
+    free(o);
+    
+    NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
+}
+
+static int get_func_getvar (void *vo, const char *name, NCDValue *out)
+{
+    struct get_instance *o = vo;
+    
+    if (!strcmp(name, "")) {
+        if (!NCDValue_InitCopy(out, &o->value)) {
+            ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
+            return 0;
+        }
+        
+        return 1;
+    }
+    
+    return 0;
+}
+
 static const struct NCDModule modules[] = {
     {
         .type = "list",
@@ -177,6 +369,16 @@ static const struct NCDModule modules[] = {
         .type = "list::append",
         .func_new = append_func_new,
         .func_die = append_func_die
+    }, {
+        .type = "list::length",
+        .func_new = length_func_new,
+        .func_die = length_func_die,
+        .func_getvar = length_func_getvar
+    }, {
+        .type = "list::get",
+        .func_new = get_func_new,
+        .func_die = get_func_die,
+        .func_getvar = get_func_getvar
     }, {
         .type = NULL
     }