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, bufSize);
88  }
89 
90  int seekFrom(int offset, SeekOrigin origin) override
91  {
92  size_t newPos;
93  switch(origin) {
94  case SeekOrigin::Start:
95  newPos = offset;
96  break;
98  newPos = readPos + offset;
99  break;
100  case SeekOrigin::End:
101  newPos = size + offset;
102  break;
103  default:
104  return -1;
105  }
106 
107  if(newPos > size) {
108  return -1;
109  }
110 
111  source.seekFrom(startOffset + newPos, SeekOrigin::Start);
112  readPos = newPos;
113  return readPos;
114  }
115 
116  bool isFinished() override
117  {
118  return available() <= 0;
119  }
120 
121 private:
122  IDataSourceStream& source;
123  uint32_t startOffset;
124  uint32_t readPos{0};
125  size_t size;
126 };
127 
128 } // 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:116
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