home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libnet / cvdisk.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.2 KB  |  118 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. /* Please leave outside of ifdef for windows precompiled headers */
  19. #include "mkutils.h"
  20.  
  21. #ifdef MOZILLA_CLIENT
  22.  
  23. #include "mkstream.h"
  24. #include "mkgeturl.h"
  25. #include "xp.h"
  26.  
  27. typedef struct _DataObject {
  28.     FILE * fp;
  29.     char * filename;
  30. } DataObject;
  31.  
  32.  
  33. PRIVATE int net_SaveToDiskWrite (NET_StreamClass *stream, CONST char* s, int32 l)
  34. {
  35.     DataObject *obj=stream->data_object;    
  36.     fwrite(s, 1, l, obj->fp); 
  37.     return(1);
  38. }
  39.  
  40. /* is the stream ready for writeing?
  41.  */
  42. PRIVATE unsigned int net_SaveToDiskWriteReady (NET_StreamClass * stream)
  43. {
  44.    DataObject *obj=stream->data_object;
  45.    return(MAX_WRITE_READY);  /* always ready for writing */ 
  46. }
  47.  
  48.  
  49. PRIVATE void net_SaveToDiskComplete (NET_StreamClass *stream)
  50. {
  51.     DataObject *obj=stream->data_object;    
  52.     fclose(obj->fp);
  53.  
  54.     FREEIF(obj->filename);
  55.  
  56.     FREE(obj);
  57.     return;
  58. }
  59.  
  60. PRIVATE void net_SaveToDiskAbort (NET_StreamClass *stream, int status)
  61. {
  62.     DataObject *obj=stream->data_object;    
  63.     fclose(obj->fp);
  64.  
  65.     if(obj->filename)
  66.       {
  67.         remove(obj->filename);
  68.         FREE(obj->filename);
  69.       }
  70.  
  71.     return;
  72. }
  73.  
  74.  
  75. PUBLIC NET_StreamClass * 
  76. fe_MakeSaveAsStream (int         format_out,
  77.                          void       *data_obj,
  78.                          URL_Struct *URL_s,
  79.                          MWContext  *window_id)
  80. {
  81.     DataObject* obj;
  82.     NET_StreamClass* stream;
  83.     static int count=0;
  84.     char filename[256];
  85.     FILE *fp = stdout;
  86.     
  87.     TRACEMSG(("Setting up display stream. Have URL: %s\n", URL_s->address));
  88.  
  89.     PR_snprintf(filename, sizeof(filename), "foo%d.unknown",count++);
  90.     fp = fopen(filename,"w");
  91.  
  92.     stream = XP_NEW(NET_StreamClass);
  93.     if(stream == NULL) 
  94.         return(NULL);
  95.  
  96.     obj = XP_NEW(DataObject);
  97.     if (obj == NULL) 
  98.         return(NULL);
  99.     
  100.     stream->name           = "FileWriter";
  101.     stream->complete       = (MKStreamCompleteFunc) net_SaveToDiskComplete;
  102.     stream->abort          = (MKStreamAbortFunc) net_SaveToDiskAbort;
  103.     stream->put_block      = (MKStreamWriteFunc) net_SaveToDiskWrite;
  104.     stream->is_write_ready = (MKStreamWriteReadyFunc) net_SaveToDiskWriteReady;
  105.     stream->data_object    = obj;  /* document info object */
  106.     stream->window_id      = window_id;
  107.  
  108.     obj->fp = fp;
  109.     obj->filename = 0;
  110.     StrAllocCopy(obj->filename, filename);
  111.  
  112.     TRACEMSG(("Returning stream from NET_SaveToDiskConverter\n"));
  113.  
  114.     return stream;
  115. }
  116.  
  117. #endif /* MOZILLA_CLIENT */
  118.