1 | <?php
|
---|
2 |
|
---|
3 | class BinaryReader
|
---|
4 | {
|
---|
5 | private $handle; /// Handle na otevreny dbc soubor
|
---|
6 |
|
---|
7 | /// Konstruktor
|
---|
8 | function __construct()
|
---|
9 | {
|
---|
10 | $this->handle = false;
|
---|
11 | }
|
---|
12 |
|
---|
13 | /// Destruktor
|
---|
14 | function __destruct()
|
---|
15 | {
|
---|
16 | if ($this->handle)
|
---|
17 | fclose($this->handle);
|
---|
18 | }
|
---|
19 |
|
---|
20 | public function loadFile($filename)
|
---|
21 | {
|
---|
22 | if ($this->handle)
|
---|
23 | fclose($this->handle);
|
---|
24 |
|
---|
25 | $this->handle = @fopen($filename, "rb");
|
---|
26 | if (!$this->handle)
|
---|
27 | die ("Soubor $filename neexistuje");
|
---|
28 | }
|
---|
29 |
|
---|
30 | /// Nacteni bytu
|
---|
31 | public function readByte()
|
---|
32 | {
|
---|
33 | return ord(fread($this->handle, 1));
|
---|
34 | }
|
---|
35 |
|
---|
36 | /// Nacteni unsigned int
|
---|
37 | public function readUint()
|
---|
38 | {
|
---|
39 | $val = unpack("V*", fread($this->handle, 4));
|
---|
40 | return $val[1];
|
---|
41 | }
|
---|
42 |
|
---|
43 | /// Nacteni signed int
|
---|
44 | public function readInt()
|
---|
45 | {
|
---|
46 | $val = unpack("I*", fread($this->handle, 4));
|
---|
47 | return $val[1];
|
---|
48 | }
|
---|
49 |
|
---|
50 | /// Nacteni float
|
---|
51 | public function readFloat()
|
---|
52 | {
|
---|
53 | $val = unpack("f*", fread($this->handle, 4));
|
---|
54 | return $val[1];
|
---|
55 | }
|
---|
56 |
|
---|
57 | /// Nacteni znaku
|
---|
58 | public function readChar()
|
---|
59 | {
|
---|
60 | return fgetc($this->handle);
|
---|
61 | }
|
---|
62 |
|
---|
63 | public function seekPos($pos, $type = SEEK_SET)
|
---|
64 | {
|
---|
65 | fseek($this->handle, $pos, $type);
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | class DbcReader extends BinaryReader
|
---|
70 | {
|
---|
71 | private $offsets; /// Offsety jednotlivych poli
|
---|
72 | private $format; /// Format dbc souboru
|
---|
73 |
|
---|
74 | private $recSize; /// Velikost jednoho zaznamu
|
---|
75 | private $recCount;/// Pocet zaznamu
|
---|
76 | private $strSize; /// Velikost string casti
|
---|
77 | private $fldCount;/// Pocet poli (v 1 zaznamu)
|
---|
78 |
|
---|
79 | /// Nacteni dbc souboru
|
---|
80 | public function loadDbc($file, $format)
|
---|
81 | {
|
---|
82 | $this->loadFile($file);
|
---|
83 |
|
---|
84 | if ($this->readUint() != 0x43424457)
|
---|
85 | die ("NO DBC!");
|
---|
86 |
|
---|
87 | $this->recCount = $this->readUint();
|
---|
88 | $this->fldCount = $this->readUint();
|
---|
89 | $this->recSize = $this->readUint();
|
---|
90 | $this->strSize = $this->readUint();
|
---|
91 |
|
---|
92 | //if (strlen($format) != $this->fldCount)
|
---|
93 | // die ("Field count error");
|
---|
94 |
|
---|
95 | $this->format = $format;
|
---|
96 | $this->generateOffsetTable($format);
|
---|
97 | }
|
---|
98 |
|
---|
99 | /// Vytvoreni tabulky offsetu
|
---|
100 | private function generateOffsetTable($format)
|
---|
101 | {
|
---|
102 | //echo(strlen($format).' '.$format);
|
---|
103 | $this->offsets = array();
|
---|
104 | $this->offsets[0] = 0;
|
---|
105 | for ($i = 1; $i < strlen($format); $i++)
|
---|
106 | {
|
---|
107 | $this->offsets[$i] = $this->offsets[$i-1];
|
---|
108 | switch ($format[$i-1])
|
---|
109 | {
|
---|
110 | case "b": case "X": $this->offsets[$i] += 1; break;
|
---|
111 | case "x": case "u": case "i": case "f": case "s": $this->offsets[$i] += 4; break;
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | $lastOff = $this->offsets[strlen($format) - 1] + ($format[strlen($format)-1] == "b" || $format[strlen($format)-1] == "X" ? 1 : 4);
|
---|
116 | if($lastOff != $this->recSize)
|
---|
117 | die ("Record size error! ".$lastOff.' <> '.$this->recSize);
|
---|
118 | }
|
---|
119 |
|
---|
120 | //////////////////////////////////////////////////////////////////////////////
|
---|
121 |
|
---|
122 | /// Nastaveni pozice v souboru
|
---|
123 | private function seekPosi($row, $col)
|
---|
124 | {
|
---|
125 | $pos = 20 + $row*$this->recSize + $this->offsets[$col];
|
---|
126 | //fseek($this->handle, $pos, SEEK_SET);
|
---|
127 | $this->seekPos($pos);
|
---|
128 | }
|
---|
129 |
|
---|
130 | //////////////////////////////////////////////////////////////////////////////
|
---|
131 |
|
---|
132 | public function getByte($row, $col)
|
---|
133 | {
|
---|
134 | $this->seekPosi($row, $col);
|
---|
135 | return $this->readByte();
|
---|
136 | }
|
---|
137 |
|
---|
138 | public function getUint($row, $col)
|
---|
139 | {
|
---|
140 | $this->seekPosi($row, $col);
|
---|
141 | return $this->readUint();
|
---|
142 | }
|
---|
143 |
|
---|
144 | public function getInt($row, $col)
|
---|
145 | {
|
---|
146 | $this->seekPosi($row, $col);
|
---|
147 | return $this->readInt();
|
---|
148 | }
|
---|
149 |
|
---|
150 | public function getFloat($row, $col)
|
---|
151 | {
|
---|
152 | $this->seekPosi($row, $col);
|
---|
153 | return $this->readFloat();
|
---|
154 | }
|
---|
155 |
|
---|
156 | public function getString($row, $col)
|
---|
157 | {
|
---|
158 | $offset = $this->getUint($row, $col);
|
---|
159 |
|
---|
160 | $pos = 20 + $this->recCount * $this->recSize + $offset;
|
---|
161 | $this->seekPos($pos);
|
---|
162 |
|
---|
163 | $str = "";
|
---|
164 | while (($char = $this->readChar()) != "\0")
|
---|
165 | $str .= $char;
|
---|
166 |
|
---|
167 | return $str;
|
---|
168 | }
|
---|
169 |
|
---|
170 | /// Vrati jeden cely radek jako pole
|
---|
171 | public function getLine($row)
|
---|
172 | {
|
---|
173 | $ret = array();
|
---|
174 | for($i = 0; $i < $this->fldCount; $i++)
|
---|
175 | {
|
---|
176 | switch($this->format[$i])
|
---|
177 | {
|
---|
178 | case "b": $ret[$i] = $this->getByte($row, $i); break;
|
---|
179 | case "u": $ret[$i] = $this->getUint($row, $i); break;
|
---|
180 | case "i": $ret[$i] = $this->getInt($row, $i); break;
|
---|
181 | case "f": $ret[$i] = $this->getFloat($row, $i); break;
|
---|
182 | case "s": $ret[$i] = $this->getString($row, $i); break;
|
---|
183 | case "x": case "X": default: break;
|
---|
184 | }
|
---|
185 | }
|
---|
186 | return $ret;
|
---|
187 | }
|
---|
188 |
|
---|
189 | /// Vrati vybrane sloupecky z jednoho radku
|
---|
190 | public function getLineCols($row, $cols)
|
---|
191 | {
|
---|
192 | $ret = array();
|
---|
193 | for($i = 0; $i < count($cols); $i++)
|
---|
194 | {
|
---|
195 | switch($this->format[$cols[$i]])
|
---|
196 | {
|
---|
197 | case "b": $ret[$i] = $this->getByte($row, $cols[$i]); break;
|
---|
198 | case "u": $ret[$i] = $this->getUint($row, $cols[$i]); break;
|
---|
199 | case "i": $ret[$i] = $this->getInt($row, $cols[$i]); break;
|
---|
200 | case "f": $ret[$i] = $this->getFloat($row, $cols[$i]); break;
|
---|
201 | case "s": $ret[$i] = $this->getString($row, $cols[$i]); break;
|
---|
202 | case "x": case "X": default: break;
|
---|
203 | }
|
---|
204 | }
|
---|
205 | return $ret;
|
---|
206 | }
|
---|
207 |
|
---|
208 | /// Pocet zaznamu
|
---|
209 | public function count() { return $this->recCount; }
|
---|
210 | /// Pocet poli
|
---|
211 | public function fieldCount() { return $this->fldCount; }
|
---|
212 | }
|
---|
213 |
|
---|
214 | ?>
|
---|