1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * test-cp-msole.c: Test gsf-outfile-msole by cloning a file the hard way
5 * Copyright (C) 2002-2006 Jody Goldberg (jody@gnome.org)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of version 2.1 of the GNU Lesser General Public
9 * License as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
22 #include <gsf/gsf-utils.h>
24 #include <gsf/gsf-input-stdio.h>
25 #include <gsf/gsf-infile.h>
26 #include <gsf/gsf-infile-msole.h>
28 #include <gsf/gsf-output-stdio.h>
29 #include <gsf/gsf-outfile.h>
30 #include <gsf/gsf-outfile-msole.h>
34 static void clone_dir (GsfInfile *in, GsfOutfile *out);
37 clone (GsfInput *input, GsfOutput *output)
39 if (gsf_input_size (input) > 0) {
43 while ((len = gsf_input_remaining (input)) > 0) {
44 /* copy in odd sized chunks to exercise system */
47 if (NULL == (data = gsf_input_read (input, len, NULL))) {
48 g_warning ("error reading ?");
51 if (!gsf_output_write (output, len, data)) {
52 g_warning ("error writing ?");
56 } else if (GSF_IS_INFILE(input))
57 clone_dir (GSF_INFILE(input), GSF_OUTFILE(output));
59 gsf_output_close (output);
60 g_object_unref (G_OBJECT (output));
61 g_object_unref (G_OBJECT (input));
65 clone_dir (GsfInfile *in, GsfOutfile *out)
68 GsfOutput *new_output;
72 for (i = 0 ; i < gsf_infile_num_children (in) ; i++) {
73 new_input = gsf_infile_child_by_index (in, i);
75 /* In theory, if new_file is a regular file (not directory),
76 * it should be GsfInput only, not GsfInfile, as it is not
77 * structured. However, having each Infile define a 2nd class
78 * that only inherited from Input was cumbersome. So in
79 * practice, the convention is that new_input is always
80 * GsfInfile, but regular file is distinguished by having -1
83 is_dir = GSF_IS_INFILE (new_input) &&
84 gsf_infile_num_children (GSF_INFILE (new_input)) >= 0;
86 new_output = gsf_outfile_new_child (out,
87 gsf_infile_name_by_index (in, i),
90 clone (new_input, new_output);
92 /* An observation: when you think about the explanation to is_dir
93 * above, you realize that clone_dir is called even for regular files.
94 * But nothing bad happens, as the loop is never entered.
107 fprintf (stderr, "%s\n", argv [1]);
108 input = gsf_input_stdio_new (argv[1], &err);
110 g_return_val_if_fail (err != NULL, 1);
112 g_warning ("'%s' error: %s", argv[1], err->message);
117 infile = gsf_infile_msole_new (input, &err);
118 g_object_unref (G_OBJECT (input));
120 if (infile == NULL) {
121 g_return_val_if_fail (err != NULL, 1);
123 g_warning ("'%s' Not an OLE file: %s", argv[1], err->message);
128 output = gsf_output_stdio_new (argv[2], &err);
129 if (output == NULL) {
130 g_return_val_if_fail (err != NULL, 1);
132 g_warning ("'%s' error: %s", argv[2], err->message);
134 g_object_unref (G_OBJECT (infile));
138 outfile = gsf_outfile_msole_new (output);
139 g_object_unref (G_OBJECT (output));
140 clone (GSF_INPUT (infile), GSF_OUTPUT (outfile));
146 main (int argc, char *argv[])
151 fprintf (stderr, "%s : infile outfile\n", argv [0]);