From 15d6206e10ec14fd91d96f276e74496765fffa7d Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Fri, 11 Oct 2019 04:22:14 +0000 Subject: [PATCH] zip-writer: New functions for adding members. These will have additional users in an upcoming commit. --- src/libpspp/zip-writer.c | 28 ++++++++++++++++++++++++++++ src/libpspp/zip-writer.h | 4 ++++ src/output/odt.c | 27 ++------------------------- 3 files changed, 34 insertions(+), 25 deletions(-) diff --git a/src/libpspp/zip-writer.c b/src/libpspp/zip-writer.c index fd36fc523a..0959551abe 100644 --- a/src/libpspp/zip-writer.c +++ b/src/libpspp/zip-writer.c @@ -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. */ diff --git a/src/libpspp/zip-writer.h b/src/libpspp/zip-writer.h index aff3f994f3..1b5b359fb9 100644 --- a/src/libpspp/zip-writer.h +++ b/src/libpspp/zip-writer.h @@ -22,6 +22,10 @@ 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 */ diff --git a/src/output/odt.c b/src/output/odt.c index 90a76186a7..a3f945421f 100644 --- a/src/output/odt.c +++ b/src/output/odt.c @@ -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); -- 2.30.2