/** * @param Index $index */ public function __construct(Index $index) { $this->index = $index; $this->filename = $index->getInfo()->getDictFilename(); $this->isCompressed = Info::isCompressedFile($this->filename); $this->open(); }
/** * Reads index data. * * @throws IndexException */ protected function readFile() { $filename = $this->getFilename(); $data = file_get_contents($filename); $fsize = filesize($filename); if ($data && Info::isCompressedFile($filename)) { $data = gzdecode($data); $fsize = strlen($data); } if (!$data) { throw new IndexException($this, 'Read data error.'); } if ($fsize != $this->info->idxfilesize) { throw new IndexException($this, 'Index file size mismatch.'); } $pos = 0; while ($pos < $fsize) { $chars = []; while (true) { $x = unpack("@{$pos}/Cch", $data); $pos++; if ($x['ch'] === 0) { break; } $chars[] = $x['ch']; } $word = call_user_func_array('pack', array_merge(['C*'], $chars)); $x = unpack("@{$pos}/Noffset/Nsize", $data); $this->words[$word] = [$x['offset'], $x['size']]; $pos += 8; } }