Annotation of elwix/tools/oldlzma/SRC/7zip/Common/FileStreams.cpp, revision 1.1

1.1     ! misho       1: // FileStreams.cpp
        !             2: 
        !             3: #include "StdAfx.h"
        !             4: 
        !             5: #ifndef _WIN32
        !             6: #include <fcntl.h>
        !             7: #include <unistd.h>
        !             8: #endif
        !             9: 
        !            10: #include "FileStreams.h"
        !            11: 
        !            12: static inline HRESULT ConvertBoolToHRESULT(bool result)
        !            13: {
        !            14:   // return result ? S_OK: E_FAIL;
        !            15:   #ifdef _WIN32
        !            16:   return result ? S_OK: (::GetLastError());
        !            17:   #else
        !            18:   return result ? S_OK: E_FAIL;
        !            19:   #endif
        !            20: }
        !            21: 
        !            22: bool CInFileStream::Open(LPCTSTR fileName)
        !            23: {
        !            24:   return File.Open(fileName);
        !            25: }
        !            26: 
        !            27: #ifdef _WIN32
        !            28: #ifndef _UNICODE
        !            29: bool CInFileStream::Open(LPCWSTR fileName)
        !            30: {
        !            31:   return File.Open(fileName);
        !            32: }
        !            33: #endif
        !            34: #endif
        !            35: 
        !            36: STDMETHODIMP CInFileStream::Read(void *data, UInt32 size, UInt32 *processedSize)
        !            37: {
        !            38:   #ifdef _WIN32
        !            39:   
        !            40:   UInt32 realProcessedSize;
        !            41:   bool result = File.Read(data, size, realProcessedSize);
        !            42:   if(processedSize != NULL)
        !            43:     *processedSize = realProcessedSize;
        !            44:   return ConvertBoolToHRESULT(result);
        !            45:   
        !            46:   #else
        !            47:   
        !            48:   if(processedSize != NULL)
        !            49:     *processedSize = 0;
        !            50:   ssize_t res = File.Read(data, (size_t)size);
        !            51:   if (res == -1)
        !            52:     return E_FAIL;
        !            53:   if(processedSize != NULL)
        !            54:     *processedSize = (UInt32)res;
        !            55:   return S_OK;
        !            56:   
        !            57:   #endif
        !            58: }
        !            59:   
        !            60: STDMETHODIMP CInFileStream::ReadPart(void *data, UInt32 size, UInt32 *processedSize)
        !            61: {
        !            62:   return Read(data, size, processedSize);
        !            63: }
        !            64: 
        !            65: #ifndef _WIN32_WCE
        !            66: STDMETHODIMP CStdInFileStream::Read(void *data, UInt32 size, UInt32 *processedSize)
        !            67: {
        !            68:   #ifdef _WIN32
        !            69:   UInt32 realProcessedSize;
        !            70:   BOOL res = ::ReadFile(GetStdHandle(STD_INPUT_HANDLE), 
        !            71:       data, size, (DWORD *)&realProcessedSize, NULL);
        !            72:   if(processedSize != NULL)
        !            73:     *processedSize = realProcessedSize;
        !            74:   if (res == FALSE && GetLastError() == ERROR_BROKEN_PIPE)
        !            75:     return S_OK;
        !            76:   return ConvertBoolToHRESULT(res != FALSE);
        !            77:   
        !            78:   #else
        !            79: 
        !            80:   if(processedSize != NULL)
        !            81:     *processedSize = 0;
        !            82:   ssize_t res = read(0, data, (size_t)size);
        !            83:   if (res == -1)
        !            84:     return E_FAIL;
        !            85:   if(processedSize != NULL)
        !            86:     *processedSize = (UInt32)res;
        !            87:   return S_OK;
        !            88:   
        !            89:   #endif
        !            90: }
        !            91:   
        !            92: STDMETHODIMP CStdInFileStream::ReadPart(void *data, UInt32 size, UInt32 *processedSize)
        !            93: {
        !            94:   return Read(data, size, processedSize);
        !            95: }
        !            96: #endif
        !            97: 
        !            98: STDMETHODIMP CInFileStream::Seek(Int64 offset, UInt32 seekOrigin, 
        !            99:     UInt64 *newPosition)
        !           100: {
        !           101:   if(seekOrigin >= 3)
        !           102:     return STG_E_INVALIDFUNCTION;
        !           103: 
        !           104:   #ifdef _WIN32
        !           105: 
        !           106:   UInt64 realNewPosition;
        !           107:   bool result = File.Seek(offset, seekOrigin, realNewPosition);
        !           108:   if(newPosition != NULL)
        !           109:     *newPosition = realNewPosition;
        !           110:   return ConvertBoolToHRESULT(result);
        !           111:   
        !           112:   #else
        !           113:   
        !           114:   off_t res = File.Seek(offset, seekOrigin);
        !           115:   if (res == -1)
        !           116:     return E_FAIL;
        !           117:   if(newPosition != NULL)
        !           118:     *newPosition = (UInt64)res;
        !           119:   return S_OK;
        !           120:   
        !           121:   #endif
        !           122: }
        !           123: 
        !           124: STDMETHODIMP CInFileStream::GetSize(UInt64 *size)
        !           125: {
        !           126:   return ConvertBoolToHRESULT(File.GetLength(*size));
        !           127: }
        !           128: 
        !           129: 
        !           130: //////////////////////////
        !           131: // COutFileStream
        !           132: 
        !           133: bool COutFileStream::Create(LPCTSTR fileName, bool createAlways)
        !           134: {
        !           135:   return File.Create(fileName, createAlways);
        !           136: }
        !           137: 
        !           138: #ifdef _WIN32
        !           139: #ifndef _UNICODE
        !           140: bool COutFileStream::Create(LPCWSTR fileName, bool createAlways)
        !           141: {
        !           142:   return File.Create(fileName, createAlways);
        !           143: }
        !           144: #endif
        !           145: #endif
        !           146: 
        !           147: STDMETHODIMP COutFileStream::Write(const void *data, UInt32 size, UInt32 *processedSize)
        !           148: {
        !           149:   #ifdef _WIN32
        !           150: 
        !           151:   UInt32 realProcessedSize;
        !           152:   bool result = File.Write(data, size, realProcessedSize);
        !           153:   if(processedSize != NULL)
        !           154:     *processedSize = realProcessedSize;
        !           155:   return ConvertBoolToHRESULT(result);
        !           156:   
        !           157:   #else
        !           158:   
        !           159:   if(processedSize != NULL)
        !           160:     *processedSize = 0;
        !           161:   ssize_t res = File.Write(data, (size_t)size);
        !           162:   if (res == -1)
        !           163:     return E_FAIL;
        !           164:   if(processedSize != NULL)
        !           165:     *processedSize = (UInt32)res;
        !           166:   return S_OK;
        !           167:   
        !           168:   #endif
        !           169: }
        !           170:   
        !           171: STDMETHODIMP COutFileStream::WritePart(const void *data, UInt32 size, UInt32 *processedSize)
        !           172: {
        !           173:   return Write(data, size, processedSize);
        !           174: }
        !           175: 
        !           176: 
        !           177: STDMETHODIMP COutFileStream::Seek(Int64 offset, UInt32 seekOrigin, 
        !           178:     UInt64 *newPosition)
        !           179: {
        !           180:   if(seekOrigin >= 3)
        !           181:     return STG_E_INVALIDFUNCTION;
        !           182:   #ifdef _WIN32
        !           183: 
        !           184:   UInt64 realNewPosition;
        !           185:   bool result = File.Seek(offset, seekOrigin, realNewPosition);
        !           186:   if(newPosition != NULL)
        !           187:     *newPosition = realNewPosition;
        !           188:   return ConvertBoolToHRESULT(result);
        !           189:   
        !           190:   #else
        !           191:   
        !           192:   off_t res = File.Seek(offset, seekOrigin);
        !           193:   if (res == -1)
        !           194:     return E_FAIL;
        !           195:   if(newPosition != NULL)
        !           196:     *newPosition = (UInt64)res;
        !           197:   return S_OK;
        !           198:   
        !           199:   #endif
        !           200: }
        !           201: 
        !           202: STDMETHODIMP COutFileStream::SetSize(Int64 newSize)
        !           203: {
        !           204:   #ifdef _WIN32
        !           205:   UInt64 currentPos;
        !           206:   if(!File.Seek(0, FILE_CURRENT, currentPos))
        !           207:     return E_FAIL;
        !           208:   bool result = File.SetLength(newSize);
        !           209:   UInt64 currentPos2;
        !           210:   result = result && File.Seek(currentPos, currentPos2);
        !           211:   return result ? S_OK : E_FAIL;
        !           212:   #else
        !           213:   return E_FAIL;
        !           214:   #endif
        !           215: }
        !           216: 
        !           217: #ifndef _WIN32_WCE
        !           218: STDMETHODIMP CStdOutFileStream::Write(const void *data, UInt32 size, UInt32 *processedSize)
        !           219: {
        !           220:   if(processedSize != NULL)
        !           221:     *processedSize = 0;
        !           222: 
        !           223:   #ifdef _WIN32
        !           224:   UInt32 realProcessedSize;
        !           225:   BOOL res = TRUE;
        !           226:   while (size > 0)
        !           227:   {
        !           228:     // Seems that Windows doesn't like big amounts writing to stdout.
        !           229:     // So we limit portions by 32KB.
        !           230:     UInt32 sizeTemp = (1 << 15); 
        !           231:     if (sizeTemp > size)
        !           232:       sizeTemp = size;
        !           233:     res = ::WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), 
        !           234:         data, sizeTemp, (DWORD *)&realProcessedSize, NULL);
        !           235:     if (realProcessedSize == 0)
        !           236:       break;
        !           237:     size -= realProcessedSize;
        !           238:     data = (const void *)((const Byte *)data + realProcessedSize);
        !           239:     if(processedSize != NULL)
        !           240:       *processedSize += realProcessedSize;
        !           241:   }
        !           242:   return ConvertBoolToHRESULT(res != FALSE);
        !           243: 
        !           244:   #else
        !           245:   
        !           246:   ssize_t res = write(1, data, (size_t)size);
        !           247:   if (res == -1)
        !           248:     return E_FAIL;
        !           249:   if(processedSize != NULL)
        !           250:     *processedSize = (UInt32)res;
        !           251:   return S_OK;
        !           252:   
        !           253:   return S_OK;
        !           254:   #endif
        !           255: }
        !           256:   
        !           257: STDMETHODIMP CStdOutFileStream::WritePart(const void *data, UInt32 size, UInt32 *processedSize)
        !           258: {
        !           259:   return Write(data, size, processedSize);
        !           260: }
        !           261: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>