diff --git a/Common/cppcf/Stream.h b/Common/cppcf/Stream.h new file mode 100644 index 0000000000..0f25d1deda --- /dev/null +++ b/Common/cppcf/Stream.h @@ -0,0 +1,11 @@ +#pragma once + + +#include +#include +#include "../DocxFormat/Source/Base/Nullable.h" + +namespace CFCPP +{ +using Stream = NSCommon::nullable; +} diff --git a/Common/cppcf/cfcpp.pro b/Common/cppcf/cfcpp.pro index 75c6623263..da80bf00a0 100644 --- a/Common/cppcf/cfcpp.pro +++ b/Common/cppcf/cfcpp.pro @@ -22,6 +22,7 @@ SOURCES += \ streamrw.cpp HEADERS += \ + Stream.h \ cfexception.h \ cfitem.h \ cfstorage.h \ diff --git a/Common/cppcf/header.cpp b/Common/cppcf/header.cpp index 62d8abf71f..18b771ebd9 100644 --- a/Common/cppcf/header.cpp +++ b/Common/cppcf/header.cpp @@ -36,7 +36,7 @@ Header::Header(ushort version) } } -void Header::Write(std::fstream &stream) +void Header::Write(Stream &stream) { StreamRW rw(stream); rw.WriteArray(headerSignature.data(), headerSignature.size()); @@ -69,7 +69,7 @@ void Header::Write(std::fstream &stream) } } -void Header::Read(std::fstream &stream) +void Header::Read(Stream &stream) { StreamRW rw(stream); diff --git a/Common/cppcf/sector.cpp b/Common/cppcf/sector.cpp index 1464a0aa5f..a142ad180c 100644 --- a/Common/cppcf/sector.cpp +++ b/Common/cppcf/sector.cpp @@ -4,8 +4,8 @@ using namespace CFCPP; int Sector::MINISECTOR_SIZE = 64; -Sector::Sector(int size, std::unique_ptr stream) : - size(size), stream(std::move(stream)) +Sector::Sector(int size, const Stream &stream) : + size(size), stream(stream) {} Sector::Sector(int size, const std::vector& data) : @@ -19,7 +19,7 @@ Sector::Sector(int size) : bool Sector::IsStreamed() { - if (stream == nullptr || size == MINISECTOR_SIZE) + if (stream.GetPointer() == nullptr || size == MINISECTOR_SIZE) return false; auto currentPossition = stream->tellg(); diff --git a/Common/cppcf/sector.h b/Common/cppcf/sector.h index 6d96ef48be..15e2d0e0ff 100644 --- a/Common/cppcf/sector.h +++ b/Common/cppcf/sector.h @@ -1,7 +1,6 @@ #pragma once -#include -#include +#include "Stream.h" #include #include "../../DesktopEditor/common/Types.h" #include @@ -9,6 +8,7 @@ namespace CFCPP { + enum SectorType { Normal, @@ -22,7 +22,7 @@ enum SectorType class Sector { public: - Sector(int size, std::unique_ptr stream); + Sector(int size, const Stream &stream); Sector(int size, const std::vector &data); Sector(int size); @@ -46,7 +46,7 @@ public: private: bool dirtyFlag = false; int size = 0; - std::unique_ptr stream; + Stream stream; SectorType type; int id = -1; std::vector data; diff --git a/Common/cppcf/sectorcollection.cpp b/Common/cppcf/sectorcollection.cpp index 888013ca16..4d1e02a02e 100644 --- a/Common/cppcf/sectorcollection.cpp +++ b/Common/cppcf/sectorcollection.cpp @@ -7,7 +7,7 @@ SectorCollection::SectorCollection() } -void SectorCollection::Add(Sector item) +void SectorCollection::Add(const Sector &item) { if (DoCheckSizeLimitReached() == false) return; @@ -15,6 +15,12 @@ void SectorCollection::Add(Sector item) add(item); } +void SectorCollection::Clear() +{ + largeArraySlices.clear(); + count = 0; +} + bool SectorCollection::DoCheckSizeLimitReached() { if (!sizeLimitReached && (count - 1 > MAX_SECTOR_V4_COUNT_LOCK_RANGE)) @@ -25,22 +31,22 @@ bool SectorCollection::DoCheckSizeLimitReached() return true; } -int SectorCollection::add(Sector item) +int SectorCollection::add(const Sector &item) { - int itemIndex = count / SLICE_SIZE; + unsigned itemIndex = count / SLICE_SIZE; - if (itemIndex < largeArraySlices.Count) - { - largeArraySlices[itemIndex].Add(item); - count++; - } - else - { - ArrayList ar = new ArrayList(SLICE_SIZE); - ar.Add(item); - largeArraySlices.Add(ar); - count++; - } + if (itemIndex < largeArraySlices.size()) + { + largeArraySlices[itemIndex]->push_back(item); + count++; + } + else + { + std::unique_ptr> ar(new std::vector(SLICE_SIZE)); + ar->emplace_back(item); + largeArraySlices.push_back(ar); + count++; + } - return count - 1; + return count - 1; } diff --git a/Common/cppcf/sectorcollection.h b/Common/cppcf/sectorcollection.h index 5b4aaf3be3..d91c7cfb06 100644 --- a/Common/cppcf/sectorcollection.h +++ b/Common/cppcf/sectorcollection.h @@ -5,16 +5,19 @@ namespace CFCPP { + class SectorCollection { public: - std::list> largeArraySlices; + std::vector>> largeArraySlices; SectorCollection(); - void Add(Sector item); + void Add(const Sector &item); + void Clear(); + inline int Count()const {return count;} private: bool DoCheckSizeLimitReached(); - int add(Sector item); + int add(const Sector &item); private: const int MAX_SECTOR_V4_COUNT_LOCK_RANGE = 524287; //0x7FFFFF00 for Version 4 const int SLICE_SIZE = 4096; diff --git a/Common/cppcf/streamrw.cpp b/Common/cppcf/streamrw.cpp index ee1c76349c..78d58f63bb 100644 --- a/Common/cppcf/streamrw.cpp +++ b/Common/cppcf/streamrw.cpp @@ -3,7 +3,7 @@ using namespace CFCPP; -StreamRW::StreamRW(std::fstream &stream) +StreamRW::StreamRW(const Stream &stream) : stream(stream) { buffer.fill(0); @@ -11,8 +11,8 @@ StreamRW::StreamRW(std::fstream &stream) T_LONG64 StreamRW::Seek(T_LONG64 offset) { - stream.seekg(offset); - return stream.tellg(); + stream->seekg(offset); + return stream->tellg(); } @@ -20,13 +20,19 @@ template std::array StreamRW::ReadArray() { std::array arr(0); - stream.read(reinterpret_cast(arr.data()), N); + stream->read(reinterpret_cast(arr.data()), N); + return arr; +} +std::vector StreamRW::ReadArray(int lenght) +{ + std::vector arr(lenght,0); + stream->read(reinterpret_cast(arr.data()), lenght); return arr; } void StreamRW::WriteArray(BYTE *arr, int lenght) { - stream.write(reinterpret_cast(arr), lenght); + stream->write(reinterpret_cast(arr), lenght); } template @@ -34,7 +40,7 @@ T StreamRW::Read() { T value; char* asByteArr = reinterpret_cast(&value); - stream.read(asByteArr, sizeof (T)); + stream->read(asByteArr, sizeof (T)); std::reverse(asByteArr, asByteArr + sizeof (T)); return value; } @@ -44,5 +50,5 @@ void StreamRW::Write(T value) { char* asByteArr = reinterpret_cast(&value); std::reverse(asByteArr, asByteArr + sizeof (T)); - stream.write(asByteArr, sizeof (T)); + stream->write(asByteArr, sizeof (T)); } diff --git a/Common/cppcf/streamrw.h b/Common/cppcf/streamrw.h index 168d344f5e..ba27aa78b8 100644 --- a/Common/cppcf/streamrw.h +++ b/Common/cppcf/streamrw.h @@ -4,6 +4,7 @@ #include #include #include "../../DesktopEditor/common/Types.h" +#include "Stream.h" namespace CFCPP @@ -11,7 +12,7 @@ namespace CFCPP class StreamRW { public: - StreamRW(std::fstream &stream); + StreamRW(const Stream &stream); T_LONG64 Seek(T_LONG64 offset); template @@ -21,11 +22,13 @@ public: void Write(T value); std::vector ReadArray(int lenght); + template + std::array ReadArray(); void WriteArray(BYTE *arr, int lenght); private: std::array buffer; - std::fstream& stream; + Stream stream; }; }