f45b5ea3b9e5243297345dc224776f115cf4b8e0
[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  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
20  * @license             GNU GPL 3.0 or any newer version
21  * @link                http://www.ship-simu.org
22  *
23  * This program is free software: you can redistribute it and/or modify
24  * it under the terms of the GNU General Public License as published by
25  * the Free Software Foundation, either version 3 of the License, or
26  * (at your option) any later version.
27  *
28  * This program is distributed in the hope that it will be useful,
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31  * GNU General Public License for more details.
32  *
33  * You should have received a copy of the GNU General Public License
34  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35  */
36 abstract class BaseFileIo extends BaseFrameworkSystem implements FilePointer, CloseableFile {
37         /**
38          * The file object
39          */
40         private $fileObject = 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_object($this->getFileObject())) {
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 setFileObject()
75          * @throws      LogicException  If there is no object being set
76          */
77         public function closeFile () {
78                 // Debug message
79                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: fileName=%s - CALLED!', __METHOD__, __LINE__, $this->getFileObject()->getPathname()));
80
81                 if (is_null($this->getFileObject())) {
82                         // Pointer not initialized
83                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
84                 } elseif (!is_object($this->getFileObject())) {
85                         // Pointer is not a valid resource!
86                         throw new LogicException(sprintf('this->fileObject[]=%s is no object', gettype($this->getFileObject())));
87                 }
88
89                 // Debug message
90                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: Closing file %s ...', __METHOD__, __LINE__, $this->getFileObject()->getPathname()));
91
92                 // Close the file pointer by NULL-ing it
93                 $this->resetFileObject();
94
95                 // Debug message
96                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: EXIT!', __METHOD__, __LINE__));
97         }
98
99         /**
100          * Resets file object instance to NULL
101          *
102          * @return      void
103          */
104         protected final function resetFileObject () {
105                 // Set it to NULL
106                 $this->fileObject = NULL;
107         }
108
109         /**
110          * Setter for the file object
111          *
112          * @param       $fileObject             An instance of a SplFileObject class
113          * @return      void
114          */
115         protected final function setFileObject (SplFileObject $fileObject) {
116                 $this->fileObject = $fileObject;
117         }
118
119         /**
120          * Getter for the file object
121          *
122          * @return      $fileObject             An instance of a SplFileObject class
123          */
124         public final function getFileObject () {
125                 return $this->fileObject;
126         }
127
128         /**
129          * Determines seek position
130          *
131          * @return      $seekPosition   Current seek position
132          */
133         public final function determineSeekPosition () {
134                 return $this->getFileObject()->ftell();
135         }
136
137         /**
138          * Determines whether the EOF has been reached
139          *
140          * @return      $isEndOfFileReached             Whether the EOF has been reached
141          */
142         public final function isEndOfFileReached () {
143                 return $this->getFileObject()->eof();
144         }
145
146         /**
147          * Seek to given offset (default) or other possibilities as fseek() gives.
148          *
149          * @param       $offset         Offset to seek to (or used as "base" for other seeks)
150          * @param       $whence         Added to offset (default: only use offset to seek to)
151          * @return      $status         Status of file seek: 0 = success, -1 = failed
152          */
153         public function seek ($offset, $whence = SEEK_SET) {
154                 // Seek to position
155                 $status = $this->getFileObject()->fseek($offset, $whence);
156
157                 // Return status
158                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] status=%d', __METHOD__, __LINE__, $status));
159                 return $status;
160         }
161
162         /**
163          * Size of this file
164          *
165          * @return      $size   Size (in bytes) of file
166          * @todo        Handle seekStatus
167          */
168         public function size () {
169                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
170
171                 // Get current seek position
172                 $seekPosition = $this->determineSeekPosition();
173
174                 // Seek to end
175                 $seekStatus = $this->seek(0, SEEK_END);
176                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] seekStatus=%d', __METHOD__, __LINE__, $seekStatus));
177
178                 // Get position again  (which is the end of the file)
179                 $size = $this->determineSeekPosition();
180
181                 // Reset seek position to old
182                 $this->seek($seekPosition);
183
184                 // Return size
185                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] size=%s - EXIT!', __METHOD__, __LINE__, $size));
186                 return $size;
187         }
188
189 }