"Initial commit to Gerrit"
[profile/ivi/libgsf.git] / tests / test-cat-zip.c
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * test-cat-zip.c: test program to list content and dump raw stream data
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-input-stdio.h>
23 #include <gsf/gsf-utils.h>
24 #include <gsf/gsf-infile.h>
25 #include <gsf/gsf-infile-zip.h>
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <ctype.h>
30
31 static void
32 ls_R (GsfInput *input)
33 {
34         int i;
35         char const *name = gsf_input_name (GSF_INPUT (input));
36         gboolean is_dir = GSF_IS_INFILE (input) &&
37                 (gsf_infile_num_children (GSF_INFILE (input)) >= 0);
38         
39         printf ("%c '%s'\t\t%" GSF_OFF_T_FORMAT "\n",
40                 (is_dir ? 'd' : ' '),
41                 (name != NULL) ? name : "",
42                 gsf_input_size (GSF_INPUT (input)));
43
44         if (is_dir) {
45                 puts ("{");
46                 for (i = 0 ; i < gsf_infile_num_children (GSF_INFILE (input)) ; i++)
47                         ls_R (gsf_infile_child_by_index (GSF_INFILE (input), i));
48                 puts ("}");
49         }
50
51         g_object_unref (G_OBJECT (input));
52 }
53
54 static int
55 test (int argc, char *argv[])
56 {
57         GsfInput  *input;
58         GsfInfile *infile;
59         GError    *err = NULL;
60
61         fprintf (stderr, "%s\n", argv [1]);
62         input = gsf_input_stdio_new (argv[1], &err);
63         if (input == NULL) {
64
65                 g_return_val_if_fail (err != NULL, 1);
66
67                 g_warning ("'%s' error: %s", argv[1], err->message);
68                 g_error_free (err);
69                 return 1;
70         }
71
72         input = gsf_input_uncompress (input);
73         infile = gsf_infile_zip_new (input, &err);
74         g_object_unref (G_OBJECT (input));
75
76         if (infile == NULL) {
77                 g_return_val_if_fail (err != NULL, 1);
78
79                 g_warning ("'%s' Not an OLE file: %s", argv[1], err->message);
80                 g_error_free (err);
81                 return 1;
82         }
83
84         if (argc > 2) {
85                 int i;
86                 GsfInput *child, *ptr = GSF_INPUT (infile);
87                 for (i = 2 ; i < argc && ptr != NULL; i++, ptr = child) {
88                         fprintf (stderr, "--> '%s'\n", argv [i]);
89                         if (GSF_IS_INFILE (ptr) &&
90                             gsf_infile_num_children (GSF_INFILE (ptr)) >= 0) {
91                                 child = gsf_infile_child_by_name (GSF_INFILE (ptr), argv [i]);
92
93                                 if (child == NULL) {
94                                         g_warning ("No child named '%s'", argv [i]);
95                                         child = NULL;
96                                         break;
97                                 }
98                         } else {
99                                 g_warning ("stream is not a directory '%s'", argv [i]);
100                                 child = NULL;
101                                 break;
102                         }
103                         g_object_unref (G_OBJECT (ptr));
104                 }
105                 if (ptr != NULL) {
106                         if (GSF_IS_INFILE (ptr) &&
107                             gsf_infile_num_children (GSF_INFILE (ptr)) >= 0)
108                                 ls_R (ptr); /* unrefs infile */
109                         else {
110                                 gsf_input_dump (GSF_INPUT (ptr), FALSE);
111                                 g_object_unref (G_OBJECT (ptr));
112                         }
113                 }
114         } else
115                 ls_R (GSF_INPUT (infile)); /* unrefs infile */
116
117         return 0;
118 }
119
120 int
121 main (int argc, char *argv[])
122 {
123         int res;
124
125         if (argc < 2) {
126                 fprintf (stderr, "%s : file stream stream ...\n", argv [0]);
127                 return 1;
128         }
129
130         gsf_init ();
131         res = test (argc, argv);
132         gsf_shutdown ();
133
134         return res;
135 }