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., 59 Temple Place - Suite 330, |
---|
19 | Boston, MA 02111-1307, 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 | |
---|
28 | #include "gtkhtml-compat.h" |
---|
29 | #include "gtkhtml-stream.h" |
---|
30 | |
---|
31 | |
---|
32 | GtkHTMLStream * |
---|
33 | gtk_html_stream_new (GtkHTML *html, |
---|
34 | GtkHTMLStreamTypesFunc types_func, |
---|
35 | GtkHTMLStreamWriteFunc write_func, |
---|
36 | GtkHTMLStreamCloseFunc close_func, |
---|
37 | gpointer user_data) |
---|
38 | { |
---|
39 | GtkHTMLStream *new_stream; |
---|
40 | |
---|
41 | new_stream = g_new (GtkHTMLStream, 1); |
---|
42 | |
---|
43 | new_stream->types_func = types_func; |
---|
44 | new_stream->write_func = write_func; |
---|
45 | new_stream->close_func = close_func; |
---|
46 | new_stream->user_data = user_data; |
---|
47 | |
---|
48 | return new_stream; |
---|
49 | } |
---|
50 | |
---|
51 | void |
---|
52 | gtk_html_stream_destroy (GtkHTMLStream *stream) |
---|
53 | { |
---|
54 | g_return_if_fail (stream != NULL); |
---|
55 | |
---|
56 | g_free (stream); |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | * gtk_html_stream_write: |
---|
61 | * @stream: |
---|
62 | * @buffer: |
---|
63 | * @size: |
---|
64 | * |
---|
65 | * Write data to a GtkHTMLStream. |
---|
66 | * |
---|
67 | */ |
---|
68 | void |
---|
69 | gtk_html_stream_write (GtkHTMLStream *stream, |
---|
70 | const gchar *buffer, |
---|
71 | size_t size) |
---|
72 | { |
---|
73 | g_return_if_fail (stream != NULL); |
---|
74 | g_return_if_fail (buffer != NULL); |
---|
75 | g_return_if_fail (size > 0); |
---|
76 | |
---|
77 | if (stream->write_func != NULL) |
---|
78 | stream->write_func (stream, buffer, size, stream->user_data); |
---|
79 | } |
---|
80 | |
---|
81 | int |
---|
82 | gtk_html_stream_vprintf (GtkHTMLStream *stream, |
---|
83 | const char *format, |
---|
84 | va_list ap) |
---|
85 | { |
---|
86 | size_t len; |
---|
87 | char *buf = NULL; |
---|
88 | char *mbuf = NULL; |
---|
89 | int rv; |
---|
90 | va_list ap_copy; |
---|
91 | |
---|
92 | G_VA_COPY (ap_copy, ap); |
---|
93 | len = g_printf_string_upper_bound (format, ap_copy); |
---|
94 | |
---|
95 | if (len < 8192) |
---|
96 | buf = alloca (len); |
---|
97 | |
---|
98 | if (buf == NULL) |
---|
99 | buf = mbuf = g_malloc (len); |
---|
100 | |
---|
101 | rv = vsprintf (buf, format, ap); |
---|
102 | gtk_html_stream_write (stream, buf, rv); |
---|
103 | |
---|
104 | g_free (mbuf); |
---|
105 | return rv; |
---|
106 | } |
---|
107 | |
---|
108 | int |
---|
109 | gtk_html_stream_printf (GtkHTMLStream *stream, |
---|
110 | const char *format, |
---|
111 | ...) |
---|
112 | { |
---|
113 | va_list ap; |
---|
114 | int rv; |
---|
115 | |
---|
116 | va_start (ap, format); |
---|
117 | rv = gtk_html_stream_vprintf (stream, format, ap); |
---|
118 | va_end (ap); |
---|
119 | |
---|
120 | return rv; |
---|
121 | } |
---|
122 | |
---|
123 | void |
---|
124 | gtk_html_stream_close (GtkHTMLStream *stream, |
---|
125 | GtkHTMLStreamStatus status) |
---|
126 | { |
---|
127 | g_return_if_fail (stream != NULL); |
---|
128 | |
---|
129 | if (stream->close_func != NULL) |
---|
130 | stream->close_func (stream, status, stream->user_data); |
---|
131 | |
---|
132 | gtk_html_stream_destroy (stream); |
---|
133 | } |
---|
134 | |
---|
135 | char ** |
---|
136 | gtk_html_stream_get_types (GtkHTMLStream *stream) |
---|
137 | { |
---|
138 | if (stream->types_func != NULL) |
---|
139 | return stream->types_func (stream, stream->user_data); |
---|
140 | |
---|
141 | return NULL; |
---|
142 | } |
---|
143 | |
---|
144 | typedef struct _GtkHTMLLog GtkHTMLLog; |
---|
145 | struct _GtkHTMLLog { |
---|
146 | GtkHTMLStream *stream; |
---|
147 | FILE *file; |
---|
148 | }; |
---|
149 | |
---|
150 | static char ** |
---|
151 | stream_log_types (GtkHTMLStream *stream, |
---|
152 | gpointer user_data) |
---|
153 | { |
---|
154 | GtkHTMLLog *log = user_data; |
---|
155 | |
---|
156 | return gtk_html_stream_get_types (log->stream); |
---|
157 | } |
---|
158 | |
---|
159 | static void |
---|
160 | stream_log_write (GtkHTMLStream *stream, |
---|
161 | const gchar *buffer, |
---|
162 | size_t size, |
---|
163 | gpointer user_data) |
---|
164 | { |
---|
165 | GtkHTMLLog *log = user_data; |
---|
166 | gint i; |
---|
167 | |
---|
168 | for (i=0; i<size; i++) |
---|
169 | fprintf (log->file, "%c", buffer [i]); |
---|
170 | |
---|
171 | gtk_html_stream_write (log->stream, buffer, size); |
---|
172 | |
---|
173 | } |
---|
174 | |
---|
175 | static void |
---|
176 | stream_log_close (GtkHTMLStream *stream, |
---|
177 | GtkHTMLStreamStatus status, |
---|
178 | gpointer user_data) |
---|
179 | { |
---|
180 | GtkHTMLLog *log = user_data; |
---|
181 | |
---|
182 | fclose (log->file); |
---|
183 | gtk_html_stream_close (log->stream, status); |
---|
184 | |
---|
185 | g_free (log); |
---|
186 | } |
---|
187 | |
---|
188 | GtkHTMLStream * |
---|
189 | gtk_html_stream_log_new (GtkHTML *html, GtkHTMLStream *stream) |
---|
190 | { |
---|
191 | GtkHTMLLog *log; |
---|
192 | GtkHTMLStream *new_stream; |
---|
193 | gchar *fname; |
---|
194 | static int log_num = 0; |
---|
195 | |
---|
196 | log = g_new (GtkHTMLLog, 1); |
---|
197 | log->stream = stream; |
---|
198 | |
---|
199 | |
---|
200 | fname = g_strdup_printf ("gtkhtml.log.%d", log_num); |
---|
201 | log->file = fopen (fname, "w+"); |
---|
202 | g_free (fname); |
---|
203 | |
---|
204 | log_num ++; |
---|
205 | |
---|
206 | new_stream = gtk_html_stream_new (html, |
---|
207 | stream_log_types, |
---|
208 | stream_log_write, |
---|
209 | stream_log_close, |
---|
210 | log); |
---|
211 | |
---|
212 | return new_stream; |
---|
213 | } |
---|
214 | |
---|
215 | |
---|
216 | |
---|
217 | |
---|