home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / lynx-2.4 / WWW / Library / Implementation / HTFWriter.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-28  |  7.3 KB  |  357 lines

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