S2OJ/web/app/models/StrictFileReader.php

82 lines
1.3 KiB
PHP
Raw Normal View History

2022-11-06 02:26:21 +00:00
<?php
class StrictFileReader {
private $f;
private $buf = '', $off = 0;
2022-11-06 02:26:21 +00:00
public function __construct($file_name) {
$this->f = fopen($file_name, 'r');
}
2022-11-06 02:26:21 +00:00
public function failed() {
return $this->f === false;
}
2022-11-06 02:26:21 +00:00
public function readChar() {
if (isset($this->buf[$this->off])) {
return $this->buf[$this->off++];
}
return fgetc($this->f);
}
public function unreadChar($c) {
$this->buf .= $c;
if ($this->off > 1000) {
$this->buf = substr($this->buf, $this->off);
$this->off = 0;
}
}
2022-11-06 02:26:21 +00:00
public function readString() {
$str = '';
while (true) {
$c = $this->readChar();
if ($c === false) {
break;
} elseif ($c === " " || $c === "\n" || $c === "\r") {
$this->unreadChar($c);
break;
} else {
$str .= $c;
}
}
return $str;
}
2022-11-06 02:26:21 +00:00
public function readLine() {
$str = '';
while (true) {
$c = $this->readChar();
if ($c === false) {
break;
} elseif ($c === "\n") {
break;
} else {
$str .= $c;
}
}
return $str;
}
2022-11-06 02:26:21 +00:00
public function ignoreWhite() {
while (true) {
$c = $this->readChar();
if ($c === false) {
break;
} elseif ($c === " " || $c === "\n" || $c === "\r") {
continue;
} else {
$this->unreadChar($c);
break;
}
}
}
public function eof() {
return feof($this->f);
}
public function close() {
fclose($this->f);
}
2022-11-06 02:26:21 +00:00
}