zip-writer: New functions for adding members.
authorBen Pfaff <blp@cs.stanford.edu>
Fri, 11 Oct 2019 04:22:14 +0000 (04:22 +0000)
committerBen Pfaff <blp@cs.stanford.edu>
Fri, 11 Oct 2019 04:46:28 +0000 (04:46 +0000)
These will have additional users in an upcoming commit.

src/libpspp/zip-writer.c
src/libpspp/zip-writer.h
src/output/odt.c

index fd36fc523a029b61e0dee5122ef330bbb8c4a4ed..0959551abefd00720cd47f1e768910bcfa9bd340 100644 (file)
@@ -29,6 +29,7 @@
 #include "gl/xalloc.h"
 
 #include "libpspp/message.h"
+#include "libpspp/temp-file.h"
 
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
@@ -188,6 +189,33 @@ zip_writer_add (struct zip_writer *zw, FILE *file, const char *member_name)
   member->name = xstrdup (member_name);
 }
 
+/* Adds a member named MEMBER_NAME whose contents is the null-terminated string
+   CONTENT. */
+void
+zip_writer_add_string (struct zip_writer *zw, const char *member_name,
+                       const char *content)
+{
+  zip_writer_add_memory (zw, member_name, content, strlen (content));
+}
+
+/* Adds a member named MEMBER_NAME whose contents is the SIZE bytes of
+   CONTENT. */
+void
+zip_writer_add_memory (struct zip_writer *zw, const char *member_name,
+                       const void *content, size_t size)
+{
+  FILE *fp = create_temp_file ();
+  if (fp == NULL)
+    {
+      msg_error (errno, _("error creating temporary file"));
+      zw->ok = false;
+      return;
+    }
+  fwrite (content, size, 1, fp);
+  zip_writer_add (zw, fp, member_name);
+  close_temp_file (fp);
+}
+
 /* Finalizes the contents of ZW and closes it.  Returns true if successful,
    false if a write error occurred while finalizing the file or at any earlier
    time. */
index aff3f994f364f0ea242559fd6001abe3c731e70e..1b5b359fb9725d7dd55e5a43f1924c72636d7548 100644 (file)
 
 struct zip_writer *zip_writer_create (const char *file_name);
 void zip_writer_add (struct zip_writer *, FILE *, const char *member_name);
+void zip_writer_add_string (struct zip_writer *, const char *member_name,
+                            const char *content);
+void zip_writer_add_memory (struct zip_writer *, const char *member_name,
+                            const void *content, size_t size);
 bool zip_writer_close (struct zip_writer *);
 
 #endif /* libpspp/zip-writer.h */
index 90a76186a7d81f7cdedd7f8510388aa30a0306ae..a3f945421f00e351fc8e84a08a56f435dc7d95b1 100644 (file)
@@ -80,26 +80,6 @@ odt_driver_cast (struct output_driver *driver)
   return UP_CAST (driver, struct odt_driver, driver);
 }
 
-/* Create the "mimetype" file needed by ODF */
-static bool
-create_mimetype (struct zip_writer *zip)
-{
-  FILE *fp;
-
-  fp = create_temp_file ();
-  if (fp == NULL)
-    {
-      msg_error (errno, _("error creating temporary file"));
-      return false;
-    }
-
-  fprintf (fp, "application/vnd.oasis.opendocument.text");
-  zip_writer_add (zip, fp, "mimetype");
-  close_temp_file (fp);
-
-  return true;
-}
-
 /* Creates a new temporary file and stores it in *FILE, then creates an XML
    writer for it and stores it in *W. */
 static void
@@ -306,11 +286,8 @@ odt_create (struct file_handle *fh, enum settings_output_devices device_type,
   odt->handle = fh;
   odt->file_name = xstrdup (file_name);
 
-  if (!create_mimetype (zip))
-    {
-      output_driver_destroy (d);
-      return NULL;
-    }
+  zip_writer_add_string (zip, "mimetype",
+                         "application/vnd.oasis.opendocument.text");
 
   /* Create the manifest */
   create_writer (&odt->manifest_file, &odt->manifest_wtr);