PartitionStream.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * PartitionStream.h
8  *
9  ****/
10 
11 #pragma once
12 
14 #include "Partition.h"
15 
16 namespace Storage
17 {
18 enum class Mode {
19  ReadOnly,
20  Write,
21  BlockErase,
22 };
23 
32 {
33 public:
45  SMING_DEPRECATED PartitionStream(Partition partition, storage_size_t offset, size_t size, bool blockErase)
46  : PartitionStream(partition, offset, size, blockErase ? Mode::BlockErase : Mode::ReadOnly)
47  {
48  }
49 
59  SMING_DEPRECATED PartitionStream(Partition partition, bool blockErase)
60  : PartitionStream(partition, blockErase ? Mode::BlockErase : Mode::ReadOnly)
61  {
62  }
63 
74  PartitionStream(Partition partition, storage_size_t offset, size_t size, Mode mode = Mode::ReadOnly)
75  : partition(partition), startOffset(offset), size(size), mode(mode)
76  {
77  }
78 
87  : partition(partition), startOffset{0}, size(partition.size()), mode(mode)
88  {
89  }
90 
91  int available() override
92  {
93  return size - readPos;
94  }
95 
96  uint16_t readMemoryBlock(char* data, int bufSize) override;
97 
98  int seekFrom(int offset, SeekOrigin origin) override;
99 
100  size_t write(const uint8_t* buffer, size_t size) override;
101 
102  bool isFinished() override
103  {
104  return available() <= 0;
105  }
106 
107 private:
108  Partition partition;
109  storage_size_t startOffset;
110  size_t size;
111  uint32_t writePos{0};
112  uint32_t readPos{0};
113  uint32_t erasePos{0};
114  Mode mode;
115 };
116 
117 } // namespace Storage
uint32_t storage_size_t
Definition: Components/Storage/src/include/Storage/Types.h:19
SeekOrigin
Stream/file seek origins.
Definition: SeekOrigin.h:18
Base class for read/write stream.
Definition: ReadWriteStream.h:20
Stream operating directory on a Storage partition.
Definition: PartitionStream.h:32
PartitionStream(Partition partition, Mode mode=Mode::ReadOnly)
Access entire partition using stream.
Definition: PartitionStream.h:86
int seekFrom(int offset, SeekOrigin origin) override
Change position in stream.
uint16_t readMemoryBlock(char *data, int bufSize) override
Read a block of memory.
size_t write(const uint8_t *buffer, size_t size) override
Write chars to stream.
PartitionStream(Partition partition, bool blockErase)
Access entire partition using stream.
Definition: PartitionStream.h:59
bool isFinished() override
Check if all data has been read.
Definition: PartitionStream.h:102
PartitionStream(Partition partition, storage_size_t offset, size_t size, bool blockErase)
Access part of a partition using a stream.
Definition: PartitionStream.h:45
PartitionStream(Partition partition, storage_size_t offset, size_t size, Mode mode=Mode::ReadOnly)
Access part of a partition using a stream.
Definition: PartitionStream.h:74
int available() override
Return the total length of the stream.
Definition: PartitionStream.h:91
Represents a flash partition.
Definition: Partition.h:86
Definition: FileDevice.h:26
Mode
Definition: PartitionStream.h:18
@ Write
Write but do not erase, region should be pre-erased.
@ BlockErase
Erase blocks as required before writing.
#define SMING_DEPRECATED
Definition: sming_attr.h:36