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