SectionStream.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  * SectionStream.h
8  *
9  * @author mikee47 <mike@sillyhouse.net> Nov 2020
10  *
11  ****/
12 
13 #pragma once
14 
15 #include "DataSourceStream.h"
16 
27 {
28 public:
29  static constexpr uint8_t maxSections = 5;
30 
31  struct Section {
32  uint32_t start; // Within stream
33  uint32_t size;
34  uint32_t recordCount;
36 
37  uint32_t end() const
38  {
39  return start + size;
40  }
41  };
42 
47 
53 
54  SectionStream(IDataSourceStream* source) : stream(source), startTag(F("{SECTION}")), endTag(F("{/SECTION}"))
55  {
56  scanSource();
57  }
58 
60  {
61  delete stream;
62  }
63 
64  int available() override
65  {
66  return -1;
67  }
68 
69  uint16_t readMemoryBlock(char* data, int bufSize) override;
70 
71  int seekFrom(int offset, SeekOrigin origin) override;
72 
73  bool isFinished() override
74  {
75  return finished;
76  }
77 
78  int sectionIndex() const
79  {
80  return currentSectionIndex;
81  }
82 
83  int recordIndex() const
84  {
85  auto section = getSection();
86  return section ? section->recordIndex : -1;
87  }
88 
89  size_t count() const
90  {
91  return sectionCount;
92  }
93 
94  const Section* getSection() const
95  {
96  return getSection(currentSectionIndex);
97  }
98 
99  const Section* getSection(unsigned index) const
100  {
101  if(index < sectionCount) {
102  return &sections[index];
103  } else {
104  return nullptr;
105  }
106  }
107 
108  void onNextSection(NextSection callback)
109  {
110  nextSectionCallback = callback;
111  }
112 
113  void onNextRecord(NextRecord callback)
114  {
115  nextRecordCallback = callback;
116  }
117 
121  bool gotoSection(uint8_t index);
122 
123  bool setNewSection(int8_t index)
124  {
125  if(index < 0 || index >= sectionCount) {
126  return false;
127  }
128  newSection = index;
129  return true;
130  }
131 
132 protected:
136  virtual void nextSection();
137 
142  virtual bool nextRecord()
143  {
144  if(nextRecordCallback) {
145  return nextRecordCallback();
146  }
147 
148  // By default, emit section once
149  return recordIndex() < 0;
150  }
151 
152 private:
153  void scanSource();
154 
155  IDataSourceStream* stream{nullptr};
156  NextSection nextSectionCallback;
157  NextRecord nextRecordCallback;
158  String startTag;
159  String endTag;
160  Section sections[maxSections]{};
161  uint32_t readOffset{0};
162  uint32_t sectionOffset{0};
163  uint8_t sectionCount{0};
164  int8_t currentSectionIndex{-1};
165  int8_t newSection{-1};
166  bool finished{false};
167 };
uint32_t recordCount
Definition: SectionStream.h:34
bool setNewSection(int8_t index)
Definition: SectionStream.h:123
int recordIndex() const
Definition: SectionStream.h:83
uint32_t end() const
Definition: SectionStream.h:37
size_t count() const
Definition: SectionStream.h:89
Base class for read-only stream.
Definition: DataSourceStream.h:40
virtual bool nextRecord()
Move to first/next record.
Definition: SectionStream.h:142
uint32_t size
Definition: SectionStream.h:33
int recordIndex
Definition: SectionStream.h:35
The String class.
Definition: WString.h:136
static constexpr uint8_t maxSections
Definition: SectionStream.h:29
int sectionIndex() const
Definition: SectionStream.h:78
SeekOrigin
Stream/file seek origins.
Definition: SeekOrigin.h:18
#define F(string_literal)
Wrap a string literal stored in flash and access it using a String object.
Definition: WString.h:113
void onNextRecord(NextRecord callback)
Definition: SectionStream.h:113
void onNextSection(NextSection callback)
Definition: SectionStream.h:108
SectionStream(IDataSourceStream *source)
Definition: SectionStream.h:54
bool gotoSection(uint8_t index)
Goto a new section immediately.
int seekFrom(int offset, SeekOrigin origin) override
Change position in stream.
uint32_t start
Definition: SectionStream.h:32
uint16_t readMemoryBlock(char *data, int bufSize) override
Read a block of memory.
Definition: SectionStream.h:31
~SectionStream()
Definition: SectionStream.h:59
int available() override
Return the total length of the stream.
Definition: SectionStream.h:64
Presents each section within a source stream as a separate stream.
Definition: SectionStream.h:26
const Section * getSection() const
Definition: SectionStream.h:94
bool isFinished() override
Check if all data has been read.
Definition: SectionStream.h:73
virtual void nextSection()
Invoked when moving to a new section.
const Section * getSection(unsigned index) const
Definition: SectionStream.h:99