Logo Search packages:      
Sourcecode: tardy version File versions  Download package

buffer.cc

//
//    tardy - a tar post-processor
//    Copyright (C) 2003 Peter Miller;
//    All rights reserved.
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program; if not, write to the Free Software
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
//
// MANIFEST: functions to manipulate buffers
//

#pragma implementation "file_output_buffer"

#include <ac/string.h>
#include <file/output/buffer.h>


00029 file_output_buffer::~file_output_buffer()
{
    if (pos > 0)
    {
      deeper->write(buffer, pos);
      pos = 0;
    }
    delete buffer;
    buffer = 0;
    delete deeper;
    deeper = 0;
}


00043 file_output_buffer::file_output_buffer(file_output *arg1, int arg2) :
    deeper(arg1),
    blocksize(arg2 > 0 ? arg2 : 512),
    buffer(0),
    pos(0)
{
}


const char *
00053 file_output_buffer::filename()
    const
{
    return deeper->filename();
}


void
00061 file_output_buffer::write(const void *data, int nbytes)
{
    while (nbytes > 0)
    {
      //
      // Don't double handle the data if we can avoid it.
      // (But only write `blocksize' bytes at a time.)
      //
      if (pos == 0 && nbytes >= blocksize)
      {
          deeper->write(data, blocksize);
          data = (char *)data + blocksize;
          nbytes -= blocksize;
          continue;
      }

      //
      // Add the data to the end of the buffer.
      //
      int len = blocksize - pos;
      if (len > nbytes)
          len = nbytes;
      if (!buffer)
          buffer = new char[blocksize];
      memcpy(buffer + pos, data, len);
      pos += len;
      if (pos >= blocksize)
      {
          //
          // If the buffer is full,
          // write it out and start again.
          //
          deeper->write(buffer, pos);
          pos = 0;
      }
      data = (char *)data + len;
      nbytes -= len;
    }
}

Generated by  Doxygen 1.6.0   Back to index