cvs: php4 /pear/Experimental System.php

From: Date: Tue, 09 Oct 2001 16:18:24 +0000
Subject: cvs: php4 /pear/Experimental System.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-962@lists.php.net to get a copy of this message
cox Tue Oct 9 12:18:24 2001 EDT Modified files: /php4/pear/Experimental System.php Log: Added the "cat" command: 1) $var = System::cat('sample.txt test.txt'); 2) System::cat('sample.txt test.txt > final.txt'); 3) System::cat('sample.txt test.txt >> final.txt'); Index: php4/pear/Experimental/System.php diff -u php4/pear/Experimental/System.php:1.2 php4/pear/Experimental/System.php:1.3 --- php4/pear/Experimental/System.php:1.2 Sun Oct 7 16:13:19 2001 +++ php4/pear/Experimental/System.php Tue Oct 9 12:18:23 2001 @@ -17,13 +17,14 @@ // | | // +----------------------------------------------------------------------+ // -// $Id: System.php,v 1.2 2001/10/07 20:13:19 cox Exp $ +// $Id: System.php,v 1.3 2001/10/09 16:18:23 cox Exp $ // // TODO: // - Test under Windows (help is really appreciated in this point) // - Build strong tests // - Error reporting (now shows standar php errors) +// - Write doc require_once 'PEAR.php'; require_once 'Console/Getopt.php'; @@ -190,6 +191,62 @@ } } return true; + } + + /** + * Concatenate files + * + * Usage: + * 1) $var = System::cat('sample.txt test.txt'); + * 2) System::cat('sample.txt test.txt > final.txt'); + * 3) System::cat('sample.txt test.txt >> final.txt'); + * + * Note: as the class use fopen, urls should work also (test that) + */ + function &cat($args) + { + $ret = null; + $files = array(); + if (!is_array($args)) { + $args = preg_split('/\s+/', $args); + } + for($i=0; $i < count($args); $i++) { + if ($args[$i] == '>') { + $mode = 'wb'; + $outputfile = $args[$i+1]; + break; + } elseif ($args[$i] == '>>') { + $mode = 'ab+'; + $outputfile = $args[$i+1]; + break; + } else { + $files[] = $args[$i]; + } + } + if (isset($mode)) { + if (!$outputfd = fopen($outputfile, $mode)) { + return $this->raiseError("Could not open $outputfile"); + } + $ret = true; + } + foreach($files as $file) { + if (!$fd = fopen($file, 'r')) { + return $this->raiseError("Could not open $file"); + } + while(!feof($fd)) { + $cont = fread($fd, 2048); + if (isset($outputfd)) { + fwrite($outputfd, $cont); + } else { + $ret .= $cont; + } + } + fclose($fd); + } + if (@is_resource($outputfd)) { + fclose($outputfd); + } + return $ret; } }

« previous php.pear.cvs (#962) next »