From 8305ec40a946ec4001af9b0721c2d960387dd81e Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Thu, 11 Dec 2008 19:40:45 +0000 Subject: handle stdin/stdout --- it.class | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/it.class b/it.class index 28a473c..7e6e541 100644 --- a/it.class +++ b/it.class @@ -683,28 +683,29 @@ function mail($p) /** * Reads a file and returns it as string or in one of several formats. Two params: filename and flags - * @param $filename name of file to read + * @param $filename name of file to read or - for stdin * @param $p['keyval'] each line of the file is one tab-separated key/value pair, return assoc array */ function file_get($filename, $p = array()) { + $data = file_get_contents($filename == "-" ? "php://stdin" : $filename); if ($p['keyval']) { - foreach (explode("\n", rtrim(file_get_contents($filename), "\n")) as $line) + foreach (explode("\n", rtrim($data, "\n")) as $line) { $arr = explode("\t", $line, 2); $result[$arr[0]] = $arr[1]; } } else - $result = file_get_contents($filename); + $result = $data; return $result; } /** * Write data to a file with several serialization modes - * @param $filename name of file to write to + * @param $filename name of file to write or - for stdout * @param $data data to write * @param $p['keyval'] $data must be an assoc array and is written as tab-separated lines */ @@ -713,7 +714,7 @@ function file_put($filename, $data, $p = array()) if ($p['keyval']) $data = join("", it::map('"$k\t$v\n"', $data)); - return file_put_contents($filename, $data); + return file_put_contents($filename == "-" ? "php://stdout" : $filename, $data); } } -- cgit v1.2.3