Exemplo n.º 1
0
void Buffer::SetPos(int value) {
    if ((value >= fileLen) && (stream != NULL) && !CanSeek()) {
        // Wanted position is after buffer and the stream
        // is not seek-able e.g. network or console,
        // thus we have to read the stream manually till
        // the wanted position is in sight.
        while ((value >= fileLen) && (ReadNextStreamChunk() > 0));
    }

    if ((value < 0) || (value > fileLen)) {
        wprintf(L"--- buffer out of bounds access, position: %d\n", value);
        MX_DEBUG_BREAK;
        exit(1);
    }

    if ((value >= bufStart) && (value < (bufStart + bufLen))) { // already in buffer
        bufPos = value - bufStart;
    } else if (stream != NULL) { // must be swapped in
        fseek(stream, value, SEEK_SET);
        bufLen = fread(buf, sizeof(unsigned char), bufCapacity, stream);
        bufStart = value;
        bufPos = 0;
    } else {
        bufPos = fileLen - bufStart; // make Pos return fileLen
    }
}
Exemplo n.º 2
0
int Buffer::Read() {
	if (bufPos < bufLen) {
		return buf[bufPos++];
	} else if (GetPos() < fileLen) {
		SetPos(GetPos()); // shift buffer start to Pos
		return buf[bufPos++];
	} else if ((stream != NULL) && !CanSeek() && (ReadNextStreamChunk() > 0)) {
		return buf[bufPos++];
	} else {
		return EoF;
	}
}