Back to index

lightning-sunbird  0.9+nobinonly
Typedefs | Functions
mimepbuf.h File Reference
#include "mimei.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct MimePartBufferData

Functions

MimePartBufferDataMimePartBufferCreate (void)
void MimePartBufferClose (MimePartBufferData *data)
void MimePartBufferReset (MimePartBufferData *data)
void MimePartBufferDestroy (MimePartBufferData *data)
int MimePartBufferWrite (MimePartBufferData *data, const char *buf, PRInt32 size)
int MimePartBufferRead (MimePartBufferData *data, nsresult(*read_fn)(const char *buf, PRInt32 size, void *closure), void *closure)

Typedef Documentation

typedef struct MimePartBufferData

Definition at line 63 of file mimepbuf.h.


Function Documentation

Definition at line 108 of file mimepbuf.cpp.

{
  NS_ASSERTION(data, "MimePartBufferClose: no data");
  if (!data) return;

       if (data->input_file_stream) 
  {
              data->input_file_stream->close();
    delete data->input_file_stream;
    data->input_file_stream = nsnull;
       }

  if (data->output_file_stream) 
  {
              data->output_file_stream->close();
    delete data->output_file_stream;
    data->output_file_stream = nsnull;
       }
}

Here is the caller graph for this function:

Definition at line 98 of file mimepbuf.cpp.

{
  MimePartBufferData *data = PR_NEW(MimePartBufferData);
  if (!data) return 0;
  memset(data, 0, sizeof(*data));
  return data;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 162 of file mimepbuf.cpp.

{
  NS_ASSERTION(data, "MimePartBufferDestroy: no data");
  if (!data) return;
  MimePartBufferReset (data);
  PR_Free(data);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int MimePartBufferRead ( MimePartBufferData data,
nsresult(*)(const char *buf, PRInt32 size, void *closure read_fn,
void closure 
)

Definition at line 273 of file mimepbuf.cpp.

{
  int status = 0;
  NS_ASSERTION(data, "no data");
  if (!data) return -1;

  if (data->part_buffer)
       {
         // Read it out of memory.
         status = read_fn(data->part_buffer, data->part_buffer_fp, closure);
       }
  else if (data->file_buffer_spec)
       {
         /* Read it off disk.
          */
         char *buf;
         PRInt32 buf_size = DISK_BUFFER_SIZE;

         NS_ASSERTION(data->part_buffer_size == 0 && data->part_buffer_fp == 0, "buffer size is not null");
         NS_ASSERTION(data->file_buffer_spec, "no file buffer name");
         if (!data->file_buffer_spec) 
      return -1;

         buf = (char *) PR_MALLOC(buf_size);
         if (!buf) 
      return MIME_OUT_OF_MEMORY;

    // First, close the output file to open the input file!
    if (data->output_file_stream)
              data->output_file_stream->close();

              data->input_file_stream = new nsInputFileStream(*(data->file_buffer_spec));
              if (!data->input_file_stream) 
    {
                     PR_Free(buf);
                     return MIME_UNABLE_TO_OPEN_TMP_FILE;
              }

    while(1)
              {
                PRInt32 rstatus = data->input_file_stream->read(buf, buf_size - 1);
                if (rstatus <= 0)
                     {
                       status = rstatus;
                       break;
                     }
                else
                     {
                       /* It would be really nice to be able to yield here, and let
                             some user events and other input sources get processed.
                             Oh well. */

                       status = read_fn (buf, rstatus, closure);
                       if (status < 0) break;
                     }
              }
         PR_Free(buf);
       }

  return 0;
}

Here is the caller graph for this function:

Definition at line 130 of file mimepbuf.cpp.

{
  NS_ASSERTION(data, "MimePartBufferReset: no data");
  if (!data) return;

  PR_FREEIF(data->part_buffer);
  data->part_buffer_fp = 0;

       if (data->input_file_stream) 
  {
              data->input_file_stream->close();
    delete data->input_file_stream;
    data->input_file_stream = nsnull;
       }

  if (data->output_file_stream) 
  {
              data->output_file_stream->close();
    delete data->output_file_stream;
    data->output_file_stream = nsnull;
       }

       if (data->file_buffer_spec) 
  {
    data->file_buffer_spec->Delete(PR_FALSE);
    delete data->file_buffer_spec;
    data->file_buffer_spec = nsnull;
       }
}

Here is the caller graph for this function:

int MimePartBufferWrite ( MimePartBufferData data,
const char *  buf,
PRInt32  size 
)

Definition at line 172 of file mimepbuf.cpp.

{
  int status = 0;

  NS_ASSERTION(data && buf && size > 0, "MimePartBufferWrite: Bad param");
  if (!data || !buf || size <= 0)
       return -1;

  /* If we don't yet have a buffer (either memory or file) try and make a
        memory buffer.
   */
  if (!data->part_buffer &&
           !data->file_buffer_spec)
       {
         int target_size = TARGET_MEMORY_BUFFER_SIZE;
         while (target_size > 0)
              {
                data->part_buffer = (char *) PR_MALLOC(target_size);
                if (data->part_buffer) break;                                /* got it! */
                target_size -= TARGET_MEMORY_BUFFER_QUANTUM;   /* decrease it and try
                                                                                                     again */
              }

         if (data->part_buffer)
              data->part_buffer_size = target_size;
         else
              data->part_buffer_size = 0;

         data->part_buffer_fp = 0;
       }

  /* Ok, if at this point we still don't have either kind of buffer, try and
        make a file buffer. */
  if (!data->part_buffer && !data->file_buffer_spec)
       {
    data->file_buffer_spec = nsMsgCreateTempFileSpec("nsma");
              if (!data->file_buffer_spec) 
      return MIME_OUT_OF_MEMORY;

    data->output_file_stream = new nsOutputFileStream(*(data->file_buffer_spec), PR_WRONLY | PR_CREATE_FILE, 00600);
              if (!data->output_file_stream) 
    {
                     return MIME_UNABLE_TO_OPEN_TMP_FILE;
              }
  }

  NS_ASSERTION(data->part_buffer || data->output_file_stream, "no part_buffer or file_stream");

  /* If this buf will fit in the memory buffer, put it there.
   */
  if (data->part_buffer &&
         data->part_buffer_fp + size < data->part_buffer_size)
       {
         memcpy(data->part_buffer + data->part_buffer_fp,
                buf, size);
         data->part_buffer_fp += size;
       }

  /* Otherwise it won't fit; write it to the file instead. */
  else
       {
         /* If the file isn't open yet, open it, and dump the memory buffer
               to it. */
         if (!data->output_file_stream)
              {
                if (!data->file_buffer_spec)
                     data->file_buffer_spec = nsMsgCreateTempFileSpec("nsma");
                if (!data->file_buffer_spec) 
        return MIME_OUT_OF_MEMORY;

      data->output_file_stream = new nsOutputFileStream(*(data->file_buffer_spec), PR_WRONLY | PR_CREATE_FILE, 00600);
                if (!data->output_file_stream) 
      {
                       return MIME_UNABLE_TO_OPEN_TMP_FILE;
                }

                if (data->part_buffer && data->part_buffer_fp)
                     {
                       status = data->output_file_stream->write(data->part_buffer,
                                                                       data->part_buffer_fp);
                       if (status < data->part_buffer_fp) 
          return MIME_OUT_OF_MEMORY;
                     }

                PR_FREEIF(data->part_buffer);
                data->part_buffer_fp = 0;
                data->part_buffer_size = 0;
              }

         /* Dump this buf to the file. */
         status = data->output_file_stream->write (buf, size);
         if (status < size) 
      return MIME_OUT_OF_MEMORY;
       }

  return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function: