home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libnet / cvsimple.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.0 KB  |  124 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. /**********************************************************************
  19.  cvsimple.c
  20.  By Daniel Malmer <malmer@netscape.com>
  21.  1/14/98
  22.  
  23.  Simple converter that just saves the data in a buffer.
  24.  
  25. **********************************************************************/
  26.  
  27. #include "cvsimple.h"
  28. #include "xp.h"
  29.  
  30. typedef void (*simple_complete_t)(void* bytes, int32 bytes_written);
  31.  
  32. typedef struct {
  33.     unsigned char* bytes;
  34.     int32 bytes_written;
  35.     int32 max_bytes;
  36.     simple_complete_t complete;
  37. } NET_SimpleStreamData;
  38.  
  39.  
  40. /*
  41.  * simple_complete
  42.  */
  43. PRIVATE void
  44. simple_complete(NET_StreamClass *stream)
  45. {
  46.     NET_SimpleStreamData* obj = (NET_SimpleStreamData*) stream->data_object;    
  47.  
  48.     if ( obj && obj->complete ) {
  49.         (obj->complete)(obj->bytes, obj->bytes_written);
  50.     }
  51.  
  52.     if ( obj && obj->bytes ) XP_FREE(obj->bytes);
  53.     if ( obj ) XP_FREE(obj);
  54. }
  55.  
  56.  
  57. /*
  58.  * simple_abort
  59.  */
  60. PRIVATE void
  61. simple_abort(NET_StreamClass *stream, int status)
  62. {
  63.     NET_SimpleStreamData* obj = (NET_SimpleStreamData*) stream->data_object;    
  64.  
  65.     if ( obj && obj->bytes ) XP_FREE(obj->bytes);
  66.     if ( obj ) XP_FREE(obj);
  67. }
  68.  
  69.  
  70. /*
  71.  * simple_write
  72.  */
  73. PRIVATE int
  74. simple_write(NET_StreamClass *stream, const char* str, int32 len)
  75. {
  76.     NET_SimpleStreamData* obj = (NET_SimpleStreamData*) stream->data_object;    
  77.  
  78.     if ( obj->bytes_written + len > obj->max_bytes ) {
  79.         /* Round to nearest 1024 */
  80.         obj->max_bytes = ( ( ( (obj->max_bytes + len) >> 10) + 1) << 10);
  81.         obj->bytes = XP_REALLOC(obj->bytes, obj->max_bytes);
  82.     }
  83.  
  84.     XP_MEMCPY(obj->bytes + obj->bytes_written, str, len);
  85.     obj->bytes_written+= len;
  86.  
  87.     return MK_DATA_LOADED;
  88. }
  89.  
  90.  
  91. /*
  92.  * simple_write_ready
  93.  */
  94. PRIVATE unsigned int
  95. simple_write_ready(NET_StreamClass *stream)
  96. {    
  97.     return MAX_WRITE_READY;
  98. }
  99.  
  100.  
  101. /*
  102.  * NET_SimpleStream
  103.  * Simple stream constructor.
  104.  */
  105. MODULE_PRIVATE NET_StreamClass*
  106. NET_SimpleStream(int fmt, void* data_obj, URL_Struct* URL_s, MWContext* w)
  107. {
  108.     NET_SimpleStreamData* obj;
  109.  
  110.     if ( (obj = XP_NEW_ZAP(NET_SimpleStreamData)) == NULL ) {
  111.         return NULL;
  112.     }
  113.  
  114.     obj->bytes = NULL;
  115.     obj->bytes_written = 0;
  116.     obj->max_bytes = 0;
  117.     obj->complete = (simple_complete_t) data_obj;
  118.  
  119.     return NET_NewStream("SimpleStream", simple_write, simple_complete,
  120.                             simple_abort, simple_write_ready, obj, w);
  121. }
  122.  
  123.  
  124.