]> git.andy128k.dev Git - missing-tools.git/commitdiff
add pushback streams
authorAndrey Kutejko <andy128k@gmail.com>
Sat, 22 Mar 2014 14:28:59 +0000 (16:28 +0200)
committerAndrey Kutejko <andy128k@gmail.com>
Sat, 22 Mar 2014 14:28:59 +0000 (16:28 +0200)
composer.lock
src/io/pushback.php [new file with mode: 0644]

index cf524046cf0d301c94fd78363dcb2774877e6870..c0c485aeb9704ff40d3a987df1295e45e947288a 100644 (file)
         },
         {
             "name": "phpunit/php-token-stream",
-            "version": "1.2.1",
+            "version": "1.2.2",
             "source": {
                 "type": "git",
                 "url": "https://github.com/sebastianbergmann/php-token-stream.git",
-                "reference": "5220af2a7929aa35cf663d97c89ad3d50cf5fa3e"
+                "reference": "ad4e1e23ae01b483c16f600ff1bebec184588e32"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/5220af2a7929aa35cf663d97c89ad3d50cf5fa3e",
-                "reference": "5220af2a7929aa35cf663d97c89ad3d50cf5fa3e",
+                "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/ad4e1e23ae01b483c16f600ff1bebec184588e32",
+                "reference": "ad4e1e23ae01b483c16f600ff1bebec184588e32",
                 "shasum": ""
             },
             "require": {
             "keywords": [
                 "tokenizer"
             ],
-            "time": "2013-09-13 04:58:23"
+            "time": "2014-03-03 05:10:30"
         },
         {
             "name": "phpunit/phpunit",
diff --git a/src/io/pushback.php b/src/io/pushback.php
new file mode 100644 (file)
index 0000000..e2dfc8b
--- /dev/null
@@ -0,0 +1,78 @@
+<?php
+
+namespace PFF\IO;
+
+interface PushBackInputStream
+{
+    public function close();
+    public function getc();
+    public function ungetc($char);
+}
+
+class FileInputStream implements PushBackInputStream
+{
+    private $fd, $buffer = array();
+
+    public function __construct($path, $mode)
+    {
+        $this->fd = fopen($path, $mode);
+    }
+
+    public function __destruct()
+    {
+        $this->close();
+    }
+
+    public function close()
+    {
+        if ($this->fd) {
+            fclose($this->fd);
+            $this->fd = null;
+        }
+    }
+
+    public function getc()
+    {
+        if (count($this->buffer)) {
+            return array_shift($this->buffer);
+        } else {
+            return fgetc($this->fd);
+        }
+    }
+
+    public function ungetc($char)
+    {
+        array_unshift($this->buffer, $char);
+    }
+}
+
+class StringInputStream implements PushBackInputStream
+{
+    private $string, $position;
+
+    public function __construct($string)
+    {
+        $this->string = $string;
+        $this->position = 0;
+    }
+
+    public function close()
+    {
+        if ($this->string !== null)
+            $this->string = null;
+    }
+
+    public function getc()
+    {
+        if ($this->position >= strlen($this->string))
+            return false;
+        else
+            return $this->string[$this->position++];
+    }
+
+    public function ungetc($char)
+    {
+        --$this->position;
+    }
+}
+