aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/3rdparty/libraries/MOBIClass/Prc.php
blob: c40b58057ac422381041f303497ec04284db5300 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php

/**
 * Description of Prc
 *
 * @author Sander
 */
class Prc extends FileElement {
	public function __construct($settings, $records){
		parent::__construct(array(
			"title"=>new FileString(32),
			"attributes"=>new FileShort(),
			"version"=>new FileShort(),
			"creationTime"=>new FileDate(),
			"modificationTime"=>new FileDate(),
			"backupTime"=>new FileDate(),
			"modificationNumber"=>new FileInt(),
			"appInfoID"=>new FileInt(),
			"sortInfoID"=>new FileInt(),
			"prcType"=>new FileString(4),
			"creator"=>new FileString(4),
			"uniqueIDSeed"=>new FileInt(),
			"nextRecordListID"=>new FileInt(),
			"numberRecords"=>new FileShort(),
			"recordList"=>new FileElement(),
			"filler"=>new FileShort(),
			"records"=>new FileElement()
		));

		//Set values from the info block
		foreach($this->elements as $name => $val){
			if($settings->exists($name)){
				$this->get($name)->set($settings->get($name));
			}
		}
		
		$this->get("numberRecords")->set(sizeof($records));

		$i = 0;
		foreach($records as $record){
			$offset = new FileInt();
			$attr = new FileByte();
			$uniqueID = new FileTri($i);
			
			$this->elements["recordList"]->add("Rec".$i, new FileElement(array(
				"offset"=>$offset,
				"attribute"=>$attr,
				"uniqueID"=>$uniqueID
			)));
			
			$this->elements["records"]->add("Rec".$i, $record);
			$i++;
		}

		$this->updateOffsets($records);
	}

	public function getByteLength(){
		throw new Exception("Test");
	}

	public function updateOffsets($records){
		$base = $this->offsetToEntry("records");

		$i = 0;
		
		foreach($records as $record){
			$el = $this->elements["recordList"]->get("Rec".$i);
			
			$local = $this->elements["records"]->offsetToEntry("Rec".$i);
			
			$el->get("offset")->set($base+$local);

			$i++;
		}
	}

	public function save($file){
		$handle = fopen($file, "w");
		fwrite($handle, $this->serialize());
		fclose($handle);
	}

	public function output(){
		echo $this->serialize();
	}

	public function __toString(){
		$output = "Prc (".$this->getByteLength()." bytes): {\n";
		foreach($this->elements as $key=>$value){
			$output .= "\t".$key.": ".$value."\n";
		}
		$output .= "}";
		return $output;
	}
}
?>