3 * Copyright (C) 2003-2004 Polytechnique.org
4 * http://opensource.polytechnique.org/
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 require_once 'diogenes/diogenes.misc.inc.php';
23 define('NODE_DUMP_BINARY', 0);
24 define('NODE_DUMP_TEXT', 1);
25 define('NODE_DUMP_NOCHILDREN', 2);
26 define('VAR_TYPE_NULL', 0);
27 define('VAR_TYPE_INT', 1);
28 define('VAR_TYPE_STRING', 2);
29 define('VAR_TYPE_ARRAY', 3);
30 define('VAR_TYPE_NODE', 4);
32 function var_encode_text($var, $level = 0, $no_children = FALSE
, $tabstr = ' ', $eol = "\n")
34 if (is_object($var) && (get_class($var) == 'Diogenes_Tree_Node')) {
36 $code = str_repeat($tabstr, $level) . "node : {$var->name}" . $eol;
38 // encode this node's data
39 $code .= str_repeat($tabstr, $level+
1) . "* data : " . var_encode_text($var->data
, $level +
2, $no_children, $tabstr, $eol). $eol;
41 // encode this node's children
44 $code .= str_repeat($tabstr, $level+
1) . "* children" . $eol;
45 foreach ($var->children
as $index => $child)
46 $code .= str_repeat($tabstr, $level+
2) . "index : $index" . $eol;
47 $code .= var_encode_text($child, $level+
2, $no_children, $tabstr, $eol);
50 } elseif (is_array($var)) {
53 foreach ($var as $key => $value) {
54 $vcode .= str_repeat($tabstr, $level +
1);
55 $vcode .= "'$key'=>".var_encode_text($value, $level +
1, $no_children, $tabstr, $eol);
58 if (substr($vcode, -strlen($arraysep)) == $arraysep)
59 $vcode = substr($vcode, 0, - strlen($arraysep));
61 $code = "array(".($vcode ?
"$eol$vcode$eol".str_repeat($tabstr, $level) : ""). ")";
63 } elseif (is_scalar($var)) {
70 function var_encode_html($var, $level = 0, $no_children = FALSE
)
72 return var_encode_text($var, $level, $no_children, ' ', $eol = "<br/>\n");
75 function var_encode_bin($var, $no_children = FALSE
)
77 if (is_object($var) && (get_class($var) == 'Diogenes_Tree_Node')) {
78 $code = pack('C', VAR_TYPE_NODE
);
79 $code .= var_encode_bin($var->name
);
80 $code .= var_encode_bin($var->data
);
82 $code .= var_encode_bin(array());
84 $code .= var_encode_bin($var->children
);
85 } elseif (is_array($var)) {
86 $code = pack('C', VAR_TYPE_ARRAY
);
88 foreach ($var as $key => $value) {
89 $contents .= var_encode_bin($key);
90 $contents .= var_encode_bin($value);
92 $code .= pack('L', strlen($contents));
94 } elseif (is_scalar($var)) {
96 $code = pack('C', VAR_TYPE_STRING
);
97 $code .= pack('L', strlen($str));
100 $code = pack('C', VAR_TYPE_NULL
);
105 function var_decode_bin(&$bin)
107 list(,$type) = unpack('C', $bin);
108 $bin = substr($bin, 1);
109 if ($type == VAR_TYPE_NODE
)
111 $name = var_decode_bin($bin);
112 $data = var_decode_bin($bin);
113 $children = var_decode_bin($bin);
114 $var = new Diogenes_Tree_Node($data, $name, $children);
115 } elseif ($type == VAR_TYPE_ARRAY
)
117 list(,$length) = unpack('L', $bin);
118 $bin = substr($bin, 4);
119 $contents = substr($bin, 0, $length);
120 $bin = substr($bin, $length);
123 while(strlen($contents)) {
124 $key = var_decode_bin($contents);
125 $var[$key] = var_decode_bin($contents);
127 } elseif ($type == VAR_TYPE_STRING
) {
128 list(,$length) = unpack('L', $bin);
129 $bin = substr($bin, 4);
130 $var = substr($bin, 0, $length);
131 $bin = substr($bin, $length);
132 } elseif ($type == VAR_TYPE_NULL
) {
135 trigger_error("unknown type in var_decode_bin : ". $type);
140 /** This class describes Diogenes' plugins.
142 class Diogenes_Tree_Node
144 /** Data for the current node */
147 /** Name for the current node */
150 /** An array of child nodes */
151 var $children = array();
153 /** The parent of this node */
156 /** Construct a new tree node.
158 function Diogenes_Tree_Node($data, $name = '', $children=array())
162 $this->children
= $children;
165 /** Return the specified child of this node.
167 function getChild($index)
169 return $this->children
[$index];
172 /** Assign the specified child of this node.
174 function setChild($index, $node)
176 $this->children
[$index] = $node;
179 /** Read a dump of a node and its children.
181 function readFile($filename)
183 $bin = file_get_contents($filename);
184 $node = var_decode_bin($bin);
185 if (!is_object($node) ||
get_class($node) != 'Diogenes_Tree_Node')
187 trigger_error('readFile : not a Diogenes_Tree_Node', E_USER_ERROR
);
192 /** Write a dump of this node and its children.
194 function writeFile($filename, $mode = NODE_DUMP_IO
)
196 if (!$fp = fopen($filename, "w")) {
197 trigger_error("writeFile : failed to open '$cachefile' for writing", E_USER_ERROR
);
200 if ($mode & NODE_DUMP_TEXT
) {
201 $out = var_encode_text($this, $level, ($mode & NODE_DUMP_NOCHILDREN
));
203 $out = var_encode_bin($this, ($mode & NODE_DUMP_NOCHILDREN
));