summaryrefslogtreecommitdiff
path: root/it_pipe.class
blob: b3c9d3938078aa0164a64629e741a70ee6a0ba10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php

class it_pipe implements Iterator
{

/**
 * Creates a pipe object from input. Named arguments:
 * $p['fn'] filename to read lines from
 * $p['cmd'] cmd to read lines from
 * $p['data'] data to read line from. either array of lines without newslines or string
 */
function __construct($p = array())
{
	if ($p['data'])
		$this->lines = $p['data'];
	else
		$this->lines = isset($p['fn']) ? file($p['fn'], FILE_IGNORE_NEW_LINES) : explode("\n", rtrim(it::exec($p['cmd'], $p['args'])));
}

/**
 * Apply an expression to every line
 */
function map($expr)
{
	$this->lines = it::map($expr, $this->lines);

	return $this;
}

/**
 * Convert pipe from utf8 to iso-latin
 */
function latin()
{
	return $this->map('utf8_decode($v)');
}

/**
 * Convert pipe from iso-latin to uft8
 */
function utf8()
{
	return $this->map('utf8_encode($v)');
}

/**
 * Return contents of pipe as key->val pair (key must be tab separated)
 */
function keyval()
{
	foreach ($this->lines as $line)
	{
		list($key, $val) = explode("\t", $line, 2);
		$result[$key] = $val;
	}

	return $result;
}

/**
 * Swap first two columns
 */
function swap()
{
	foreach ($this->lines as $idx => $line)
	{
		list($col1, $col2) = explode("\t", $line, 2);
		$this->lines[$idx] = "$col2\t$col1";
	}

	return $this;
}

function ED()
{
	ED($this->lines);

	return $this;
}

function cols($collist)
{
	$keys = explode(",", $collist);
	foreach ($this->lines as $idx => $line)
		$this->lines[$idx] = (object)array_combine($keys, explode("\t", $line));

	return $this;
}

/**
 * Return contents of pipe as associative records
 */
function csv()
{
	$schema = explode("\t", it::replace(array('"' => ''), trim(array_shift($this->lines), "#\n")));

	$oldlocale = setlocale(LC_CTYPE, 'de_CH.iso-8859-1');

	foreach ($this->lines as $line)
		$records[] = (object)array_combine($schema, str_getcsv($line, "\t", '"')); # could do a function_exists('str_getcsv') here...

	setlocale(LC_CTYPE, $oldlocale);

	return $records;
}

function pipe($cmd)
{
	$descriptors = array(0 => array("pipe", "r"), 1 => array("pipe", "w"));
	$process = proc_open($cmd, $descriptors, $pipes);

	fwrite($pipes[0], join("\n", $this->lines) . ($this->lines ? "\n" :""));
	fclose($pipes[0]);

	$this->lines = explode("\n", rtrim(stream_get_contents($pipes[1])));
	$this->key = 0;
	fclose($pipes[1]);
	proc_close($process);

	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()
{
	return $this->valid[0];
}

}