]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/test-database.c
stringpool: Make them initializable right from the file
[people/ms/libloc.git] / src / test-database.c
index 1137326fb09b6468e9a2ec9cd12e3f1317bf6cd8..600a983235207fb6ac1d05c0671e70ce1193b0f2 100644 (file)
 #include <unistd.h>
 
 #include <loc/libloc.h>
+#include <loc/writer.h>
 #include "database.h"
 
+const char* VENDOR = "Test Vendor";
 const char* DESCRIPTION =
        "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
        "Proin ultrices pulvinar dolor, et sollicitudin eros ultricies "
@@ -43,36 +45,36 @@ int main(int argc, char** argv) {
                exit(EXIT_FAILURE);
 
        // Create a database
-       struct loc_database* db;
-       err = loc_database_new(ctx, &db, 1024);
+       struct loc_writer* writer;
+       err = loc_writer_new(ctx, &writer);
        if (err < 0)
                exit(EXIT_FAILURE);
 
        // Set the vendor
-       err = loc_database_set_vendor(db, "Test Vendor");
+       err = loc_writer_set_vendor(writer, VENDOR);
        if (err) {
                fprintf(stderr, "Could not set vendor\n");
                exit(EXIT_FAILURE);
        }
 
        // Retrieve vendor
-       const char* vendor1 = loc_database_get_vendor(db);
-       if (vendor1) {
-               printf("Vendor is: %s\n", vendor1);
+       const char* vendor = loc_writer_get_vendor(writer);
+       if (vendor) {
+               printf("Vendor is: %s\n", vendor);
        } else {
                fprintf(stderr, "Could not retrieve vendor\n");
                exit(EXIT_FAILURE);
        }
 
        // Set a description
-       err = loc_database_set_description(db, DESCRIPTION);
+       err = loc_writer_set_description(writer, DESCRIPTION);
        if (err) {
                fprintf(stderr, "Could not set description\n");
                exit(EXIT_FAILURE);
        }
 
        // Retrieve description
-       const char* description = loc_database_get_description(db);
+       const char* description = loc_writer_get_description(writer);
        if (description) {
                printf("Description is: %s\n", description);
        } else {
@@ -86,18 +88,16 @@ int main(int argc, char** argv) {
                exit(EXIT_FAILURE);
        }
 
-       err = loc_database_write(db, f);
+       err = loc_writer_write(writer, f);
        if (err) {
                fprintf(stderr, "Could not write database: %s\n", strerror(err));
                exit(EXIT_FAILURE);
        }
+       loc_writer_unref(writer);
 
        // Close the file
        fclose(f);
 
-       // Close the database
-       //loc_database_unref(db);
-
        // And open it again from disk
        f = fopen("test.db", "r");
        if (!f) {
@@ -105,25 +105,24 @@ int main(int argc, char** argv) {
                exit(EXIT_FAILURE);
        }
 
-       struct loc_database* db2;
-       err = loc_database_open(ctx, &db2, f);
+       struct loc_database* db;
+       err = loc_database_new(ctx, &db, f);
        if (err) {
                fprintf(stderr, "Could not open database: %s\n", strerror(-err));
                exit(EXIT_FAILURE);
        }
 
-       const char* vendor2 = loc_database_get_vendor(db2);
-       if (!vendor2) {
+       vendor = loc_database_get_vendor(db);
+       if (!vendor) {
                fprintf(stderr, "Could not retrieve vendor\n");
                exit(EXIT_FAILURE);
-       } else if (strcmp(vendor1, vendor2) != 0) {
-               fprintf(stderr, "Vendor doesn't match: %s != %s\n", vendor1, vendor2);
+       } else if (strcmp(vendor, VENDOR) != 0) {
+               fprintf(stderr, "Vendor doesn't match: %s != %s\n", vendor, VENDOR);
                exit(EXIT_FAILURE);
        }
 
        // Close the database
-       loc_database_unref(db2);
-       fclose(f);
+       loc_database_unref(db);
 
        loc_unref(ctx);