*
* @author Roland Haeder <webmaster@shipsimu.org>
* @version 0.0.0
- * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2014 Core Developer Team
+ * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
* @license GNU GPL 3.0 or any newer version
* @link http://www.shipsimu.org
*
* be verified here.
*
* @param $fileName The file name we shall pass to fopen()
- * @throws FileIsEmptyException If the provided file name is empty.
- * @throws FileIoException If fopen() returns not a file resource
+ * @throws FileIsEmptyException If the provided file name is empty.
+ * @throws FileIoException If the file is not reachable
+ * @throws FileReadProtectedException If the file cannot be read from
* @return void
*/
public static final function createFrameworkTextFileInputPointer ($fileName) {
if ((is_null($fileName)) || (empty($fileName))) {
// No filename given
throw new FileIsEmptyException(NULL, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
- } elseif (!file_exists($fileName)) {
- // File does not exist!
- throw new FileIoException($fileName, self::EXCEPTION_FILE_NOT_FOUND);
- } elseif (!is_readable($fileName)) {
+ } elseif (!BaseFrameworkSystem::isReachableFilePath($fileName)) {
+ // File cannot be reached
+ throw new FileIoException($fileName, self::EXCEPTION_FILE_NOT_REACHABLE);
+ } elseif ((!BaseFrameworkSystem::isReadableFile($fileName)) && (!file_exists($fileName))) {
// File does not exist!
+ throw new FileNotFoundException($fileName, self::EXCEPTION_FILE_CANNOT_BE_READ);
+ } elseif ((!BaseFrameworkSystem::isReadableFile($fileName)) && (file_exists($fileName))) {
+ // File cannot be read from (but exists)
throw new FileReadProtectedException($fileName, self::EXCEPTION_FILE_CANNOT_BE_READ);
}
/**
* Read data a file pointer
*
- * @return mixed The result of fread()
+ * @return $data Read data from file
+ */
+ public function readFromFile () {
+ // Read 1024 Byte data from the file pointer and return it
+ return $this->read(1024);
+ }
+
+ /**
+ * Reads a line, maximum 4096 Bytes from current file pointer
+ *
+ * @return $data Read data from file
+ */
+ public function readLine () {
+ // Read whole line from the file pointer and return it
+ return $this->read();
+ }
+
+ /**
+ * Reads given amount of bytes from file.
+ *
+ * @param $bytes Amount of bytes to read or whole line (only text files)
+ * @return $data Data read from file
* @throws NullPointerException If the file pointer instance
* is not set by setPointer()
* @throws InvalidResourceException If there is being set
*/
- public function readFromFile () {
+ public function read ($bytes = NULL) {
+ // Some sanity checks
if (is_null($this->getPointer())) {
// Pointer not initialized
throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE);
}
- // Read data from the file pointer and return it
- return $this->read(1024);
- }
-
- /**
- * Reads given amount of bytes from file.
- *
- * @param $bytes Amount of bytes to read
- * @return $data Data read from file
- */
- public function read ($bytes) {
- // Try to read given characters
- $data = fgets($this->getPointer(), $bytes);
-
- // Was this successfull?
- assert(is_string($data));
+ // Is $bytes set?
+ if (is_int($bytes)) {
+ // Try to read given characters
+ $data = fgets($this->getPointer(), $bytes);
+ } else {
+ // Try to read whole line
+ $data = fgets($this->getPointer());
+ }
// Then return it
return $data;