From 8f74b9362aa8063b3352bf54c8d6bd82b2eb7bb3 Mon Sep 17 00:00:00 2001 From: Ivan Morozov Date: Mon, 1 Aug 2022 19:41:46 +0300 Subject: [PATCH] Revert "test NSFile::CFileBinary as Stream" This reverts commit 54261fd1a18eda898ba08de9acad55caf4407bc5. --- Common/cppcf/cfcpp.pro | 1 + Common/cppcf/cfstream.cpp | 7 +- Common/cppcf/compoundfile.cpp | 124 +++++++++++++------------ Common/cppcf/compoundfile.h | 3 +- Common/cppcf/directoryentry.cpp | 17 ++-- Common/cppcf/sector.cpp | 17 ++-- Common/cppcf/stream.cpp | 15 +++ Common/cppcf/stream.h | 8 +- Common/cppcf/streamrw.cpp | 30 +++++- Common/cppcf/streamrw.h | 31 +++---- Common/cppcf/test/data/ex.ppt | Bin 8192 -> 0 bytes Common/cppcf/test/stream_wrapper.h | 38 -------- Common/cppcf/test/test.pro | 1 - Common/cppcf/test/tst_compondfile.h | 3 +- Common/cppcf/test/tst_directoryentry.h | 22 ++--- Common/cppcf/test/tst_header.h | 19 ++-- Common/cppcf/test/tst_streamrw.h | 27 +++--- 17 files changed, 179 insertions(+), 184 deletions(-) create mode 100644 Common/cppcf/stream.cpp delete mode 100644 Common/cppcf/test/stream_wrapper.h diff --git a/Common/cppcf/cfcpp.pro b/Common/cppcf/cfcpp.pro index 56dab559a8..557adc0068 100644 --- a/Common/cppcf/cfcpp.pro +++ b/Common/cppcf/cfcpp.pro @@ -25,6 +25,7 @@ SOURCES += \ header.cpp \ sector.cpp \ sectorcollection.cpp \ + stream.cpp \ streamrw.cpp \ streamview.cpp diff --git a/Common/cppcf/cfstream.cpp b/Common/cppcf/cfstream.cpp index ac4d22b151..e12a5aa13b 100644 --- a/Common/cppcf/cfstream.cpp +++ b/Common/cppcf/cfstream.cpp @@ -69,15 +69,14 @@ void CFStream::CopyFrom(const Stream &input) { CheckDisposed(); - std::vector buffer(input->SizeFile()); + std::vector buffer(Length(input)); // if (input.CanSeek) { - input->SeekFile(0, SEEK_SET); + input->seekg(0, std::ios::beg); } - DWORD bytesWasRead(0); - input->ReadFile(buffer.data(), input->SizeFile(), bytesWasRead); + input->read(reinterpret_cast(buffer.data()), Length(input)); SetData(buffer); } diff --git a/Common/cppcf/compoundfile.cpp b/Common/cppcf/compoundfile.cpp index c145f4f001..52be03e997 100644 --- a/Common/cppcf/compoundfile.cpp +++ b/Common/cppcf/compoundfile.cpp @@ -100,10 +100,10 @@ void CompoundFile::Commit(bool releaseMemory) if (header->majorVersion != (ushort)CFSVersion::Ver_3) CheckForLockSector(); - sourceStream->SeekFile(0, SEEK_SET); + sourceStream->seekp(0, std::ios::beg); - std::vector zeroArray(sSize, 0); - sourceStream->WriteFile(zeroArray.data(), zeroArray.size()); + std::vector zeroArray(sSize, 0); + sourceStream->write(zeroArray.data(), zeroArray.size()); zeroArray.clear(); CommitDirectory(); @@ -122,10 +122,10 @@ void CompoundFile::Commit(bool releaseMemory) if (s.get() != nullptr && s->dirtyFlag) { if (gap) - sourceStream->SeekFile((long)((long)(sSize) + (long)i * (long)sSize), SEEK_SET); + sourceStream->seekp((long)((long)(sSize) + (long)i * (long)sSize), std::ios::beg); - sourceStream->WriteFile(s->GetData().data(), sSize); - sourceStream->Flush(); + sourceStream->write(reinterpret_cast(s->GetData().data()), sSize); + sourceStream->flush(); s->dirtyFlag = false; gap = false; @@ -146,11 +146,11 @@ void CompoundFile::Commit(bool releaseMemory) // Seek to beginning position and save header (first 512 or 4096 bytes) - sourceStream->SeekFile(0, SEEK_SET); + sourceStream->seekg(0, std::ios::beg); header->Write(sourceStream); // sourceStream-> SetLength((long)(sectors.Count + 1) * sSize); - sourceStream->Flush(); + sourceStream->flush(); // if (releaseMemory) // GC.Collect(); @@ -201,9 +201,9 @@ void CompoundFile::Load(Stream stream) header->Read(stream); - int n_sector = std::ceil(((double)(stream->SizeFile() - GetSectorSize()) / (double)GetSectorSize())); + int n_sector = std::ceil(((double)(Length(stream) - GetSectorSize()) / (double)GetSectorSize())); - if (stream->SizeFile() > 0x7FFFFF0) + if (Length(stream) > 0x7FFFFF0) this->_transactionLockAllocated = true; @@ -220,9 +220,9 @@ void CompoundFile::Load(Stream stream) } catch (...) { - if (stream != nullptr && closeStream) + if (std::dynamic_pointer_cast(stream) != nullptr && stream.get() != nullptr && closeStream) { - stream->CloseFile(); + std::static_pointer_cast(stream)->close(); } throw; @@ -234,8 +234,12 @@ void CompoundFile::Save(std::wstring wFileName) if (_disposed) throw CFException("Compound File closed: cannot save data"); - Stream fs(new NSFile::CStreamWriter); - fs->CreateFileW(wFileName); + BYTE* pUtf8 = NULL; + LONG lLen = 0; + NSFile::CUtf8Converter::GetUtf8StringFromUnicode(wFileName.c_str(), wFileName.length(), pUtf8, lLen, false); + std::string utf8FileName = std::string(pUtf8, pUtf8 + lLen); + delete [] pUtf8; + Stream fs(new std::fstream(utf8FileName, std::ios::out)); try { @@ -243,15 +247,15 @@ void CompoundFile::Save(std::wstring wFileName) } catch (std::exception& ex) { - throw CFException(L"Error saving file [" + fileName + L"]", ex); + throw CFException("Error saving file [" + fileName + "]", ex); } // finally { - if (fs != nullptr) - fs->Flush(); + if (fs.get() != nullptr) + fs->flush(); - if (fs != nullptr) - fs->CloseFile(); + if (fs.get() != nullptr) + static_cast(fs.get())->close(); } } @@ -269,8 +273,8 @@ void CompoundFile::Save(Stream stream) try { - std::vector zeroArray(sSize, 0); - stream->WriteFile(zeroArray.data(), zeroArray.size()); + std::vector zeroArray(sSize, 0); + stream->write(zeroArray.data(), zeroArray.size()); zeroArray.clear(); CommitDirectory(); @@ -293,13 +297,13 @@ void CompoundFile::Save(Stream stream) } - stream->WriteFile(s->GetData().data(), sSize); + stream->write(reinterpret_cast(s->GetData().data()), sSize); //s.ReleaseData(); } - stream->TellFile(); + stream->seekp(0, std::ios::beg); header->Write(stream); } catch (std::exception &ex) @@ -502,7 +506,7 @@ SVector CompoundFile::GetNormalSectorChain(int secID) result.push_back(s); - fatStream.Seek(nextSecID * 4, SEEK_SET); + fatStream.Seek(nextSecID * 4, std::ios::beg); int next = fatStream.ReadInt32(); EnsureUniqueSectorIndex(next, processedSectors); @@ -545,12 +549,12 @@ SVector CompoundFile::GetMiniSectorChain(int secID) ms->id = nextSecID; ms->type = SectorType::Mini; - miniStreamView.Seek(nextSecID * Sector::MINISECTOR_SIZE, SEEK_SET); + miniStreamView.Seek(nextSecID * Sector::MINISECTOR_SIZE, std::ios::beg); miniStreamView.Read(reinterpret_cast(ms->GetData().data()), 0, Sector::MINISECTOR_SIZE); result.push_back(ms); - miniFATView.Seek(nextSecID * 4, SEEK_SET); + miniFATView.Seek(nextSecID * 4, std::ios::beg); int next = miniFATView.ReadInt32(); nextSecID = next; @@ -583,8 +587,7 @@ SVector CompoundFile::GetSectorChain(int secID, SectorType chainType) void CompoundFile::EnsureUniqueSectorIndex(int nextSecID, std::unordered_set& processedSectors) { - bool isContain = processedSectors.find(nextSecID) != processedSectors.end(); - if (isContain && this->validationExceptionEnabled) + if (processedSectors.find(nextSecID) != processedSectors.end() && this->validationExceptionEnabled) { throw CFCorruptedFileException("The file is corrupted."); } @@ -849,7 +852,7 @@ void CompoundFile::FreeMiniChain(SVector §orChain, int nth_sector_to if (s->id != -1) { // Overwrite - miniStreamView.Seek(Sector::MINISECTOR_SIZE * s->id, SEEK_SET); + miniStreamView.Seek(Sector::MINISECTOR_SIZE * s->id, std::ios::beg); miniStreamView.Write(ZEROED_MINI_SECTOR.data(), 0, Sector::MINISECTOR_SIZE); } } @@ -860,7 +863,7 @@ void CompoundFile::FreeMiniChain(SVector §orChain, int nth_sector_to { int currentId = sectorChain[i]->id; - miniFATView.Seek(currentId * 4, SEEK_SET); + miniFATView.Seek(currentId * 4, std::ios::beg); const int freesec = Sector::FREESECT; miniFATView.Write(reinterpret_cast(&freesec), 0, 4); } @@ -872,7 +875,7 @@ void CompoundFile::FreeMiniChain(SVector §orChain, int nth_sector_to // Write End of Chain in MiniFAT --------------------------------------- if (nth_sector_to_remove > 0 && sectorChain.size() > 0) { - miniFATView.Seek(sectorChain[nth_sector_to_remove - 1]->id * 4, SEEK_SET); + miniFATView.Seek(sectorChain[nth_sector_to_remove - 1]->id * 4, std::ios::beg); const int endofchain = Sector::ENDOFCHAIN; miniFATView.Write(reinterpret_cast(&endofchain), 0, 4); } @@ -915,7 +918,7 @@ void CompoundFile::FreeChain(SVector §orChain, int nth_sector_to_rem { int currentId = sectorChain[i]->id; - FATView.Seek(currentId * 4, SEEK_SET); + FATView.Seek(currentId * 4, std::ios::beg); const int freesec = Sector::FREESECT; FATView.Write(reinterpret_cast(&freesec), 0, 4); } @@ -923,7 +926,7 @@ void CompoundFile::FreeChain(SVector §orChain, int nth_sector_to_rem // Write new end of chain if partial free ---------- if (nth_sector_to_remove > 0 && sectorChain.size() > 0) { - FATView.Seek(sectorChain[nth_sector_to_remove - 1]->id * 4, SEEK_SET); + FATView.Seek(sectorChain[nth_sector_to_remove - 1]->id * 4, std::ios::beg); const int endofchain = Sector::ENDOFCHAIN; FATView.Write(reinterpret_cast(&endofchain), 0, 4); } @@ -970,11 +973,11 @@ void CompoundFile::AllocateFATSectorChain(SVector §orChain) auto sN = sectorChain[i + 1]; auto sC = sectorChain[i]; - fatStream.Seek(sC->id * 4, SEEK_SET); + fatStream.Seek(sC->id * 4, std::ios::beg); fatStream.Write(reinterpret_cast(&(sN->id)), 0, 4); } - fatStream.Seek(sectorChain[sectorChain.size() - 1]->id * 4, SEEK_SET); + fatStream.Seek(sectorChain[sectorChain.size() - 1]->id * 4, std::ios::beg); const int endofchain = Sector::ENDOFCHAIN; fatStream.Write(reinterpret_cast(&endofchain), 0, 4); @@ -1111,14 +1114,14 @@ void CompoundFile::AllocateDIFATSectorChain(SVector &FATsectorChain) for (int i = 0; i < (int)header->difatSectorsNumber; i++) { - fatSv.Seek(difatStream.BaseSectorChain()[i]->id * 4, SEEK_SET); + fatSv.Seek(difatStream.BaseSectorChain()[i]->id * 4, std::ios::beg); const int difsect = Sector::DIFSECT; fatSv.Write(reinterpret_cast(&difsect), 0, 4); } for (int i = 0; i < header->fatSectorsNumber; i++) { - fatSv.Seek(fatSv.BaseSectorChain()[i]->id * 4, SEEK_SET); + fatSv.Seek(fatSv.BaseSectorChain()[i]->id * 4, std::ios::beg); const int fatsect = Sector::FATSECT; fatSv.Write(reinterpret_cast(&fatsect), 0, 4); } @@ -1166,7 +1169,7 @@ void CompoundFile::AllocateMiniSectorChain(SVector §orChain) // Allocate, position ministream at the end of already allocated // ministream's sectors - miniStreamView.Seek(rootStorage->size() + Sector::MINISECTOR_SIZE, SEEK_SET); + miniStreamView.Seek(rootStorage->size() + Sector::MINISECTOR_SIZE, std::ios::beg); //miniStreamView.Write(s.GetData(), 0, Sector.MINISECTOR_SIZE); s->id = (int)(miniStreamView.position - Sector::MINISECTOR_SIZE) / Sector::MINISECTOR_SIZE; @@ -1180,12 +1183,12 @@ void CompoundFile::AllocateMiniSectorChain(SVector §orChain) int currentId = sectorChain[i]->id; int nextId = sectorChain[i + 1]->id; - miniFATView.Seek(currentId * 4, SEEK_SET); + miniFATView.Seek(currentId * 4, std::ios::beg); miniFATView.Write(reinterpret_cast(&nextId), 0, 4); } // Write End of Chain in MiniFAT - miniFATView.Seek(sectorChain[sectorChain.size() - 1]->id * SIZE_OF_SID, SEEK_SET); + miniFATView.Seek(sectorChain[sectorChain.size() - 1]->id * SIZE_OF_SID, std::ios::beg); const int endofchain = Sector::ENDOFCHAIN; miniFATView.Write(reinterpret_cast(&endofchain), 0, 4); @@ -1221,7 +1224,7 @@ void CompoundFile::PersistMiniStreamToStream(const SVector &miniSectorCh throw CFException("Invalid minisector index"); // Ministream sectors already allocated - miniStreamView.Seek(Sector::MINISECTOR_SIZE * s->id, SEEK_SET); + miniStreamView.Seek(Sector::MINISECTOR_SIZE * s->id, std::ios::beg); miniStreamView.Write(reinterpret_cast(s->GetData().data()), 0, Sector::MINISECTOR_SIZE); } } @@ -1610,7 +1613,7 @@ SList CompoundFile::FindFreeSectors(SectorType sType) ms->id = idx; ms->type = SectorType::Mini; - miniStreamView.Seek(ms->id * Sector::MINISECTOR_SIZE, SEEK_SET); + miniStreamView.Seek(ms->id * Sector::MINISECTOR_SIZE, std::ios::beg); miniStreamView.Read(reinterpret_cast(ms->GetData().data()), 0, Sector::MINISECTOR_SIZE); freeList.enqueue(ms); @@ -1684,7 +1687,7 @@ int CompoundFile::ReadData(CFStream *cFStream, std::streamsize position, std::ve } - sView->Seek(position, SEEK_SET); + sView->Seek(position, std::ios::beg); int result = sView->Read(reinterpret_cast(buffer.data()), 0, count); return result; @@ -1709,7 +1712,7 @@ int CompoundFile::ReadData(CFStream *cFStream, std::streamsize position, std::ve } - sView->Seek(position, SEEK_SET); + sView->Seek(position, std::ios::beg); int result = sView->Read(reinterpret_cast(buffer.data()), offset, count); return result; @@ -1801,7 +1804,7 @@ void CompoundFile::WriteData(std::shared_ptr cfItem, const std::vector zeroQueue; StreamView sv(sectorChain, _sectorSize, newLength, zeroQueue, sourceStream); - sv.Seek(position, SEEK_SET); + sv.Seek(position, std::ios::beg); sv.Write(reinterpret_cast(buffer.data()), offset, count); if (cfItem->size() < header->minSizeStandardStream) @@ -1835,9 +1838,10 @@ void CompoundFile::Dispose(bool disposing) fileName.clear(); } - if (closeStream && !(configuration & CFSConfiguration::LeaveOpen)) + if (std::dynamic_pointer_cast(sourceStream) != nullptr && + closeStream && !(configuration & CFSConfiguration::LeaveOpen)) { - sourceStream->CloseFile(); + std::static_pointer_cast(sourceStream)->close(); } } } @@ -1858,7 +1862,7 @@ void CompoundFile::CheckForLockSector() { StreamView fatStream(GetFatSectorChain(), GetSectorSize(), sourceStream); - fatStream.Seek(_lockSectorId * 4, SEEK_SET); + fatStream.Seek(_lockSectorId * 4, std::ios::beg); const int endofchain = Sector::ENDOFCHAIN; fatStream.Write(reinterpret_cast(&endofchain), 0, 4); @@ -1873,24 +1877,25 @@ void CompoundFile::LoadFile(std::wstring fileName) try { + NSFile::CFileBinary file; + file.OpenFile(fileName); if (updateMode == CFSUpdateMode::ReadOnly) { - fs.reset(new NSFile::CStreamWriter); - fs->OpenFile(this->fileName, false); + fs.reset(new std::fstream(this->fileName, std::ios::out)); + } else { - fs.reset(new NSFile::CStreamWriter); - fs->CreateFileW(this->fileName); + fs.reset(new std::fstream(this->fileName, std::ios::in | std::ios::out)); } - fs->TellFile(); Load(fs); + } catch(...) { - if (fs != nullptr) - fs->CloseFile(); // close + if (fs.get() != nullptr) + fs->clear(); // close throw; } @@ -1898,7 +1903,12 @@ void CompoundFile::LoadFile(std::wstring fileName) void CompoundFile::SetFileName(std::wstring fileName) { - this->fileName = fileName; + BYTE* pUtf8 = NULL; + std::streamsize lLen = 0; + NSFile::CUtf8Converter::GetUtf8StringFromUnicode(fileName.c_str(), fileName.length(), pUtf8, lLen, false); + this->fileName = std::string(pUtf8, pUtf8 + lLen); + delete [] pUtf8; + } void CompoundFile::LoadStream(Stream stream) @@ -1910,7 +1920,7 @@ void CompoundFile::LoadStream(Stream stream) throw CFException("Cannot load a non-seekable Stream"); - stream->TellFile(); + stream->seekp(0, std::ios::beg); Load(stream); } diff --git a/Common/cppcf/compoundfile.h b/Common/cppcf/compoundfile.h index 98817e085f..6baf8629b9 100644 --- a/Common/cppcf/compoundfile.h +++ b/Common/cppcf/compoundfile.h @@ -137,7 +137,8 @@ private: static constexpr int FLUSHING_QUEUE_SIZE = 6000; static constexpr int FLUSHING_BUFFER_MAX_SIZE = 1024 * 1024 * 16; SectorCollection sectors; - std::wstring fileName; + std::fstream stream; + std::string fileName; std::shared_ptr rootStorage; bool closeStream = true; diff --git a/Common/cppcf/directoryentry.cpp b/Common/cppcf/directoryentry.cpp index 84af104228..1782832fff 100644 --- a/Common/cppcf/directoryentry.cpp +++ b/Common/cppcf/directoryentry.cpp @@ -2,7 +2,6 @@ #include "cfexception.h" #include "streamrw.h" #include -#include using namespace CFCPP; @@ -92,17 +91,17 @@ int DirectoryEntry::GetHashCode() const void DirectoryEntry::Write(Stream stream) const { StreamRW rw(stream); - rw.WriteArray(reinterpret_cast(entryName), sizeof (entryName)); + rw.WriteArray(entryName, sizeof (entryName)); rw.Write(nameLength); rw.Write((BYTE)stgType); rw.Write((BYTE)stgColor); rw.Write(leftSibling); rw.Write(rightSibling); rw.Write(child); - rw.WriteArray(reinterpret_cast(&storageCLSID), sizeof (storageCLSID)); + rw.WriteArray(reinterpret_cast(&storageCLSID), sizeof (storageCLSID)); rw.Write(stateBits); - rw.WriteArray(reinterpret_cast(&creationDate), sizeof (creationDate)); - rw.WriteArray(reinterpret_cast(&modifyDate), sizeof (modifyDate)); + rw.WriteArray(reinterpret_cast(&creationDate), sizeof (creationDate)); + rw.WriteArray(reinterpret_cast(&modifyDate), sizeof (modifyDate)); rw.Write(startSetc); rw.Write(size); @@ -113,7 +112,7 @@ void DirectoryEntry::Read(Stream stream, CFSVersion ver) { StreamRW rw(stream); - rw.ReadArray(reinterpret_cast(entryName), 64); + rw.ReadArray(entryName, 64); nameLength = rw.Read(); stgType = (StgType)rw.Read(); stgColor = (StgColor)rw.Read(); @@ -129,10 +128,10 @@ void DirectoryEntry::Read(Stream stream, CFSVersion ver) child = NOSTREAM; } - rw.ReadArray(reinterpret_cast(&storageCLSID), 16); + rw.ReadArray(reinterpret_cast(&storageCLSID), 16); stateBits = rw.Read(); - rw.ReadArray(reinterpret_cast(&creationDate), 8); - rw.ReadArray(reinterpret_cast(&modifyDate), 8); + rw.ReadArray(reinterpret_cast(&creationDate), 8); + rw.ReadArray(reinterpret_cast(&modifyDate), 8); startSetc = rw.Read(); if (ver == CFSVersion::Ver_3) diff --git a/Common/cppcf/sector.cpp b/Common/cppcf/sector.cpp index cb21041404..bc1a8c60c2 100644 --- a/Common/cppcf/sector.cpp +++ b/Common/cppcf/sector.cpp @@ -22,15 +22,15 @@ bool Sector::IsStreamed() if (stream == nullptr || size == MINISECTOR_SIZE) return false; - auto currentPossition = stream->TellFile(); + auto currentPossition = stream->tellg(); - stream->SeekFile(0, SEEK_END); - auto endPossition = stream->TellFile(); - stream->SeekFile(0, SEEK_SET); - auto begPossition = stream->TellFile(); + stream->seekg(std::ios_base::end); + auto endPossition = stream->tellg(); + stream->seekg(std::ios_base::beg); + auto begPossition = stream->tellg(); auto streamSize = endPossition - begPossition; - stream->SeekFile(currentPossition, SEEK_SET); + stream->seekg(currentPossition, std::ios_base::beg); return (this->id * size) + size < streamSize; } @@ -85,9 +85,8 @@ std::vector &Sector::GetData() data = std::vector(size, 0); if (IsStreamed()) { - DWORD bytesWasRead(0); - stream->SeekFile(size + id * size, SEEK_SET); - stream->ReadFile(data.data(), size, bytesWasRead); + stream->seekg(size + id * size, std::ios_base::beg); + stream->read(reinterpret_cast(data.data()), size); } } diff --git a/Common/cppcf/stream.cpp b/Common/cppcf/stream.cpp new file mode 100644 index 0000000000..a939af5f20 --- /dev/null +++ b/Common/cppcf/stream.cpp @@ -0,0 +1,15 @@ +#include "stream.h" + + +std::streamsize CFCPP::Length(const CFCPP::Stream& st) +{ + if (st.get() == nullptr) + return 0; + + auto curPos = st->tellg(); + st->seekg(0, std::ios_base::end); + auto ssize = st->tellg(); + st->seekg(curPos); + + return ssize; +} diff --git a/Common/cppcf/stream.h b/Common/cppcf/stream.h index df120662bd..727bf0466a 100644 --- a/Common/cppcf/stream.h +++ b/Common/cppcf/stream.h @@ -1,9 +1,13 @@ #pragma once + +#include +#include #include -#include "../../DesktopEditor/common/StreamWriter.h" +#include "../../DesktopEditor/common/Types.h" namespace CFCPP { -using Stream = std::shared_ptr; +using Stream = std::shared_ptr; +std::streamsize Length(const Stream& st); } diff --git a/Common/cppcf/streamrw.cpp b/Common/cppcf/streamrw.cpp index 485575f1e5..eb02308aca 100644 --- a/Common/cppcf/streamrw.cpp +++ b/Common/cppcf/streamrw.cpp @@ -5,12 +5,32 @@ using namespace CFCPP; StreamRW::StreamRW(const Stream &stream) : stream(stream) -{} +{ + buffer.fill(0); +} T_LONG64 StreamRW::Seek(T_LONG64 offset) { - if(!stream->SeekFile(offset, SEEK_SET)) - return -1; - - return stream->TellFile(); + stream->seekp(offset, std::ios::beg); + return stream->tellp(); +} + +void StreamRW::ReadArray(char *data, int lenght) +{ + stream->read(data, lenght); +} + +void StreamRW::ReadArray(BYTE* data, int lenght) +{ + stream->read(reinterpret_cast(data), lenght); +} + +void StreamRW::WriteArray(const BYTE *arr, int lenght) +{ + stream->write(reinterpret_cast(arr), lenght); +} + +void StreamRW::WriteArray(const char *arr, int lenght) +{ + stream->write(arr, lenght); } diff --git a/Common/cppcf/streamrw.h b/Common/cppcf/streamrw.h index 4e7c8b111a..9552f26e1e 100644 --- a/Common/cppcf/streamrw.h +++ b/Common/cppcf/streamrw.h @@ -1,5 +1,8 @@ #pragma once +#include +#include +#include #include "stream.h" @@ -14,36 +17,28 @@ public: template T Read() { - DWORD bytesWasRead(0); T value; - auto asByteArr = reinterpret_cast(&value); - stream->ReadFile(asByteArr, sizeof (T), bytesWasRead); + char* asByteArr = reinterpret_cast(&value); + stream->read(asByteArr, sizeof (T)); return value; } template void Write(T value) { - auto asByteArr = reinterpret_cast(&value); - stream->WriteFile(asByteArr, sizeof (T)); + char* asByteArr = reinterpret_cast(&value); + stream->write(asByteArr, sizeof (T)); } - inline void ReadArray(BYTE* data, DWORD lenght) - { - DWORD bytesWasRead; - stream->ReadFile(data, lenght, bytesWasRead); - } - inline void WriteArray(const BYTE *data, DWORD lenght) - { - stream->WriteFile(data, lenght); - } + void ReadArray(char* data, int lenght); + void ReadArray(BYTE* data, int lenght); + void WriteArray(const BYTE *arr, int lenght); + void WriteArray(const char *arr, int lenght); - inline void Close() - { - stream->CloseFile(); - } + inline void Close(){return;} private: + std::array buffer; Stream stream; }; diff --git a/Common/cppcf/test/data/ex.ppt b/Common/cppcf/test/data/ex.ppt index de9f293e00ef3f63bfd5b560a697a19716f3dba7..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 100644 GIT binary patch literal 0 HcmV?d00001 literal 8192 zcmeHMeQeZc9smAb?s|o?-}Oac;NS-v12))73*&7oS12$Xg%g-BF@_SL(wWj)Y8srz zBg@pxs0)^em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*fG01~TSXOkauz=fq6>D%QES>mL6kiA%c~ zr3$H@T>MPc8&B~iy-prKdX5&1PI{6t_yM#k#F*ERQy=#GRllM9e^uF}`J}#A`dUJ$ zMExVa5_z#$8ehK9vK?^4msUPrEdeKTHKM>`c6{(ANN>r<`c?bnPIX3z+!vx4#Bm^f;Btmkjv0Ke&e zBgq>BRq`bACF80YRJ|gv(kpFv|CqdX`?luI^>UFOg?@#ly#~*-qDNS52^hOb?U~qZ*9HKZ+auM9i)oxsbE0q!a8+g%FZW1N%Eww z>+0$^Z*x~|ZgJN%)Nk0T-huyeF&019bGEnZtb0D@UN{%)`LLt6>xQ~X-*Rs*07n`_ zBY35Iv+sPY=i0wxogKZsSI@ea<6Rf~uPV_rgH${!PIkQC6YF(jp47wuilor)E`1=x zI#U$t9P<3*f*Bd}_aXgTk*u@Kc&!hws9L}L$rV*8?^mLA74S6Pm8)bvUZTrTT852w zCSIt^@K=t~Ec{iewo3Fa1^=0-mEt|RQss}}MS7m1pDo`2x53kY9R3<90oF&KKm9LE z3Lo}s3$sO?S)63INK5eFk0$)-|FgJwzuty&(=A41c8!DGJcqoL*_*reSu{_{-E~{7 Qo^0l~#o&J&bSKOIFE-j8!vFvP diff --git a/Common/cppcf/test/stream_wrapper.h b/Common/cppcf/test/stream_wrapper.h deleted file mode 100644 index b7200bf1a6..0000000000 --- a/Common/cppcf/test/stream_wrapper.h +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once -#include "stream.h" - - -CFCPP::Stream getStream(std::wstring filename, bool bWriteble = false) -{ - CFCPP::Stream st(new NSFile::CStreamWriter); - bool status; - if (bWriteble) - status = st->CreateFileW(filename); - else - status = st->OpenFile(filename); - - if (status) - return st; - else - return CFCPP::Stream(); -} - -void flush(CFCPP::Stream& st) -{ - st->Flush(); -} - -void seek(CFCPP::Stream& st, int pos) -{ - st->SeekFile(pos, SEEK_SET); -} - -long tell(CFCPP::Stream& st) -{ - return st->TellFile(); -} - -long size(CFCPP::Stream& st) -{ - return st->SizeFile(); -} diff --git a/Common/cppcf/test/test.pro b/Common/cppcf/test/test.pro index 6e8e9a0a33..3d00306535 100644 --- a/Common/cppcf/test/test.pro +++ b/Common/cppcf/test/test.pro @@ -16,7 +16,6 @@ ADD_DEPENDENCY(UnicodeConverter, kernel, cfcpp) INCLUDEPATH += $$PWD/../ HEADERS += \ - stream_wrapper.h \ tst_compondfile.h \ tst_directoryentry.h \ tst_header.h \ diff --git a/Common/cppcf/test/tst_compondfile.h b/Common/cppcf/test/tst_compondfile.h index d0c7fbf300..387b900b59 100644 --- a/Common/cppcf/test/tst_compondfile.h +++ b/Common/cppcf/test/tst_compondfile.h @@ -22,6 +22,5 @@ struct CompoundFileTest : testing::Test TEST_F(CompoundFileTest, test_compoundfile_read) { - bool suc = cf.HasSourceStream(); - EXPECT_TRUE(suc); + EXPECT_TRUE(cf.HasSourceStream()); } diff --git a/Common/cppcf/test/tst_directoryentry.h b/Common/cppcf/test/tst_directoryentry.h index 0d1d8ad272..28f17d3c1b 100644 --- a/Common/cppcf/test/tst_directoryentry.h +++ b/Common/cppcf/test/tst_directoryentry.h @@ -3,7 +3,6 @@ #include #include #include "directoryentry.h" -#include "stream_wrapper.h" using namespace testing; using namespace std; @@ -12,11 +11,12 @@ using namespace CFCPP; struct DirEntryTest : testing::Test { - wstring filename = L"../../../data/ex.ppt"; Stream stream; + string filename = "../../../data/ex.ppt"; - DirEntryTest() : stream(getStream(filename)) + DirEntryTest() { + stream.reset(new std::fstream(filename, ios::app | ios::in | ios::out | ios::binary)); } }; @@ -48,27 +48,27 @@ void test_dirEntry_read(const DirectoryEntry& de) TEST_F(DirEntryTest, test_directoryentry_read) { DirectoryEntry de(L"", StgInvalid, {}); - seek(stream, 0x400); + stream->seekg(0x400, std::ios::beg); de.Read(stream); - EXPECT_EQ(tell(stream), 0x480); + EXPECT_EQ(stream->tellg(), 0x480); test_dirEntry_read(de); } TEST_F(DirEntryTest, test_directoryentry_write) { DirectoryEntry de(L"", StgInvalid, {}); - seek(stream, 0x400); + stream->seekg(0x400, std::ios::beg); de.Read(stream); - std::wstring other_filename(L"../../../data/types/direntry.bin"); - stream = getStream(other_filename, true); + std::string other_filename("../../../data/types/direntry.bin"); + stream.reset(new std::fstream(other_filename, ios::app | ios::in | ios::out | ios::binary)); de.Write(stream); - EXPECT_EQ(tell(stream), 0x80); - seek(stream, 0); + EXPECT_EQ(stream->tellg(), 0x80); + stream->seekp(0, std::ios::beg); DirectoryEntry other(L"", StgInvalid, {}); other.Read(stream); test_dirEntry_read(other); - NSFile::CFileBinary::Remove(other_filename); + remove(other_filename.c_str()); } diff --git a/Common/cppcf/test/tst_header.h b/Common/cppcf/test/tst_header.h index a37542f4ac..2cd6de23a7 100644 --- a/Common/cppcf/test/tst_header.h +++ b/Common/cppcf/test/tst_header.h @@ -3,7 +3,6 @@ #include #include #include "header.h" -#include "stream_wrapper.h" using namespace testing; using namespace std; @@ -12,13 +11,13 @@ using namespace CFCPP; struct HeaderTest : testing::Test { - wstring filename = L"../../../data/ex.ppt"; Stream stream; Header hd; - bool isOpen = true; + string filename = "../../../data/ex.ppt"; - HeaderTest() : stream(getStream(filename)) + HeaderTest() { + stream.reset(new std::fstream(filename, ios::app | ios::in | ios::out | ios::binary)); } }; @@ -47,7 +46,6 @@ void test_header_state(const Header& hd) TEST_F(HeaderTest, test_header_read) { - EXPECT_TRUE(isOpen); hd.Read(stream); test_header_state(hd); } @@ -56,16 +54,13 @@ TEST_F(HeaderTest, test_header_write) { hd.Read(stream); - std::wstring other_filename(L"../../../data/types/header.bin"); - stream = getStream(other_filename, true); - + std::string other_filename("../../../data/types/header.bin"); + stream.reset(new std::fstream(other_filename, ios::app | ios::in | ios::out | ios::binary)); hd.Write(stream); - flush(stream); - stream->CloseFile(); Header other; - seek(stream, 0); + stream->seekg(0, std::ios::beg); other.Read(stream); test_header_state(other); - NSFile::CFileBinary::Remove(other_filename); + remove(other_filename.c_str()); } diff --git a/Common/cppcf/test/tst_streamrw.h b/Common/cppcf/test/tst_streamrw.h index 07b5203c34..06de8eadc5 100644 --- a/Common/cppcf/test/tst_streamrw.h +++ b/Common/cppcf/test/tst_streamrw.h @@ -3,7 +3,6 @@ #include #include #include "streamrw.h" -#include "stream_wrapper.h" using namespace testing; using namespace std; @@ -11,14 +10,15 @@ using namespace CFCPP; struct StreamRWTest : testing::Test { - wstring filename = L"../../../data/types/types.bin"; Stream stream; shared_ptr rw; + string filename = "../../../data/types/types.bin"; const char symbol = 'a'; const int integer = 13; - StreamRWTest() : stream(getStream(filename, true)) + StreamRWTest() { + stream.reset(new std::fstream(filename, ios::app | ios::in | ios::out | ios::binary)); rw.reset(new StreamRW(stream)); } @@ -30,8 +30,7 @@ struct StreamRWTest : testing::Test TEST_F(StreamRWTest, test_stream_open) { - EXPECT_TRUE(stream != nullptr); - NSFile::CFileBinary::Remove(filename); + EXPECT_EQ(static_pointer_cast(stream)->is_open(), 1); } TEST_F(StreamRWTest, test_stream_write) @@ -39,8 +38,8 @@ TEST_F(StreamRWTest, test_stream_write) rw->Seek(0); rw->Write(symbol); rw->Write(integer); - flush(stream); - EXPECT_EQ(size(stream), 5); + stream->flush(); + EXPECT_EQ((int)Length(stream), 5); } @@ -49,19 +48,17 @@ TEST_F(StreamRWTest, test_stream_read) EXPECT_EQ(rw->Seek(0), 0); EXPECT_EQ(rw->Read(), symbol); EXPECT_EQ(rw->Read(), integer); - NSFile::CFileBinary::Remove(filename); + remove(filename.c_str()); } TEST_F(StreamRWTest, test_stream_rw_array) { int sarr[3] = {99, 0, -3}; - int darr[3] = {1, 2, 4}; - rw->WriteArray(reinterpret_cast(sarr), sizeof (sarr)); - flush(stream); - EXPECT_EQ(rw->Seek(0), 0); - EXPECT_EQ(stream->SizeFile(), 12); - rw->ReadArray(reinterpret_cast(darr), sizeof (darr)); + int darr[3] = {-1,-1,-1}; + rw->WriteArray(reinterpret_cast(sarr), sizeof (sarr)); + rw->Seek(0); + rw->ReadArray(reinterpret_cast(darr), sizeof (darr)); EXPECT_EQ(sarr[2], darr[2]); - NSFile::CFileBinary::Remove(filename); + remove(filename.c_str()); }