a76760a387517a5b7e59b06666eb628ce1d6ecb5
[PHP-P10.git] / BotLoader / saxdb.class.php
1 <?php
2 /******************************* PHP-P10 v2 *****************************
3  * Copyright (C) 2011  Philipp Kreil (pk910)                            *
4  *                                                                      *
5  * This program is free software: you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation, either version 3 of the License, or    *
8  * (at your option) any later version.                                  *
9  *                                                                      * 
10  * This program is distributed in the hope that it will be useful,      *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
13  * GNU General Public License for more details.                         *
14  *                                                                      *
15  * You should have received a copy of the GNU General Public License    *
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17  *                                                                      *
18  ************************************************************************
19  * 
20  *  BotLoader/saxdb.class.php
21  *
22  * Simple PHP P10 database.
23  *
24  */
25
26 class saxdb {
27         const PARSER_ISSTRING  = 0x0001;
28         const PARSER_ISESCAPED = 0x0002;
29         const PARSER_ISBLOCK   = 0x0004;
30         const PARSER_EXPECT_VALUE = 0x0008;
31         const PARSER_STRING_LIST  = 0x0010;
32         private $database = array();
33         
34         public function loadDB($name) {
35                 if(file_exists($name)) {
36                         $fp = fopen($name, "r");
37                         $db = fread($fp, filesize($name));
38                         fclose($fp);
39                         $this->database = $this->parseDB($db);
40                 }
41         }
42         
43         public function writeDB($name) {
44                 $fp = fopen($name, "w");
45                 $db = $this->serializeDB($this->database);
46                 fwrite($fp, $db);
47                 fclose($fp);
48         }
49         
50         public function getSection($name) {
51                 if(array_key_exists($name, $this->database)) {
52                         return $this->database[$name];
53                 } else {
54                         return array();
55                 }
56         }
57         
58         public function setSection($name, $value) {
59                 $this->database[$name] = $value;
60         }
61         
62         private function parseDB($db) {
63                 $parserflags = 0;
64                 $openblocks = 0;
65                 $buffer = "";
66                 $cache = array();
67                 $output = array();
68                 for($i = 0; $i < strlen($db); $i++) {
69                         if(($parserflags & self::PARSER_ISBLOCK) && $db[$i] != "{" && $db[$i] != "}") {
70                                 $buffer .= $db[$i];
71                                 continue;
72                         }
73                         if(($parserflags & self::PARSER_ISSTRING) && $db[$i] != "\"") {
74                                 $buffer .= $db[$i];
75                                 continue;
76                         }
77                         if($parserflags & self::PARSER_ISESCAPED) {
78                                 $buffer .= $db[$i];
79                                 $parserflags &= ~self::PARSER_ISESCAPED;
80                                 continue;
81                         }
82                         switch($db[$i]) {
83                                 case "\\":
84                                         $parserflags |= self::PARSER_ISESCAPED;
85                                         break;
86                                 case "\"":
87                                         if($parserflags & self::PARSER_ISSTRING) {
88                                                 $parserflags &= ~self::PARSER_ISSTRING;
89                                                 if($parserflags & self::PARSER_STRING_LIST) {
90                                                         $cache['list'][] = $buffer;
91                                                 } else if($parserflags & self::PARSER_EXPECT_VALUE) {
92                                                         //we've got a full entry
93                                                         $output[$cache['name']] = $buffer;
94                                                         $parserflags &= ~self::PARSER_EXPECT_VALUE;
95                                                 } else {
96                                                         //we've only got the name of the next entry
97                                                         $cache['name'] = $buffer;
98                                                         $parserflags |= self::PARSER_EXPECT_VALUE;
99                                                 }
100                                         } else {
101                                                 $buffer = ""; //clear the buffer
102                                                 $parserflags |= self::PARSER_ISSTRING;
103                                         }
104                                         break;
105                                 case "{":
106                                         //block (it must be a value)
107                                         if($parserflags & self::PARSER_ISBLOCK) {
108                                                 $openblocks++;
109                                                 $buffer .= $db[$i];
110                                         } else {
111                                                 $parserflags |= self::PARSER_ISBLOCK;
112                                                 $buffer = "";
113                                         }
114                                         break;
115                                 case "}":
116                                         if($parserflags & self::PARSER_ISBLOCK) {
117                                                 $openblocks--;
118                                                 if($openblocks == -1) {
119                                                         $parserflags &= ~self::PARSER_ISBLOCK;
120                                                         if($parserflags & self::PARSER_EXPECT_VALUE) {
121                                                                 $output[$cache['name']] = $this->parseDB($buffer);
122                                                                 $parserflags &= ~self::PARSER_EXPECT_VALUE;
123                                                         }
124                                                 } else {
125                                                         $buffer .= $db[$i];
126                                                 }
127                                         }
128                                         break;
129                                 case "(":
130                                         if(!($parserflags & self::PARSER_STRING_LIST)) {
131                                                 $cache['list'] = array();
132                                                 $parserflags |= self::PARSER_STRING_LIST;
133                                         }
134                                         break;
135                                 case ")":
136                                         if(($parserflags & self::PARSER_STRING_LIST)) {
137                                                 $parserflags &= ~self::PARSER_STRING_LIST;
138                                                 if($parserflags & self::PARSER_EXPECT_VALUE) {
139                                                         $output[$cache['name']] = $cache['list'];
140                                                         $parserflags &= ~self::PARSER_EXPECT_VALUE;
141                                                 }
142                                         }
143                                         break;
144                                 default:
145                                         $buffer .= $db[$i];
146                                         break;
147                         }
148                 }
149                 return $output;
150         }
151         
152         private function serializeDB($db) {
153                 $dbstring = "";
154                 foreach($db as $name => $value) {
155                         if(!is_array($value) && !is_string($value) && !is_numeric($value)) {
156                                 echo"invalid type ".gettype($value)."\n";
157                                 continue;
158                         }
159                         $dbstring .= "\"".str_replace("\"","\\\"", $name)."\"";
160                         if(is_array($value)) {
161                                 $dbstring .= "{";
162                                 $dbstring .= $this->serializeDB($value);
163                                 $dbstring .= "}";
164                         } else {
165                                 $dbstring .= "\"".str_replace("\"","\\\"", $value)."\"";
166                         }
167                 }
168                 return $dbstring;
169         }
170         
171 }
172
173 ?>