diff options
| author | Urban Müller | 2011-04-08 21:50:53 +0000 | 
|---|---|---|
| committer | Urban Müller | 2011-04-08 21:50:53 +0000 | 
| commit | 90867f6dde5b3a44b66af5890aef980d99a65bf1 (patch) | |
| tree | 299f16eda4cea5dfc74fb92ccdf0584090503219 | |
| parent | 231c1b12e198753decfba9e1679a1d1a70f233ce (diff) | |
| download | itools-90867f6dde5b3a44b66af5890aef980d99a65bf1.tar.gz itools-90867f6dde5b3a44b66af5890aef980d99a65bf1.tar.bz2 itools-90867f6dde5b3a44b66af5890aef980d99a65bf1.zip | |
save function
| -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" : "");  }  } |