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
|
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* test-cp-msole.c: Test gsf-outfile-msole by cloning a file the hard way
*
* Copyright (C) 2002-2006 Jody Goldberg (jody@gnome.org)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2.1 of the GNU Lesser General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
* USA
*/
#include <gsf/gsf-utils.h>
#include <gsf/gsf-input-stdio.h>
#include <gsf/gsf-infile.h>
#include <gsf/gsf-infile-msole.h>
#include <gsf/gsf-output-stdio.h>
#include <gsf/gsf-outfile.h>
#include <gsf/gsf-outfile-msole.h>
#include <stdio.h>
static void clone_dir (GsfInfile *in, GsfOutfile *out);
static void
clone (GsfInput *input, GsfOutput *output)
{
if (gsf_input_size (input) > 0) {
guint8 const *data;
size_t len;
while ((len = gsf_input_remaining (input)) > 0) {
/* copy in odd sized chunks to exercise system */
if (len > 314)
len = 314;
if (NULL == (data = gsf_input_read (input, len, NULL))) {
g_warning ("error reading ?");
return;
}
if (!gsf_output_write (output, len, data)) {
g_warning ("error writing ?");
return;
}
}
} else if (GSF_IS_INFILE(input))
clone_dir (GSF_INFILE(input), GSF_OUTFILE(output));
gsf_output_close (output);
g_object_unref (G_OBJECT (output));
g_object_unref (G_OBJECT (input));
}
static void
clone_dir (GsfInfile *in, GsfOutfile *out)
{
GsfInput *new_input;
GsfOutput *new_output;
gboolean is_dir;
int i;
for (i = 0 ; i < gsf_infile_num_children (in) ; i++) {
new_input = gsf_infile_child_by_index (in, i);
/* In theory, if new_file is a regular file (not directory),
* it should be GsfInput only, not GsfInfile, as it is not
* structured. However, having each Infile define a 2nd class
* that only inherited from Input was cumbersome. So in
* practice, the convention is that new_input is always
* GsfInfile, but regular file is distinguished by having -1
* children.
*/
is_dir = GSF_IS_INFILE (new_input) &&
gsf_infile_num_children (GSF_INFILE (new_input)) >= 0;
new_output = gsf_outfile_new_child (out,
gsf_infile_name_by_index (in, i),
is_dir);
clone (new_input, new_output);
}
/* An observation: when you think about the explanation to is_dir
* above, you realize that clone_dir is called even for regular files.
* But nothing bad happens, as the loop is never entered.
*/
}
static int
test (char *argv[])
{
GsfInput *input;
GsfInfile *infile;
GsfOutput *output;
GsfOutfile *outfile;
GError *err = NULL;
fprintf (stderr, "%s\n", argv [1]);
input = gsf_input_stdio_new (argv[1], &err);
if (input == NULL) {
g_return_val_if_fail (err != NULL, 1);
g_warning ("'%s' error: %s", argv[1], err->message);
g_error_free (err);
return 1;
}
infile = gsf_infile_msole_new (input, &err);
g_object_unref (G_OBJECT (input));
if (infile == NULL) {
g_return_val_if_fail (err != NULL, 1);
g_warning ("'%s' Not an OLE file: %s", argv[1], err->message);
g_error_free (err);
return 1;
}
output = gsf_output_stdio_new (argv[2], &err);
if (output == NULL) {
g_return_val_if_fail (err != NULL, 1);
g_warning ("'%s' error: %s", argv[2], err->message);
g_error_free (err);
g_object_unref (G_OBJECT (infile));
return 1;
}
outfile = gsf_outfile_msole_new (output);
g_object_unref (G_OBJECT (output));
clone (GSF_INPUT (infile), GSF_OUTPUT (outfile));
return 0;
}
int
main (int argc, char *argv[])
{
int res;
if (argc != 3) {
fprintf (stderr, "%s : infile outfile\n", argv [0]);
return 1;
}
gsf_init ();
res = test (argv);
gsf_shutdown ();
return res;
}
|