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) && (strtolower(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)
47 $code .= str_repeat($tabstr, $level+
2) . "index : $index" . $eol;
48 $code .= var_encode_text($child, $level+
2, $no_children, $tabstr, $eol);
52 } elseif (is_array($var)) {
55 foreach ($var as $key => $value) {
56 $vcode .= str_repeat($tabstr, $level +
1);
57 $vcode .= "'$key'=>".var_encode_text($value, $level +
1, $no_children, $tabstr, $eol);
60 if (substr($vcode, -strlen($arraysep)) == $arraysep)
61 $vcode = substr($vcode, 0, - strlen($arraysep));
63 $code = "array(".($vcode ?
"$eol$vcode$eol".str_repeat($tabstr, $level) : ""). ")";
65 } elseif (is_scalar($var)) {
72 function var_encode_html($var, $level = 0, $no_children = FALSE
)
74 return var_encode_text($var, $level, $no_children, ' ', $eol = "<br/>\n");
77 function var_encode_bin($var, $no_children = FALSE
)
79 if (is_object($var) && (strtolower(get_class($var)) == 'diogenes_tree_node')) {
80 $code = pack('C', VAR_TYPE_NODE
);
81 $code .= var_encode_bin($var->name
);
82 $code .= var_encode_bin($var->data
);
84 $code .= var_encode_bin(array());
86 $code .= var_encode_bin($var->children
);
87 } elseif (is_array($var)) {
88 $code = pack('C', VAR_TYPE_ARRAY
);
90 foreach ($var as $key => $value) {
91 $contents .= var_encode_bin($key);
92 $contents .= var_encode_bin($value);
94 $code .= pack('L', strlen($contents));
96 } elseif (is_scalar($var)) {
98 $code = pack('C', VAR_TYPE_STRING
);
99 $code .= pack('L', strlen($str));
102 $code = pack('C', VAR_TYPE_NULL
);
107 function var_decode_bin(&$bin)
109 list(,$type) = unpack('C', $bin);
110 $bin = substr($bin, 1);
111 if ($type == VAR_TYPE_NODE
)
113 $name = var_decode_bin($bin);
114 $data = var_decode_bin($bin);
115 $children = var_decode_bin($bin);
116 $var = new Diogenes_Tree_Node($data, $name, $children);
117 } elseif ($type == VAR_TYPE_ARRAY
)
119 list(,$length) = unpack('L', $bin);
120 $bin = substr($bin, 4);
121 $contents = substr($bin, 0, $length);
122 $bin = substr($bin, $length);
125 while(strlen($contents)) {
126 $key = var_decode_bin($contents);
127 $var[$key] = var_decode_bin($contents);
129 } elseif ($type == VAR_TYPE_STRING
) {
130 list(,$length) = unpack('L', $bin);
131 $bin = substr($bin, 4);
132 $var = substr($bin, 0, $length);
133 $bin = substr($bin, $length);
134 } elseif ($type == VAR_TYPE_NULL
) {
137 trigger_error("unknown type in var_decode_bin : ". $type);
142 /** This class describes Diogenes' plugins.
144 class Diogenes_Tree_Node
146 /** Data for the current node */
149 /** Name for the current node */
152 /** An array of child nodes */
153 var $children = array();
155 /** The parent of this node */
158 /** Construct a new tree node.
160 function Diogenes_Tree_Node($data, $name = '', $children=array())
164 $this->children
= $children;
167 /** Return the specified child of this node.
169 function getChild($index)
171 return $this->children
[$index];
174 /** Assign the specified child of this node.
176 function setChild($index, $node)
178 $this->children
[$index] = $node;
181 /** Read a dump of a node and its children.
183 function readFile($filename)
185 $bin = file_get_contents($filename);
186 $node = var_decode_bin($bin);
187 if (!is_object($node) ||
strtolower(get_class($node)) != 'diogenes_tree_node')
189 trigger_error('readFile : not a Diogenes_Tree_Node', E_USER_ERROR
);
194 /** Write a dump of this node and its children.
196 function writeFile($filename, $mode = NODE_DUMP_IO
)
198 if (!$fp = fopen($filename, "w")) {
199 trigger_error("writeFile : failed to open '$cachefile' for writing", E_USER_ERROR
);
202 if ($mode & NODE_DUMP_TEXT
) {
203 $out = var_encode_text($this, $level, ($mode & NODE_DUMP_NOCHILDREN
));
205 $out = var_encode_bin($this, ($mode & NODE_DUMP_NOCHILDREN
));