ChangeLog::readAdjacentChunk PHP Method

readAdjacentChunk() protected method

Returns the next lines of the changelog of the chunck before head or after tail
protected readAdjacentChunk ( resource $fp, integer $head, integer $tail, integer $direction ) : array
$fp resource filepointer
$head integer position head of last chunk
$tail integer position tail of last chunk
$direction integer positive forward, negative backward
return array with entries: - $lines: changelog lines of readed chunk - $head: head of chunk - $tail: tail of chunk
    protected function readAdjacentChunk($fp, $head, $tail, $direction)
    {
        if (!$fp) {
            return array(array(), $head, $tail);
        }
        if ($direction > 0) {
            //read forward
            $head = $tail;
            $tail = $head + floor($this->chunk_size * (2 / 3));
            $tail = $this->getNewlinepointer($fp, $tail);
        } else {
            //read backward
            $tail = $head;
            $head = max($tail - $this->chunk_size, 0);
            while (true) {
                $nl = $this->getNewlinepointer($fp, $head);
                // was the chunk big enough? if not, take another bite
                if ($nl > 0 && $tail <= $nl) {
                    $head = max($head - $this->chunk_size, 0);
                } else {
                    $head = $nl;
                    break;
                }
            }
        }
        //load next chunck
        $lines = $this->readChunk($fp, $head, $tail);
        return array($lines, $head, $tail);
    }