3 * A class for reading files
5 class FrameworkFileInputPointer extends BaseFrameworkSystem {
7 * The current file we are working in
9 private $fileName = "";
14 private $filePointer = null;
19 private final function __construct () {
20 // Call parent constructor
21 parent::constructor(__CLASS__);
23 // Set part description
24 $this->setPartDescr("Dateiausgabe-Handler");
27 $this->createUniqueID();
30 $this->removeNumberFormaters();
34 * Destructor for cleaning purposes, etc
36 public final function __destruct() {
37 // Is there a resource pointer? Then we have to close the file here!
38 if (is_resource($this->getPointer())) {
39 // Try to close a file
43 // Call the parent destructor
48 * Create a file pointer based on the given file. The file will also
51 * @param $fileName The file name we shall pass
53 * @throws FileIsEmptyException If the provided file name is empty.
54 * @throws FilePointerNotOpenedException If fopen() returns not a
58 public final static function createFrameworkFileInputPointer ($fileName) {
59 // Some pre-sanity checks...
60 if (is_null($fileName)) {
62 throw new FileIsEmptyException(null, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
65 // Try to open a handler
66 $filePointer = @fopen($fileName, 'rb');
67 if (($filePointer === null) || ($filePointer === false)) {
68 // Something bad happend
69 throw new FilePointerNotOpenedException ($fileName, self::EXCEPTION_FILE_POINTER_INVALID);
72 // Create new instance
73 $pointerInstance = new FrameworkFileInputPointer();
75 // Set file pointer and file name
76 $pointerInstance->setPointer($filePointer);
77 $pointerInstance->setFileName($fileName);
79 // Return the instance
80 return $pointerInstance;
84 * Read data a file pointer
86 * @return mixed The result of fread()
87 * @throws NullPointerException If the file pointer instance
88 * is not set by setPointer()
89 * @throws InvalidFileResourceException If there is being set
90 * an invalid file resource
92 public function readFromFile () {
93 if (is_null($this->getPointer())) {
94 // Pointer not initialized
95 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
96 } elseif (!is_resource($this->getPointer())) {
97 // Pointer is not a valid resource!
98 throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
101 // Read data from the file pointer and return it
102 return fread($this->getPointer(), 1024);
106 * Read lines from a file pointer
108 * @return mixed The result of fread()
109 * @throws NullPointerException If the file pointer instance
110 * is not set by setPointer()
111 * @throws InvalidFileResourceException If there is being set
112 * an invalid file resource
114 public function readLinesFromFile () {
115 if (is_null($this->getPointer())) {
116 // Pointer not initialized
117 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
118 } elseif (!is_resource($this->getPointer())) {
119 // Pointer is not a valid resource!
120 throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
123 // Read data from the file pointer and return it
124 return fgets($this->getPointer(), 1024);
128 * Close a file source and set it's instance to null and the file name
132 * @throws NullPointerException If the file pointer instance
133 * is not set by setPointer()
134 * @throws InvalidFileResourceException If there is being set
136 public function closeFile () {
137 if (is_null($this->getPointer())) {
138 // Pointer not initialized
139 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
140 } elseif (!is_resource($this->getPointer())) {
141 // Pointer is not a valid resource!
142 throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
145 // Close the file pointer and reset the instance variable
146 @fclose($this->getPointer());
147 $this->setPointer(null);
148 $this->setFileName("");
152 * Setter for the file pointer
154 * @param $filePointer File resource
157 public final function setPointer ($filePointer) {
158 // Sanity-check if the pointer is a valid file resource
159 if (is_resource($filePointer) || is_null($filePointer)) {
160 // Is a valid resource
161 $this->filePointer = $filePointer;
164 throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
169 * Getter for the file pointer
171 * @return $filePointer The file pointer which shall be a valid
174 public final function getPointer () {
175 return $this->filePointer;
179 * Setter for file name
181 * @param $fileName The new file name
184 public final function setFileName ($fileName) {
185 $fileName = (string) $fileName;
186 $this->fileName = $fileName;
190 * Getter for file name
192 * @return $fileName The current file name
194 public final function getFileName () {
195 return $this->fileName;