NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/Chameleon/trunk/Chameleon/MapImageSharedResource/expression.php

Revision 13 (checked in by jcleary, 17 years ago)

Latest Chameleon code checkout from previous repository

Line 
1 <?php
2 /**
3  *
4  * This function evaluate a simple expression and return true
5  * false. If and error is detected in the expression the function
6  * return -1.
7  *
8  * Expression can contain "==", "!=", "<=", ">=", "<", ">", "&&",
9  * "||", "(" and ")". All string should be beetween double quotes.
10  *
11  * Example:
12  *
13  *          $szEmployeeName = "Sacha Fournier";
14  *          $szExp = "(\"$szEmployeeName\" != Joe && ".
15  *                   "\"$szEmployeeName\" != \"Dirt\" || ".
16  *                   "($szEmployeeName == \"\" && 1==1))";
17  *          $bRet = evaluateExpression("($szEmployeeName != )");
18  */
19
20 /**
21  * Main function to call
22  */
23 function evaluateExpression($szExpression)
24 {
25     // Initialise empty array that will contain all string vars.
26     $aszValues = array();
27
28     $szExpression = trim($szExpression);
29     if (substr($szExpression, 0,1) != "(" && substr($szExpression, -1) != ")")
30         $szExpression = "(".$szExpression.")";
31
32     // Remove strings beetween double quotes
33     // and store them in aszValues.
34     $nStart = $nEnd = false;
35     $i=0;
36     do
37     {
38         $nStart = strpos($szExpression, "\"");
39         if ($nStart !== false)
40         {
41             $nEnd = strpos($szExpression, "\"", $nStart+1);
42
43             if ($nEnd !== false)
44             {
45                 $aszValues["--str".$i."--"] = substr($szExpression, $nStart+1, $nEnd-$nStart-1);
46                 $szExpression = substr($szExpression, 0, $nStart)."--str$i--".substr($szExpression, $nEnd+1);
47                 $i++;
48             }
49         }
50
51     } while ($nStart !== false && $nEnd !== false);
52
53     $nRet = processExpression($szExpression, $aszValues);
54
55     return $nRet;
56 }
57
58 /**
59  * This function shouldn't be called by user.
60  */
61 function processExpression(&$szExpression, &$aszValues)
62 {
63     $nStrLen = strlen($szExpression);
64     $nPos = 0;
65     $nEnd = $nStrLen;
66     $nOpen = 0;
67     $bFind = false;
68
69     // Process all sub expressions beetween () in
70     // correct order.
71     $nStart = strpos($szExpression, "(", $nPos);
72     $nEnd = strpos($szExpression, ")", $nPos);
73    
74     if ($nStart === false && $nEnd !== false)
75         return -1;
76
77     if ($nStart === false && $nEnd === false)
78     {
79         $bRet = simpleEval($szExpression, $aszValues);
80         return $bRet;
81     }
82     else
83     while (!$bFind)
84     {
85         $nTmpPos1 = strpos($szExpression, "(", $nPos);
86         $nTmpPos2 = strpos($szExpression, ")", $nPos);
87
88         if ($nTmpPos1 !== false && $nTmpPos2 !== false && $nTmpPos1 < $nTmpPos2)
89         {
90             $nOpen++;
91             $nPos = $nTmpPos1 + 1;
92         }
93
94         if ($nTmpPos1 !== false && $nTmpPos2 !== false && $nTmpPos1 > $nTmpPos2)
95         {
96             $nOpen--;
97             $nPos = $nTmpPos2 + 1;
98         }
99
100         if ($nTmpPos1 === false && $nTmpPos2 !== false)
101         {
102             $nOpen--;
103             $nPos = $nTmpPos2 + 1;
104         }
105
106         if ($nTmpPos2 === false && $nTmpPos1 !== false)
107         {
108             $nOpen++;
109             $nPos = $nTmpPos1 + 1;
110         }
111
112         if ($nOpen == 0)
113         {
114            $nEnd = $nPos;
115
116            $szExp = substr($szExpression, $nStart+1, $nEnd-$nStart-2);
117            $szExpression = str_replace("(".$szExp.")", processExpression($szExp, $aszValues), $szExpression);
118
119            $nPos = 0;
120            $nStrLen = strlen($szExpression);
121
122            $nStart = strpos($szExpression, "(", 0);
123            $nEnd = strpos($szExpression, ")", 0);
124
125            if ($nStart === false && $nEnd !== false)
126                 return -1;
127
128            if ($nStart === false && $nEnd === false)
129                 return simpleEval($szExpression, $aszValues);
130         }
131
132         if ($nPos >= $nStrLen)
133         {
134             $bFind = true;
135         }
136     }
137 }
138
139 function simpleEval(&$szExpression, &$aszValues)
140 {
141 //    if (trim($szExpression) == "")
142 //        return false;
143
144     $nRet = 0;
145
146     if (strpos($szExpression, "||") !== false)
147     {
148         $aszExpression = explode("||", $szExpression);
149         $nCnt = count($aszExpression);
150         for($i=0;$i<$nCnt;$i++)
151         {
152             $nRet = (simpleEval(trim($aszExpression[$i]), $aszValues) || $nRet) ? 1 : 0;
153             $aszExpression[$i] = $nRet;
154         }
155
156         $szExpression = implode("||", $aszExpression);
157     }
158
159     if (strpos($szExpression, "&&") !== false)
160     {
161         $nRet = 1;
162
163         $aszExpression = explode("&&", $szExpression);
164         $nCnt = count($aszExpression);
165         for($i=0;$i<$nCnt;$i++)
166         {
167             $nRet = (simpleEval(trim($aszExpression[$i]), $aszValues) && $nRet) ? 1 : 0;
168             $szExpression = implode("&&", $aszExpression);
169         }
170     }
171
172     if (strpos($szExpression, "&&") === false && strpos($szExpression, "||") === false)
173     {
174         $nRet = myEval($szExpression, $aszValues);
175
176         $szExpression = $nRet;
177     }
178
179     return $nRet;
180 }
181
182
183 function myEval($szExpression, &$aszValues)
184 {
185     $szExpression = trim($szExpression);
186
187     if ($szExpression == "1" || $szExpression == "0")
188         return $szExpression;
189
190     if (isset($aszValues[$szExpression]) && strlen($aszValues[$szExpression]) > 0)
191         return 1;
192
193     if (strpos($szExpression, "==") !== false)
194     {
195         $aszOper = explode("==", $szExpression);
196
197         if (count($aszOper) != 2)
198             return -1;
199
200         if (isset($aszValues[trim($aszOper[0])]))
201             $szVal1 = $aszValues[trim($aszOper[0])];
202         else
203             $szVal1 = trim($aszOper[0]);
204         if (isset($aszValues[trim($aszOper[1])]))
205             $szVal2 = $aszValues[trim($aszOper[1])];
206         else
207             $szVal2 = trim($aszOper[1]);
208
209         if ($szVal1 == $szVal2)
210             return 1;
211         else
212             return 0;
213     }
214     else
215     if (strpos($szExpression, "!="))
216     {
217         $aszOper = explode("!=", $szExpression);
218
219         if (count($aszOper) != 2)
220             return -1;
221
222         if (isset($aszValues[trim($aszOper[0])]))
223             $szVal1 = $aszValues[trim($aszOper[0])];
224         else
225             $szVal1 = trim($aszOper[0]);
226         if (isset($aszValues[trim($aszOper[1])]))
227             $szVal2 = $aszValues[trim($aszOper[1])];
228         else
229             $szVal2 = trim($aszOper[1]);
230
231         if ($szVal1 != $szVal2)
232             return 1;
233         else
234             return 0;
235     }
236     else
237     if (strpos($szExpression, ">="))
238     {
239         $aszOper = explode(">=", $szExpression);
240
241         if (count($aszOper) != 2)
242             return -1;
243
244         if (isset($aszValues[trim($aszOper[0])]))
245             $szVal1 = $aszValues[trim($aszOper[0])];
246         else
247             $szVal1 = trim($aszOper[0]);
248         if (isset($aszValues[trim($aszOper[1])]))
249             $szVal2 = $aszValues[trim($aszOper[1])];
250         else
251             $szVal2 = trim($aszOper[1]);
252
253         if ($szVal1 >= $szVal2)
254             return 1;
255         else
256             return 0;
257     }
258     else
259     if (strpos($szExpression, "<="))
260     {
261         $aszOper = explode("<=", $szExpression);
262
263         if (count($aszOper) != 2)
264             return -1;
265
266         if (isset($aszValues[trim($aszOper[0])]))
267             $szVal1 = $aszValues[trim($aszOper[0])];
268         else
269             $szVal1 = trim($aszOper[0]);
270         if (isset($aszValues[trim($aszOper[1])]))
271             $szVal2 = $aszValues[trim($aszOper[1])];
272         else
273             $szVal2 = trim($aszOper[1]);
274
275         if ($szVal1 <= $szVal2)
276             return 1;
277         else
278             return 0;
279     }
280     else
281     if (strpos($szExpression, ">"))
282     {
283         $aszOper = explode(">", $szExpression);
284
285         if (count($aszOper) != 2)
286             return -1;
287
288         if (isset($aszValues[trim($aszOper[0])]))
289             $szVal1 = $aszValues[trim($aszOper[0])];
290         else
291             $szVal1 = trim($aszOper[0]);
292         if (isset($aszValues[trim($aszOper[1])]))
293             $szVal2 = $aszValues[trim($aszOper[1])];
294         else
295             $szVal2 = trim($aszOper[1]);
296
297         if ($szVal1 > $szVal2)
298             return 1;
299         else
300             return 0;
301     }
302     else
303     if (strpos($szExpression, "<"))
304     {
305         $aszOper = explode("<", $szExpression);
306
307         if (count($aszOper) != 2)
308             return -1;
309
310         if (isset($aszValues[trim($aszOper[0])]))
311             $szVal1 = $aszValues[trim($aszOper[0])];
312         else
313             $szVal1 = trim($aszOper[0]);
314         if (isset($aszValues[trim($aszOper[1])]))
315             $szVal2 = $aszValues[trim($aszOper[1])];
316         else
317             $szVal2 = trim($aszOper[1]);
318
319         if ($szVal1 < $szVal2)
320             return 1;
321         else
322             return 0;
323     }
324     else
325     {
326         if(isset($aszValues[trim($szExpression)]) && strlen($aszValues[trim($szExpression)]) > 0)
327             return 1;
328
329         if(isset($aszValues[trim($szExpression)]) && strlen($aszValues[trim($szExpression)]) == 0)
330             return 0;
331
332         return 1;
333     }   
334     return 1;
335 }
336 /*
337 if (evaluateExpression("1||1&&1")) echo "true"; else echo "false";
338 if (evaluateExpression("1&&1&&1")) echo "true"; else echo "false";
339 if (evaluateExpression("1&&1||1")) echo "true"; else echo "false";
340 if (evaluateExpression("1||1||1")) echo "true"; else echo "false";
341
342 if (evaluateExpression("1||1&&0")) echo "true"; else echo "false";
343 if (evaluateExpression("1&&1&&0")) echo "true"; else echo "false";
344 if (evaluateExpression("1&&1||0")) echo "true"; else echo "false";
345 if (evaluateExpression("1||1||0")) echo "true"; else echo "false";
346
347 if (evaluateExpression("1||0&&1")) echo "true"; else echo "false";
348 if (evaluateExpression("1&&0&&1")) echo "true"; else echo "false";
349 if (evaluateExpression("1&&0||1")) echo "true"; else echo "false";
350 if (evaluateExpression("1||0||1")) echo "true"; else echo "false";
351
352 if (evaluateExpression("0||1&&1")) echo "true"; else echo "false";
353 if (evaluateExpression("0&&1&&1")) echo "true"; else echo "false";
354 if (evaluateExpression("0&&1||1")) echo "true"; else echo "false";
355 if (evaluateExpression("0||1||1")) echo "true"; else echo "false";
356
357 if (evaluateExpression("1||0&&0")) echo "true"; else echo "false";
358 if (evaluateExpression("1&&0&&0")) echo "true"; else echo "false";
359 if (evaluateExpression("1&&0||0")) echo "true"; else echo "false";
360 if (evaluateExpression("1||0||0")) echo "true"; else echo "false";
361
362 if (evaluateExpression("0||0&&1")) echo "true"; else echo "false";
363 if (evaluateExpression("0&&0&&1")) echo "true"; else echo "false";
364 if (evaluateExpression("0&&0||1")) echo "true"; else echo "false";
365 if (evaluateExpression("0||0||1")) echo "true"; else echo "false";
366
367 if (evaluateExpression("0||1&&0")) echo "true"; else echo "false";
368 if (evaluateExpression("0&&1&&0")) echo "true"; else echo "false";
369 if (evaluateExpression("0&&1||0")) echo "true"; else echo "false";
370 if (evaluateExpression("0||1||0")) echo "true"; else echo "false";
371
372 if (evaluateExpression("0||0&&0")) echo "true"; else echo "false";
373 if (evaluateExpression("0&&0&&0")) echo "true"; else echo "false";
374 if (evaluateExpression("0&&0||0")) echo "true"; else echo "false";
375 if (evaluateExpression("0||0||0")) echo "true"; else echo "false";
376
377 echo "\n";
378 */
379 ?>
Note: See TracBrowser for help on using the browser.