"Initial commit to Gerrit"
[profile/ivi/libgsf.git] / tests / test-cp-zip.c
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * test-cp-zip.c: Test gsf-outfile-zip 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-zip.h>
27
28 #include <gsf/gsf-output-stdio.h>
29 #include <gsf/gsf-outfile.h>
30 #include <gsf/gsf-outfile-zip.h>
31
32 #include <stdio.h>
33
34 static void
35 clone (GsfInfile *in, GsfOutfile *out)
36 {
37         GsfInput *input = GSF_INPUT (in);
38         GsfOutput *output = GSF_OUTPUT (out);
39
40         if (gsf_input_size (input) > 0) {
41                 size_t len;
42                 while ((len = gsf_input_remaining (input)) > 0) {
43                         guint8 const *data;
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                                 break;
50                         }
51                         if (!gsf_output_write (output, len, data)) {
52                                 g_warning ("error writing ?");
53                                 break;
54                         }
55                 }
56         } else {
57                 int i, n = gsf_infile_num_children (in);
58                 for (i = 0 ; i < n; i++) {
59                         int level;
60                         gboolean is_dir;
61                         char const *name = gsf_infile_name_by_index (in, i);
62                         char *display_name = name
63                                 ? g_filename_display_name (name)
64                                 : NULL;
65
66                         input = gsf_infile_child_by_index (in, i);
67                         if (NULL == input) {
68                                 g_print ("Error opening '%s, index = %d\n",
69                                          display_name ? display_name : "?", i);
70                                 g_free (display_name);
71                                 continue;
72                         }
73
74                         is_dir = gsf_infile_num_children (GSF_INFILE (input)) >= 0;
75
76                         g_object_get (G_OBJECT (input), "compression-level", &level, NULL);
77
78                         g_print ("%s: size=%ld, level=%d, %s\n",
79                                  display_name ? display_name : "?",
80                                  (long)gsf_input_size (input),
81                                  level,
82                                  is_dir ? "directory" : "file");
83                         g_free (display_name);
84
85                         output = gsf_outfile_new_child_full  (out, name, is_dir,
86                                                               "compression-level", level,
87                                                               NULL);
88                         clone (GSF_INFILE (input), GSF_OUTFILE (output));
89                 }
90         }
91         gsf_output_close (GSF_OUTPUT (out));
92         g_object_unref (G_OBJECT (out));
93         g_object_unref (G_OBJECT (in));
94 }
95
96 static int
97 test (char *argv[])
98 {
99         GsfInput   *input;
100         GsfInfile  *infile;
101         GsfOutput  *output;
102         GsfOutfile *outfile;
103         GError    *err = NULL;
104
105         fprintf (stderr, "%s\n", argv [1]);
106         input = gsf_input_stdio_new (argv[1], &err);
107         if (input == NULL) {
108
109                 g_return_val_if_fail (err != NULL, 1);
110
111                 g_warning ("'%s' error: %s", argv[1], err->message);
112                 g_error_free (err);
113                 return 1;
114         }
115
116         infile = gsf_infile_zip_new (input, &err);
117         g_object_unref (G_OBJECT (input));
118
119         if (infile == NULL) {
120                 g_return_val_if_fail (err != NULL, 1);
121
122                 g_warning ("'%s' Not a zip file: %s", argv[1], err->message);
123                 g_error_free (err);
124                 return 1;
125         }
126
127         output = gsf_output_stdio_new (argv[2], &err);
128         if (output == NULL) {
129
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_zip_new (output, &err);
139         g_object_unref (G_OBJECT (output));
140         clone (infile, 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 }