]> 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                         $this->closeFile();
65                 }
66
67                 // Call the parent destructor
68                 parent::__destruct();
69
70                 // Trace message
71                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-IO: EXIT!');
72         }
73
74         /**
75          * Close a file source and set it's instance to null and the file name
76          * to empty.
77          *
78          * @return      void
79          * @throws      NullPointerException    If the file pointer instance is not set by setFileObject()
80          * @throws      LogicException  If there is no object being set
81          */
82         public function closeFile () {
83                 // Validate parameter
84                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: fileName=%s - CALLED!', $this->getFileObject()->getPathname()));
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                 // Close the file pointer by NULL-ing it
94                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: Closing file %s ...', $this->getFileObject()->getPathname()));
95                 $this->resetFileObject();
96
97                 // Trace message
98                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-IO: EXIT!');
99         }
100
101         /**
102          * Resets file object instance to NULL
103          *
104          * @return      void
105          */
106         protected final function resetFileObject () {
107                 // Set it to NULL
108                 $this->fileObject = NULL;
109         }
110
111         /**
112          * Setter for the file object
113          *
114          * @param       $fileObject             An instance of a SplFileObject class
115          * @return      void
116          */
117         protected final function setFileObject (SplFileObject $fileObject) {
118                 $this->fileObject = $fileObject;
119         }
120
121         /**
122          * Getter for the file object
123          *
124          * @return      $fileObject             An instance of a SplFileObject class
125          */
126         public final function getFileObject () {
127                 return $this->fileObject;
128         }
129
130         /**
131          * Determines seek position
132          *
133          * @return      $seekPosition   Current seek position
134          */
135         public final function determineSeekPosition () {
136                 return $this->getFileObject()->ftell();
137         }
138
139         /**
140          * Determines whether the EOF has been reached
141          *
142          * @return      $isEndOfFileReached             Whether the EOF has been reached
143          */
144         public final function isEndOfFileReached () {
145                 return $this->getFileObject()->eof();
146         }
147
148         /**
149          * Seek to given offset (default) or other possibilities as fseek() gives.
150          *
151          * @param       $offset         Offset to seek to (or used as "base" for other seeks)
152          * @param       $whence         Added to offset (default: only use offset to seek to)
153          * @return      $status         Status of file seek: 0 = success, -1 = failed
154          */
155         public function seek (int $offset, int $whence = SEEK_SET) {
156                 // Validate parameter
157                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: offset=%d,whence=%d - CALLED!', $offset, $whence));
158                 if ($offset < 0) {
159                         // Throw IAE
160                         throw new InvalidArgumentException(sprintf('offset=%d is not valid', $offset));
161                 }
162
163                 // Seek to position
164                 $status = $this->getFileObject()->fseek($offset, $whence);
165
166                 // Return status
167                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: status=%d - EXIT!', $status));
168                 return $status;
169         }
170
171         /**
172          * Size of this file
173          *
174          * @return      $size   Size (in bytes) of file
175          * @todo        Handle seekStatus
176          */
177         public function size () {
178                 // Get current seek position
179                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-IO: CALLED!');
180                 $seekPosition = $this->determineSeekPosition();
181
182                 // Seek to end
183                 $seekStatus = $this->seek(0, SEEK_END);
184
185                 // Get position again (which is the end of the file)
186                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: seekStatus=%d', $seekStatus));
187                 $size = $this->determineSeekPosition();
188
189                 // Reset seek position to old
190                 $this->seek($seekPosition);
191
192                 // Return size
193                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: size=%s - EXIT!', $size));
194                 return $size;
195         }
196
197 }