diff options
Diffstat (limited to 'it_pipe.class')
-rw-r--r-- | it_pipe.class | 104 |
1 files changed, 58 insertions, 46 deletions
diff --git a/it_pipe.class b/it_pipe.class index 3fc756b..8b12fec 100644 --- a/it_pipe.class +++ b/it_pipe.class @@ -20,6 +20,56 @@ function __construct($p = array()) $this->lines = array_merge((array)$this->lines, file($fn, FILE_IGNORE_NEW_LINES)); } +# +# Magic functions +# +function __call($name, $params) +{ + array_unshift($params, ""); + $func = ($t = it::match('(\w+)__(\w+)', $name)) ? array($t[0], $t[1]) : (method_exists($this, $name) ? array($this, $name) : $name); + + foreach($this->lines as $i => $params[0]) + $this->lines[$i] = call_user_func_array($func, $params); + + return $this; +} + +function __toString() +{ + return is_array($this->lines) ? join("\n", $this->lines) . "\n" : ""; +} + + +# +# Internal: Implement iterator +# +function rewind() +{ + reset($this->lines); + $this->next(); +} + +function next() +{ + $this->valid = each($this->lines); +} + +function valid() +{ + return $this->valid; +} + +function current() +{ + return $this->valid[1]; +} + +function key() +{ + return $this->valid[0]; +} + + /** * Apply an expression to every line */ @@ -117,6 +167,9 @@ function lines() return $this->lines; } +/** + * Pipe our contents through a shell command + */ function pipe($cmd) { $descriptors = array(0 => array("pipe", "r"), 1 => array("pipe", "w")); @@ -133,53 +186,12 @@ function pipe($cmd) return $this; } -# -# Magic functions -# -function __call($name, $params) -{ - array_unshift($params, ""); - $func = ($t = it::match('(\w+)__(\w+)', $name)) ? array($t[0], $t[1]) : (method_exists($this, $name) ? array($this, $name) : $name); - - foreach($this->lines as $i => $params[0]) - $this->lines[$i] = call_user_func_array($func, $params); - - return $this; -} - -function __toString() -{ - return is_array($this->lines) ? join("\n", $this->lines) . "\n" : ""; -} - - -# -# Internal: Implement iterator -# -function rewind() -{ - reset($this->lines); - $this->next(); -} - -function next() -{ - $this->valid = each($this->lines); -} - -function valid() -{ - return $this->valid; -} - -function current() -{ - return $this->valid[1]; -} - -function key() +/** + * Save our contents in a file + */ +function save($fn) { - return $this->valid[0]; + file_put_contents($fn, $this->lines ? join("\n", $this->lines) . "\n" : ""); } } |