1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * test-outmem-printf.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-memory.h>
24 
25 #include <stdio.h>
26 
27 static gboolean
test_write_once(GsfOutput * output)28 test_write_once (GsfOutput *output)
29 {
30 	if (!gsf_output_printf (output,
31 				"The %s sat on the %s.\n", "cat", "mat"))
32 		return FALSE;
33 	if (!gsf_output_printf (output, "%d %ss sat on the %s.\n",
34 				2, "cat", "mat"))
35 		return FALSE;
36 	if (!gsf_output_puts (output,
37 			      "The quick brown fox is afraid of the cats.\n"))
38 		return FALSE;
39 
40 	return TRUE;
41 }
42 
43 static int
test(int argc,char * argv[])44 test (int argc, char *argv[])
45 {
46 	GsfOutput  *output;
47 	guint8 const *buf;
48 	gsf_off_t size;
49 	FILE *fout;
50 	int i;
51 	int res;
52 
53 	if (argc != 2) {
54 		fprintf (stderr, "Usage : %s outfile\n", argv[0]);
55 		return 1;
56 	}
57 
58 	fout = fopen (argv[1], "w");
59 	if (!fout) {
60 		perror (argv[1]);
61 		return 1;
62 	}
63 
64 	output = gsf_output_memory_new ();
65 	for (i = 1; i <= 100; i++) {
66 		if (!gsf_output_printf (output, "=== Round %d ===\n", i)) {
67 			res = 1;
68 			goto out;
69 		}
70 		if (!test_write_once (output)) {
71 			res = 1;
72 			goto out;
73 		}
74 	}
75 
76 	buf = gsf_output_memory_get_bytes (GSF_OUTPUT_MEMORY (output));
77 	size = gsf_output_size (output);
78 	res = fwrite (buf, size, 1, fout) == 1 ? 0 : 1;
79 
80  out:
81 	fclose (fout);
82 	gsf_output_close (output);
83 	g_object_unref (output);
84 	return res;
85 }
86 
87 int
main(int argc,char * argv[])88 main (int argc, char *argv[])
89 {
90 	int res;
91 
92 	gsf_init ();
93 	res = test (argc, argv);
94 	gsf_shutdown ();
95 
96 	return res;
97 }
98