File: OutputFileHelper.cxx

package info (click to toggle)
calligra 1:2.8.5+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 397,516 kB
  • ctags: 185,064
  • sloc: cpp: 1,505,875; python: 45,987; ansic: 28,421; xml: 26,472; sh: 23,986; java: 18,214; objc: 3,965; makefile: 2,816; perl: 2,792; yacc: 2,601; lex: 1,410; sql: 903; ruby: 737; asm: 236; lisp: 121
file content (244 lines) | stat: -rw-r--r-- 6,148 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* writerperfect
 * Version: MPL 2.0 / LGPLv2.1+
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Major Contributor(s):
 * Copyright (C) 2007 Fridrich Strba (fridrich.strba@bluewin.ch)
 *
 * For minor contributions see the git repository.
 *
 * Alternatively, the contents of this file may be used under the terms
 * of the GNU Lesser General Public License Version 2.1 or later
 * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
 * applicable instead of those above.
 *
 * For further information visit http://libwpd.sourceforge.net
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "OutputFileHelper.hxx"

#ifdef USE_GSF_OUTPUT
#include <gsf/gsf-utils.h>
#include <gsf/gsf-output-stdio.h>
#include <gsf/gsf-outfile.h>
#include <gsf/gsf-outfile-zip.h>
#include <gsf/gsf-input-stdio.h>
#else
#include "FemtoZip.hxx"
#endif

#include "DiskDocumentHandler.hxx"
#include "StdOutHandler.hxx"
#include <librevenge-stream/librevenge-stream.h>

struct OutputFileHelperImpl
{
	OutputFileHelperImpl(const char *password) : mpOutfile(0), mpPassword(password) {}
#ifdef USE_GSF_OUTPUT
	GsfOutfile *mpOutfile;
#else
	FemtoZip *mpOutfile;
#endif
	const char *mpPassword;
private:
	OutputFileHelperImpl(OutputFileHelperImpl const &);
	OutputFileHelperImpl &operator=(OutputFileHelperImpl const &);
};


OutputFileHelper::OutputFileHelper(const char *outFileName, const char *password) :
#ifdef USE_GSF_OUTPUT
	m_impl(new OutputFileHelperImpl(password))
#else
	m_impl(new OutputFileHelperImpl(password))
#endif
{
	m_impl->mpOutfile = 0;
#ifdef USE_GSF_OUTPUT
	GsfOutput  *pOutput = 0;
	GError   *err = 0;

	gsf_init ();

	if (!outFileName)
		pOutput = 0;
	else
	{
		pOutput = GSF_OUTPUT(gsf_output_stdio_new (outFileName, &err));
		if (pOutput == 0)
		{
			if (err)
			{
				g_warning ("'%s' error: %s", outFileName, err->message);
				g_error_free (err);
			}
			gsf_shutdown ();
		}
		else
		{
			if (err)
				g_error_free (err);
			err = 0;
			m_impl->mpOutfile = GSF_OUTFILE(gsf_outfile_zip_new (pOutput, &err));
			if (m_impl->mpOutfile == 0)
			{
				if (err)
				{
					g_warning ("'%s' error: %s",
					           "gsf_outfile_zip_new", err->message);
					g_error_free (err);
				}
				gsf_shutdown ();
			}
			else
			{
				if (err)
					g_error_free (err);
				err = 0;
				g_object_unref (pOutput);
			}
		}
	}
#else
	if (outFileName)
		m_impl->mpOutfile = new FemtoZip(outFileName);
#endif
}

OutputFileHelper::~OutputFileHelper()
{
#ifdef USE_GSF_OUTPUT
	if (m_impl->mpOutfile && !gsf_output_close ((GsfOutput *) m_impl->mpOutfile))
		fprintf(stderr, "ERROR : Couldn't close outfile\n");

	if (m_impl->mpOutfile)
		g_object_unref (m_impl->mpOutfile);

	gsf_shutdown ();
#else
	if (m_impl->mpOutfile)
		delete m_impl->mpOutfile;
#endif
	if (m_impl)
		delete m_impl;
}

bool OutputFileHelper::writeChildFile(const char *childFileName, const char *str)
{
	if (!m_impl->mpOutfile)
		return true;
#ifdef USE_GSF_OUTPUT
	GsfOutput *child;
	if (0 != (child = gsf_outfile_new_child  (m_impl->mpOutfile, childFileName, FALSE)))
	{
		bool res = gsf_output_puts (child, str) &&
		           gsf_output_close (child);
		g_object_unref (child);
		return res;
	}
	return false;
#else
	m_impl->mpOutfile->createEntry(childFileName, 0);
	if (m_impl->mpOutfile->errorCode())
		return false;
	m_impl->mpOutfile->writeString(str);
	if (m_impl->mpOutfile->errorCode())
		return false;
	m_impl->mpOutfile->closeEntry();
	if (m_impl->mpOutfile->errorCode())
		return false;
	return true;
#endif
}

#if defined(USE_GSF_OUTPUT) && defined(GSF_HAS_COMPRESSION_LEVEL)
bool OutputFileHelper::writeChildFile(const char *childFileName, const char *str, const char compression_level)
#else
bool OutputFileHelper::writeChildFile(const char *childFileName, const char *str, const char )
#endif
{
	if (!m_impl->mpOutfile)
		return true;
#ifdef USE_GSF_OUTPUT
	GsfOutput *child;
#ifdef GSF_HAS_COMPRESSION_LEVEL
	if (0 != (child = gsf_outfile_new_child_full  (m_impl->mpOutfile, childFileName, FALSE,"compression-level", compression_level, (void *)0)))
#else
	if (0 != (child = gsf_outfile_new_child  (m_impl->mpOutfile, childFileName, FALSE)))
#endif
	{
		bool res = gsf_output_puts (child, str) &&
		           gsf_output_close (child);
		g_object_unref (child);
		return res;
	}
	return false;
#else
	m_impl->mpOutfile->createEntry(childFileName, 0); // only storing without compressing works with FemtoZip
	if (m_impl->mpOutfile->errorCode())
		return false;
	m_impl->mpOutfile->writeString(str);
	if (m_impl->mpOutfile->errorCode())
		return false;
	m_impl->mpOutfile->closeEntry();
	if (m_impl->mpOutfile->errorCode())
		return false;
	return true;
#endif
}

bool OutputFileHelper::writeConvertedContent(const char *childFileName, const char *inFileName, const OdfStreamType streamType)
{
	librevenge::RVNGFileStream input(inFileName);

	if (!_isSupportedFormat(&input, m_impl->mpPassword))
		return false;

	input.seek(0, librevenge::RVNG_SEEK_SET);

	OdfDocumentHandler *pHandler;
#ifdef USE_GSF_OUTPUT
	GsfOutput *pContentChild = 0;
	if (m_impl->mpOutfile)
	{
		pContentChild = gsf_outfile_new_child(m_impl->mpOutfile, childFileName, FALSE);
		pHandler = new DiskOdfDocumentHandler(pContentChild); // WLACH_REFACTORING: rename to DiskHandler
#else
	if (m_impl->mpOutfile)
	{
		m_impl->mpOutfile->createEntry(childFileName, 0);
		if (m_impl->mpOutfile->errorCode())
			return false;
		pHandler = new DiskOdfDocumentHandler(m_impl->mpOutfile);
#endif
	}
	else
		pHandler = new StdOutHandler();

	bool bRetVal = _convertDocument(&input, m_impl->mpPassword, pHandler, m_impl->mpOutfile ? streamType : ODF_FLAT_XML);

#ifdef USE_GSF_OUTPUT
	if (pContentChild)
	{
		gsf_output_close(pContentChild);
		g_object_unref(G_OBJECT (pContentChild));
	}

#else
	if (m_impl->mpOutfile)
		m_impl->mpOutfile->closeEntry();
#endif
	delete pHandler;

	return bRetVal;
}

/* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */