Changeset 443 for trunk/includes/dbc.php
- Timestamp:
- Apr 11, 2010, 11:48:39 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/includes/dbc.php
r297 r443 4 4 5 5 define('NOT_DBC_FILE', 'Není DBC soubor.'); 6 define('FIELD_COUNT_NOT_MATCH', 'Počet sloupců neodpovídá délce formátu.');7 6 define('RECORD_SIZE_NOT_MATCH', 'Velikost řádku neodpovídá zadanému formátu.'); 8 7 8 define('DBC_SIGNATURE', 0x43424457); 9 10 define('FORMAT_UINT32', 0); 11 define('FORMAT_SINT32', 1); 12 define('FORMAT_SINGLE', 2); 13 define('FORMAT_STRING', 3); 14 define('FORMAT_BYTE', 4); 15 9 16 class DBCFile extends FileStream 10 17 { … … 14 21 private $StringList = array(); 15 22 private $StringListOffset = array(); 16 private $Format; 23 private $ColumnFormat; // Array (Index => Type) 24 private $EndOffset; // Calculated RecordSize according columns type 17 25 18 26 private $RecordSize; … … 21 29 private $FieldCount; 22 30 23 public function OpenFile($FileName, $ Format)31 public function OpenFile($FileName, $ColumnFormat = array()) 24 32 { 25 33 parent::OpenFile($FileName); 26 34 27 $this-> Format = $Format;28 if($this->ReadUint() != 0x43424457) die(NOT_DBC_FILE);35 $this->ColumnFormat = $ColumnFormat; 36 if($this->ReadUint() != DBC_SIGNATURE) die(NOT_DBC_FILE); 29 37 30 38 $this->RecordCount = $this->ReadUint(); … … 33 41 $this->StringBlockSize = $this->ReadUint(); 34 42 35 if(strlen($this->Format) != $this->FieldCount) 36 die(FIELD_COUNT_NOT_MATCH.' Počet v souboru:'.$this->FieldCount.' Počet v struktuře: '.strlen($Format)); 37 38 $this->GenerateOffsetTable($Format); 39 if($this->Offsets[count($this->Offsets) - 1] != $this->RecordSize) 40 die(RECORD_SIZE_NOT_MATCH.$this->Offsets[count($this->Offsets) - 1].' <> '.$this->RecordSize); 41 } 42 43 public function CreateFile($FileName, $Format) 43 $this->GenerateOffsetTable(); 44 if($this->EndOffset != $this->RecordSize) 45 die(RECORD_SIZE_NOT_MATCH.$this->EndOffset.' <> '.$this->RecordSize); 46 } 47 48 public function CreateFile($FileName, $ColumnFormat = array()) 44 49 { 45 50 parent::CreateFile($FileName); 46 51 47 $this->WriteUint( 0x43424457);52 $this->WriteUint(DBC_SIGNATURE); 48 53 49 54 $this->StringList = array(); 50 55 $this->StringOffset = 1; 51 $this-> Format = $Format;52 $this-> GenerateOffsetTable($Format);53 $this-> FieldCount = strlen($Format);56 $this->ColumnFormat = $ColumnFormat; 57 $this->FieldCount = 0; 58 $this->GenerateOffsetTable(); 54 59 $this->RecordCount = 0; 55 $this->RecordSize = $this-> Offsets[count($this->Offsets) - 1];60 $this->RecordSize = $this->EndOffset; 56 61 $this->StringBlockSize = 0; 57 62 … … 62 67 } 63 68 64 private function GenerateOffsetTable($Format) 65 { 66 $this->Offsets = array(); 67 $this->Offsets[0] = 0; 68 for($I = 0; $I < strlen($Format); $I++) 69 { 70 $this->Offsets[$I + 1] = $this->Offsets[$I]; 71 switch($Format[$I]) 69 private function GenerateOffsetTable() 70 { 71 // Preallocate array 72 if($this->FieldCount > 0) $this->Offsets = array_fill(0, $this->FieldCount, 0); 73 else $this->Offsets = array(); 74 75 $Offset = 0; 76 $I = 0; 77 while($I < $this->FieldCount) 78 { 79 $this->Offsets[$I] = $Offset; 80 if(array_key_exists($I, $this->ColumnFormat)) $Format = $this->ColumnFormat[$I]; 81 else $Format = FORMAT_UINT32; 82 switch($Format) 72 83 { 73 case "b": 74 case "X": 75 $this->Offsets[$I + 1] += 1; 76 break; 77 case "x": 78 case "u": 79 case "i": 80 case "f": 81 case "s": 82 $this->Offsets[$I + 1] += 4; 84 case FORMAT_BYTE: 85 $Offset += 1; 86 break; 87 case FORMAT_UINT32: 88 case FORMAT_SINT32: 89 case FORMAT_SINGLE: 90 case FORMAT_STRING: 91 $Offset += 4; 83 92 break; 84 93 } 94 $I++; 85 95 } 96 $this->EndOffset = $Offset; 86 97 } 87 98 … … 98 109 } 99 110 100 public function GetU int($Row, $Column)111 public function GetUInt($Row, $Column) 101 112 { 102 113 $this->SeekPosi($Row, $Column); … … 142 153 public function GetString($Row, $Column) 143 154 { 144 $Offset = $this->GetUint($Row, $Column);155 $Offset = $this->GetUint($Row, $Column); 145 156 146 157 $Position = $this->HeaderSize + $this->RecordCount * $this->RecordSize + $Offset; … … 172 183 public function Commit() 173 184 { 185 $this->SetSize($this->HeaderSize + $this->RecordSize * $this->RecordCount); // Preallocate file 174 186 $this->Seek(0); 175 $this->WriteUint( 0x43424457);187 $this->WriteUint(DBC_SIGNATURE); 176 188 $this->WriteUint($this->RecordCount); 177 189 $this->WriteUint($this->FieldCount); … … 188 200 public function GetLine($Row) 189 201 { 190 $Line = array(); 202 // Preallocate array 203 if($this->FieldCount > 0) $Line = array_fill(0, $this->FieldCount, 0); 204 else $Line = array(); 191 205 for($I = 0; $I < $this->FieldCount; $I++) 192 206 { 193 switch($this->Format[$I]) 207 if(array_key_exists($I, $this->ColumnFormat)) $Format = $this->ColumnFormat[$I]; 208 else $Format = FORMAT_UINT32; 209 switch($Format) 194 210 { 195 case 'b':211 case FORMAT_BYTE: 196 212 $Line[$I] = $this->GetByte($Row, $I); 197 213 break; 198 case 'u':199 $Line[$I] = $this->GetU int($Row, $I);200 break; 201 case 'i':214 case FORMAT_UINT32: 215 $Line[$I] = $this->GetUInt($Row, $I); 216 break; 217 case FORMAT_SINT32: 202 218 $Line[$I] = $this->GetInt($Row, $I); 203 219 break; 204 case 'f':220 case FORMAT_SINGLE: 205 221 $Line[$i] = $this->GetFloat($Row, $I); 206 222 break; 207 case 's':223 case FORMAT_STRING: 208 224 $Line[$I] = $this->GetString($Row, $I); 209 225 break; 210 case 'x':211 case 'X':212 226 default: 213 227 break; … … 221 235 for($I = 0; $I < $this->FieldCount; $I++) 222 236 { 223 switch($this->Format[$I]) 237 if(array_key_exists($I, $this->ColumnFormat)) $Format = $this->ColumnFormat[$I]; 238 else $Format = FORMAT_UINT32; 239 switch($Format) 224 240 { 225 case 'b':241 case FORMAT_BYTE: 226 242 $this->SetByte($Row, $I, $Line[$I]); 227 243 break; 228 case 'u':244 case FORMAT_UINT32: 229 245 $this->SetUint($Row, $I, $Line[$I]); 230 246 break; 231 case 'i':247 case FORMAT_SINT32: 232 248 $this->SetInt($Row, $I, $Line[$I]); 233 249 break; 234 case 'f':250 case FORMAT_SINGLE: 235 251 $this->SetFloat($Row, $I, $Line[$i]); 236 252 break; 237 case 's':253 case FORMAT_STRING: 238 254 $this->SetString($Row, $I, $Line[$I]); 239 255 break; 240 case 'x':241 case 'X':242 256 default: 243 257 break; … … 249 263 public function GetLineCols($Row, $Columns) 250 264 { 251 $Line = array(); 265 // Preallocate array 266 if(count($Columns) > 0) $Line = array_fill(0, count($Columns), 0); 267 else $Line = array(); 252 268 for($I = 0; $I < count($Columns); $I++) 253 269 { 254 switch($this->Format[$Columns[$I]]) 270 if(array_key_exists($I, $this->ColumnFormat)) $Format = $this->ColumnFormat[$I]; 271 else $Format = FORMAT_UINT32; 272 switch($Format) 255 273 { 256 case 'b':274 case FORMAT_BYTE: 257 275 $Line[$I] = $this->GetByte($Row, $Columns[$I]); 258 276 break; 259 case 'u':277 case FORMAT_UINT32: 260 278 $Line[$I] = $this->GetUint($Row, $Columns[$I]); 261 279 break; 262 case 'i':280 case FORMAT_SINT32: 263 281 $Line[$I] = $this->GetInt($Row, $Columns[$I]); 264 282 break; 265 case 'f':283 case FORMAT_SINGLE: 266 284 $Line[$i] = $this->GetFloat($Row, $Columns[$I]); 267 285 break; 268 case 's':286 case FORMAT_STRING: 269 287 $Line[$I] = $this->GetString($Row, $Columns[$I]); 270 288 break; 271 case 'x':272 case 'X':273 289 default: 274 290 break; … … 285 301 public function SetRecordCount($Value) 286 302 { 287 $this->RecordCount = $Value;303 $this->RecordCount = $Value; 288 304 } 289 305 … … 292 308 return($this->FieldCount); 293 309 } 310 311 public function SetFieldCount($Value) 312 { 313 $this->FieldCount = $Value; 314 $this->GenerateOffsetTable(); 315 $this->RecordSize = $this->EndOffset; 316 } 294 317 } 295 318
Note:
See TracChangeset
for help on using the changeset viewer.