diff --git a/Common/cppcf/cfcpp.pro b/Common/cppcf/cfcpp.pro index 557adc0068..56dab559a8 100644 --- a/Common/cppcf/cfcpp.pro +++ b/Common/cppcf/cfcpp.pro @@ -25,7 +25,6 @@ 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 e12a5aa13b..ac4d22b151 100644 --- a/Common/cppcf/cfstream.cpp +++ b/Common/cppcf/cfstream.cpp @@ -69,14 +69,15 @@ void CFStream::CopyFrom(const Stream &input) { CheckDisposed(); - std::vector buffer(Length(input)); + std::vector buffer(input->SizeFile()); // if (input.CanSeek) { - input->seekg(0, std::ios::beg); + input->SeekFile(0, SEEK_SET); } - input->read(reinterpret_cast(buffer.data()), Length(input)); + DWORD bytesWasRead(0); + input->ReadFile(buffer.data(), input->SizeFile(), bytesWasRead); SetData(buffer); } diff --git a/Common/cppcf/compoundfile.cpp b/Common/cppcf/compoundfile.cpp index 52be03e997..c145f4f001 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->seekp(0, std::ios::beg); + sourceStream->SeekFile(0, SEEK_SET); - std::vector zeroArray(sSize, 0); - sourceStream->write(zeroArray.data(), zeroArray.size()); + std::vector zeroArray(sSize, 0); + sourceStream->WriteFile(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->seekp((long)((long)(sSize) + (long)i * (long)sSize), std::ios::beg); + sourceStream->SeekFile((long)((long)(sSize) + (long)i * (long)sSize), SEEK_SET); - sourceStream->write(reinterpret_cast(s->GetData().data()), sSize); - sourceStream->flush(); + sourceStream->WriteFile(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->seekg(0, std::ios::beg); + sourceStream->SeekFile(0, SEEK_SET); 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)(Length(stream) - GetSectorSize()) / (double)GetSectorSize())); + int n_sector = std::ceil(((double)(stream->SizeFile() - GetSectorSize()) / (double)GetSectorSize())); - if (Length(stream) > 0x7FFFFF0) + if (stream->SizeFile() > 0x7FFFFF0) this->_transactionLockAllocated = true; @@ -220,9 +220,9 @@ void CompoundFile::Load(Stream stream) } catch (...) { - if (std::dynamic_pointer_cast(stream) != nullptr && stream.get() != nullptr && closeStream) + if (stream != nullptr && closeStream) { - std::static_pointer_cast(stream)->close(); + stream->CloseFile(); } throw; @@ -234,12 +234,8 @@ void CompoundFile::Save(std::wstring wFileName) if (_disposed) throw CFException("Compound File closed: cannot save data"); - 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)); + Stream fs(new NSFile::CStreamWriter); + fs->CreateFileW(wFileName); try { @@ -247,15 +243,15 @@ void CompoundFile::Save(std::wstring wFileName) } catch (std::exception& ex) { - throw CFException("Error saving file [" + fileName + "]", ex); + throw CFException(L"Error saving file [" + fileName + L"]", ex); } // finally { - if (fs.get() != nullptr) - fs->flush(); + if (fs != nullptr) + fs->Flush(); - if (fs.get() != nullptr) - static_cast(fs.get())->close(); + if (fs != nullptr) + fs->CloseFile(); } } @@ -273,8 +269,8 @@ void CompoundFile::Save(Stream stream) try { - std::vector zeroArray(sSize, 0); - stream->write(zeroArray.data(), zeroArray.size()); + std::vector zeroArray(sSize, 0); + stream->WriteFile(zeroArray.data(), zeroArray.size()); zeroArray.clear(); CommitDirectory(); @@ -297,13 +293,13 @@ void CompoundFile::Save(Stream stream) } - stream->write(reinterpret_cast(s->GetData().data()), sSize); + stream->WriteFile(s->GetData().data(), sSize); //s.ReleaseData(); } - stream->seekp(0, std::ios::beg); + stream->TellFile(); header->Write(stream); } catch (std::exception &ex) @@ -506,7 +502,7 @@ SVector CompoundFile::GetNormalSectorChain(int secID) result.push_back(s); - fatStream.Seek(nextSecID * 4, std::ios::beg); + fatStream.Seek(nextSecID * 4, SEEK_SET); int next = fatStream.ReadInt32(); EnsureUniqueSectorIndex(next, processedSectors); @@ -549,12 +545,12 @@ SVector CompoundFile::GetMiniSectorChain(int secID) ms->id = nextSecID; ms->type = SectorType::Mini; - miniStreamView.Seek(nextSecID * Sector::MINISECTOR_SIZE, std::ios::beg); + miniStreamView.Seek(nextSecID * Sector::MINISECTOR_SIZE, SEEK_SET); miniStreamView.Read(reinterpret_cast(ms->GetData().data()), 0, Sector::MINISECTOR_SIZE); result.push_back(ms); - miniFATView.Seek(nextSecID * 4, std::ios::beg); + miniFATView.Seek(nextSecID * 4, SEEK_SET); int next = miniFATView.ReadInt32(); nextSecID = next; @@ -587,7 +583,8 @@ SVector CompoundFile::GetSectorChain(int secID, SectorType chainType) void CompoundFile::EnsureUniqueSectorIndex(int nextSecID, std::unordered_set& processedSectors) { - if (processedSectors.find(nextSecID) != processedSectors.end() && this->validationExceptionEnabled) + bool isContain = processedSectors.find(nextSecID) != processedSectors.end(); + if (isContain && this->validationExceptionEnabled) { throw CFCorruptedFileException("The file is corrupted."); } @@ -852,7 +849,7 @@ void CompoundFile::FreeMiniChain(SVector §orChain, int nth_sector_to if (s->id != -1) { // Overwrite - miniStreamView.Seek(Sector::MINISECTOR_SIZE * s->id, std::ios::beg); + miniStreamView.Seek(Sector::MINISECTOR_SIZE * s->id, SEEK_SET); miniStreamView.Write(ZEROED_MINI_SECTOR.data(), 0, Sector::MINISECTOR_SIZE); } } @@ -863,7 +860,7 @@ void CompoundFile::FreeMiniChain(SVector §orChain, int nth_sector_to { int currentId = sectorChain[i]->id; - miniFATView.Seek(currentId * 4, std::ios::beg); + miniFATView.Seek(currentId * 4, SEEK_SET); const int freesec = Sector::FREESECT; miniFATView.Write(reinterpret_cast(&freesec), 0, 4); } @@ -875,7 +872,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, std::ios::beg); + miniFATView.Seek(sectorChain[nth_sector_to_remove - 1]->id * 4, SEEK_SET); const int endofchain = Sector::ENDOFCHAIN; miniFATView.Write(reinterpret_cast(&endofchain), 0, 4); } @@ -918,7 +915,7 @@ void CompoundFile::FreeChain(SVector §orChain, int nth_sector_to_rem { int currentId = sectorChain[i]->id; - FATView.Seek(currentId * 4, std::ios::beg); + FATView.Seek(currentId * 4, SEEK_SET); const int freesec = Sector::FREESECT; FATView.Write(reinterpret_cast(&freesec), 0, 4); } @@ -926,7 +923,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, std::ios::beg); + FATView.Seek(sectorChain[nth_sector_to_remove - 1]->id * 4, SEEK_SET); const int endofchain = Sector::ENDOFCHAIN; FATView.Write(reinterpret_cast(&endofchain), 0, 4); } @@ -973,11 +970,11 @@ void CompoundFile::AllocateFATSectorChain(SVector §orChain) auto sN = sectorChain[i + 1]; auto sC = sectorChain[i]; - fatStream.Seek(sC->id * 4, std::ios::beg); + fatStream.Seek(sC->id * 4, SEEK_SET); fatStream.Write(reinterpret_cast(&(sN->id)), 0, 4); } - fatStream.Seek(sectorChain[sectorChain.size() - 1]->id * 4, std::ios::beg); + fatStream.Seek(sectorChain[sectorChain.size() - 1]->id * 4, SEEK_SET); const int endofchain = Sector::ENDOFCHAIN; fatStream.Write(reinterpret_cast(&endofchain), 0, 4); @@ -1114,14 +1111,14 @@ void CompoundFile::AllocateDIFATSectorChain(SVector &FATsectorChain) for (int i = 0; i < (int)header->difatSectorsNumber; i++) { - fatSv.Seek(difatStream.BaseSectorChain()[i]->id * 4, std::ios::beg); + fatSv.Seek(difatStream.BaseSectorChain()[i]->id * 4, SEEK_SET); 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, std::ios::beg); + fatSv.Seek(fatSv.BaseSectorChain()[i]->id * 4, SEEK_SET); const int fatsect = Sector::FATSECT; fatSv.Write(reinterpret_cast(&fatsect), 0, 4); } @@ -1169,7 +1166,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, std::ios::beg); + miniStreamView.Seek(rootStorage->size() + Sector::MINISECTOR_SIZE, SEEK_SET); //miniStreamView.Write(s.GetData(), 0, Sector.MINISECTOR_SIZE); s->id = (int)(miniStreamView.position - Sector::MINISECTOR_SIZE) / Sector::MINISECTOR_SIZE; @@ -1183,12 +1180,12 @@ void CompoundFile::AllocateMiniSectorChain(SVector §orChain) int currentId = sectorChain[i]->id; int nextId = sectorChain[i + 1]->id; - miniFATView.Seek(currentId * 4, std::ios::beg); + miniFATView.Seek(currentId * 4, SEEK_SET); miniFATView.Write(reinterpret_cast(&nextId), 0, 4); } // Write End of Chain in MiniFAT - miniFATView.Seek(sectorChain[sectorChain.size() - 1]->id * SIZE_OF_SID, std::ios::beg); + miniFATView.Seek(sectorChain[sectorChain.size() - 1]->id * SIZE_OF_SID, SEEK_SET); const int endofchain = Sector::ENDOFCHAIN; miniFATView.Write(reinterpret_cast(&endofchain), 0, 4); @@ -1224,7 +1221,7 @@ void CompoundFile::PersistMiniStreamToStream(const SVector &miniSectorCh throw CFException("Invalid minisector index"); // Ministream sectors already allocated - miniStreamView.Seek(Sector::MINISECTOR_SIZE * s->id, std::ios::beg); + miniStreamView.Seek(Sector::MINISECTOR_SIZE * s->id, SEEK_SET); miniStreamView.Write(reinterpret_cast(s->GetData().data()), 0, Sector::MINISECTOR_SIZE); } } @@ -1613,7 +1610,7 @@ SList CompoundFile::FindFreeSectors(SectorType sType) ms->id = idx; ms->type = SectorType::Mini; - miniStreamView.Seek(ms->id * Sector::MINISECTOR_SIZE, std::ios::beg); + miniStreamView.Seek(ms->id * Sector::MINISECTOR_SIZE, SEEK_SET); miniStreamView.Read(reinterpret_cast(ms->GetData().data()), 0, Sector::MINISECTOR_SIZE); freeList.enqueue(ms); @@ -1687,7 +1684,7 @@ int CompoundFile::ReadData(CFStream *cFStream, std::streamsize position, std::ve } - sView->Seek(position, std::ios::beg); + sView->Seek(position, SEEK_SET); int result = sView->Read(reinterpret_cast(buffer.data()), 0, count); return result; @@ -1712,7 +1709,7 @@ int CompoundFile::ReadData(CFStream *cFStream, std::streamsize position, std::ve } - sView->Seek(position, std::ios::beg); + sView->Seek(position, SEEK_SET); int result = sView->Read(reinterpret_cast(buffer.data()), offset, count); return result; @@ -1804,7 +1801,7 @@ void CompoundFile::WriteData(std::shared_ptr cfItem, const std::vector zeroQueue; StreamView sv(sectorChain, _sectorSize, newLength, zeroQueue, sourceStream); - sv.Seek(position, std::ios::beg); + sv.Seek(position, SEEK_SET); sv.Write(reinterpret_cast(buffer.data()), offset, count); if (cfItem->size() < header->minSizeStandardStream) @@ -1838,10 +1835,9 @@ void CompoundFile::Dispose(bool disposing) fileName.clear(); } - if (std::dynamic_pointer_cast(sourceStream) != nullptr && - closeStream && !(configuration & CFSConfiguration::LeaveOpen)) + if (closeStream && !(configuration & CFSConfiguration::LeaveOpen)) { - std::static_pointer_cast(sourceStream)->close(); + sourceStream->CloseFile(); } } } @@ -1862,7 +1858,7 @@ void CompoundFile::CheckForLockSector() { StreamView fatStream(GetFatSectorChain(), GetSectorSize(), sourceStream); - fatStream.Seek(_lockSectorId * 4, std::ios::beg); + fatStream.Seek(_lockSectorId * 4, SEEK_SET); const int endofchain = Sector::ENDOFCHAIN; fatStream.Write(reinterpret_cast(&endofchain), 0, 4); @@ -1877,25 +1873,24 @@ void CompoundFile::LoadFile(std::wstring fileName) try { - NSFile::CFileBinary file; - file.OpenFile(fileName); if (updateMode == CFSUpdateMode::ReadOnly) { - fs.reset(new std::fstream(this->fileName, std::ios::out)); - + fs.reset(new NSFile::CStreamWriter); + fs->OpenFile(this->fileName, false); } else { - fs.reset(new std::fstream(this->fileName, std::ios::in | std::ios::out)); + fs.reset(new NSFile::CStreamWriter); + fs->CreateFileW(this->fileName); } + fs->TellFile(); Load(fs); - } catch(...) { - if (fs.get() != nullptr) - fs->clear(); // close + if (fs != nullptr) + fs->CloseFile(); // close throw; } @@ -1903,12 +1898,7 @@ void CompoundFile::LoadFile(std::wstring fileName) void CompoundFile::SetFileName(std::wstring 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; - + this->fileName = fileName; } void CompoundFile::LoadStream(Stream stream) @@ -1920,7 +1910,7 @@ void CompoundFile::LoadStream(Stream stream) throw CFException("Cannot load a non-seekable Stream"); - stream->seekp(0, std::ios::beg); + stream->TellFile(); Load(stream); } diff --git a/Common/cppcf/compoundfile.h b/Common/cppcf/compoundfile.h index 6baf8629b9..98817e085f 100644 --- a/Common/cppcf/compoundfile.h +++ b/Common/cppcf/compoundfile.h @@ -137,8 +137,7 @@ private: static constexpr int FLUSHING_QUEUE_SIZE = 6000; static constexpr int FLUSHING_BUFFER_MAX_SIZE = 1024 * 1024 * 16; SectorCollection sectors; - std::fstream stream; - std::string fileName; + std::wstring fileName; std::shared_ptr rootStorage; bool closeStream = true; diff --git a/Common/cppcf/directoryentry.cpp b/Common/cppcf/directoryentry.cpp index 1782832fff..84af104228 100644 --- a/Common/cppcf/directoryentry.cpp +++ b/Common/cppcf/directoryentry.cpp @@ -2,6 +2,7 @@ #include "cfexception.h" #include "streamrw.h" #include +#include using namespace CFCPP; @@ -91,17 +92,17 @@ int DirectoryEntry::GetHashCode() const void DirectoryEntry::Write(Stream stream) const { StreamRW rw(stream); - rw.WriteArray(entryName, sizeof (entryName)); + rw.WriteArray(reinterpret_cast(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); @@ -112,7 +113,7 @@ void DirectoryEntry::Read(Stream stream, CFSVersion ver) { StreamRW rw(stream); - rw.ReadArray(entryName, 64); + rw.ReadArray(reinterpret_cast(entryName), 64); nameLength = rw.Read(); stgType = (StgType)rw.Read(); stgColor = (StgColor)rw.Read(); @@ -128,10 +129,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 bc1a8c60c2..cb21041404 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->tellg(); + auto currentPossition = stream->TellFile(); - stream->seekg(std::ios_base::end); - auto endPossition = stream->tellg(); - stream->seekg(std::ios_base::beg); - auto begPossition = stream->tellg(); + stream->SeekFile(0, SEEK_END); + auto endPossition = stream->TellFile(); + stream->SeekFile(0, SEEK_SET); + auto begPossition = stream->TellFile(); auto streamSize = endPossition - begPossition; - stream->seekg(currentPossition, std::ios_base::beg); + stream->SeekFile(currentPossition, SEEK_SET); return (this->id * size) + size < streamSize; } @@ -85,8 +85,9 @@ std::vector &Sector::GetData() data = std::vector(size, 0); if (IsStreamed()) { - stream->seekg(size + id * size, std::ios_base::beg); - stream->read(reinterpret_cast(data.data()), size); + DWORD bytesWasRead(0); + stream->SeekFile(size + id * size, SEEK_SET); + stream->ReadFile(data.data(), size, bytesWasRead); } } diff --git a/Common/cppcf/stream.cpp b/Common/cppcf/stream.cpp deleted file mode 100644 index a939af5f20..0000000000 --- a/Common/cppcf/stream.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#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 727bf0466a..df120662bd 100644 --- a/Common/cppcf/stream.h +++ b/Common/cppcf/stream.h @@ -1,13 +1,9 @@ #pragma once - -#include -#include #include -#include "../../DesktopEditor/common/Types.h" +#include "../../DesktopEditor/common/StreamWriter.h" namespace CFCPP { -using Stream = std::shared_ptr; -std::streamsize Length(const Stream& st); +using Stream = std::shared_ptr; } diff --git a/Common/cppcf/streamrw.cpp b/Common/cppcf/streamrw.cpp index eb02308aca..485575f1e5 100644 --- a/Common/cppcf/streamrw.cpp +++ b/Common/cppcf/streamrw.cpp @@ -5,32 +5,12 @@ using namespace CFCPP; StreamRW::StreamRW(const Stream &stream) : stream(stream) -{ - buffer.fill(0); -} +{} T_LONG64 StreamRW::Seek(T_LONG64 offset) { - stream->seekp(offset, std::ios::beg); - return stream->tellp(); -} + if(!stream->SeekFile(offset, SEEK_SET)) + return -1; -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); + return stream->TellFile(); } diff --git a/Common/cppcf/streamrw.h b/Common/cppcf/streamrw.h index 9552f26e1e..4e7c8b111a 100644 --- a/Common/cppcf/streamrw.h +++ b/Common/cppcf/streamrw.h @@ -1,8 +1,5 @@ #pragma once -#include -#include -#include #include "stream.h" @@ -17,28 +14,36 @@ public: template T Read() { + DWORD bytesWasRead(0); T value; - char* asByteArr = reinterpret_cast(&value); - stream->read(asByteArr, sizeof (T)); + auto asByteArr = reinterpret_cast(&value); + stream->ReadFile(asByteArr, sizeof (T), bytesWasRead); return value; } template void Write(T value) { - char* asByteArr = reinterpret_cast(&value); - stream->write(asByteArr, sizeof (T)); + auto asByteArr = reinterpret_cast(&value); + stream->WriteFile(asByteArr, sizeof (T)); } - 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 ReadArray(BYTE* data, DWORD lenght) + { + DWORD bytesWasRead; + stream->ReadFile(data, lenght, bytesWasRead); + } + inline void WriteArray(const BYTE *data, DWORD lenght) + { + stream->WriteFile(data, lenght); + } - inline void Close(){return;} + inline void Close() + { + stream->CloseFile(); + } private: - std::array buffer; Stream stream; }; diff --git a/Common/cppcf/test/data/ex.ppt b/Common/cppcf/test/data/ex.ppt index e69de29bb2..de9f293e00 100644 Binary files a/Common/cppcf/test/data/ex.ppt and b/Common/cppcf/test/data/ex.ppt differ diff --git a/Common/cppcf/test/stream_wrapper.h b/Common/cppcf/test/stream_wrapper.h new file mode 100644 index 0000000000..b7200bf1a6 --- /dev/null +++ b/Common/cppcf/test/stream_wrapper.h @@ -0,0 +1,38 @@ +#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 3d00306535..6e8e9a0a33 100644 --- a/Common/cppcf/test/test.pro +++ b/Common/cppcf/test/test.pro @@ -16,6 +16,7 @@ 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 387b900b59..d0c7fbf300 100644 --- a/Common/cppcf/test/tst_compondfile.h +++ b/Common/cppcf/test/tst_compondfile.h @@ -22,5 +22,6 @@ struct CompoundFileTest : testing::Test TEST_F(CompoundFileTest, test_compoundfile_read) { - EXPECT_TRUE(cf.HasSourceStream()); + bool suc = cf.HasSourceStream(); + EXPECT_TRUE(suc); } diff --git a/Common/cppcf/test/tst_directoryentry.h b/Common/cppcf/test/tst_directoryentry.h index 28f17d3c1b..0d1d8ad272 100644 --- a/Common/cppcf/test/tst_directoryentry.h +++ b/Common/cppcf/test/tst_directoryentry.h @@ -3,6 +3,7 @@ #include #include #include "directoryentry.h" +#include "stream_wrapper.h" using namespace testing; using namespace std; @@ -11,12 +12,11 @@ using namespace CFCPP; struct DirEntryTest : testing::Test { + wstring filename = L"../../../data/ex.ppt"; Stream stream; - string filename = "../../../data/ex.ppt"; - DirEntryTest() + DirEntryTest() : stream(getStream(filename)) { - 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, {}); - stream->seekg(0x400, std::ios::beg); + seek(stream, 0x400); de.Read(stream); - EXPECT_EQ(stream->tellg(), 0x480); + EXPECT_EQ(tell(stream), 0x480); test_dirEntry_read(de); } TEST_F(DirEntryTest, test_directoryentry_write) { DirectoryEntry de(L"", StgInvalid, {}); - stream->seekg(0x400, std::ios::beg); + seek(stream, 0x400); de.Read(stream); - std::string other_filename("../../../data/types/direntry.bin"); - stream.reset(new std::fstream(other_filename, ios::app | ios::in | ios::out | ios::binary)); + std::wstring other_filename(L"../../../data/types/direntry.bin"); + stream = getStream(other_filename, true); de.Write(stream); - EXPECT_EQ(stream->tellg(), 0x80); - stream->seekp(0, std::ios::beg); + EXPECT_EQ(tell(stream), 0x80); + seek(stream, 0); DirectoryEntry other(L"", StgInvalid, {}); other.Read(stream); test_dirEntry_read(other); - remove(other_filename.c_str()); + NSFile::CFileBinary::Remove(other_filename); } diff --git a/Common/cppcf/test/tst_header.h b/Common/cppcf/test/tst_header.h index 2cd6de23a7..a37542f4ac 100644 --- a/Common/cppcf/test/tst_header.h +++ b/Common/cppcf/test/tst_header.h @@ -3,6 +3,7 @@ #include #include #include "header.h" +#include "stream_wrapper.h" using namespace testing; using namespace std; @@ -11,13 +12,13 @@ using namespace CFCPP; struct HeaderTest : testing::Test { + wstring filename = L"../../../data/ex.ppt"; Stream stream; Header hd; - string filename = "../../../data/ex.ppt"; + bool isOpen = true; - HeaderTest() + HeaderTest() : stream(getStream(filename)) { - stream.reset(new std::fstream(filename, ios::app | ios::in | ios::out | ios::binary)); } }; @@ -46,6 +47,7 @@ void test_header_state(const Header& hd) TEST_F(HeaderTest, test_header_read) { + EXPECT_TRUE(isOpen); hd.Read(stream); test_header_state(hd); } @@ -54,13 +56,16 @@ TEST_F(HeaderTest, test_header_write) { hd.Read(stream); - std::string other_filename("../../../data/types/header.bin"); - stream.reset(new std::fstream(other_filename, ios::app | ios::in | ios::out | ios::binary)); + std::wstring other_filename(L"../../../data/types/header.bin"); + stream = getStream(other_filename, true); + hd.Write(stream); + flush(stream); + stream->CloseFile(); Header other; - stream->seekg(0, std::ios::beg); + seek(stream, 0); other.Read(stream); test_header_state(other); - remove(other_filename.c_str()); + NSFile::CFileBinary::Remove(other_filename); } diff --git a/Common/cppcf/test/tst_streamrw.h b/Common/cppcf/test/tst_streamrw.h index 06de8eadc5..07b5203c34 100644 --- a/Common/cppcf/test/tst_streamrw.h +++ b/Common/cppcf/test/tst_streamrw.h @@ -3,6 +3,7 @@ #include #include #include "streamrw.h" +#include "stream_wrapper.h" using namespace testing; using namespace std; @@ -10,15 +11,14 @@ 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() + StreamRWTest() : stream(getStream(filename, true)) { - stream.reset(new std::fstream(filename, ios::app | ios::in | ios::out | ios::binary)); rw.reset(new StreamRW(stream)); } @@ -30,7 +30,8 @@ struct StreamRWTest : testing::Test TEST_F(StreamRWTest, test_stream_open) { - EXPECT_EQ(static_pointer_cast(stream)->is_open(), 1); + EXPECT_TRUE(stream != nullptr); + NSFile::CFileBinary::Remove(filename); } TEST_F(StreamRWTest, test_stream_write) @@ -38,8 +39,8 @@ TEST_F(StreamRWTest, test_stream_write) rw->Seek(0); rw->Write(symbol); rw->Write(integer); - stream->flush(); - EXPECT_EQ((int)Length(stream), 5); + flush(stream); + EXPECT_EQ(size(stream), 5); } @@ -48,17 +49,19 @@ TEST_F(StreamRWTest, test_stream_read) EXPECT_EQ(rw->Seek(0), 0); EXPECT_EQ(rw->Read(), symbol); EXPECT_EQ(rw->Read(), integer); - remove(filename.c_str()); + NSFile::CFileBinary::Remove(filename); } TEST_F(StreamRWTest, test_stream_rw_array) { int sarr[3] = {99, 0, -3}; - int darr[3] = {-1,-1,-1}; - rw->WriteArray(reinterpret_cast(sarr), sizeof (sarr)); - rw->Seek(0); - rw->ReadArray(reinterpret_cast(darr), sizeof (darr)); + 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)); EXPECT_EQ(sarr[2], darr[2]); - remove(filename.c_str()); + NSFile::CFileBinary::Remove(filename); }