Add Horde_Util_Filter_Eol
authorMichael M Slusarz <slusarz@curecanti.org>
Thu, 2 Jul 2009 18:30:32 +0000 (12:30 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Thu, 2 Jul 2009 18:30:32 +0000 (12:30 -0600)
framework/Util/lib/Horde/Util/Filter/Eol.php [new file with mode: 0644]
framework/Util/package.xml
framework/Util/test/Horde/Util/eol_filter.phpt [new file with mode: 0644]

diff --git a/framework/Util/lib/Horde/Util/Filter/Eol.php b/framework/Util/lib/Horde/Util/Filter/Eol.php
new file mode 100644 (file)
index 0000000..3082d21
--- /dev/null
@@ -0,0 +1,56 @@
+<?php
+/**
+ * Stream filter class to convert WOL characters.
+ *
+ * Usage:
+ *   stream_filter_register('horde_eol', 'Horde_Util_Filter_Eol');
+ *   stream_filter_[app|pre]pend($stream, 'horde_eol', $params);
+ *
+ * $params can contain the following:
+ * <pre>
+ * 'eol' - The EOL string to use.
+ *         DEFAULT: <CR><LF> ("\r\n")
+ * </pre>
+ *
+ * Copyright 2009 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @author  Michael Slusarz <slusarz@horde.org>
+ * @package Horde_Util
+ */
+class Horde_Util_Filter_Eol extends php_user_filter
+{
+    protected $_search;
+    protected $_replace;
+
+    public function onCreate()
+    {
+        $eol = isset($this->params['eol']) ? $this->params['eol'] : "\r\n";
+        if (!strlen($eol)) {
+            $this->_search = array("\r", "\n");
+            $this->_replace = '';
+        } elseif (in_array($eol, array("\r", "\n"))) {
+            $this->_search = array("\r\n", ($eol == "\r") ? "\n" : "\r");
+            $this->_replace = $eol;
+        } else {
+            $this->_search = array("\r\n", "\r", "\n");
+            $this->_replace = array("\n", "\n", $eol);
+        }
+
+        return true;
+    }
+
+    public function filter($in, $out, &$consumed, $closing)
+    {
+        while ($bucket = stream_bucket_make_writeable($in)) {
+            $bucket->data = str_replace($this->_search, $this->_replace, $bucket->data);
+            $consumed += $bucket->datalen;
+            stream_bucket_append($out, $bucket);
+        }
+
+        return PSFS_PASS_ON;
+    }
+
+}
index 4318542..fa2fe8d 100644 (file)
@@ -29,7 +29,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
   <api>beta</api>
  </stability>
  <license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
- <notes>* Removed Horde_Array::combine() and Horde_Util::hmac().
+ <notes>* Added Horde_Util_Filter_Eol.
+ * Removed Horde_Array::combine() and Horde_Util::hmac().
  * Initial Horde 4 package.
  </notes>
  <contents>
@@ -41,6 +42,11 @@ http://pear.php.net/dtd/package-2.0.xsd">
        <file name="Helper.php" role="php" />
       </dir> <!-- /lib/Horde/Array/Sort -->
      </dir> <!-- /lib/Horde/Array -->
+     <dir name="Util">
+      <dir name="Filter">
+       <file name="Eol.php" role="php" />
+      </dir> <!-- /lib/Horde/Util/Filter -->
+     </dir> <!-- /lib/Horde/Util -->
      <file name="Array.php" role="php" />
      <file name="String.php" role="php" />
      <file name="Util.php" role="php" />
@@ -55,6 +61,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
       <file name="addParameter.phpt" role="test" />
       <file name="case.phpt" role="test" />
       <file name="case_php6.phpt" role="test" />
+      <file name="eol_filter.phpt" role="test" />
       <file name="length.phpt" role="test" />
       <file name="pad.phpt" role="test" />
       <file name="removeParameter.phpt" role="test" />
@@ -96,6 +103,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
    <install name="lib/Horde/Array/Sort/Helper.php" as="Horde/Array/Sort/Helper.php" />
    <install name="lib/Horde/String.php" as="Horde/String.php" />
    <install name="lib/Horde/Util.php" as="Horde/Util.php" />
+   <install name="lib/Horde/Util/Filter/Eol.php" as="Horde/Util/Filter/Eol.php" />
    <install name="lib/Horde/Variables.php" as="Horde/Variables.php" />
   </filelist>
  </phprelease>
diff --git a/framework/Util/test/Horde/Util/eol_filter.phpt b/framework/Util/test/Horde/Util/eol_filter.phpt
new file mode 100644 (file)
index 0000000..b2f5582
--- /dev/null
@@ -0,0 +1,62 @@
+--TEST--
+Horde_Util_Filter_Eol:: tests
+--FILE--
+<?php
+
+require_once dirname(__FILE__) . '/../../../lib/Horde/Util/Filter/Eol.php';
+
+stream_filter_register('horde_eol', 'Horde_Util_Filter_Eol');
+
+$test = fopen('php://temp', 'r+');
+fwrite($test, "A\r\nB\rC\nD\r\n\r\nE\r\rF\n\nG\r\n\n\r\nH\r\n\r\r\nI");
+
+foreach (array("\r", "\n", "\r\n", "") as $val) {
+    $filter = stream_filter_prepend($test, 'horde_eol', STREAM_FILTER_READ, array('eol' => $val));
+    rewind($test);
+    fpassthru($test);
+    stream_filter_remove($filter);
+
+    echo "\n---\n";
+}
+
+fclose($test);
+?>
+--EXPECT--
+A\rB\rC\rD\r\rE\r\rF\r\rG\r\r\rH\r\r\rI
+---
+A
+B
+C
+D
+
+E
+
+F
+
+G
+
+
+H
+
+
+I
+---
+A\r
+B\r
+C\r
+D\r
+\r
+E\r
+\r
+F\r
+\r
+G\r
+\r
+\r
+H\r
+\r
+\r
+I
+---
+ABCDEFGHI
+---