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