4ba2e9628dc38094381c61939cd6276506ff12a2
[core.git] / framework / main / classes / file_directories / class_BaseFileIo.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\FileSystem;
4
5 // Import framework stuff
6 use CoreFramework\Filesystem\CloseableFile;
7 use CoreFramework\Filesystem\FilePointer;
8 use CoreFramework\Generic\NullPointerException;
9 use CoreFramework\Object\BaseFrameworkSystem;
10
11 /**
12  * A general FileIo class
13  *
14  * @author              Roland Haeder <webmaster@ship-simu.org>
15  * @version             0.0.0
16  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
17  * @license             GNU GPL 3.0 or any newer version
18  * @link                http://www.ship-simu.org
19  *
20  * This program is free software: you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License as published by
22  * the Free Software Foundation, either version 3 of the License, or
23  * (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  * GNU General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32  */
33 class BaseFileIo extends BaseFrameworkSystem implements FilePointer, CloseableFile {
34         /**
35          * The current file we are working in
36          */
37         private $fileName = '';
38
39         /**
40          * The file pointer
41          */
42         private $filePointer = NULL;
43
44         /**
45          * Protected constructor
46          *
47          * @param       $className      Name of the class
48          * @return      void
49          */
50         protected function __construct ($className) {
51                 // Call parent constructor
52                 parent::__construct($className);
53         }
54
55         /**
56          * Destructor for cleaning purposes, etc
57          *
58          * @return      void
59          */
60         public final function __destruct() {
61                 // Is there a resource pointer? Then we have to close the file here!
62                 if (is_resource($this->getPointer())) {
63                         // Try to close a file
64                         $this->closeFile();
65                 } // END - if
66
67                 // Call the parent destructor
68                 parent::__destruct();
69         }
70
71         /**
72          * Close a file source and set it's instance to null and the file name
73          * to empty.
74          *
75          * @return      void
76          * @throws      NullPointerException    If the file pointer instance is not set by setPointer()
77          * @throws      InvalidResourceException        If there is being set
78          */
79         public function closeFile () {
80                 // Debug message
81                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: fileName=%s - CALLED!', __METHOD__, __LINE__, $this->getFileName()));
82
83                 if (is_null($this->getPointer())) {
84                         // Pointer not initialized
85                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
86                 } elseif (!is_resource($this->getPointer())) {
87                         // Pointer is not a valid resource!
88                         throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE);
89                 }
90
91                 // Debug message
92                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: Closing file %s ...', __METHOD__, __LINE__, $this->getFileName()));
93
94                 // Close the file pointer and reset the instance variable
95                 @fclose($this->getPointer());
96                 $this->setPointer(NULL);
97                 $this->setFileName('');
98
99                 // Debug message
100                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: EXIT!', __METHOD__, __LINE__));
101         }
102
103         /**
104          * Setter for the file pointer
105          *
106          * @param       $filePointer    File resource
107          * @return      void
108          */
109         protected final function setPointer ($filePointer) {
110                 $this->filePointer = $filePointer;
111         }
112
113         /**
114          * Getter for the file pointer
115          *
116          * @return      $filePointer    The file pointer which shall be a valid file resource
117          */
118         public final function getPointer () {
119                 return $this->filePointer;
120         }
121
122         /**
123          * Setter for file name
124          *
125          * @param       $fileName       The new file name
126          * @return      void
127          */
128         protected final function setFileName ($fileName) {
129                 $fileName = (string) $fileName;
130                 $this->fileName = $fileName;
131         }
132
133         /**
134          * Getter for file name
135          *
136          * @return      $fileName       The current file name
137          */
138         public final function getFileName () {
139                 return $this->fileName;
140         }
141
142         /**
143          * Determines seek position
144          *
145          * @return      $seekPosition   Current seek position
146          */
147         public final function determineSeekPosition () {
148                 return ftell($this->getPointer());
149         }
150
151         /**
152          * Determines whether the EOF has been reached
153          *
154          * @return      $isEndOfFileReached             Whether the EOF has been reached
155          */
156         public final function isEndOfFileReached () {
157                 return feof($this->getPointer());
158         }
159
160         /**
161          * Seek to given offset (default) or other possibilities as fseek() gives.
162          *
163          * @param       $offset         Offset to seek to (or used as "base" for other seeks)
164          * @param       $whence         Added to offset (default: only use offset to seek to)
165          * @return      $status         Status of file seek: 0 = success, -1 = failed
166          */
167         public function seek ($offset, $whence = SEEK_SET) {
168                 // Seek to position
169                 $status = fseek($this->getPointer(), $offset, $whence);
170
171                 // Return status
172                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] status=%d', __METHOD__, __LINE__, $status));
173                 return $status;
174         }
175
176         /**
177          * Size of this file
178          *
179          * @return      $size   Size (in bytes) of file
180          * @todo        Handle seekStatus
181          */
182         public function size () {
183                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
184
185                 // Get current seek position
186                 $seekPosition = $this->determineSeekPosition();
187
188                 // Seek to end
189                 $seekStatus = $this->seek(0, SEEK_END);
190                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] seekStatus=%d', __METHOD__, __LINE__, $seekStatus));
191
192                 // Get position again  (which is the end of the file)
193                 $size = $this->determineSeekPosition();
194
195                 // Reset seek position to old
196                 $this->seek($seekPosition);
197
198                 // Return size
199                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] size=%s - EXIT!', __METHOD__, __LINE__, $size));
200                 return $size;
201         }
202
203 }