Ignore:
Timestamp:
Apr 12, 2010, 5:56:17 PM (15 years ago)
Author:
Pieter Naber
Message:

Adding a lot of checks for the variables in the status and the location file. Added even more comments everywhere

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/inc/FileHandler.class.php

    r7661 r7720  
    77
    88class FileHandler {
    9         private $file;
     9        private $filename;
     10        private $handle;
     11       
     12        /*
     13         * Function: __construct (constructor)
     14         * Description: Handling with a node file
     15         * Parameters: string $filename, string $mode
     16         * Returns: true is we've succesfully opened the file, otherwise false
     17         */
     18        public function __construct($filename, $mode) {
     19                $this->filename = $filename;
     20
     21                try {
     22                        trigger_log(SYSLOG_DEBUG, 'Opening the file "' . $this->filename . '"', __FILE__, __LINE__);
     23                        $this->handle = fopen($this->filename, $mode);
     24                } catch (Exception $err) {
     25                        trigger_log(SYSLOG_ERR, 'Reading from file "' . $this->filename . '" failed', __FILE__, __LINE__);
     26                        return false;
     27                }
     28
     29                return true;
     30        }
    1031
    1132        /*
    12          * Function: __construct (constructor)
    13          * Parameters: string $filename
    14          * Function: Handling with a node file
     33         * Function: read
     34         * Description: Try to read the file we've opened in the constructor
     35         * Parameters: -
     36         * Returns: The content of the file if we can read from the file, otherwise false
    1537         */
    16         public function __construct($filename) {
     38        public function read() {
     39                $file = '';
     40
    1741                try {
    18                         trigger_log(SYSLOG_DEBUG, 'Opening the file "' . $filename . '"', __FILE__, __LINE__);
    19                         $handle = fopen($filename, 'r');
     42                        trigger_log(SYSLOG_DEBUG, 'Reading from file "' . $this->filename . '"', __FILE__, __LINE__);
     43                        $file = stream_get_contents($this->handle);
     44                } catch (Exception $err) {
     45                        trigger_log(SYSLOG_ERR, 'Reading from file "' . $this->filename . '" failed', __FILE__, __LINE__);
     46                        return false;
     47                }
    2048
    21                         trigger_log(SYSLOG_DEBUG, 'Reading from file "' . $filename . '"', __FILE__, __LINE__);
    22                         $this->file = '';
    23                         while (!feof($handle)) {
    24                                 $this->file .= fread($handle, 8192);
    25                         }
    26 
    27                         trigger_log(SYSLOG_DEBUG, 'Closing file "' . $filename . '"', __FILE__, __LINE__);
    28                         fclose($handle);
    29 
    30                         trigger_log(SYSLOG_INFO, 'Reading from file "' . $filename . '" is done', __FILE__, __LINE__);
    31                 } catch (Exception $err) {
    32                         trigger_log(SYSLOG_ERR, 'Reading from file "' . $filename . '" failed', __FILE__, __LINE__);
    33                 }
     49                return $file;
    3450        }
    3551
    36         public function getFile() {
    37                 return $this->file;
     52        /*
     53         * Function: write
     54         * Description: Try to write to the file we've opened in the constructor
     55         * Parameters: string $data
     56         * Returns: true if we can write to the file, otherwise false
     57         */
     58        public function write($data) {
     59                // TODO David: Make write function
     60
     61                return false;
     62        }
     63
     64        public function __destruct() {
     65                trigger_log(SYSLOG_DEBUG, 'Closing file "' . $this->filename . '"', __FILE__, __LINE__);
     66                fclose($this->handle);
    3867        }
    3968}
Note: See TracChangeset for help on using the changeset viewer.