1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * test-restore-msole.c: Create a msole file from a directory tree
4 *
5 * Copyright (C) 2002-2006 Jody Goldberg (jody@gnome.org)
6 *
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.
10 *
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.
15 *
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
19 * USA
20 */
21
22 #include <gsf/gsf-utils.h>
23
24 #include <gsf/gsf-input-stdio.h>
25 #include <gsf/gsf-infile.h>
26 #include <gsf/gsf-infile-stdio.h>
27
28 #include <gsf/gsf-output-stdio.h>
29 #include <gsf/gsf-outfile.h>
30 #include <gsf/gsf-outfile-msole.h>
31
32 #include <stdio.h>
33
34 static void
clone_(GsfInput * input,GsfOutput * output)35 clone_ (GsfInput *input, GsfOutput *output)
36 {
37 guint8 const *data;
38 size_t len;
39 int i;
40
41 if (gsf_input_size (input) > 0) {
42 while ((len = gsf_input_remaining (input)) > 0) {
43 /* copy in odd sized chunks to exercise system */
44 if (len > 314)
45 len = 314;
46 if (NULL == (data = gsf_input_read (input, len, NULL))) {
47 g_warning ("error reading ?");
48 return;
49 }
50 if (!gsf_output_write (output, len, data)) {
51 g_warning ("error writing ?");
52 return;
53 }
54 }
55 }
56
57 /* See test-cp-msole.c for explanation how to distinct directories
58 * from regular files.
59 */
60 if (GSF_IS_INFILE (input) &&
61 gsf_infile_num_children (GSF_INFILE (input)) > 0) {
62 GsfInfile *in = GSF_INFILE (input);
63 GsfOutfile *out = GSF_OUTFILE (output);
64 GsfInput *src;
65 GsfOutput *dst;
66 gboolean is_dir;
67
68 for (i = 0 ; i < gsf_infile_num_children (in) ; i++) {
69 src = gsf_infile_child_by_index (in, i);
70 is_dir = GSF_IS_INFILE (src) &&
71 gsf_infile_num_children (GSF_INFILE (src)) >= 0;
72 dst = gsf_outfile_new_child (out,
73 gsf_infile_name_by_index (in, i),
74 is_dir);
75 clone_ (src, dst);
76 }
77 }
78
79 gsf_output_close (output);
80 g_object_unref (G_OBJECT (output));
81 g_object_unref (G_OBJECT (input));
82 }
83
84 static int
test(char * argv[])85 test (char *argv[])
86 {
87 GsfInfile *infile;
88 GsfOutfile *outfile;
89 GsfOutput *output;
90 GError *err = NULL;
91
92 fprintf (stderr, "%s\n", argv [1]);
93 infile = gsf_infile_stdio_new (argv[1], &err);
94 if (infile == NULL) {
95 g_return_val_if_fail (err != NULL, 1);
96
97 g_warning ("'%s' error: %s", argv[1], err->message);
98 g_error_free (err);
99 return 1;
100 }
101
102 output = gsf_output_stdio_new (argv[2], &err);
103 if (output == NULL) {
104 g_return_val_if_fail (err != NULL, 1);
105
106 g_warning ("'%s' error: %s", argv[2], err->message);
107 g_error_free (err);
108 g_object_unref (G_OBJECT (infile));
109 return 1;
110 }
111
112 outfile = gsf_outfile_msole_new (output);
113 g_object_unref (G_OBJECT (output));
114 clone_ (GSF_INPUT (infile), GSF_OUTPUT (outfile));
115
116 return 0;
117 }
118
119 int
main(int argc,char * argv[])120 main (int argc, char *argv[])
121 {
122 int res;
123
124 if (argc != 3) {
125 fprintf (stderr, "%s : inputdir outfile\n", argv [0]);
126 return 1;
127 }
128
129 gsf_init ();
130 res = test (argv);
131 gsf_shutdown ();
132
133 return res;
134 }
135