Commit | Line | Data |
---|---|---|
55d2e17d JL |
1 | <?php |
2 | /* | |
3 | * Copyright (C) 2003-2004 Polytechnique.org | |
4 | * http://opensource.polytechnique.org/ | |
5 | * | |
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. | |
10 | * | |
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. | |
15 | * | |
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 | |
19 | */ | |
20 | ||
56aefc1e JL |
21 | require_once 'diogenes/diogenes.misc.inc.php'; |
22 | ||
55d2e17d JL |
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); | |
31 | ||
32 | function var_encode_text($var, $level = 0, $no_children = FALSE, $tabstr = ' ', $eol = "\n") | |
33 | { | |
7aff44b2 | 34 | if (is_object($var) && (strtolower(get_class($var)) == 'diogenes_tree_node')) { |
55d2e17d JL |
35 | // node name |
36 | $code = str_repeat($tabstr, $level) . "node : {$var->name}" . $eol; | |
37 | ||
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; | |
40 | ||
41 | // encode this node's children | |
42 | if (!$no_children) | |
43 | { | |
44 | $code .= str_repeat($tabstr, $level+1) . "* children" . $eol; | |
45 | foreach ($var->children as $index => $child) | |
279d696b | 46 | { |
55d2e17d JL |
47 | $code .= str_repeat($tabstr, $level+2) . "index : $index" . $eol; |
48 | $code .= var_encode_text($child, $level+2, $no_children, $tabstr, $eol); | |
279d696b | 49 | } |
55d2e17d JL |
50 | } |
51 | return $code; | |
52 | } elseif (is_array($var)) { | |
53 | $arraysep = ",$eol"; | |
35574d7c | 54 | $vcode = ''; |
55d2e17d | 55 | foreach ($var as $key => $value) { |
35574d7c JL |
56 | $vcode .= str_repeat($tabstr, $level + 1); |
57 | $vcode .= "'$key'=>".var_encode_text($value, $level + 1, $no_children, $tabstr, $eol); | |
58 | $vcode .= $arraysep; | |
55d2e17d | 59 | } |
35574d7c JL |
60 | if (substr($vcode, -strlen($arraysep)) == $arraysep) |
61 | $vcode = substr($vcode, 0, - strlen($arraysep)); | |
62 | ||
63 | $code = "array(".($vcode ? "$eol$vcode$eol".str_repeat($tabstr, $level) : ""). ")"; | |
55d2e17d | 64 | return $code; |
56aefc1e | 65 | } elseif (is_scalar($var)) { |
55d2e17d | 66 | return "'".$var."'"; |
55d2e17d JL |
67 | } else { |
68 | return 'NULL'; | |
69 | } | |
70 | } | |
71 | ||
56aefc1e JL |
72 | function var_encode_html($var, $level = 0, $no_children = FALSE) |
73 | { | |
74 | return var_encode_text($var, $level, $no_children, ' ', $eol = "<br/>\n"); | |
75 | } | |
76 | ||
55d2e17d JL |
77 | function var_encode_bin($var, $no_children = FALSE) |
78 | { | |
7aff44b2 | 79 | if (is_object($var) && (strtolower(get_class($var)) == 'diogenes_tree_node')) { |
55d2e17d JL |
80 | $code = pack('C', VAR_TYPE_NODE); |
81 | $code .= var_encode_bin($var->name); | |
82 | $code .= var_encode_bin($var->data); | |
83 | if ($no_children) | |
84 | $code .= var_encode_bin(array()); | |
85 | else | |
86 | $code .= var_encode_bin($var->children); | |
87 | } elseif (is_array($var)) { | |
88 | $code = pack('C', VAR_TYPE_ARRAY); | |
89 | $contents = ''; | |
90 | foreach ($var as $key => $value) { | |
91 | $contents .= var_encode_bin($key); | |
92 | $contents .= var_encode_bin($value); | |
93 | } | |
94 | $code .= pack('L', strlen($contents)); | |
95 | $code .= $contents; | |
96 | } elseif (is_scalar($var)) { | |
97 | $str = "$var"; | |
98 | $code = pack('C', VAR_TYPE_STRING); | |
99 | $code .= pack('L', strlen($str)); | |
100 | $code .= $str; | |
101 | } else { | |
102 | $code = pack('C', VAR_TYPE_NULL); | |
103 | } | |
104 | return $code; | |
105 | } | |
106 | ||
107 | function var_decode_bin(&$bin) | |
108 | { | |
109 | list(,$type) = unpack('C', $bin); | |
110 | $bin = substr($bin, 1); | |
111 | if ($type == VAR_TYPE_NODE) | |
112 | { | |
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) | |
118 | { | |
119 | list(,$length) = unpack('L', $bin); | |
120 | $bin = substr($bin, 4); | |
121 | $contents = substr($bin, 0, $length); | |
122 | $bin = substr($bin, $length); | |
123 | ||
124 | $var = array(); | |
125 | while(strlen($contents)) { | |
126 | $key = var_decode_bin($contents); | |
127 | $var[$key] = var_decode_bin($contents); | |
128 | } | |
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) { | |
135 | $var = NULL; | |
136 | } else { | |
137 | trigger_error("unknown type in var_decode_bin : ". $type); | |
138 | } | |
139 | return $var; | |
140 | } | |
141 | ||
142 | /** This class describes Diogenes' plugins. | |
143 | */ | |
144 | class Diogenes_Tree_Node | |
145 | { | |
146 | /** Data for the current node */ | |
147 | var $data; | |
148 | ||
149 | /** Name for the current node */ | |
150 | var $name; | |
151 | ||
152 | /** An array of child nodes */ | |
153 | var $children = array(); | |
154 | ||
155 | /** The parent of this node */ | |
156 | // var $parent; | |
157 | ||
158 | /** Construct a new tree node. | |
159 | */ | |
160 | function Diogenes_Tree_Node($data, $name = '', $children=array()) | |
161 | { | |
162 | $this->data = $data; | |
163 | $this->name = $name; | |
164 | $this->children = $children; | |
165 | } | |
166 | ||
55d2e17d JL |
167 | /** Return the specified child of this node. |
168 | */ | |
169 | function getChild($index) | |
170 | { | |
171 | return $this->children[$index]; | |
172 | } | |
173 | ||
174 | /** Assign the specified child of this node. | |
175 | */ | |
176 | function setChild($index, $node) | |
177 | { | |
178 | $this->children[$index] = $node; | |
179 | } | |
180 | ||
181 | /** Read a dump of a node and its children. | |
182 | */ | |
183 | function readFile($filename) | |
184 | { | |
185 | $bin = file_get_contents($filename); | |
186 | $node = var_decode_bin($bin); | |
7aff44b2 | 187 | if (!is_object($node) || strtolower(get_class($node)) != 'diogenes_tree_node') |
55d2e17d JL |
188 | { |
189 | trigger_error('readFile : not a Diogenes_Tree_Node', E_USER_ERROR); | |
190 | } | |
191 | return $node; | |
192 | } | |
193 | ||
194 | /** Write a dump of this node and its children. | |
195 | */ | |
196 | function writeFile($filename, $mode = NODE_DUMP_IO) | |
197 | { | |
198 | if (!$fp = fopen($filename, "w")) { | |
199 | trigger_error("writeFile : failed to open '$cachefile' for writing", E_USER_ERROR); | |
200 | } | |
201 | ||
202 | if ($mode & NODE_DUMP_TEXT) { | |
203 | $out = var_encode_text($this, $level, ($mode & NODE_DUMP_NOCHILDREN)); | |
204 | } else { | |
205 | $out = var_encode_bin($this, ($mode & NODE_DUMP_NOCHILDREN)); | |
206 | } | |
207 | fputs($fp, $out); | |
208 | ||
209 | fclose($fp); | |
210 | } | |
211 | ||
212 | } | |
213 | ||
214 | ?> |