home *** CD-ROM | disk | FTP | other *** search
- /* FILE WRITER HTFWrite.h
- ** ===========
- **
- ** This version of the stream object just writes to a C file.
- ** The file is assumed open and left open.
- **
- ** Bugs:
- ** strings written must be less than buffer size.
- */
- #define Uses_TFileDialog
- #define Uses_TProgram
- #define Uses_TDeskTop
- #define Uses_TObject
- #include"globals.h"
-
- // C callable
- extern "C" {
-
- #include"capalloc.h"
- #include"capstdio.h"
- #include "HTFWriter.h"
-
- #include "HTFormat.h"
- #include "HTAlert.h"
- #include "HTFile.h"
- #include<dir.h>
-
- /* Stream Object
- ** ------------
- */
-
- struct _HTStream {
- CONST HTStreamClass * isa;
-
- FILE * fp;
- char * end_command;
- char * remove_command;
- BOOL announce;
- };
-
-
- /*_________________________________________________________________________
- **
- ** B L A C K H O L E C L A S S
- **
- ** There is only one black hole instance shared by anyone
- ** who wanst a black hole. These black holes don't radiate,
- ** they just absorb data.
- */
- PRIVATE void HTBlackHole_put_character ARGS2(HTStream *, me, char, c)
- {}
- PRIVATE void HTBlackHole_put_string ARGS2(HTStream *, me, CONST char*, s)
- {}
- PRIVATE void HTBlackHole_write ARGS3(HTStream *, me, CONST char*, s, int, l)
- {}
- PRIVATE void HTBlackHole_free ARGS1(HTStream *, me)
- {}
- PRIVATE void HTBlackHole_abort ARGS2(HTStream *, me, HTError, e)
- {}
-
-
- /* Black Hole stream
- ** -----------------
- */
- PRIVATE CONST HTStreamClass HTBlackHoleClass =
- {
- "BlackHole",
- HTBlackHole_free,
- HTBlackHole_abort,
- HTBlackHole_put_character, HTBlackHole_put_string,
- HTBlackHole_write
- };
-
- PRIVATE HTStream HTBlackHoleInstance =
- {
- &HTBlackHoleClass,
- NULL,
- NULL,
- NULL,
- NO
- };
-
- /* Black hole craetion
- */
- PUBLIC HTStream * HTBlackHole NOARGS
- {
- return &HTBlackHoleInstance;
- }
-
-
- /*_________________________________________________________________________
- **
- ** F I L E A C T I O N R O U T I N E S
- ** Bug:
- ** All errors are ignored.
- */
-
- /* Character handling
- ** ------------------
- */
-
- PRIVATE void HTFWriter_put_character ARGS2(HTStream *, me, char, c)
- {
- putc(c, me->fp);
- }
-
-
-
- /* String handling
- ** ---------------
- **
- ** Strings must be smaller than this buffer size.
- */
- PRIVATE void HTFWriter_put_string ARGS2(HTStream *, me, CONST char*, s)
- {
- fputs(s, me->fp);
- }
-
-
- /* Buffer write. Buffers can (and should!) be big.
- ** ------------
- */
- PRIVATE void HTFWriter_write ARGS3(HTStream *, me, CONST char*, s, int, l)
- {
- fwrite(s, 1, l, me->fp);
- }
-
-
-
-
- /* Free an HTML object
- ** -------------------
- **
- ** Note that the SGML parsing context is freed, but the created
- ** object is not,
- ** as it takes on an existence of its own unless explicitly freed.
- */
- PRIVATE void HTFWriter_free ARGS1(HTStream *, me)
- {
- fclose(me->fp);
- if (me->end_command) { /* Temp file */
- HTProgress(me->end_command); /* Tell user what's happening */
- system(me->end_command);
- free (me->end_command);
- if (me->remove_command) {
- system(me->remove_command);
- free(me->remove_command);
- }
- }
-
- free(me);
- }
-
- /* End writing
- */
-
- PRIVATE void HTFWriter_abort ARGS2(HTStream *, me, HTError, e)
- {
- fclose(me->fp);
- if (me->end_command) { /* Temp file */
- #ifndef RELEASE
- if (TRACE) fprintf(stderr,
- "HTFWriter: Aborting: file not executed.\n");
- #endif /* RELEASE */
- free (me->end_command);
- if (me->remove_command) {
- system(me->remove_command);
- free(me->remove_command);
- }
- }
-
- free(me);
- }
-
-
-
- /* Structured Object Class
- ** -----------------------
- */
- PRIVATE CONST HTStreamClass HTFWriter = /* As opposed to print etc */
- {
- "FileWriter",
- HTFWriter_free,
- HTFWriter_abort,
- HTFWriter_put_character, HTFWriter_put_string,
- HTFWriter_write
- };
-
-
- /* Subclass-specific Methods
- ** -------------------------
- */
-
- PUBLIC HTStream* HTFWriter_new ARGS1(FILE *, fp)
- {
- HTStream* me;
-
- if (!fp) return NULL;
-
- me = (HTStream*)malloc(sizeof(*me));
- if (me == NULL) outofmem(__FILE__, "HTML_new");
- me->isa = &HTFWriter;
-
- me->fp = fp;
- me->end_command = NULL;
- me->remove_command = NULL;
- me->announce = NO;
-
- return me;
- }
-
- /* Make system command from template
- ** ---------------------------------
- **
- ** See mailcap spec for description of template.
- */
- /* @@ to be written. sprintfs will do for now. */
-
-
-
- extern HTStream *HTSaveLocally(HTPresentation *HTPp_pres,
- HTParentAnchor *HTPAp_anchor, HTStream *HTSp_sink) {
- // Purpose: Prompt user for a local name to save under and return
- // an open stream for WWW to write to that file.
- // Arguments: HTPp_pres The routines used to present the
- // file to save.
- // HTPAp_anchor Not used, but is the anchor pointing
- // to this document.
- // HTSp_sink Not used, but is the stream to write
- // the presentation to.
- // Return Value: HTStream * The opened stream for WWW to save the
- // file to.
- // It appears NULL can be returned in
- // the case of an error, but in such
- // cases we are just returning a null
- // stream so that the rest of the
- // code may continue to work.
- // Remarks/Portability/Dependencies/Restrictions:
- // On top of performing the normal functionality stated above,
- // HTSaveLocally will have to inform the current TURLView that
- // it should not try to display the URL that we are actually
- // saving.
- // Revision History:
- // 03-30-94 created
-
- // First, inform the owning view that this we won't be able to
- // display this.
- TURLV_current->DoNotDisplay();
-
- // Secondly, check to make sure we can save locally.
- // This is taken directly from the WWW code and may not be
- // needed.
- if(HTClientHost) {
- HTAlert("Can't save data to file -- please run WWW locally");
- // Return a stream that goes nowhere.
- return(HTBlackHole());
- }
-
-
- // Allocate the stream we will be returning.
- auto HTStream *HTSp_me = new HTStream;
- if(HTSp_me == NULL) {
- doslynxmessage("Could not allocate memory to save locally.");
- // Attempt to keep running by returning a black hole.
- return(HTBlackHole());
- }
- // Fill up a few members right now.
- // Use action routines of the HTFWriter.
- // No command to execute at the end.
- // No remove command to delete the file, will keep.
- // Announce??
- HTSp_me->isa = &HTFWriter;
- HTSp_me->end_command = NULL;
- HTSp_me->remove_command = NULL;
- HTSp_me->announce = YES;
-
- // Create a file dialog to ask the user for a file name in which
- // to save in.
- auto TFileDialog *TFD = new TFileDialog("*.*", HTPAp_anchor->address,
- "Save as", fdOKButton, usi_SaveHist);
-
- // If the dialog was not allocated
- if(TFD == NULL) {
- doslynxmessage("Could not allocate memory to save locally.");
- // Keep running by returning the black hole.
- return(HTBlackHole());
- }
-
- // Execute the dialog.
- auto unsigned short int usi_retval = TProgram::deskTop->execView(TFD);
-
- // If the user didn't cancel.
- if(usi_retval == cmCancel) {
- TObject::destroy(TFD);
- // Return the black hole to save in, user doesn't want
- // it.
- return(HTBlackHole());
- }
-
- // Small buffer to hold file name
- auto char ca_buffer[usi_TILURLSize];
-
- // Get the information from the Dialog
- TFD->getFileName(ca_buffer);
-
- // Done with the dialog.
- TObject::destroy(TFD);
-
- // Open the stream's file.
- HTSp_me->fp = fopen(ca_buffer, "wb");
- if(HTSp_me->fp == NULL) {
- doslynxmessage("Unable to open local file " << ca_buffer
- << " for writing.");
- // Return the black hole.
- return(HTBlackHole());
- }
-
- // Done, return the allocated stream.
- return(HTSp_me);
- }
-
-
- extern HTStream *HTHandleImage(HTPresentation *HTPp_pres,
- HTParentAnchor *HTPAp_anchor, HTStream *HTSp_sink) {
- // Purpose: Prompt user for a local name to save under and return
- // an open stream for WWW to write to that file. Also,
- // inform the owning view that this is a viewable image.
- // Arguments: HTPp_pres The routines used to present the
- // file to save.
- // HTPAp_anchor Not used, but is the anchor pointing
- // to this document.
- // HTSp_sink Not used, but is the stream to write
- // the presentation to.
- // Return Value: HTStream * The opened stream for WWW to save the
- // file to.
- // It appears NULL can be returned in
- // the case of an error, but in such
- // cases we are just returning a null
- // stream so that the rest of the
- // code may continue to work.
- // Remarks/Portability/Dependencies/Restrictions:
- // On top of performing the normal functionality stated above,
- // HTHandleImage will have to inform the current TURLView that
- // it should not try to display the URL that we are actually
- // saving. Also, we will inform the owning view that this is
- // a viewable image, so that once the load is accomplished, to
- // display.
- // Revision History:
- // 03-30-94 created
- // 04-06-94 Converted for images.
-
- // First, inform the owning view that this we won't be able to
- // display this.
- TURLV_current->DoNotDisplay();
-
- // Secondly, check to make sure we can save locally.
- // This is taken directly from the WWW code and may not be
- // needed.
- if(HTClientHost) {
- HTAlert("Can't save data to file -- please run WWW locally");
- // Return a stream that goes nowhere.
- return(HTBlackHole());
- }
-
-
- // Allocate the stream we will be returning.
- auto HTStream *HTSp_me = new HTStream;
- if(HTSp_me == NULL) {
- doslynxmessage("Could not allocate memory to save locally.");
- // Attempt to keep running by returning a black hole.
- return(HTBlackHole());
- }
- // Fill up a few members right now.
- // Use action routines of the HTFWriter.
- // No command to execute at the end.
- // No remove command to delete the file, will keep.
- // Announce??
- HTSp_me->isa = &HTFWriter;
- HTSp_me->end_command = NULL;
- HTSp_me->remove_command = NULL;
- HTSp_me->announce = YES;
-
- // Create a file dialog to ask the user for a file name in which
- // to save in.
- auto TFileDialog *TFD = new TFileDialog("*.gif", HTPAp_anchor->
- address, "Save image as", fdOKButton, usi_SaveHist);
-
- // If the dialog was not allocated
- if(TFD == NULL) {
- doslynxmessage("Could not allocate memory to save locally.");
- // Keep running by returning the black hole.
- return(HTBlackHole());
- }
-
- // Execute the dialog.
- auto unsigned short int usi_retval = TProgram::deskTop->execView(TFD);
-
- // If the user didn't cancel.
- if(usi_retval == cmCancel) {
- TObject::destroy(TFD);
- // Return the black hole to save in, user doesn't want
- // it.
- return(HTBlackHole());
- }
-
- // Small buffer to hold file name
- auto char ca_buffer[usi_TILURLSize];
-
- // Get the information from the Dialog
- TFD->getFileName(ca_buffer);
-
- // Done with the dialog.
- TObject::destroy(TFD);
-
- // Open the stream's file.
- HTSp_me->fp = fopen(ca_buffer, "wb");
- if(HTSp_me->fp == NULL) {
- doslynxmessage("Unable to open local file " << ca_buffer
- << " for writing.");
- // Return the black hole.
- return(HTBlackHole());
- }
-
- // Here we can inform the owning window that this is an image.
- // Pass the file name so that the view can take action upon it.
- TURLV_current->isImage(ca_buffer);
-
- // Done, return the allocated stream.
- return(HTSp_me);
- }
-
-
-
-
- /* Take action using a system command
- ** ----------------------------------
- **
- ** originally from Ghostview handling by Marc Andreseen.
- ** Creates temporary file, writes to it, executes system command
- ** on end-document. The suffix of the temp file can be given
- ** in case the application is fussy, or so that a generic opener can
- ** be used.
- */
- PUBLIC HTStream* HTSaveAndExecute ARGS3(
- HTPresentation *, pres,
- HTParentAnchor *, anchor, /* Not used */
- HTStream *, sink) /* Not used */
-
- #ifdef unix
- #define REMOVE_COMMAND "/bin/rm -f %s\n"
- #endif
- #ifdef VMS
- #define REMOVE_COMMAND "delete/noconfirm/nolog %s.."
- #endif
-
- #ifdef REMOVE_COMMAND
- {
- char *fnam;
- CONST char * suffix;
-
- HTStream* me;
-
- if (HTClientHost) {
- HTAlert("Can't save data to file -- please run WWW locally");
- return HTBlackHole();
- }
-
- me = (HTStream*)malloc(sizeof(*me));
- if (me == NULL) outofmem(__FILE__, "Save and execute");
- me->isa = &HTFWriter;
-
- /* Save the file under a suitably suffixed name */
-
- suffix = HTFileSuffix(pres->rep);
-
- fnam = (char *)malloc (L_tmpnam + 16 + strlen(suffix));
- tmpnam (fnam);
- if (suffix) strcat(fnam, suffix);
-
- me->fp = fopen (fnam, "w");
- if (!me->fp) {
- HTAlert("Can't open temporary file!");
- free(fnam);
- free(me);
- return NULL;
- }
-
- /* Make command to process file
- */
- me->end_command = (char *)malloc (
- (strlen (pres->command) + 10+ 3*strlen(fnam))
- * sizeof (char));
- if (me == NULL) outofmem(__FILE__, "SaveAndExecute");
-
- sprintf (me->end_command, pres->command, fnam, fnam, fnam);
-
- me->remove_command = NULL; /* If needed, put into end_command */
- #ifdef NOPE
- /* Make command to delete file
- */
- me->remove_command = (char *)malloc (
- (strlen (REMOVE_COMMAND) + 10+ strlen(fnam))
- * sizeof (char));
- if (me == NULL) outofmem(__FILE__, "SaveAndExecute");
-
- sprintf (me->remove_command, REMOVE_COMMAND, fnam);
- #endif
-
- me->announce = NO;
- free (fnam);
- return me;
- }
-
- #else /* can do remove */
- { return NULL; }
- #endif
-
-
- /* Format Converter using system command
- ** -------------------------------------
- */
-
- }; // extern "C"