earlybrowserreborn - Blame information for rev 4

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 roytam 1 /*              FILE WRITER                             HTFWrite.h
2 **              ===========
3 **
4 **      This version of the stream object just writes to a C file.
5 **      The file is assumed open and left open.
6 **
7 **      Bugs:
8 **              strings written must be less than buffer size.
9 */
10  
11 #include "HTFWriter.h"
12  
13 #include "HTFormat.h"
14 #include "HTAlert.h"
15 #include "HTFile.h"
16  
17 /*              Stream Object
18 **              ------------
19 */
20  
21 struct _HTStream {
22         CONST HTStreamClass *   isa;
23  
24         FILE *                  fp;
25         char *                  end_command;
26         char *                  remove_command;
27         BOOL                    announce;
28 };
29  
30  
31 /*_________________________________________________________________________
32 **
33 **              B L A C K    H O L E    C L A S S
34 **
35 **      There is only one black hole instance shared by anyone
36 **      who wanst a black hole.  These black holes don't radiate,
37 **      they just absorb data.
38 */
39 PRIVATE void HTBlackHole_put_character ARGS2(HTStream *, me, char, c)
40 {}
41 PRIVATE void HTBlackHole_put_string ARGS2(HTStream *, me, CONST char*, s)
42 {}
43 PRIVATE void HTBlackHole_progress ARGS2(HTStream *, me, int, l)
44 {}
45 PRIVATE void HTBlackHole_write ARGS3(HTStream *, me, CONST char*, s, int, l)
46 {}
47 PRIVATE void HTBlackHole_free ARGS1(HTStream *, me)
48 {}
49 PRIVATE void HTBlackHole_end ARGS1(HTStream *, me)
50 {}
51 PRIVATE void HTBlackHole_abort ARGS2(HTStream *, me, HTError, e)
52 {}
53  
54  
55 /*      Black Hole stream
56 **      -----------------
57 */
58 PRIVATE CONST HTStreamClass HTBlackHoleClass =
59 {              
60         "BlackHole",
61         HTBlackHole_free,
62         HTBlackHole_end,/*PYW*/
63         HTBlackHole_abort,
64         HTBlackHole_put_character,      HTBlackHole_put_string,
65         HTBlackHole_progress,
66         HTBlackHole_write,
67 };
68  
69 PRIVATE HTStream HTBlackHoleInstance =
70 {
71         &HTBlackHoleClass,
72         NULL,
73         NULL,
74         NULL,
75         NO
76 };
77  
78 /*      Black hole craetion
79 */
80 PUBLIC HTStream * HTBlackHole NOARGS
81 {
82     return &HTBlackHoleInstance;
83 }
84  
85  
86 /*_________________________________________________________________________
87 **
88 **              F I L E     A C T I O N         R O U T I N E S
89 **  Bug:
90 **      All errors are ignored.
91 */
92  
93 /*      Character handling
94 **      ------------------
95 */
96  
97 PRIVATE void HTFWriter_put_character ARGS2(HTStream *, me, char, c)
98 {
99     putc(c, me->fp);
100 }
101  
102  
103  
104 /*      String handling
105 **      ---------------
106 **
107 **      Strings must be smaller than this buffer size.
108 */
109 PRIVATE void HTFWriter_put_string ARGS2(HTStream *, me, CONST char*, s)
110 {
111     fputs(s, me->fp);
112 }
113  
114  
115 /*      Buffer write.  Buffers can (and should!) be big.
116 **      ------------
117 */
118 PRIVATE void HTFWriter_progress ARGS2(HTStream *, me, int, l)
119 {
120 #ifdef VIOLA  /* KLUDGE ALERT */
121   http_progress_notify(l);
122 #endif
123 }
124  
125 /*      Buffer write.  Buffers can (and should!) be big.
126 **      ------------
127 */
128 PRIVATE void HTFWriter_write ARGS3(HTStream *, me, CONST char*, s, int, l)
129 {
130     fwrite(s, 1, l, me->fp);
131 }
132  
133  
134  
135  
136 /*      Free an HTML object
137 **      -------------------
138 **
139 **      Note that the SGML parsing context is freed, but the created
140 **      object is not,
141 **      as it takes on an existence of its own unless explicitly freed.
142 */
143 PRIVATE void HTFWriter_free ARGS1(HTStream *, me)
144 {
145     fclose(me->fp);
146     if (me->end_command) {              /* Temp file */
147         HTProgress(me->end_command);    /* Tell user what's happening */
148         system(me->end_command);
149         free (me->end_command);
150         if (me->remove_command) {
151             system(me->remove_command);
152             free(me->remove_command);
153         }
154     }
155  
156     free(me);
157 }
158  
159 PRIVATE void HTFWriter_end ARGS1(HTStream *, me)
160 {
161 }
162  
163 /*      End writing
164 */
165  
166 PRIVATE void HTFWriter_abort ARGS2(HTStream *, me, HTError, e)
167 {
168     fclose(me->fp);
169     if (me->end_command) {              /* Temp file */
170         if (TRACE) fprintf(stderr,
171                 "HTFWriter: Aborting: file not executed.\n");
172         free (me->end_command);
173         if (me->remove_command) {
174             system(me->remove_command);
175             free(me->remove_command);
176         }
177     }
178  
179     free(me);
180 }
181  
182  
183  
184 /*      Structured Object Class
185 **      -----------------------
186 */
187 PRIVATE CONST HTStreamClass HTFWriter = /* As opposed to print etc */
188 {              
189         "FileWriter",
190         HTFWriter_free,
191         HTFWriter_end,
192         HTFWriter_abort,
193         HTFWriter_put_character,        HTFWriter_put_string,
194         HTFWriter_progress,
195         HTFWriter_write
196 };
197  
198  
199 /*      Subclass-specific Methods
200 **      -------------------------
201 */
202  
203 PUBLIC HTStream* HTFWriter_new ARGS1(FILE *, fp)
204 {
205     HTStream* me;
206  
207     if (!fp) return NULL;
208  
209     me = (HTStream*)malloc(sizeof(*me));
210     if (me == NULL) outofmem(__FILE__, "HTML_new");
211     me->isa = &HTFWriter;      
212  
213     me->fp = fp;
214     me->end_command = NULL;
215     me->remove_command = NULL;
216     me->announce = NO;
217  
218     return me;
219 }
220  
221 /*      Make system command from template
222 **      ---------------------------------
223 **
224 **      See mailcap spec for description of template.
225 */
226 /* @@ to be written.  sprintfs will do for now.  */
227  
228  
229  
230 /*      Take action using a system command
231 **      ----------------------------------
232 **
233 **      originally from Ghostview handling by Marc Andreseen.
234 **      Creates temporary file, writes to it, executes system command
235 **      on end-document.  The suffix of the temp file can be given
236 **      in case the application is fussy, or so that a generic opener can
237 **      be used.
238 */
239 PUBLIC HTStream* HTSaveAndExecute ARGS3(
240         HTPresentation *,       pres,
241         HTParentAnchor *,       anchor, /* Not used */
242         HTStream *,             sink)   /* Not used */
243  
244 #ifdef unix
245 #define REMOVE_COMMAND "/bin/rm -f %s\n"
246 #endif
247 #ifdef VMS
248 #define REMOVE_COMMAND "delete/noconfirm/nolog %s.."
249 #endif
250  
251 #ifdef REMOVE_COMMAND
252 {
253     char *fnam;
254     CONST char * suffix;
255  
256     HTStream* me;
257  
258     if (HTClientHost) {
259         HTAlert("Can't save data to file -- please run WWW locally");
260         return HTBlackHole();
261     }
262  
263     me = (HTStream*)malloc(sizeof(*me));
264     if (me == NULL) outofmem(__FILE__, "Save and execute");
265     me->isa = &HTFWriter;  
266  
267     /* Save the file under a suitably suffixed name */
268  
269     suffix = HTFileSuffix(pres->rep);
270  
271     fnam = (char *)malloc (L_tmpnam + 16 + strlen(suffix));
272     tmpnam (fnam);
273     if (suffix) strcat(fnam, suffix);
274  
275     me->fp = fopen (fnam, "w");
276     if (!me->fp) {
277         HTAlert("Can't open temporary file!");
278         free(fnam);
279         free(me);
280         return NULL;
281     }
282  
283 /*      Make command to process file
284 */
285     me->end_command = (char *)malloc (
286                         (strlen (pres->command) + 10+ 3*strlen(fnam))
287                          * sizeof (char));
288     if (me == NULL) outofmem(__FILE__, "SaveAndExecute");
289  
290     sprintf (me->end_command, pres->command, fnam, fnam, fnam);
291  
292     me->remove_command = NULL;  /* If needed, put into end_command */
293 #ifdef NOPE
294 /*      Make command to delete file
295 */
296     me->remove_command = (char *)malloc (
297                         (strlen (REMOVE_COMMAND) + 10+ strlen(fnam))
298                          * sizeof (char));
299     if (me == NULL) outofmem(__FILE__, "SaveAndExecute");
300  
301     sprintf (me->remove_command, REMOVE_COMMAND, fnam);
302 #endif
303  
304     me->announce = NO;
305     free (fnam);
306     return me;
307 }
308  
309 #else   /* can do remove */
310 { return NULL; }
311 #endif
312  
313  
314 /*      Save Locally
315 **      ------------
316 **
317 **  Bugs:
318 **      GUI Apps should open local Save panel here really.
319 **
320 */
321 PUBLIC HTStream* HTSaveLocally ARGS3(
322         HTPresentation *,       pres,
323         HTParentAnchor *,       anchor, /* Not used */
324         HTStream *,             sink)   /* Not used */
325  
326 {
327     char *fnam;
328     char *answer;
329     CONST char * suffix;
330  
331     HTStream* me;
332  
333     if (HTClientHost) {
334         HTAlert("Can't save data to file -- please run WWW locally");
335         return HTBlackHole();
336     }
337  
338     me = (HTStream*)malloc(sizeof(*me));
339     if (me == NULL) outofmem(__FILE__, "SaveLocally");
340     me->isa = &HTFWriter;  
341     me->end_command = NULL;
342     me->remove_command = NULL;  /* If needed, put into end_command */
343     me->announce = YES;
344  
345     /* Save the file under a suitably suffixed name */
346 #ifndef VIOLA    
347     suffix = HTFileSuffix(pres->rep);
348  
349     fnam = (char *)malloc (L_tmpnam + 16 + strlen(suffix));
350     tmpnam (fnam);
351     if (suffix) strcat(fnam, suffix);
352  
353     /*  Save Panel */
354     answer = HTPrompt("Give name of file to save in", fnam);
355  
356     free(fnam);
357  
358     me->fp = fopen (answer, "w");
359     if (!me->fp) {
360         HTAlert("Can't open local file to write into.");
361         free(answer);
362         free(me);
363         return NULL;
364     }
365  
366     free(answer);
367 #else
4 roytam 368     if(sink)
369         me->fp = sink->fp; /* kludge kludge */
1 roytam 370 #endif
371  
372     return me;
373 }
374  
375  
376  
377 /*      Format Converter using system command
378 **      -------------------------------------
379 */