1 | <?php
|
---|
2 |
|
---|
3 | define('FILE_NOT_FOUND', 'Soubor "%s" nenalezen.');
|
---|
4 |
|
---|
5 | class FileStream
|
---|
6 | {
|
---|
7 | private $Handle;
|
---|
8 | private $FileName;
|
---|
9 |
|
---|
10 | function __construct()
|
---|
11 | {
|
---|
12 | $this->Handle = false;
|
---|
13 | }
|
---|
14 |
|
---|
15 | function __destruct()
|
---|
16 | {
|
---|
17 | if ($this->Handle)
|
---|
18 | fclose($this->Handle);
|
---|
19 | }
|
---|
20 |
|
---|
21 | public function OpenFile($FileName)
|
---|
22 | {
|
---|
23 | $this->FileName = $FileName;
|
---|
24 | $this->CloseFile();
|
---|
25 | $this->Handle = fopen($FileName, 'rb');
|
---|
26 | if (!$this->Handle) die(str_replace('%s', $FileName, FILE_NOT_FOUND));
|
---|
27 | }
|
---|
28 |
|
---|
29 | public function CreateFile($FileName)
|
---|
30 | {
|
---|
31 | $this->FileName = $FileName;
|
---|
32 | $this->CloseFile();
|
---|
33 | $this->Handle = fopen($FileName, 'wb+');
|
---|
34 | if (!$this->Handle) die(str_replace('%s', $FileName, FILE_NOT_FOUND));
|
---|
35 | }
|
---|
36 |
|
---|
37 | public function CloseFile()
|
---|
38 | {
|
---|
39 | if ($this->Handle)
|
---|
40 | fclose($this->Handle);
|
---|
41 | }
|
---|
42 |
|
---|
43 | public function ReadBlock($Count)
|
---|
44 | {
|
---|
45 | return fread($this->Handle, $Count);
|
---|
46 | }
|
---|
47 |
|
---|
48 | public function ReadByte()
|
---|
49 | {
|
---|
50 | return fread($this->Handle, 1);
|
---|
51 | }
|
---|
52 |
|
---|
53 | public function ReadUint()
|
---|
54 | {
|
---|
55 | $val = unpack('V*', fread($this->Handle, 4));
|
---|
56 | return $val[1];
|
---|
57 | }
|
---|
58 |
|
---|
59 | public function ReadInt()
|
---|
60 | {
|
---|
61 | $val = unpack('I*', fread($this->Handle, 4));
|
---|
62 | return $val[1];
|
---|
63 | }
|
---|
64 |
|
---|
65 | public function ReadFloat()
|
---|
66 | {
|
---|
67 | $val = unpack('f*', fread($this->Handle, 4));
|
---|
68 | return $val[1];
|
---|
69 | }
|
---|
70 |
|
---|
71 | public function ReadChar()
|
---|
72 | {
|
---|
73 | return fread($this->Handle, 1);
|
---|
74 | }
|
---|
75 |
|
---|
76 | public function ReadLine()
|
---|
77 | {
|
---|
78 | return fgets($this->Handle);
|
---|
79 | }
|
---|
80 |
|
---|
81 | public function ReadTextLine()
|
---|
82 | {
|
---|
83 | return fgets($this->Handle);
|
---|
84 | }
|
---|
85 |
|
---|
86 | public function WriteBlock($Value, $Count)
|
---|
87 | {
|
---|
88 | fwrite($this->Handle, $Value, $Count);
|
---|
89 | }
|
---|
90 |
|
---|
91 | public function WriteByte($Value)
|
---|
92 | {
|
---|
93 | fwrite($this->Handle, pack('C*', $Value), 1);
|
---|
94 | }
|
---|
95 |
|
---|
96 | public function WriteUint($Value)
|
---|
97 | {
|
---|
98 | fwrite($this->Handle, pack('V*', $Value), 4);
|
---|
99 | }
|
---|
100 |
|
---|
101 | public function WriteInt($Value)
|
---|
102 | {
|
---|
103 | fwrite($this->Handle, pack('I*', $Value), 4);
|
---|
104 | }
|
---|
105 |
|
---|
106 | public function WriteFloat($Value)
|
---|
107 | {
|
---|
108 | fwrite($this->Handle, pack('f*', $Value), 4);
|
---|
109 | }
|
---|
110 |
|
---|
111 | public function WriteChar($Value)
|
---|
112 | {
|
---|
113 | fwrite($this->Handle, $Value, 1);
|
---|
114 | }
|
---|
115 |
|
---|
116 | public function WriteString($Value)
|
---|
117 | {
|
---|
118 | fwrite($this->Handle, $Value);
|
---|
119 | }
|
---|
120 |
|
---|
121 | public function WriteLine($Value)
|
---|
122 | {
|
---|
123 | fputs($this->Handle, $Value);
|
---|
124 | }
|
---|
125 |
|
---|
126 | public function SetPosition($Position)
|
---|
127 | {
|
---|
128 | fseek($this->Handle, $Position, SEEK_SET);
|
---|
129 | }
|
---|
130 |
|
---|
131 | public function GetPosition()
|
---|
132 | {
|
---|
133 | return ftell($this->Handle);
|
---|
134 | }
|
---|
135 |
|
---|
136 | public function GetSize()
|
---|
137 | {
|
---|
138 | return filesize($this->FileName);
|
---|
139 | }
|
---|
140 |
|
---|
141 | public function SetSize($Size)
|
---|
142 | {
|
---|
143 | return ftruncate($this->Handle, $Size);
|
---|
144 | }
|
---|
145 |
|
---|
146 | public function EOF()
|
---|
147 | {
|
---|
148 | return feof($this->Handle);
|
---|
149 | }
|
---|
150 | }
|
---|