summaryrefslogtreecommitdiff
path: root/ext/spl/examples/dba_dump.php
blob: d32f1761c9398568018510122f8b17f1ed54b0bd (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
<?php

/* dba dump utility
 *
 * Usage php dba_dump <file> <handler>
 *
 * Note: configure with --enable-dba 
 */

class dba_reader implements spl::iterator {

	public $db = NULL;

	function __construct($file, $handler) {
		$this->db = dba_open($file, 'r', $handler);
	}
	
	function new_iterator() {
		return new dba_iter($this);
	}
	
	function __destruct() {
		if ($this->db) {
			dba_close($this->db);
		}
	}
}

class dba_iter implements spl::sequence_assoc {

	private $obj;
	private $key = NULL;
	private $val = NULL;

	function __construct($obj) {
		$this->obj = $obj;
	}

	function reset() {
		if ($this->obj->db) {
			$this->key = dba_firstkey($this->obj->db);
		}
	}

	function elem() {
		return $this->val;
	}

	function next() {
		$this->key = dba_nextkey($this->obj->db);
	}

	function more() {
		if ($this->obj->db && $this->key !== false) {
			$this->val = dba_fetch($this->key, $this->obj->db);
			return true;
		} else {
			return false;
		}
	}

	function key() {
		return $this->key;
	}
}

$db = new dba_reader($argv[1], $argv[2]);
foreach($db as $key => $val) {
	echo "'$key' => '$val'\n";
}

?>