⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 htfwriter.c

📁 基于rtos开发的浏览器!
💻 C
📖 第 1 页 / 共 3 页
字号:
/*		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.*/#include "HTUtils.h"#include "tcp.h"#include "LYCurses.h"#include "HTFWriter.h"#include "HTSaveToFile.h"#include "HTFormat.h"#include "UCDefs.h"#include "HTAlert.h"#include "HTFile.h"#include "HTPlain.h"#include "HTFile.h"#ifdef VMS#include "HTVMSUtils.h"#endif /* VMS */#ifdef DOSPATH#include "HTDOS.h"#endif#include "LYStrings.h"#include "LYUtils.h"#include "LYGlobalDefs.h"#include "LYSignal.h"#include "LYSystem.h"#include "GridText.h"#include "LYexit.h"#include "LYLeaks.h"#include "LYKeymap.h"PUBLIC char * WWW_Download_File=NULL; /* contains the name of the temp file				      ** which is being downloaded into				      */PUBLIC char LYCancelDownload=FALSE;   /* exported to HTFormat.c in libWWW */#ifdef VMSextern BOOLEAN HadVMSInterrupt;      /* flag from cleanup_sig() 	*/PRIVATE char * FIXED_RECORD_COMMAND = NULL;#ifdef USE_COMMAND_FILE 	     /* Keep this as an option. - FM	*/#define FIXED_RECORD_COMMAND_MASK "@Lynx_Dir:FIXED512 %s"#else#define FIXED_RECORD_COMMAND_MASK "%s"PUBLIC unsigned long LYVMS_FixedLengthRecords PARAMS((char *filename));#endif /* USE_COMMAND_FILE */#endif /* VMS */PUBLIC HTStream* HTSaveToFile PARAMS((	HTPresentation *       pres,	HTParentAnchor *       anchor,	HTStream *	       sink));#define FREE(x) if (x) {free(x); x = NULL;}/*	Stream Object**	-------------*/struct _HTStream {	CONST HTStreamClass *	isa;	FILE *			fp;		/* The file we've opened */	char *			end_command;	/* What to do on _free.  */	char *			remove_command; /* What to do on _abort. */	char *			viewer_command; /* Saved external viewer */	HTFormat		input_format;  /* Original pres->rep	 */	HTFormat		output_format; /* Original pres->rep_out */	HTParentAnchor *	anchor;     /* Original stream's anchor. */	HTStream *		sink;	    /* Original stream's sink.	 */#ifdef FNAMES_8_3	int			idash; /* remember position to become '.'*/#endif};/*_________________________________________________________________________****			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){    FILE *fp = NULL;    int len;    char *path = NULL;    char *addr = NULL;    int status;    BOOL use_gzread = NO;    fflush(me->fp);    if (me->end_command) {		/* Temp file */	fclose(me->fp);#ifdef VMS	if (0 == strcmp(me->end_command, "SaveVMSBinaryFile")) {	/*	 *  It's a binary file saved to disk on VMS, which	 *  we want to convert to fixed records format. - FM	 */#ifdef USE_COMMAND_FILE	    system(FIXED_RECORD_COMMAND);#else	    LYVMS_FixedLengthRecords(FIXED_RECORD_COMMAND);#endif /* USE_COMMAND_FILE */	    FREE(FIXED_RECORD_COMMAND);	    if (me->remove_command) {		/* NEVER REMOVE THE FILE unless during an abort!*/		/* system(me->remove_command); */		FREE(me->remove_command);	    }	} else#endif /* VMS */	if (me->input_format == HTAtom_for("www/compressed")) {	    /*	     *	It's a compressed file supposedly cached to	     *	a temporary file for uncompression. - FM	     */	    if (me->anchor->FileCache != NULL) {		BOOL skip_loadfile = (me->viewer_command != NULL);		/*		 *  Save the path with the "gz" or "Z" suffix trimmed,		 *  and remove any previous uncompressed copy. - FM		 */		StrAllocCopy(path, me->anchor->FileCache);		if ((len = strlen(path)) > 2) {		    if (!strcasecomp((char *)&path[len-2], "gz")) {#ifdef USE_ZLIB			if (!skip_loadfile) {			    use_gzread = YES;			} else#endif /* USE_ZLIB */			{			    path[len-3] = '\0';			    remove(path);			}		    } else if (!strcasecomp((char *)&path[len-1], "Z")) {			path[len-2] = '\0';			remove(path);		    }		}		if (!use_gzread) {		    if (!dump_output_immediately) {			/*			 *  Tell user what's happening. - FM			 */			_HTProgress(me->end_command);		    }		    /*		     *	Uncompress it. - FM		     */		    if (me->end_command && me->end_command[0])			system(me->end_command);		    fp = fopen(me->anchor->FileCache, "r");		}		if (fp != NULL) {		    /*		     *	It's still there with the "gz" or "Z" suffix,		     *	so the uncompression failed. - FM		     */		    fclose(fp);		    fp = NULL;		    if (!dump_output_immediately) {			lynx_force_repaint();			refresh();		    }		    HTAlert(ERROR_UNCOMPRESSING_TEMP);		    remove(me->anchor->FileCache);		    FREE(me->anchor->FileCache);		} else {		    /*		     *	Succeeded!  Create a complete address		     *	for the uncompressed file and invoke		     *	HTLoadFile() to handle it. - FM		     */#ifdef FNAMES_8_3		    /*		     *	Assuming we have just uncompressed e.g.		     *	FILE-mpeg.gz -> FILE-mpeg, restore/shorten		     *	the name to be fit for passing to an external		     *	viewer, by renaming FILE-mpeg -> FILE.mpe  - kw		     */		    if (skip_loadfile) {			char *new_path = NULL;			if (me->idash > 1 && path[me->idash] == '-') {			    StrAllocCopy(new_path, path);			    new_path[me->idash] = '.';			    if (strlen(new_path + me->idash) > 4)				new_path[me->idash + 4] = '\0';			    if (rename(path, new_path) == 0) {				FREE(path);				path = new_path;			    } else {				FREE(new_path);			    }			}		    }#endif /* FNAMES_8_3 */		    StrAllocCopy(addr, "file://localhost");#ifdef DOSPATH		    StrAllocCat(addr, "/");		    StrAllocCat(addr, HTDOS_wwwName(path));#else#ifdef VMS		    StrAllocCat(addr, HTVMS_wwwName(path));#else		    StrAllocCat(addr, path);#endif /* VMS */#endif /* DOSPATH */		    if (!use_gzread) {			StrAllocCopy(me->anchor->FileCache, path);			StrAllocCopy(me->anchor->content_encoding, "binary");		    }		    FREE(path);		    if (!skip_loadfile) {			/*			 *  Lock the chartrans info we may possibly have,			 *  so HTCharsetFormat() will not apply the default			 *  for local files. - KW			 */			if (HTAnchor_getUCLYhndl(me->anchor,						 UCT_STAGE_PARSER) < 0 ) {			    /*			     *	If not yet set - KW			     */			    HTAnchor_copyUCInfoStage(me->anchor,						     UCT_STAGE_PARSER,						     UCT_STAGE_MIME,						     UCT_SETBY_DEFAULT+1);			}			HTAnchor_copyUCInfoStage(me->anchor,						 UCT_STAGE_PARSER,						 UCT_STAGE_MIME, -1);		    }		    /*		     *	Create a complete address for		     *	the uncompressed file. - FM		     */		    if (!dump_output_immediately) {			/*			 *  Tell user what's happening. - FM			 */			_user_message(WWW_USING_MESSAGE, addr);		    }		    if (skip_loadfile) {			/*			 *  It's a temporary file we're passing to a			 *  viewer or helper application.			 *  Loading the temp file through HTLoadFile()			 *  would result in yet another HTStream (created			 *  with HTSaveAndExecute()) which would just			 *  copy the temp file to another temp file			 *  (or even the same!).  We can skip this			 *  needless duplication by using the			 *  viewer_command which has already been			 *  determind when the HTCompressed stream was			 *  created. - kw			 */			FREE(me->end_command);			me->end_command = (char *)calloc (			    (strlen (me->viewer_command) + 10 +			     strlen(me->anchor->FileCache))			    * sizeof (char),1);			if (me->end_command == NULL)			    outofmem(__FILE__, "HTFWriter_free (HTCompressed)");			sprintf(me->end_command,				me->viewer_command, me->anchor->FileCache,				"", "", "", "", "", "");			if (!dump_output_immediately) {			    /*			     *	Tell user what's happening. - FM			     */			    HTProgress(me->end_command);			    stop_curses();			}			system(me->end_command);			if (me->remove_command) {			    /* NEVER REMOVE THE FILE unless during an abort!!!*/			    /* system(me->remove_command); */			    FREE(me->remove_command);			}			if (!dump_output_immediately)			    start_curses();		    } else		    status = HTLoadFile(addr,					me->anchor,					me->output_format,					me->sink);		    if (dump_output_immediately &&			me->output_format == HTAtom_for("www/present")) {			FREE(addr);			remove(me->anchor->FileCache);			FREE(me->anchor->FileCache);			FREE(me->remove_command);			FREE(me->end_command);			FREE(me->viewer_command);			FREE(me);			return;		    }		}		FREE(addr);	    }	    if (me->remove_command) {		/* NEVER REMOVE THE FILE unless during an abort!!!*/		/* system(me->remove_command); */		FREE(me->remove_command);	    }	} else if (strcmp(me->end_command, "SaveToFile")) {	    /*	     *	It's a temporary file we're passing to a	     *	viewer or helper application. - FM	     */	    if (!dump_output_immediately) {		/*		 *  Tell user what's happening. - FM		 */		_HTProgress(me->end_command);		stop_curses();	    }	    system(me->end_command);	    if (me->remove_command) {		/* NEVER REMOVE THE FILE unless during an abort!!!*/		/* system(me->remove_command); */		FREE(me->remove_command);	    }	    if (!dump_output_immediately)		start_curses();	} else {	    /*	     *	It's a file we saved to disk for handling	     *	via a menu. - FM	     */	    if (me->remove_command) {		/* NEVER REMOVE THE FILE unless during an abort!!!*/		/* system(me->remove_command); */		FREE(me->remove_command);	    }	}	FREE(me->end_command);    }    FREE(me->viewer_command);    if (dump_output_immediately) {	if (me->anchor->FileCache)	    remove(me->anchor->FileCache);	FREE(me);#ifndef NOSIGHUP	(void) signal(SIGHUP, SIG_DFL);#endif /* NOSIGHUP */	(void) signal(SIGTERM, SIG_DFL);#ifndef VMS	(void) signal(SIGINT, SIG_DFL);#endif /* !VMS */#ifdef SIGTSTP	if (no_suspend)	  (void) signal(SIGTSTP,SIG_DFL);#endif /* SIGTSTP */	exit(0);    }    FREE(me);    return;}/*	Abort writing**	-------------*/PRIVATE void HTFWriter_abort ARGS2(	HTStream *,	me,	HTError,	e GCC_UNUSED){    if (TRACE)	fprintf(stderr,"HTFWriter_abort called\n");    fclose(me->fp);    FREE(me->viewer_command);    if (me->end_command) {		/* Temp file */	if (TRACE)	    fprintf(stderr, "HTFWriter: Aborting: file not executed.\n");	FREE(me->end_command);	if (me->remove_command) {	    system(me->remove_command);	    FREE(me->remove_command);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -