diff src/win32/7zip/7z/CPP/7zip/Common/OutBuffer.cpp @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/win32/7zip/7z/CPP/7zip/Common/OutBuffer.cpp	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,119 @@
     1.4 +// OutByte.cpp
     1.5 +
     1.6 +#include "StdAfx.h"
     1.7 +
     1.8 +#include "OutBuffer.h"
     1.9 +
    1.10 +extern "C"
    1.11 +{
    1.12 +  #include "../../../C/Alloc.h"
    1.13 +}
    1.14 +
    1.15 +bool COutBuffer::Create(UInt32 bufferSize)
    1.16 +{
    1.17 +  const UInt32 kMinBlockSize = 1;
    1.18 +  if (bufferSize < kMinBlockSize)
    1.19 +    bufferSize = kMinBlockSize;
    1.20 +  if (_buffer != 0 && _bufferSize == bufferSize)
    1.21 +    return true;
    1.22 +  Free();
    1.23 +  _bufferSize = bufferSize;
    1.24 +  _buffer = (Byte *)::MidAlloc(bufferSize);
    1.25 +  return (_buffer != 0);
    1.26 +}
    1.27 +
    1.28 +void COutBuffer::Free()
    1.29 +{
    1.30 +  ::MidFree(_buffer);
    1.31 +  _buffer = 0;
    1.32 +}
    1.33 +
    1.34 +void COutBuffer::SetStream(ISequentialOutStream *stream)
    1.35 +{
    1.36 +  _stream = stream;
    1.37 +}
    1.38 +
    1.39 +void COutBuffer::Init()
    1.40 +{
    1.41 +  _streamPos = 0;
    1.42 +  _limitPos = _bufferSize;
    1.43 +  _pos = 0;
    1.44 +  _processedSize = 0;
    1.45 +  _overDict = false;
    1.46 +  #ifdef _NO_EXCEPTIONS
    1.47 +  ErrorCode = S_OK;
    1.48 +  #endif
    1.49 +}
    1.50 +
    1.51 +UInt64 COutBuffer::GetProcessedSize() const
    1.52 +{
    1.53 +  UInt64 res = _processedSize + _pos - _streamPos;
    1.54 +  if (_streamPos > _pos)
    1.55 +    res += _bufferSize;
    1.56 +  return res;
    1.57 +}
    1.58 +
    1.59 +
    1.60 +HRESULT COutBuffer::FlushPart()
    1.61 +{
    1.62 +  // _streamPos < _bufferSize
    1.63 +  UInt32 size = (_streamPos >= _pos) ? (_bufferSize - _streamPos) : (_pos - _streamPos);
    1.64 +  HRESULT result = S_OK;
    1.65 +  #ifdef _NO_EXCEPTIONS
    1.66 +  result = ErrorCode;
    1.67 +  #endif
    1.68 +  if (_buffer2 != 0)
    1.69 +  {
    1.70 +    memmove(_buffer2, _buffer + _streamPos, size);
    1.71 +    _buffer2 += size;
    1.72 +  }
    1.73 +
    1.74 +  if (_stream != 0
    1.75 +      #ifdef _NO_EXCEPTIONS
    1.76 +      && (ErrorCode == S_OK)
    1.77 +      #endif
    1.78 +     )
    1.79 +  {
    1.80 +    UInt32 processedSize = 0;
    1.81 +    result = _stream->Write(_buffer + _streamPos, size, &processedSize);
    1.82 +    size = processedSize;
    1.83 +  }
    1.84 +  _streamPos += size;
    1.85 +  if (_streamPos == _bufferSize)
    1.86 +    _streamPos = 0;
    1.87 +  if (_pos == _bufferSize)
    1.88 +  {
    1.89 +    _overDict = true;
    1.90 +    _pos = 0;
    1.91 +  }
    1.92 +  _limitPos = (_streamPos > _pos) ? _streamPos : _bufferSize;
    1.93 +  _processedSize += size;
    1.94 +  return result;
    1.95 +}
    1.96 +
    1.97 +HRESULT COutBuffer::Flush()
    1.98 +{
    1.99 +  #ifdef _NO_EXCEPTIONS
   1.100 +  if (ErrorCode != S_OK)
   1.101 +    return ErrorCode;
   1.102 +  #endif
   1.103 +
   1.104 +  while(_streamPos != _pos)
   1.105 +  {
   1.106 +    HRESULT result = FlushPart();
   1.107 +    if (result != S_OK)
   1.108 +      return result;
   1.109 +  }
   1.110 +  return S_OK;
   1.111 +}
   1.112 +
   1.113 +void COutBuffer::FlushWithCheck()
   1.114 +{
   1.115 +  HRESULT result = Flush();
   1.116 +  #ifdef _NO_EXCEPTIONS
   1.117 +  ErrorCode = result;
   1.118 +  #else
   1.119 +  if (result != S_OK)
   1.120 +    throw COutBufferException(result);
   1.121 +  #endif
   1.122 +}