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