Add support for reading and writing SPV files.
[pspp] / src / libpspp / zip-writer.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. */