Libraries/Graphics/src/include/Graphics/Stream.h
Go to the documentation of this file.
1 /****
2  * Stream.h
3  *
4  * Copyright 2021 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the Sming-Graphics Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  * @author: May 2021 - mikee47 <mike@sillyhouse.net>
19  *
20  ****/
21 
22 #pragma once
23 
25 
26 namespace Graphics
27 {
29 {
30 public:
31  WriteStream(Print& stream) : stream(stream)
32  {
33  }
34 
35  void write(const void* buffer, size_t count);
36 
37  void flush();
38 
39 private:
40  static constexpr size_t size{256};
41  Print& stream;
42  uint8_t data[size];
43  uint16_t length{0};
44 };
45 
47 {
48 public:
49  ReadStream(IDataSourceStream& stream) : stream(stream)
50  {
51  }
52 
53  size_t read(uint32_t offset, void* buffer, size_t count);
54  uint8_t read(uint32_t offset);
55 
56 private:
57  static constexpr size_t size{64};
58  IDataSourceStream& stream;
59  uint8_t data[size];
60  uint32_t start{0};
61  uint16_t length{0};
62 };
63 
65 {
66 public:
67  SubStream(IDataSourceStream& source, uint32_t offset, size_t size) : source(source), startOffset(offset), size(size)
68  {
69  int sourceSize = source.seekFrom(0, SeekOrigin::End);
70  if(sourceSize < int(offset)) {
71  this->size = 0;
72  return;
73  }
74 
75  this->size = std::min(size, size_t(sourceSize - offset));
76 
77  source.seekFrom(offset, SeekOrigin::Start);
78  }
79 
80  int available() override
81  {
82  return size - readPos;
83  }
84 
85  uint16_t readMemoryBlock(char* data, int bufSize) override
86  {
87  return source.readMemoryBlock(data, std::min(bufSize, available()));
88  }
89 
90  int seekFrom(int offset, SeekOrigin origin) override
91  {
92 
93  size_t newPos;
94  switch(origin) {
95  case SeekOrigin::Start:
96  newPos = offset;
97  break;
99  newPos = readPos + offset;
100  break;
101  case SeekOrigin::End:
102  newPos = size + offset;
103  break;
104  default:
105  return -1;
106  }
107 
108  if(newPos > size) {
109  return -1;
110  }
111 
112  source.seekFrom(startOffset + newPos, SeekOrigin::Start);
113  readPos = newPos;
114  return readPos;
115  }
116 
117  bool isFinished() override
118  {
119  return available() <= 0;
120  }
121 
122 private:
123  IDataSourceStream& source;
124  uint32_t startOffset;
125  uint32_t readPos{0};
126  size_t size;
127 };
128 
129 } // namespace Graphics
SeekOrigin
Stream/file seek origins.
Definition: SeekOrigin.h:18
@ Current
SEEK_CUR: Current position in file.
@ End
SEEK_END: End of file.
@ Start
SEEK_SET: Start of file.
Definition: Libraries/Graphics/src/include/Graphics/Stream.h:47
uint8_t read(uint32_t offset)
ReadStream(IDataSourceStream &stream)
Definition: Libraries/Graphics/src/include/Graphics/Stream.h:49
size_t read(uint32_t offset, void *buffer, size_t count)
Definition: Libraries/Graphics/src/include/Graphics/Stream.h:65
SubStream(IDataSourceStream &source, uint32_t offset, size_t size)
Definition: Libraries/Graphics/src/include/Graphics/Stream.h:67
int available() override
Return the total length of the stream.
Definition: Libraries/Graphics/src/include/Graphics/Stream.h:80
uint16_t readMemoryBlock(char *data, int bufSize) override
Read a block of memory.
Definition: Libraries/Graphics/src/include/Graphics/Stream.h:85
int seekFrom(int offset, SeekOrigin origin) override
Change position in stream.
Definition: Libraries/Graphics/src/include/Graphics/Stream.h:90
bool isFinished() override
Check if all data has been read.
Definition: Libraries/Graphics/src/include/Graphics/Stream.h:117
Definition: Libraries/Graphics/src/include/Graphics/Stream.h:29
WriteStream(Print &stream)
Definition: Libraries/Graphics/src/include/Graphics/Stream.h:31
void write(const void *buffer, size_t count)
Base class for read-only stream.
Definition: DataSourceStream.h:46
virtual uint16_t readMemoryBlock(char *data, int bufSize)=0
Read a block of memory.
virtual int seekFrom(int offset, SeekOrigin origin)
Change position in stream.
Definition: DataSourceStream.h:97
Provides formatted output to stream.
Definition: Print.h:37
Definition: Virtual.h:31