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