Changeset 815 for trunk/includes/dbc.php
- Timestamp:
- Feb 22, 2015, 11:05:49 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/includes/dbc.php
r553 r815 24 24 private $ColumnFormat; // Array (Index => Type) 25 25 private $EndOffset; // Calculated RecordSize according columns type 26 26 27 27 private $RecordSize; 28 28 private $RecordCount; 29 29 private $StringBlockSize; 30 30 private $FieldCount; 31 31 32 32 public function OpenFile($FileName, $ColumnFormat = array()) 33 33 { 34 34 parent::OpenFile($FileName); 35 35 36 36 $this->ColumnFormat = $ColumnFormat; 37 37 if($this->ReadUint() != DBC_SIGNATURE) die(NOT_DBC_FILE); 38 38 39 39 $this->RecordCount = $this->ReadUint(); 40 40 $this->FieldCount = $this->ReadUint(); 41 41 $this->RecordSize = $this->ReadUint(); 42 42 $this->StringBlockSize = $this->ReadUint(); 43 43 44 44 $this->GenerateOffsetTable(); 45 45 if($this->EndOffset != $this->RecordSize) 46 die(RECORD_SIZE_NOT_MATCH.$this->EndOffset.' <> '.$this->RecordSize); 46 die(RECORD_SIZE_NOT_MATCH.$this->EndOffset.' <> '.$this->RecordSize); 47 47 } 48 48 … … 50 50 { 51 51 parent::CreateFile($FileName); 52 52 53 53 $this->WriteUint(DBC_SIGNATURE); 54 54 55 55 $this->StringList = array(); 56 56 $this->StringOffset = 1; … … 65 65 $this->WriteUint($this->FieldCount); 66 66 $this->WriteUint($this->RecordSize); 67 $this->WriteUint($this->StringBlockSize); 68 } 69 67 $this->WriteUint($this->StringBlockSize); 68 } 69 70 70 private function GenerateOffsetTable() 71 71 { 72 72 // Preallocate array 73 if($this->FieldCount > 0) $this->Offsets = array_fill(0, $this->FieldCount, 0); 73 if($this->FieldCount > 0) $this->Offsets = array_fill(0, $this->FieldCount, 0); 74 74 else $this->Offsets = array(); 75 75 76 76 $Offset = 0; 77 77 $I = 0; … … 83 83 switch($Format) 84 84 { 85 case FORMAT_BYTE: 86 $Offset += 1; 87 break; 88 case FORMAT_UINT32: 89 case FORMAT_SINT32: 90 case FORMAT_SINGLE: 91 case FORMAT_STRING: 92 $Offset += 4; 93 break; 94 } 85 case FORMAT_BYTE: 86 $Offset += 1; 87 break; 88 case FORMAT_UINT32: 89 case FORMAT_SINT32: 90 case FORMAT_SINGLE: 91 case FORMAT_STRING: 92 $Offset += 4; 93 break; 94 } 95 95 $I++; 96 } 96 } 97 97 $this->EndOffset = $Offset; 98 98 } 99 99 100 100 private function CellPos($Row, $Column) 101 101 { 102 102 return($this->HeaderSize + $Row * $this->RecordSize + $this->Offsets[$Column]); 103 103 } 104 104 105 105 public function GetByte($Row, $Column) 106 106 { 107 $this->SetPosition($this->CellPos($Row, $Column)); 107 $this->SetPosition($this->CellPos($Row, $Column)); 108 108 return($this->ReadByte()); 109 } 110 109 } 110 111 111 public function GetUInt($Row, $Column) 112 112 { 113 $this->SetPosition($this->CellPos($Row, $Column)); 113 $this->SetPosition($this->CellPos($Row, $Column)); 114 114 return($this->ReadUint()); 115 115 } 116 116 117 117 public function GetInt($Row, $Column) 118 118 { 119 $this->SetPosition($this->CellPos($Row, $Column)); 119 $this->SetPosition($this->CellPos($Row, $Column)); 120 120 return($this->ReadInt()); 121 } 122 121 } 122 123 123 public function GetFloat($Row, $Column) 124 124 { 125 $this->SetPosition($this->CellPos($Row, $Column)); 125 $this->SetPosition($this->CellPos($Row, $Column)); 126 126 return($this->ReadFloat()); 127 127 } … … 129 129 public function SetByte($Row, $Column, $Value) 130 130 { 131 $this->SetPosition($this->CellPos($Row, $Column)); 131 $this->SetPosition($this->CellPos($Row, $Column)); 132 132 $this->WriteByte($Value); 133 } 134 133 } 134 135 135 public function SetUint($Row, $Column, $Value) 136 136 { 137 $this->SetPosition($this->CellPos($Row, $Column)); 137 $this->SetPosition($this->CellPos($Row, $Column)); 138 138 $this->WriteUint($Value); 139 139 } 140 140 141 141 public function SetInt($Row, $Column, $Value) 142 142 { 143 $this->SetPosition($this->CellPos($Row, $Column)); 143 $this->SetPosition($this->CellPos($Row, $Column)); 144 144 $this->WriteInt($Value); 145 } 146 145 } 146 147 147 public function SetFloat($Row, $Column, $Value) 148 148 { 149 $this->SetPosition($this->CellPos($Row, $Column)); 149 $this->SetPosition($this->CellPos($Row, $Column)); 150 150 $this->WriteFloat($Value); 151 151 } 152 152 153 153 public function GetString($Row, $Column) 154 154 { 155 155 $Offset = $this->GetUint($Row, $Column); 156 156 157 157 $Position = $this->HeaderSize + $this->RecordCount * $this->RecordSize + $Offset; 158 158 if($Position >= $this->GetSize()) return(''); 159 159 $this->SetPosition($Position); 160 160 161 161 $String = ''; 162 162 while(($Char = $this->ReadChar()) != "\0") … … 165 165 } 166 166 return($String); 167 } 168 167 } 168 169 169 public function SetString($Row, $Column, $Value) 170 170 { 171 171 if(in_array($Value, $this->StringList)) 172 { 172 { 173 173 $this->SetUint($Row, $Column, $this->StringListOffset[array_search($Value, $this->StringList)]); 174 } else 174 } else 175 175 { 176 176 $this->SetUint($Row, $Column, $this->StringOffset); … … 189 189 $this->WriteUint($this->FieldCount); 190 190 $this->WriteUint($this->RecordSize); 191 $this->WriteUint($this->StringOffset); 191 $this->WriteUint($this->StringOffset); 192 192 $this->SetPosition($this->HeaderSize + $this->RecordCount * $this->RecordSize); 193 193 $this->WriteByte(0); 194 194 foreach($this->StringList as $Index => $Item) 195 { 195 { 196 196 $this->WriteString($Item."\0"); 197 197 } 198 } 199 198 } 199 200 200 public function GetLine($Row) 201 201 { … … 204 204 $this->SetPosition($this->CellPos($Row, 0)); 205 205 $Record->Data = $this->ReadBlock($this->RecordSize); 206 206 207 207 // Preallocate array 208 if($this->FieldCount > 0) $Line = array_fill(0, $this->FieldCount, 0); 208 if($this->FieldCount > 0) $Line = array_fill(0, $this->FieldCount, 0); 209 209 else $Line = array(); 210 210 for($I = 0; $I < $this->FieldCount; $I++) … … 212 212 if(array_key_exists($I, $this->ColumnFormat)) $Format = $this->ColumnFormat[$I]; 213 213 else $Format = FORMAT_UINT32; 214 $Record->SetPosition($this->Offsets[$I]); 214 $Record->SetPosition($this->Offsets[$I]); 215 215 switch($Format) 216 216 { 217 case FORMAT_BYTE: 218 $Line[$I] = $Record->ReadByte(); 219 break; 220 case FORMAT_UINT32: 221 $Line[$I] = $Record->ReadUInt(); 222 break; 223 case FORMAT_SINT32: 224 $Line[$I] = $Record->ReadInt(); 225 break; 226 case FORMAT_SINGLE: 227 $Line[$I] = $Record->ReadFloat(); 228 break; 229 case FORMAT_STRING: 217 case FORMAT_BYTE: 218 $Line[$I] = $Record->ReadByte(); 219 break; 220 case FORMAT_UINT32: 221 $Line[$I] = $Record->ReadUInt(); 222 break; 223 case FORMAT_SINT32: 224 $Line[$I] = $Record->ReadInt(); 225 break; 226 case FORMAT_SINGLE: 227 $Line[$I] = $Record->ReadFloat(); 228 break; 229 case FORMAT_STRING: 230 230 $Offset = $Record->ReadUint(); 231 231 232 232 $Position = $this->HeaderSize + $this->RecordCount * $this->RecordSize + $Offset; 233 233 if($Position >= $this->GetSize()) $String = ''; 234 else 234 else 235 235 { 236 $this->SetPosition($Position); 236 $this->SetPosition($Position); 237 237 $String = ''; 238 238 while(($Char = $this->ReadChar()) != "\0") … … 241 241 $Line[$I] = $String; 242 242 break; 243 default: 243 default: 244 244 break; 245 245 } … … 252 252 // Cache record data 253 253 $Record = new MemoryStream(); 254 254 255 255 for($I = 0; $I < $this->FieldCount; $I++) 256 256 { 257 257 if(array_key_exists($I, $this->ColumnFormat)) $Format = $this->ColumnFormat[$I]; 258 258 else $Format = FORMAT_UINT32; 259 $Record->SetPosition($this->Offsets[$I]); 259 $Record->SetPosition($this->Offsets[$I]); 260 260 switch($Format) 261 261 { 262 case FORMAT_BYTE: 263 $Record->WriteByte($Line[$I]); 264 break; 265 case FORMAT_UINT32: 266 $Record->WriteUint($Line[$I]); 267 break; 268 case FORMAT_SINT32: 269 $Record->WriteInt($Line[$I]); 270 break; 271 case FORMAT_SINGLE: 272 $Record->WriteFloat($Line[$I]); 273 break; 274 case FORMAT_STRING: 262 case FORMAT_BYTE: 263 $Record->WriteByte($Line[$I]); 264 break; 265 case FORMAT_UINT32: 266 $Record->WriteUint($Line[$I]); 267 break; 268 case FORMAT_SINT32: 269 $Record->WriteInt($Line[$I]); 270 break; 271 case FORMAT_SINGLE: 272 $Record->WriteFloat($Line[$I]); 273 break; 274 case FORMAT_STRING: 275 275 if(in_array($Line[$I], $this->StringList)) 276 { 276 { 277 277 $Record->WriteUint($this->StringListOffset[array_search($Line[$I], $this->StringList)]); 278 } else 278 } else 279 279 { 280 280 $Record->WriteUint($this->StringOffset); … … 284 284 } 285 285 break; 286 default: 286 default: 287 287 break; 288 288 } 289 289 } 290 290 291 291 $this->SetPosition($this->CellPos($Row, 0)); 292 292 $this->WriteBlock($Record->Data, $this->RecordSize); 293 293 return($Line); 294 } 295 296 public function GetRecordCount() 297 { 298 return($this->RecordCount); 299 } 300 301 public function SetRecordCount($Value) 302 { 294 } 295 296 public function GetRecordCount() 297 { 298 return($this->RecordCount); 299 } 300 301 public function SetRecordCount($Value) 302 { 303 303 $this->RecordCount = $Value; 304 304 } 305 305 306 public function GetFieldCount() 307 { 308 return($this->FieldCount); 309 } 310 311 public function SetFieldCount($Value) 312 { 306 public function GetFieldCount() 307 { 308 return($this->FieldCount); 309 } 310 311 public function SetFieldCount($Value) 312 { 313 313 $this->FieldCount = $Value; 314 314 $this->GenerateOffsetTable();
Note:
See TracChangeset
for help on using the changeset viewer.