52 lines
1.3 KiB
PHP
52 lines
1.3 KiB
PHP
|
<?php
|
||
|
namespace nulib\file\base;
|
||
|
|
||
|
/**
|
||
|
* Class FileReader: un fichier accédé en lecture
|
||
|
*/
|
||
|
class FileReader extends _File {
|
||
|
/** @var bool *à l'ouverture du fichier*, faut-il ignorer le BOM? */
|
||
|
const IGNORE_BOM = true;
|
||
|
|
||
|
const DEFAULT_MODE = "rb";
|
||
|
|
||
|
/** @var bool */
|
||
|
protected $ignoreBom;
|
||
|
|
||
|
function __construct($input, ?string $mode=null, bool $throwOnError=true, ?bool $allowLocking=null, ?bool $ignoreBom=null) {
|
||
|
if ($ignoreBom === null) $ignoreBom = static::IGNORE_BOM;
|
||
|
$this->ignoreBom = $ignoreBom;
|
||
|
if ($input === null) {
|
||
|
$fd = STDIN;
|
||
|
$close = false;
|
||
|
} elseif (is_resource($input)) {
|
||
|
$fd = $input;
|
||
|
$close = false;
|
||
|
} else {
|
||
|
$file = $input;
|
||
|
if ($mode === null) $mode = static::DEFAULT_MODE;
|
||
|
$this->file = $file;
|
||
|
$this->mode = $mode;
|
||
|
$fd = $this->open();
|
||
|
$close = true;
|
||
|
}
|
||
|
parent::__construct($fd, $close, $throwOnError, $allowLocking);
|
||
|
}
|
||
|
|
||
|
/** @return resource */
|
||
|
protected function open() {
|
||
|
$fd = parent::open();
|
||
|
$this->haveBom = false;
|
||
|
if ($this->ignoreBom) {
|
||
|
$bom = fread($fd, 3);
|
||
|
if ($bom === "\xEF\xBB\xBF") $this->seekOffset = 3;
|
||
|
else rewind($fd);
|
||
|
}
|
||
|
return $fd;
|
||
|
}
|
||
|
|
||
|
function haveBom(): bool {
|
||
|
return $this->seekOffset !== 0;
|
||
|
}
|
||
|
}
|