"Initial commit to Gerrit"
[profile/ivi/libgsf.git] / tests / test-zip-out-subdirs.c
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * test-zip-out.c:
4  *
5  * Copyright (C) 2002-2006 Jon K Hellan (hellan@acm.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 #include <gsf/gsf-output-stdio.h>
24 #include <gsf/gsf-outfile.h>
25 #include <gsf/gsf-outfile-zip.h>
26
27 #include <stdio.h>
28 #include <string.h>
29
30 static gboolean
31 test_write_once (GsfOutput *output)
32 {
33         char const *str = "The cat sat on the mat. 2 cats sat on the mat. The quick brown fox is afraid of the cats.\n";
34
35         return gsf_output_write (output, strlen (str), str);
36 }
37
38 static int
39 test (int argc, char *argv[])
40 {
41         GsfOutput  *output;
42         GsfOutfile *outfile, *dir1, *dir2;
43         GsfOutput  *ch1, *ch2, *ch3;
44         GError   *err = NULL;
45
46         if (argc != 2) {
47                 fprintf (stderr, "Usage : %s outfile\n", argv[0]);
48                 return 1;
49         }
50
51         output = gsf_output_stdio_new (argv[1], &err);
52         if (output == NULL) {
53                 g_return_val_if_fail (err != NULL, 1);
54
55                 g_warning ("'%s' error: %s", argv[1], err->message);
56                 g_error_free (err);
57                 return 1;
58         }
59         outfile = gsf_outfile_zip_new (output, &err);
60         if (output == NULL) {
61                 g_return_val_if_fail (err != NULL, 1);
62
63                 g_warning ("'%s' error: %s",
64                            "gsf_outfile_zip_new", err->message);
65                 g_error_free (err);
66                 return 1;
67         }
68         g_object_unref (G_OBJECT (output));
69         dir1 = GSF_OUTFILE (gsf_outfile_new_child  (outfile, "test", TRUE));
70         ch1  = gsf_outfile_new_child  (dir1, "portfolio.abw", FALSE);
71         ch2  = gsf_outfile_new_child  (dir1, "test.abw", FALSE);
72         dir2 = GSF_OUTFILE (gsf_outfile_new_child  (dir1, "1", TRUE));
73         if (!gsf_output_close ((GsfOutput *) dir1))
74                 return 1;
75         ch3  = gsf_outfile_new_child  (dir2, "simply.pve", FALSE);
76         
77         if (!test_write_once (ch1))
78                 return 1;
79         if (!gsf_output_close ((GsfOutput *) ch1))
80                 return 1;
81         if (!test_write_once (ch2))
82                 return 1;
83         if (!gsf_output_close ((GsfOutput *) ch2))
84                 return 1;
85         if (!test_write_once (ch3))
86                 return 1;
87         if (!gsf_output_close ((GsfOutput *) ch3))
88                 return 1;
89         if (!gsf_output_close ((GsfOutput *) dir2))
90                 return 1;
91         if (!gsf_output_close ((GsfOutput *) outfile))
92                 return 1;
93         g_object_unref (G_OBJECT (outfile));
94         g_object_unref (ch1);
95         g_object_unref (ch2);
96         g_object_unref (ch3);
97         g_object_unref (dir1);
98         g_object_unref (dir2);
99
100         return 0;
101 }
102
103 int
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 }