From edcca2d0c58508caa7cdc65fa68578348e74757f Mon Sep 17 00:00:00 2001 From: lslqtz Date: Thu, 15 Feb 2024 06:07:08 +0800 Subject: [PATCH] Add revert method --- src/FontLib/BinaryStream.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/FontLib/BinaryStream.php b/src/FontLib/BinaryStream.php index 5e170c6..294f614 100644 --- a/src/FontLib/BinaryStream.php +++ b/src/FontLib/BinaryStream.php @@ -18,6 +18,7 @@ class BinaryStream { * @var resource The file pointer */ protected $f; + protected $f2; const uint8 = 1; const int8 = 2; @@ -66,6 +67,9 @@ public function open($filename, $mode = self::modeRead) { throw new \Exception("Unknown file open mode"); } + if ($this->f != null && $this->f != false) { + $this->f2 = $this->f; + } $this->f = fopen($filename, $mode); return $this->f != false; @@ -75,9 +79,22 @@ public function open($filename, $mode = self::modeRead) { * Close the internal file pointer */ public function close() { + if ($this->f2 !== null && $this->f2 !== false) { + fclose($this->f2); + } return fclose($this->f) != false; } + public function revert() { + if ($this->f2 !== null && $this->f !== null && $this->f !== false) { + fclose($this->f); + $this->f = $this->f2; + $this->f2 = null; + return true; + } + return false; + } + /** * Change the internal file pointer *