diff src/win32/7zip/7z/CPP/7zip/Common/InOutTempBuffer.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/InOutTempBuffer.cpp	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,122 @@
     1.4 +// InOutTempBuffer.cpp
     1.5 +
     1.6 +#include "StdAfx.h"
     1.7 +
     1.8 +#include "InOutTempBuffer.h"
     1.9 +#include "../../Common/Defs.h"
    1.10 +// #include "Windows/Defs.h"
    1.11 +
    1.12 +#include "StreamUtils.h"
    1.13 +
    1.14 +using namespace NWindows;
    1.15 +using namespace NFile;
    1.16 +using namespace NDirectory;
    1.17 +
    1.18 +static UInt32 kTmpBufferMemorySize = (1 << 20);
    1.19 +
    1.20 +static LPCTSTR kTempFilePrefixString = TEXT("iot");
    1.21 +
    1.22 +CInOutTempBuffer::CInOutTempBuffer():
    1.23 +  _buffer(NULL)
    1.24 +{
    1.25 +}
    1.26 +
    1.27 +void CInOutTempBuffer::Create()
    1.28 +{
    1.29 +  _buffer = new Byte[kTmpBufferMemorySize];
    1.30 +}
    1.31 +
    1.32 +CInOutTempBuffer::~CInOutTempBuffer()
    1.33 +{
    1.34 +  delete []_buffer;
    1.35 +}
    1.36 +void CInOutTempBuffer::InitWriting()
    1.37 +{
    1.38 +  _bufferPosition = 0;
    1.39 +  _tmpFileCreated = false;
    1.40 +  _fileSize = 0;
    1.41 +}
    1.42 +
    1.43 +bool CInOutTempBuffer::WriteToFile(const void *data, UInt32 size)
    1.44 +{
    1.45 +  if (size == 0)
    1.46 +    return true;
    1.47 +  if(!_tmpFileCreated)
    1.48 +  {
    1.49 +    CSysString tempDirPath;
    1.50 +    if(!MyGetTempPath(tempDirPath))
    1.51 +      return false;
    1.52 +    if (_tempFile.Create(tempDirPath, kTempFilePrefixString, _tmpFileName) == 0)
    1.53 +      return false;
    1.54 +    // _outFile.SetOpenCreationDispositionCreateAlways();
    1.55 +    if(!_outFile.Create(_tmpFileName, true))
    1.56 +      return false;
    1.57 +    _tmpFileCreated = true;
    1.58 +  }
    1.59 +  UInt32 processedSize;
    1.60 +  if(!_outFile.Write(data, size, processedSize))
    1.61 +    return false;
    1.62 +  _fileSize += processedSize;
    1.63 +  return (processedSize == size);
    1.64 +}
    1.65 +
    1.66 +bool CInOutTempBuffer::FlushWrite()
    1.67 +{
    1.68 +  return _outFile.Close();
    1.69 +}
    1.70 +
    1.71 +bool CInOutTempBuffer::Write(const void *data, UInt32 size)
    1.72 +{
    1.73 +  if(_bufferPosition < kTmpBufferMemorySize)
    1.74 +  {
    1.75 +    UInt32 curSize = MyMin(kTmpBufferMemorySize - _bufferPosition, size);
    1.76 +    memmove(_buffer + _bufferPosition, (const Byte *)data, curSize);
    1.77 +    _bufferPosition += curSize;
    1.78 +    size -= curSize;
    1.79 +    data = ((const Byte *)data) + curSize;
    1.80 +    _fileSize += curSize;
    1.81 +  }
    1.82 +  return WriteToFile(data, size);
    1.83 +}
    1.84 +
    1.85 +bool CInOutTempBuffer::InitReading()
    1.86 +{
    1.87 +  _currentPositionInBuffer = 0;
    1.88 +  if(_tmpFileCreated)
    1.89 +    return _inFile.Open(_tmpFileName);
    1.90 +  return true;
    1.91 +}
    1.92 +
    1.93 +HRESULT CInOutTempBuffer::WriteToStream(ISequentialOutStream *stream)
    1.94 +{
    1.95 +  if (_currentPositionInBuffer < _bufferPosition)
    1.96 +  {
    1.97 +    UInt32 sizeToWrite = _bufferPosition - _currentPositionInBuffer;
    1.98 +    RINOK(WriteStream(stream, _buffer + _currentPositionInBuffer, sizeToWrite));
    1.99 +    _currentPositionInBuffer += sizeToWrite;
   1.100 +  }
   1.101 +  if (!_tmpFileCreated)
   1.102 +    return true;
   1.103 +  for (;;)
   1.104 +  {
   1.105 +    UInt32 localProcessedSize;
   1.106 +    if (!_inFile.ReadPart(_buffer, kTmpBufferMemorySize, localProcessedSize))
   1.107 +      return E_FAIL;
   1.108 +    if (localProcessedSize == 0)
   1.109 +      return S_OK;
   1.110 +    RINOK(WriteStream(stream, _buffer, localProcessedSize));
   1.111 +  }
   1.112 +}
   1.113 +
   1.114 +STDMETHODIMP CSequentialOutTempBufferImp::Write(const void *data, UInt32 size, UInt32 *processedSize)
   1.115 +{
   1.116 +  if (!_buffer->Write(data, size))
   1.117 +  {
   1.118 +    if (processedSize != NULL)
   1.119 +      *processedSize = 0;
   1.120 +    return E_FAIL;
   1.121 +  }
   1.122 +  if (processedSize != NULL)
   1.123 +    *processedSize = size;
   1.124 +  return S_OK;
   1.125 +}