1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * test-zip2.c:
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 #include <gsf/gsf-input-textline.h>
27 
28 #include <stdio.h>
29 #include <string.h>
30 
31 static void
dump_child(GsfInfile * infile,char const * childname)32 dump_child (GsfInfile *infile, char const *childname)
33 {
34 	GsfInput *child = gsf_infile_child_by_name (infile, childname);
35 	GsfInputTextline *textinput;
36 	unsigned char *res;
37 	int len = 0;
38 
39 	if (child == NULL) {
40 		printf ("not an OpenOffice document\n");
41 		return;
42 	}
43 
44 	textinput = (GsfInputTextline *)gsf_input_textline_new (child);
45 	if (textinput == NULL) {
46 		printf ("Could not read lines from %s",
47 			gsf_input_name (child));
48 		return;
49 	}
50 
51 	do {
52 		res = gsf_input_textline_ascii_gets (textinput);
53 		if (res) {
54 			printf ("'%s'\n", res);
55 			len += strlen (res) + 1;
56 		}
57 	} while (res);
58 
59 	printf ("Finished reading - %d bytes\n", len);
60 	g_object_unref (G_OBJECT (textinput));
61 	g_object_unref (G_OBJECT (child));
62 }
63 
64 static int
test(int argc,char * argv[])65 test (int argc, char *argv[])
66 {
67 	GsfInput  *input;
68 	GsfInfile *infile;
69 	GError    *err = NULL;
70 	int i;
71 
72 	puts (argv[1]);
73 	input = gsf_input_stdio_new (argv[1], &err);
74 	if (input == NULL) {
75 
76 		g_return_val_if_fail (err != NULL, 1);
77 
78 		g_warning ("'%s' error: %s", argv[1], err->message);
79 		g_error_free (err);
80 		return -1;
81 	}
82 
83 	infile = gsf_infile_zip_new (input, &err);
84 	if (infile == NULL) {
85 
86 		g_return_val_if_fail (err != NULL, 1);
87 
88 		g_warning ("'%s' Not a Zip file: %s", argv[1], err->message);
89 		g_error_free (err);
90 		g_object_unref (G_OBJECT (input));
91 		return -1;
92 	}
93 
94 	for (i = 2 ; i < argc ; i++) {
95 		dump_child (infile, argv[i]);
96 	}
97 	g_object_unref (G_OBJECT (infile));
98 	g_object_unref (G_OBJECT (input));
99 
100 	return 0;
101 }
102 
103 int
main(int argc,char * argv[])104 main (int argc, char *argv[])
105 {
106 	int res;
107 
108 	gsf_init ();
109 	res = test (argc, argv);
110 	gsf_shutdown ();
111 
112 	return res;
113 }
114