home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libmime / mimetpla.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.2 KB  |  139 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. /* mimetpla.c --- definition of the MimeInlineTextPlain class (see mimei.h)
  20.    Created: Jamie Zawinski <jwz@netscape.com>, 15-May-96.
  21.  */
  22.  
  23. #include "mimetpla.h"
  24.  
  25. #define MIME_SUPERCLASS mimeInlineTextClass
  26. MimeDefClass(MimeInlineTextPlain, MimeInlineTextPlainClass,
  27.              mimeInlineTextPlainClass, &MIME_SUPERCLASS);
  28.  
  29. static int MimeInlineTextPlain_parse_begin (MimeObject *);
  30. static int MimeInlineTextPlain_parse_line (char *, int32, MimeObject *);
  31. static int MimeInlineTextPlain_parse_eof (MimeObject *, XP_Bool);
  32.  
  33. static int
  34. MimeInlineTextPlainClassInitialize(MimeInlineTextPlainClass *class)
  35. {
  36.   MimeObjectClass *oclass = (MimeObjectClass *) class;
  37.   XP_ASSERT(!oclass->class_initialized);
  38.   oclass->parse_begin = MimeInlineTextPlain_parse_begin;
  39.   oclass->parse_line  = MimeInlineTextPlain_parse_line;
  40.   oclass->parse_eof   = MimeInlineTextPlain_parse_eof;
  41.   return 0;
  42. }
  43.  
  44. static int
  45. MimeInlineTextPlain_parse_begin (MimeObject *obj)
  46. {
  47.   int status = 0;
  48.  
  49.   status = ((MimeObjectClass*)&MIME_SUPERCLASS)->parse_begin(obj);
  50.   if (status < 0) return status;
  51.  
  52.   if (!obj->output_p) return 0;
  53.  
  54.   if (obj->options &&
  55.       obj->options->write_html_p &&
  56.       obj->options->output_fn)
  57.     {
  58.       char* strs[4];
  59.       char* s;
  60.       strs[0] = "<PRE>";
  61.       strs[1] = "<PRE VARIABLE>";
  62.       strs[2] = "<PRE WRAP>";
  63.       strs[3] = "<PRE VARIABLE WRAP>";
  64.       s = XP_STRDUP(strs[(obj->options->variable_width_plaintext_p ? 1 : 0) +
  65.                         (obj->options->wrap_long_lines_p ? 2 : 0)]);
  66.       if (!s) return MK_OUT_OF_MEMORY;
  67.       status = MimeObject_write(obj, s, XP_STRLEN(s), FALSE);
  68.       XP_FREE(s);
  69.       if (status < 0) return status;
  70.  
  71.       /* text/plain objects always have separators before and after them.
  72.          Note that this is not the case for text/enriched objects. */
  73.       status = MimeObject_write_separator(obj);
  74.       if (status < 0) return status;
  75.     }
  76.  
  77.   return 0;
  78. }
  79.  
  80. static int
  81. MimeInlineTextPlain_parse_eof (MimeObject *obj, XP_Bool abort_p)
  82. {
  83.   int status;
  84.   if (obj->closed_p) return 0;
  85.   
  86.   /* Run parent method first, to flush out any buffered data. */
  87.   status = ((MimeObjectClass*)&MIME_SUPERCLASS)->parse_eof(obj, abort_p);
  88.   if (status < 0) return status;
  89.  
  90.   if (!obj->output_p) return 0;
  91.  
  92.   if (obj->options &&
  93.       obj->options->write_html_p &&
  94.       obj->options->output_fn &&
  95.       !abort_p)
  96.     {
  97.       char s[] = "</PRE>";
  98.       status = MimeObject_write(obj, s, XP_STRLEN(s), FALSE);
  99.       if (status < 0) return status;
  100.  
  101.       /* text/plain objects always have separators before and after them.
  102.          Note that this is not the case for text/enriched objects.
  103.        */
  104.       status = MimeObject_write_separator(obj);
  105.       if (status < 0) return status;
  106.     }
  107.  
  108.   return 0;
  109. }
  110.  
  111.  
  112. static int
  113. MimeInlineTextPlain_parse_line (char *line, int32 length, MimeObject *obj)
  114. {
  115.   int status;
  116.  
  117.   XP_ASSERT(length > 0);
  118.   if (length <= 0) return 0;
  119.  
  120.   status = MimeObject_grow_obuffer (obj, length * 2 + 40);
  121.   if (status < 0) return status;
  122.  
  123.   /* Copy `line' to `out', quoting HTML along the way.
  124.      Note: this function does no charset conversion; that has already
  125.      been done.
  126.    */
  127.   *obj->obuffer = 0;
  128.   status = NET_ScanForURLs (
  129. #ifndef MOZILLA_30
  130.                             (obj->options ? obj->options->pane : 0),
  131. #endif /* !MOZILLA_30 */
  132.                             line, length, obj->obuffer, obj->obuffer_size - 10,
  133.                             (obj->options ?
  134.                              obj->options->dont_touch_citations_p : FALSE));
  135.   if (status < 0) return status;
  136.   XP_ASSERT(*line == 0 || *obj->obuffer);
  137.   return MimeObject_write(obj, obj->obuffer, XP_STRLEN(obj->obuffer), TRUE);
  138. }
  139.