File indexing completed on 2024-05-12 16:29:20

0001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
0002 /* writerperfect
0003  * Version: MPL 2.0 / LGPLv2.1+
0004  *
0005  * This Source Code Form is subject to the terms of the Mozilla Public
0006  * License, v. 2.0. If a copy of the MPL was not distributed with this
0007  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
0008  *
0009  * Major Contributor(s):
0010  * Copyright (C) 2007 Fridrich Strba (fridrich.strba@bluewin.ch)
0011  *
0012  * For minor contributions see the git repository.
0013  *
0014  * Alternatively, the contents of this file may be used under the terms
0015  * of the GNU Lesser General Public License Version 2.1 or later
0016  * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
0017  * applicable instead of those above.
0018  *
0019  * For further information visit http://libwpd.sourceforge.net
0020  */
0021 
0022 #ifdef HAVE_CONFIG_H
0023 #include "config.h"
0024 #endif
0025 
0026 #include <stdio.h>
0027 
0028 #include "OutputFileHelper.hxx"
0029 
0030 #ifdef USE_GSF_OUTPUT
0031 #include <gsf/gsf-utils.h>
0032 #include <gsf/gsf-output-stdio.h>
0033 #include <gsf/gsf-outfile.h>
0034 #include <gsf/gsf-outfile-zip.h>
0035 #include <gsf/gsf-input-stdio.h>
0036 #else
0037 #include "FemtoZip.hxx"
0038 #endif
0039 
0040 struct OutputFileHelperImpl
0041 {
0042     OutputFileHelperImpl(const char *password) : mpOutfile(0), mpPassword(password) {}
0043 #ifdef USE_GSF_OUTPUT
0044     GsfOutfile *mpOutfile;
0045 #else
0046     FemtoZip *mpOutfile;
0047 #endif
0048     const char *mpPassword;
0049 private:
0050     OutputFileHelperImpl(OutputFileHelperImpl const &);
0051     OutputFileHelperImpl &operator=(OutputFileHelperImpl const &);
0052 };
0053 
0054 
0055 OutputFileHelper::OutputFileHelper(const char *outFileName, const char *password) :
0056 #ifdef USE_GSF_OUTPUT
0057     m_impl(new OutputFileHelperImpl(password))
0058 #else
0059     m_impl(new OutputFileHelperImpl(password))
0060 #endif
0061 {
0062     m_impl->mpOutfile = 0;
0063 #ifdef USE_GSF_OUTPUT
0064     GsfOutput  *pOutput = 0;
0065     GError   *err = 0;
0066 
0067     gsf_init();
0068 
0069     if (!outFileName)
0070         pOutput = 0;
0071     else
0072     {
0073         pOutput = GSF_OUTPUT(gsf_output_stdio_new(outFileName, &err));
0074         if (pOutput == 0)
0075         {
0076             if (err)
0077             {
0078                 g_warning("'%s' error: %s", outFileName, err->message);
0079                 g_error_free(err);
0080             }
0081             gsf_shutdown();
0082         }
0083         else
0084         {
0085             if (err)
0086                 g_error_free(err);
0087             err = 0;
0088             m_impl->mpOutfile = GSF_OUTFILE(gsf_outfile_zip_new(pOutput, &err));
0089             if (m_impl->mpOutfile == 0)
0090             {
0091                 if (err)
0092                 {
0093                     g_warning("'%s' error: %s",
0094                               "gsf_outfile_zip_new", err->message);
0095                     g_error_free(err);
0096                 }
0097                 gsf_shutdown();
0098             }
0099             else
0100             {
0101                 if (err)
0102                     g_error_free(err);
0103                 err = 0;
0104                 g_object_unref(pOutput);
0105             }
0106         }
0107     }
0108 #else
0109     if (outFileName)
0110         m_impl->mpOutfile = new FemtoZip(outFileName);
0111 #endif
0112 }
0113 
0114 OutputFileHelper::~OutputFileHelper()
0115 {
0116 #ifdef USE_GSF_OUTPUT
0117     if (m_impl->mpOutfile && !gsf_output_close((GsfOutput *) m_impl->mpOutfile))
0118         fprintf(stderr, "ERROR : Couldn't close outfile\n");
0119 
0120     if (m_impl->mpOutfile)
0121         g_object_unref(m_impl->mpOutfile);
0122 
0123     gsf_shutdown();
0124 #else
0125     if (m_impl->mpOutfile)
0126         delete m_impl->mpOutfile;
0127 #endif
0128     if (m_impl)
0129         delete m_impl;
0130 }
0131 
0132 bool OutputFileHelper::writeChildFile(const char *childFileName, const char *str)
0133 {
0134     if (!m_impl->mpOutfile)
0135         return true;
0136 #ifdef USE_GSF_OUTPUT
0137     GsfOutput *child;
0138     if (0 != (child = gsf_outfile_new_child(m_impl->mpOutfile, childFileName, FALSE)))
0139     {
0140         bool res = gsf_output_puts(child, str) &&
0141                    gsf_output_close(child);
0142         g_object_unref(child);
0143         return res;
0144     }
0145     return false;
0146 #else
0147     m_impl->mpOutfile->createEntry(childFileName, 0);
0148     if (m_impl->mpOutfile->errorCode())
0149         return false;
0150     m_impl->mpOutfile->writeString(str);
0151     if (m_impl->mpOutfile->errorCode())
0152         return false;
0153     m_impl->mpOutfile->closeEntry();
0154     if (m_impl->mpOutfile->errorCode())
0155         return false;
0156     return true;
0157 #endif
0158 }
0159 
0160 #if defined(USE_GSF_OUTPUT) && defined(GSF_HAS_COMPRESSION_LEVEL)
0161 bool OutputFileHelper::writeChildFile(const char *childFileName, const char *str, const char compression_level)
0162 #else
0163 bool OutputFileHelper::writeChildFile(const char *childFileName, const char *str, const char)
0164 #endif
0165 {
0166     if (!m_impl->mpOutfile)
0167         return true;
0168 #ifdef USE_GSF_OUTPUT
0169     GsfOutput *child;
0170 #ifdef GSF_HAS_COMPRESSION_LEVEL
0171     if (0 != (child = gsf_outfile_new_child_full(m_impl->mpOutfile, childFileName, FALSE,"compression-level", compression_level, (void *)0)))
0172 #else
0173     if (0 != (child = gsf_outfile_new_child(m_impl->mpOutfile, childFileName, FALSE)))
0174 #endif
0175     {
0176         bool res = gsf_output_puts(child, str) &&
0177                    gsf_output_close(child);
0178         g_object_unref(child);
0179         return res;
0180     }
0181     return false;
0182 #else
0183     m_impl->mpOutfile->createEntry(childFileName, 0); // only storing without compressing works with FemtoZip
0184     if (m_impl->mpOutfile->errorCode())
0185         return false;
0186     m_impl->mpOutfile->writeString(str);
0187     if (m_impl->mpOutfile->errorCode())
0188         return false;
0189     m_impl->mpOutfile->closeEntry();
0190     if (m_impl->mpOutfile->errorCode())
0191         return false;
0192     return true;
0193 #endif
0194 }
0195 
0196 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */