| 45 | // ***************************************** Getting data out of Chunks |
| 46 | |
| 47 | QByteArray Chunks::data(qint64 pos, qint64 maxSize, QByteArray *highlighted) |
| 48 | { |
| 49 | qint64 ioDelta = 0; |
| 50 | int chunkIdx = 0; |
| 51 | |
| 52 | Chunk chunk; |
| 53 | QByteArray buffer; |
| 54 | |
| 55 | // Do some checks and some arrangements |
| 56 | if (highlighted) |
| 57 | highlighted->clear(); |
| 58 | |
| 59 | if (pos >= _size) |
| 60 | return buffer; |
| 61 | |
| 62 | if (maxSize < 0) |
| 63 | maxSize = _size; |
| 64 | else |
| 65 | if ((pos + maxSize) > _size) |
| 66 | maxSize = _size - pos; |
| 67 | |
| 68 | _ioDevice->open(QIODevice::ReadOnly); |
| 69 | |
| 70 | while (maxSize > 0) |
| 71 | { |
| 72 | chunk.absPos = LLONG_MAX; |
| 73 | bool chunksLoopOngoing = true; |
| 74 | while ((chunkIdx < _chunks.count()) && chunksLoopOngoing) |
| 75 | { |
| 76 | // In this section, we track changes before our required data and |
| 77 | // we take the editdet data, if availible. ioDelta is a difference |
| 78 | // counter to justify the read pointer to the original data, if |
| 79 | // data in between was deleted or inserted. |
| 80 | |
| 81 | chunk = _chunks[chunkIdx]; |
| 82 | if (chunk.absPos > pos) |
| 83 | chunksLoopOngoing = false; |
| 84 | else |
| 85 | { |
| 86 | chunkIdx += 1; |
| 87 | qint64 count; |
| 88 | qint64 chunkOfs = pos - chunk.absPos; |
| 89 | if (maxSize > ((qint64)chunk.data.size() - chunkOfs)) |
| 90 | { |
| 91 | count = (qint64)chunk.data.size() - chunkOfs; |
| 92 | ioDelta += CHUNK_SIZE - chunk.data.size(); |
| 93 | } |
| 94 | else |
| 95 | count = maxSize; |
| 96 | if (count > 0) |
| 97 | { |
| 98 | buffer += chunk.data.mid(chunkOfs, (int)count); |
| 99 | maxSize -= count; |
| 100 | pos += count; |
| 101 | if (highlighted) |
| 102 | *highlighted += chunk.dataChanged.mid(chunkOfs, (int)count); |
| 103 | } |
| 104 | } |