1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*  This file is part of the GtkHTML library.
3  *
4  *  Copyright 1999, 2000 Helix Code, Inc.
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Library General Public
8  *  License as published by the Free Software Foundation; either
9  *  version 2 of the License, or (at your option) any later version.
10  *
11  *  This library 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 GNU
14  *  Library General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Library General Public License
17  *  along with this library; see the file COPYING.LIB.  If not, write to
18  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  *  Boston, MA 02110-1301, USA.
20 */
21 
22 #include <config.h>
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <glib.h>
26 #include <stdlib.h>
27 #include <string.h>
28 
29 #include "gtkhtml-compat.h"
30 #include "gtkhtml-stream.h"
31 
32 
33 GtkHTMLStream *
gtk_html_stream_new(GtkHTML * html,GtkHTMLStreamTypesFunc types_func,GtkHTMLStreamWriteFunc write_func,GtkHTMLStreamCloseFunc close_func,gpointer user_data)34 gtk_html_stream_new (GtkHTML *html,
35                      GtkHTMLStreamTypesFunc types_func,
36                      GtkHTMLStreamWriteFunc write_func,
37                      GtkHTMLStreamCloseFunc close_func,
38                      gpointer user_data)
39 {
40 	GtkHTMLStream *new_stream;
41 
42 	new_stream = g_new (GtkHTMLStream, 1);
43 
44 	new_stream->types_func = types_func;
45 	new_stream->write_func = write_func;
46 	new_stream->close_func = close_func;
47 	new_stream->user_data = user_data;
48 
49 	return new_stream;
50 }
51 
52 void
gtk_html_stream_destroy(GtkHTMLStream * stream)53 gtk_html_stream_destroy (GtkHTMLStream *stream)
54 {
55 	g_return_if_fail (stream != NULL);
56 
57 	g_free (stream);
58 }
59 
60 /**
61  * gtk_html_stream_write:
62  * @stream:
63  * @buffer:
64  * @size:
65  *
66  * Write data to a GtkHTMLStream.
67  *
68  */
69 void
gtk_html_stream_write(GtkHTMLStream * stream,const gchar * buffer,gsize size)70 gtk_html_stream_write (GtkHTMLStream *stream,
71                        const gchar *buffer,
72                        gsize size)
73 {
74 	g_return_if_fail (stream != NULL);
75 	g_return_if_fail (buffer != NULL);
76 	g_return_if_fail (size > 0);
77 
78 	if (stream->write_func != NULL)
79 		stream->write_func (stream, buffer, size, stream->user_data);
80 }
81 
82 gint
gtk_html_stream_vprintf(GtkHTMLStream * stream,const gchar * format,va_list ap)83 gtk_html_stream_vprintf (GtkHTMLStream *stream,
84                          const gchar *format,
85                          va_list ap)
86 {
87 	gsize len;
88 	gchar *buf = NULL;
89 	gchar *mbuf = NULL;
90 	gchar *result_string = NULL;
91 	gint rv;
92 	va_list ap_copy;
93 
94 	G_VA_COPY (ap_copy, ap);
95 
96 	result_string = g_strdup_vprintf (format, ap_copy);
97 	g_return_val_if_fail (result_string != NULL, 0);
98 
99 	len = strlen (result_string) + 1;
100 	g_free (result_string);
101 
102 	if (len < 8192)
103 		buf = alloca (len);
104 
105 	if (buf == NULL)
106 		buf = mbuf = g_malloc (len);
107 
108 	rv = vsprintf (buf, format, ap);
109 	gtk_html_stream_write (stream, buf, rv);
110 
111 	g_free (mbuf);
112 	return rv;
113 }
114 
115 gint
gtk_html_stream_printf(GtkHTMLStream * stream,const gchar * format,...)116 gtk_html_stream_printf (GtkHTMLStream *stream,
117                         const gchar *format,
118                         ...)
119 {
120 	va_list ap;
121 	gint rv;
122 
123 	va_start (ap, format);
124 	rv = gtk_html_stream_vprintf (stream, format, ap);
125 	va_end (ap);
126 
127 	return rv;
128 }
129 
130 void
gtk_html_stream_close(GtkHTMLStream * stream,GtkHTMLStreamStatus status)131 gtk_html_stream_close (GtkHTMLStream *stream,
132                        GtkHTMLStreamStatus status)
133 {
134 	g_return_if_fail (stream != NULL);
135 
136 	if (stream->close_func != NULL)
137 		stream->close_func (stream, status, stream->user_data);
138 
139 	gtk_html_stream_destroy (stream);
140 }
141 
142 gchar **
gtk_html_stream_get_types(GtkHTMLStream * stream)143 gtk_html_stream_get_types (GtkHTMLStream *stream)
144 {
145 	if (stream->types_func != NULL)
146 		return stream->types_func (stream, stream->user_data);
147 
148 	return NULL;
149 }
150 
151 typedef struct _GtkHTMLLog GtkHTMLLog;
152 struct _GtkHTMLLog {
153 	GtkHTMLStream *stream;
154 	FILE *file;
155 };
156 
157 static gchar **
stream_log_types(GtkHTMLStream * stream,gpointer user_data)158 stream_log_types (GtkHTMLStream *stream,
159                   gpointer user_data)
160 {
161 	GtkHTMLLog *log = user_data;
162 
163 	return gtk_html_stream_get_types (log->stream);
164 }
165 
166 static void
stream_log_write(GtkHTMLStream * stream,const gchar * buffer,gsize size,gpointer user_data)167 stream_log_write (GtkHTMLStream *stream,
168                const gchar *buffer,
169                gsize size,
170                gpointer user_data)
171 {
172 	GtkHTMLLog *log = user_data;
173 	gint i;
174 
175 	for (i = 0; i < size; i++)
176 		fprintf (log->file, "%c", buffer[i]);
177 
178 	gtk_html_stream_write (log->stream, buffer, size);
179 
180 }
181 
182 static void
stream_log_close(GtkHTMLStream * stream,GtkHTMLStreamStatus status,gpointer user_data)183 stream_log_close (GtkHTMLStream *stream,
184                GtkHTMLStreamStatus status,
185                gpointer user_data)
186 {
187 	GtkHTMLLog *log = user_data;
188 
189 	fclose (log->file);
190 	gtk_html_stream_close (log->stream, status);
191 
192 	g_free (log);
193 }
194 
195 GtkHTMLStream *
gtk_html_stream_log_new(GtkHTML * html,GtkHTMLStream * stream)196 gtk_html_stream_log_new (GtkHTML *html,
197                          GtkHTMLStream *stream)
198 {
199 	GtkHTMLLog *log;
200 	GtkHTMLStream *new_stream;
201 	gchar *fname;
202 	static gint log_num = 0;
203 
204 	log = g_new (GtkHTMLLog, 1);
205 	log->stream = stream;
206 
207 	fname = g_strdup_printf ("gtkhtml.log.%d.html", log_num);
208 	log->file = fopen (fname, "w+");
209 	g_free (fname);
210 
211 	log_num++;
212 
213 	new_stream = gtk_html_stream_new (html,
214 					  stream_log_types,
215 					  stream_log_write,
216 					  stream_log_close,
217 					  log);
218 
219 	return new_stream;
220 }
221 
222