Jelajahi Sumber

Refactoring using MemRef.

Ambroz Bizjak 11 tahun lalu
induk
melakukan
98d2fba833
3 mengubah file dengan 8 tambahan dan 9 penghapusan
  1. 6 6
      misc/write_file.h
  2. 1 2
      ncd/modules/file.c
  3. 1 1
      ncd/modules/net_backend_waitdevice.c

+ 6 - 6
misc/write_file.h

@@ -35,24 +35,24 @@
 #include <stdio.h>
 
 #include <misc/debug.h>
+#include <misc/memref.h>
 
-static int write_file (const char *file, const uint8_t *data, size_t len)
+static int write_file (const char *file, MemRef data)
 {
     FILE *f = fopen(file, "w");
     if (!f) {
         goto fail0;
     }
     
-    while (len > 0) {
-        size_t res = fwrite(data, 1, len, f);
+    while (data.len > 0) {
+        size_t res = fwrite(data.ptr, 1, data.len, f);
         if (res == 0) {
             goto fail1;
         }
         
-        ASSERT(res <= len)
+        ASSERT(res <= data.len)
         
-        data += res;
-        len -= res;
+        data = MemRef_SubFrom(data, res);
     }
     
     if (fclose(f) != 0) {

+ 1 - 2
ncd/modules/file.c

@@ -186,8 +186,7 @@ static void write_func_new (void *unused, NCDModuleInst *i, const struct NCDModu
     }
     
     // write file
-    MemRef contents_mr = NCDVal_StringMemRef(contents_arg);
-    int res = write_file(filename_nts.data, (uint8_t const *)contents_mr.ptr, contents_mr.len);
+    int res = write_file(filename_nts.data, NCDVal_StringMemRef(contents_arg));
     NCDValNullTermString_Free(&filename_nts);
     if (!res) {
         ModuleLog(i, BLOG_ERROR, "failed to write file");

+ 1 - 1
ncd/modules/net_backend_waitdevice.c

@@ -79,7 +79,7 @@ static void client_handler (struct instance *o, char *devpath, int have_map, BSt
         const char *ifindex_str = BStringMap_Get(cache_map, "IFINDEX");
         
         uintmax_t ifindex;
-        if (!(!match_res && interface && strlen(interface) == o->ifname.len && !memcmp(interface, o->ifname.ptr, o->ifname.len) && ifindex_str && parse_unsigned_integer(MemRef_MakeCstr(ifindex_str), &ifindex))) {
+        if (!(!match_res && interface && MemRef_Equal(MemRef_MakeCstr(interface), o->ifname) && ifindex_str && parse_unsigned_integer(MemRef_MakeCstr(ifindex_str), &ifindex))) {
             goto out;
         }