* Scan through entire source stream to map location and size of sections. * Called once by constructor. */
| 18 | * Called once by constructor. |
| 19 | */ |
| 20 | void SectionStream::scanSource(uint8_t maxSections) |
| 21 | { |
| 22 | constexpr size_t bufSize{512}; |
| 23 | char buffer[bufSize]; |
| 24 | |
| 25 | sections = std::make_unique<Section[]>(maxSections); |
| 26 | |
| 27 | size_t offset{0}; |
| 28 | while(sectionCount < maxSections && !stream->isFinished()) { |
| 29 | auto& sect = sections[sectionCount]; |
| 30 | stream->seekFrom(offset, SeekOrigin::Start); |
| 31 | size_t bytesRead = stream->readMemoryBlock(buffer, bufSize); |
| 32 | if(sect.start == 0) { |
| 33 | auto tag = (char*)memmem(buffer, bytesRead, startTag.c_str(), startTag.length()); |
| 34 | if(tag == nullptr) { |
| 35 | if(bytesRead < bufSize) { |
| 36 | break; |
| 37 | } |
| 38 | // If tag starts near end of buffer, this ensures it'll be contained in next read |
| 39 | offset += bytesRead - startTag.length(); |
| 40 | continue; |
| 41 | } |
| 42 | |
| 43 | offset += (tag - buffer) + startTag.length(); |
| 44 | sect.start = offset; |
| 45 | continue; |
| 46 | } |
| 47 | |
| 48 | auto tag = (char*)memmem(buffer, bytesRead, endTag.c_str(), endTag.length()); |
| 49 | if(tag == nullptr) { |
| 50 | if(bytesRead < bufSize) { |
| 51 | break; |
| 52 | } |
| 53 | offset += bytesRead - endTag.length(); |
| 54 | continue; |
| 55 | } |
| 56 | offset += (tag - buffer); |
| 57 | sect.size = offset - sect.start; |
| 58 | offset += endTag.length(); |
| 59 | ++sectionCount; |
| 60 | } |
| 61 | |
| 62 | // No sections? Then consider the entire stream as a single section |
| 63 | if(sectionCount == 0) { |
| 64 | sections[0].start = 0; |
| 65 | sections[0].size = stream->seekFrom(0, SeekOrigin::End); |
| 66 | sectionCount = 1; |
| 67 | } |
| 68 | |
| 69 | stream->seekFrom(0, SeekOrigin::Start); |
| 70 | } |
| 71 | |
| 72 | bool SectionStream::gotoSection(uint8_t index) |
| 73 | { |
nothing calls this directly
no test coverage detected