]> git.mxchange.org Git - core.git/blob - framework/main/classes/file_directories/class_BaseFileIo.php
Continued:
[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\FrameworkInterface;
9 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
10 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
11
12 // Import SPL stuff
13 use \OutOfBoundsException;
14 use \SplFileObject;
15
16 /**
17  * A general FileIo class
18  *
19  * @author              Roland Haeder <webmaster@ship-simu.org>
20  * @version             0.0.0
21  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team
22  * @license             GNU GPL 3.0 or any newer version
23  * @link                http://www.ship-simu.org
24  *
25  * This program is free software: you can redistribute it and/or modify
26  * it under the terms of the GNU General Public License as published by
27  * the Free Software Foundation, either version 3 of the License, or
28  * (at your option) any later version.
29  *
30  * This program is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33  * GNU General Public License for more details.
34  *
35  * You should have received a copy of the GNU General Public License
36  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
37  */
38 abstract class BaseFileIo extends BaseFrameworkSystem implements FilePointer, CloseableFile {
39         /**
40          * The file object
41          */
42         private $fileObject = NULL;
43
44         /**
45          * Protected constructor
46          *
47          * @param       $className      Name of the class
48          * @return      void
49          */
50         protected function __construct (string $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                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: this->fileObject[]=%s - DESTRUCTOR!', gettype($this->getFileObject())));
63                 if (is_object($this->getFileObject())) {
64                         // Try to close a file
65                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-IO: Invoking this->closeFile() ...');
66                         $this->closeFile();
67                 }
68
69                 // Call the parent destructor
70                 parent::__destruct();
71
72                 // Trace message
73                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-IO: EXIT!');
74         }
75
76         /**
77          * Close a file source and set it's instance to null and the file name
78          * to empty.
79          *
80          * @return      void
81          * @throws      NullPointerException    If the file pointer instance is not set by setFileObject()
82          * @throws      LogicException  If there is no object being set
83          */
84         public function closeFile () {
85                 // Validate parameter
86                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: fileName=%s - CALLED!', $this->getFileObject()->getPathname()));
87                 if (is_null($this->getFileObject())) {
88                         // Pointer not initialized
89                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
90                 } elseif (!is_object($this->getFileObject())) {
91                         // Pointer is not a valid resource!
92                         throw new LogicException(sprintf('this->fileObject[]=%s is no object', gettype($this->getFileObject())), FrameworkInterface::EXCEPTION_LOGIC_EXCEPTION);
93                 }
94
95                 // Close the file pointer by NULL-ing it
96                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: Closing file %s ...', $this->getFileObject()->getPathname()));
97                 $this->resetFileObject();
98
99                 // Trace message
100                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-IO: EXIT!');
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 (int $offset, int $whence = SEEK_SET) {
158                 // Validate parameter
159                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: offset=%d,whence=%d - CALLED!', $offset, $whence));
160                 if ($offset < 0) {
161                         // Throw exception
162                         throw new OutOfBoundsException(sprintf('offset=%d is not valid', $offset));
163                 }
164
165                 // Seek to position
166                 $status = $this->getFileObject()->fseek($offset, $whence);
167
168                 // Return status
169                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: status=%d - EXIT!', $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                 // Get current seek position
181                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-IO: CALLED!');
182                 $seekPosition = $this->determineSeekPosition();
183
184                 // Seek to end
185                 $seekStatus = $this->seek(0, SEEK_END);
186
187                 // Get position again (which is the end of the file)
188                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: seekStatus[%s]=%d', gettype($seekStatus), $seekStatus));
189                 $size = $this->determineSeekPosition();
190
191                 // Reset seek position to old
192                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: size[%s]=%d', gettype($size), $size));
193                 $this->seek($seekPosition);
194
195                 // Return size
196                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: size=%d - EXIT!', $size));
197                 return $size;
198         }
199
200 }