"Initial commit to Gerrit"
[profile/ivi/libgsf.git] / tests / test-cp-msole.c
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * test-cp-msole.c: Test gsf-outfile-msole by cloning a file the hard way
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-msole.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_dir (GsfInfile *in, GsfOutfile *out);
35
36 static void
37 clone (GsfInput *input, GsfOutput *output)
38 {
39         if (gsf_input_size (input) > 0) {
40                 guint8 const *data;
41                 size_t len;
42
43                 while ((len = gsf_input_remaining (input)) > 0) {
44                         /* copy in odd sized chunks to exercise system */
45                         if (len > 314)
46                                 len = 314;
47                         if (NULL == (data = gsf_input_read (input, len, NULL))) {
48                                 g_warning ("error reading ?");
49                                 return;
50                         }
51                         if (!gsf_output_write (output, len, data)) {
52                                 g_warning ("error writing ?");
53                                 return;
54                         }
55                 }
56         } else if (GSF_IS_INFILE(input))
57                 clone_dir (GSF_INFILE(input), GSF_OUTFILE(output));
58
59         gsf_output_close (output);
60         g_object_unref (G_OBJECT (output));
61         g_object_unref (G_OBJECT (input));
62 }
63
64 static void
65 clone_dir (GsfInfile *in, GsfOutfile *out)
66 {
67         GsfInput *new_input;
68         GsfOutput *new_output;
69         gboolean is_dir;
70         int i;
71
72         for (i = 0 ; i < gsf_infile_num_children (in) ; i++) {
73                 new_input = gsf_infile_child_by_index (in, i);
74
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
81                  * children.
82                  */
83                 is_dir = GSF_IS_INFILE (new_input) &&
84                         gsf_infile_num_children (GSF_INFILE (new_input)) >= 0;
85
86                 new_output = gsf_outfile_new_child  (out,
87                                 gsf_infile_name_by_index  (in, i),
88                                 is_dir);
89
90                 clone (new_input, new_output);
91         }
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.
95          */
96 }
97
98 static int
99 test (char *argv[])
100 {
101         GsfInput   *input;
102         GsfInfile  *infile;
103         GsfOutput  *output;
104         GsfOutfile *outfile;
105         GError    *err = NULL;
106
107         fprintf (stderr, "%s\n", argv [1]);
108         input = gsf_input_stdio_new (argv[1], &err);
109         if (input == NULL) {
110                 g_return_val_if_fail (err != NULL, 1);
111
112                 g_warning ("'%s' error: %s", argv[1], err->message);
113                 g_error_free (err);
114                 return 1;
115         }
116
117         infile = gsf_infile_msole_new (input, &err);
118         g_object_unref (G_OBJECT (input));
119
120         if (infile == NULL) {
121                 g_return_val_if_fail (err != NULL, 1);
122
123                 g_warning ("'%s' Not an OLE file: %s", argv[1], err->message);
124                 g_error_free (err);
125                 return 1;
126         }
127
128         output = gsf_output_stdio_new (argv[2], &err);
129         if (output == NULL) {
130                 g_return_val_if_fail (err != NULL, 1);
131
132                 g_warning ("'%s' error: %s", argv[2], err->message);
133                 g_error_free (err);
134                 g_object_unref (G_OBJECT (infile));
135                 return 1;
136         }
137
138         outfile = gsf_outfile_msole_new (output);
139         g_object_unref (G_OBJECT (output));
140         clone (GSF_INPUT (infile), GSF_OUTPUT (outfile));
141
142         return 0;
143 }
144
145 int
146 main (int argc, char *argv[])
147 {
148         int res;
149
150         if (argc != 3) {
151                 fprintf (stderr, "%s : infile outfile\n", argv [0]);
152                 return 1;
153         }
154
155         gsf_init ();
156         res = test (argv);
157         gsf_shutdown ();
158
159         return res;
160 }