home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libmime / mimeleaf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.5 KB  |  186 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. /* mimeleaf.c --- definition of the MimeLeaf class (see mimei.h)
  20.    Created: Jamie Zawinski <jwz@netscape.com>, 15-May-96.
  21.  */
  22.  
  23. #include "mimeleaf.h"
  24.  
  25. #define MIME_SUPERCLASS mimeObjectClass
  26. MimeDefClass(MimeLeaf, MimeLeafClass, mimeLeafClass, &MIME_SUPERCLASS);
  27.  
  28. static int MimeLeaf_initialize (MimeObject *);
  29. static void MimeLeaf_finalize (MimeObject *);
  30. static int MimeLeaf_parse_begin (MimeObject *);
  31. static int MimeLeaf_parse_buffer (char *, int32, MimeObject *);
  32. static int MimeLeaf_parse_line (char *, int32, MimeObject *);
  33. static int MimeLeaf_parse_eof (MimeObject *, XP_Bool);
  34. static XP_Bool MimeLeaf_displayable_inline_p (MimeObjectClass *class,
  35.                                               MimeHeaders *hdrs);
  36.  
  37. static int
  38. MimeLeafClassInitialize(MimeLeafClass *class)
  39. {
  40.   MimeObjectClass *oclass = (MimeObjectClass *) class;
  41.   XP_ASSERT(!oclass->class_initialized);
  42.   oclass->initialize   = MimeLeaf_initialize;
  43.   oclass->finalize     = MimeLeaf_finalize;
  44.   oclass->parse_begin  = MimeLeaf_parse_begin;
  45.   oclass->parse_buffer = MimeLeaf_parse_buffer;
  46.   oclass->parse_line   = MimeLeaf_parse_line;
  47.   oclass->parse_eof    = MimeLeaf_parse_eof;
  48.   oclass->displayable_inline_p = MimeLeaf_displayable_inline_p;
  49.  
  50.   /* Default `parse_buffer' method is one which line-buffers the now-decoded
  51.      data and passes it on to `parse_line'.  (We snarf the implementation of
  52.      this method from our superclass's implementation of `parse_buffer', which
  53.      inherited it from MimeObject.)
  54.    */
  55.   class->parse_decoded_buffer =
  56.     ((MimeObjectClass*)&MIME_SUPERCLASS)->parse_buffer;
  57.  
  58.   return 0;
  59. }
  60.  
  61.  
  62. static int
  63. MimeLeaf_initialize (MimeObject *obj)
  64. {
  65.   /* This is an abstract class; it shouldn't be directly instanciated. */
  66.   XP_ASSERT(obj->class != (MimeObjectClass *) &mimeLeafClass);
  67.  
  68.   return ((MimeObjectClass*)&MIME_SUPERCLASS)->initialize(obj);
  69. }
  70.  
  71.  
  72. static void
  73. MimeLeaf_finalize (MimeObject *object)
  74. {
  75.   MimeLeaf *leaf = (MimeLeaf *)object;
  76.   object->class->parse_eof (object, FALSE);
  77.  
  78.   /* Free the decoder data, if it's still around.  It was probably freed
  79.      in MimeLeaf_parse_eof(), but just in case... */
  80.   if (leaf->decoder_data)
  81.     {
  82.       MimeDecoderDestroy(leaf->decoder_data, TRUE);
  83.       leaf->decoder_data = 0;
  84.     }
  85.  
  86.   ((MimeObjectClass*)&MIME_SUPERCLASS)->finalize (object);
  87. }
  88.  
  89.  
  90. static int
  91. MimeLeaf_parse_begin (MimeObject *obj)
  92. {
  93.   MimeLeaf *leaf = (MimeLeaf *) obj;
  94.   MimeDecoderData *(*fn) (int (*) (const char*, int32, void*), void*) = 0;
  95.  
  96.   /* Initialize a decoder if necessary.
  97.    */
  98.   if (!obj->encoding)
  99.     ;
  100.   else if (!strcasecomp(obj->encoding, ENCODING_BASE64))
  101.     fn = &MimeB64DecoderInit;
  102.   else if (!strcasecomp(obj->encoding, ENCODING_QUOTED_PRINTABLE))
  103.     fn = &MimeQPDecoderInit;
  104.   else if (!strcasecomp(obj->encoding, ENCODING_UUENCODE) ||
  105.            !strcasecomp(obj->encoding, ENCODING_UUENCODE2) ||
  106.            !strcasecomp(obj->encoding, ENCODING_UUENCODE3) ||
  107.            !strcasecomp(obj->encoding, ENCODING_UUENCODE4))
  108.     fn = &MimeUUDecoderInit;
  109.  
  110.   if (fn)
  111.     {
  112.       leaf->decoder_data =
  113.         fn (/* The (int (*) ...) cast is to turn the `void' argument
  114.                into `MimeObject'. */
  115.             ((int (*) (const char *, int32, void *))
  116.              ((MimeLeafClass *)obj->class)->parse_decoded_buffer),
  117.             obj);
  118.  
  119.       if (!leaf->decoder_data)
  120.         return MK_OUT_OF_MEMORY;
  121.     }
  122.  
  123.   return ((MimeObjectClass*)&MIME_SUPERCLASS)->parse_begin(obj);
  124. }
  125.  
  126.  
  127. static int
  128. MimeLeaf_parse_buffer (char *buffer, int32 size, MimeObject *obj)
  129. {
  130.   MimeLeaf *leaf = (MimeLeaf *) obj;
  131.  
  132.   XP_ASSERT(!obj->closed_p);
  133.   if (obj->closed_p) return -1;
  134.  
  135.   /* If we're not supposed to write this object, bug out now.
  136.    */
  137.   if (!obj->output_p ||
  138.       !obj->options ||
  139.       !obj->options->output_fn)
  140.     return 0;
  141.  
  142.   if (leaf->decoder_data)
  143.     return MimeDecoderWrite (leaf->decoder_data, buffer, size);
  144.   else
  145.     return ((MimeLeafClass *)obj->class)->parse_decoded_buffer (buffer, size,
  146.                                                                 obj);
  147. }
  148.  
  149. static int
  150. MimeLeaf_parse_line (char *line, int32 length, MimeObject *obj)
  151. {
  152.   XP_ASSERT(0);
  153.   /* This method shouldn't ever be called. */
  154.   return -1;
  155. }
  156.  
  157.  
  158. static int
  159. MimeLeaf_parse_eof (MimeObject *obj, XP_Bool abort_p)
  160. {
  161.   MimeLeaf *leaf = (MimeLeaf *) obj;
  162.   if (obj->closed_p) return 0;
  163.  
  164.   /* Close off the decoder, to cause it to give up any buffered data that
  165.      it is still holding.
  166.    */
  167.   if (leaf->decoder_data)
  168.     {
  169.       int status = MimeDecoderDestroy(leaf->decoder_data, FALSE);
  170.       leaf->decoder_data = 0;
  171.       if (status < 0) return status;
  172.     }
  173.  
  174.   /* Now run the superclass's parse_eof, which will force out the line
  175.      buffer (which we may have just repopulated, above.)
  176.    */
  177.   return ((MimeObjectClass*)&MIME_SUPERCLASS)->parse_eof (obj, abort_p);
  178. }
  179.  
  180.  
  181. static XP_Bool
  182. MimeLeaf_displayable_inline_p (MimeObjectClass *class, MimeHeaders *hdrs)
  183. {
  184.   return TRUE;
  185. }
  186.