]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/writer.c
Allow creating networks in memory
[people/ms/libloc.git] / src / writer.c
index da6959a4d77d3366b988c4f3b80d9aad0eebbc24..1e3e360fea5b97c02c660a638a8f667e0454e74a 100644 (file)
@@ -22,6 +22,7 @@
 #include <time.h>
 
 #include <loc/format.h>
+#include <loc/network.h>
 #include <loc/writer.h>
 
 #include "libloc-private.h"
@@ -37,6 +38,8 @@ struct loc_writer {
 
        struct loc_as** as;
        size_t as_count;
+
+       struct loc_network_tree* networks;
 };
 
 LOC_EXPORT int loc_writer_new(struct loc_ctx* ctx, struct loc_writer** writer) {
@@ -53,6 +56,13 @@ LOC_EXPORT int loc_writer_new(struct loc_ctx* ctx, struct loc_writer** writer) {
                return r;
        }
 
+       // Initialize the network tree
+       r = loc_network_tree_new(ctx, &w->networks);
+       if (r) {
+               loc_writer_unref(w);
+               return r;
+       }
+
        *writer = w;
        return 0;
 }
@@ -71,6 +81,10 @@ static void loc_writer_free(struct loc_writer* writer) {
                loc_as_unref(writer->as[i]);
        }
 
+       // Release network tree
+       if (writer->networks)
+               loc_network_tree_unref(writer->networks);
+
        // Unref the string pool
        loc_stringpool_unref(writer->pool);
 
@@ -141,6 +155,18 @@ LOC_EXPORT int loc_writer_add_as(struct loc_writer* writer, struct loc_as** as,
        return 0;
 }
 
+LOC_EXPORT int loc_writer_add_network(struct loc_writer* writer, struct loc_network** network, const char* string) {
+       int r;
+
+       // Create a new network object
+       r = loc_network_new_from_string(writer->ctx, network, string);
+       if (r)
+               return r;
+
+       // Add it to the local tree
+       return loc_network_tree_add_network(writer->networks, *network);
+}
+
 static void make_magic(struct loc_writer* writer, struct loc_database_magic* magic) {
        // Copy magic bytes
        for (unsigned int i = 0; i < strlen(LOC_DATABASE_MAGIC); i++)