From c3e060d13bfb8271dae0a8ad029562e7b534f879 Mon Sep 17 00:00:00 2001 From: Ivan Morozov Date: Thu, 16 Jun 2022 23:29:48 +0300 Subject: [PATCH] add part of class compoundfile --- Common/cppcf/Stream.h | 16 +++++++- Common/cppcf/compoundfile.cpp | 65 +++++++++++++++++++++++++++---- Common/cppcf/compoundfile.h | 6 ++- Common/cppcf/header.cpp | 2 +- Common/cppcf/header.h | 4 +- Common/cppcf/sectorcollection.cpp | 7 ++-- Common/cppcf/sectorcollection.h | 3 +- Common/cppcf/svector.cpp | 7 ++++ Common/cppcf/svector.h | 1 + 9 files changed, 93 insertions(+), 18 deletions(-) diff --git a/Common/cppcf/Stream.h b/Common/cppcf/Stream.h index 0f25d1deda..fa0480d63d 100644 --- a/Common/cppcf/Stream.h +++ b/Common/cppcf/Stream.h @@ -3,9 +3,21 @@ #include #include -#include "../DocxFormat/Source/Base/Nullable.h" +#include namespace CFCPP { -using Stream = NSCommon::nullable; +using Stream = std::shared_ptr; +std::streamsize Length(Stream st) +{ + if (st.get() == nullptr) + return 0; + + auto curPos = st->tellg(); + st->seekg(std::ios_base::end); + auto ssize = st->tellg(); + st->seekg(curPos); + + return ssize; +} } diff --git a/Common/cppcf/compoundfile.cpp b/Common/cppcf/compoundfile.cpp index 5092ee0af9..69cba37c00 100644 --- a/Common/cppcf/compoundfile.cpp +++ b/Common/cppcf/compoundfile.cpp @@ -1,5 +1,8 @@ #include "compoundfile.h" #include "cfexception.h" +#include "streamview.h" +#include "../../DesktopEditor/common/File.h" +#include using namespace CFCPP; @@ -245,24 +248,24 @@ void CompoundFile::Commit(bool releaseMemory) /// Load compound file from an existing stream. /// /// Stream to load compound file from -private void Load(Stream stream) +void CompoundFile::Load(Stream stream) { try { - this.header = new Header(); + this->header = Header(); this.directoryEntries = new List(); - this.sourceStream = stream; + this->sourceStream = stream; header.Read(stream); - int n_sector = Ceiling(((double)(stream.Length - GetSectorSize()) / (double)GetSectorSize())); + int n_sector = std::ceil(((double)(Length(stream) - GetSectorSize()) / (double)GetSectorSize())); - if (stream.Length > 0x7FFFFF0) - this._transactionLockAllocated = true; + if (Length(stream) > 0x7FFFFF0) + this->_transactionLockAllocated = true; - sectors = new SectorCollection(); + sectors.Clear(); //sectors = new ArrayList(); for (int i = 0; i < n_sector; i++) { @@ -295,7 +298,7 @@ void CompoundFile::CheckForLockSector() //If transaction lock has been added and not yet allocated in the FAT... if (_transactionLockAdded && !_transactionLockAllocated) { - StreamView fatStream = new StreamView(GetFatSectorChain(), GetSectorSize(), sourceStream); + StreamView fatStream(GetFatSectorChain(), GetSectorSize(), sourceStream); fatStream.Seek(_lockSectorId * 4, SeekOrigin.Begin); fatStream.Write(BitConverter.GetBytes(Sector.ENDOFCHAIN), 0, 4); @@ -303,3 +306,49 @@ void CompoundFile::CheckForLockSector() _transactionLockAllocated = true; } } + +void CompoundFile::LoadFile(std::wstring fileName) +{ + SetFileName(fileName); + Stream fs; + + try + { + NSFile::CFileBinary file; + file.OpenFile(fileName); + if (updateMode == CFSUpdateMode::ReadOnly) + { + fs.reset(new std::fstream(this->fileName, std::ios::in | std::ios::out)); + + } + else + { + fs.reset(new std::fstream(this->fileName, std::ios::in | std::ios::out)); + } + + Load(fs); + + } + catch(...) + { + if (fs.get() != nullptr) + fs->clear(); // close + + throw; + } +} + +void CompoundFile::SetFileName(std::wstring fileName) +{ + BYTE* pUtf8 = NULL; + LONG 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) +{ + +} diff --git a/Common/cppcf/compoundfile.h b/Common/cppcf/compoundfile.h index 4745884a85..4a76443e57 100644 --- a/Common/cppcf/compoundfile.h +++ b/Common/cppcf/compoundfile.h @@ -55,6 +55,10 @@ protected: private: void CheckForLockSector(); + void LoadFile(std::wstring fileName); + void SetFileName(std::wstring fileName); + void LoadStream(Stream stream); + void Load(Stream stream); public: CFSConfiguration configuration = Default; @@ -72,7 +76,7 @@ private: static constexpr int FLUSHING_BUFFER_MAX_SIZE = 1024 * 1024 * 16; SectorCollection sectors; std::fstream stream; - std::wstring fileName; + std::string fileName; bool _transactionLockAdded = false; int _lockSectorId = -1; diff --git a/Common/cppcf/header.cpp b/Common/cppcf/header.cpp index 18b771ebd9..a3a0f21e42 100644 --- a/Common/cppcf/header.cpp +++ b/Common/cppcf/header.cpp @@ -69,7 +69,7 @@ void Header::Write(Stream &stream) } } -void Header::Read(Stream &stream) +void Header::Read(Stream stream) { StreamRW rw(stream); diff --git a/Common/cppcf/header.h b/Common/cppcf/header.h index bb3d7052c6..e1e9ae57f3 100644 --- a/Common/cppcf/header.h +++ b/Common/cppcf/header.h @@ -11,8 +11,8 @@ class Header public: Header(); Header(ushort version); - void Write(std::fstream &stream); - void Read(std::fstream &stream); + void Write(Stream stream); + void Read(Stream stream); private: void CheckVersion()const; diff --git a/Common/cppcf/sectorcollection.cpp b/Common/cppcf/sectorcollection.cpp index 4d1e02a02e..c4bccbfb74 100644 --- a/Common/cppcf/sectorcollection.cpp +++ b/Common/cppcf/sectorcollection.cpp @@ -37,13 +37,14 @@ int SectorCollection::add(const Sector &item) if (itemIndex < largeArraySlices.size()) { - largeArraySlices[itemIndex]->push_back(item); + largeArraySlices[itemIndex].push_back(item); count++; } else { - std::unique_ptr> ar(new std::vector(SLICE_SIZE)); - ar->emplace_back(item); +// std::unique_ptr> ar(new std::vector(SLICE_SIZE)); + SVector ar; + ar.push_back(item); largeArraySlices.push_back(ar); count++; } diff --git a/Common/cppcf/sectorcollection.h b/Common/cppcf/sectorcollection.h index d91c7cfb06..6187af1ebc 100644 --- a/Common/cppcf/sectorcollection.h +++ b/Common/cppcf/sectorcollection.h @@ -2,6 +2,7 @@ #include "sector.h" #include +#include "svector.h" namespace CFCPP { @@ -9,7 +10,7 @@ namespace CFCPP class SectorCollection { public: - std::vector>> largeArraySlices; + std::vector> largeArraySlices; SectorCollection(); void Add(const Sector &item); void Clear(); diff --git a/Common/cppcf/svector.cpp b/Common/cppcf/svector.cpp index ac02241789..4e334c0023 100644 --- a/Common/cppcf/svector.cpp +++ b/Common/cppcf/svector.cpp @@ -34,6 +34,13 @@ void SVector::push_back(T &&value) array.push_back(std::shared_ptr(value)); } +template +void SVector::push_back(const T &value) +{ + isInit = true; + array.push_back(std::shared_ptr(value)); +} + template std::shared_ptr SVector::back() const { diff --git a/Common/cppcf/svector.h b/Common/cppcf/svector.h index d63bb8c3e5..7b6ffce8b8 100644 --- a/Common/cppcf/svector.h +++ b/Common/cppcf/svector.h @@ -18,6 +18,7 @@ public: inline bool empty() {array.empty();} std::shared_ptr operator[](size_t pos)const; void push_back(T&& value); + void push_back(const T& value); inline void pop_back() {array.pop_back();} std::shared_ptr back()const; std::shared_ptr front()const;