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