Added the Search API Synonym module to deal specifically with licence and license...
[yaffs-website] / vendor / ezyang / htmlpurifier / extras / FSTools / File.php
1 <?php
2
3 /**
4  * Represents a file in the filesystem
5  *
6  * @warning Be sure to distinguish between get() and write() versus
7  *      read() and put(), the former operates on the entire file, while
8  *      the latter operates on a handle.
9  */
10 class FSTools_File
11 {
12
13     /** Filename of file this object represents */
14     protected $name;
15
16     /** Handle for the file */
17     protected $handle = false;
18
19     /** Instance of FSTools for interfacing with filesystem */
20     protected $fs;
21
22     /**
23      * Filename of file you wish to instantiate.
24      * @note This file need not exist
25      */
26     public function __construct($name, $fs = false)
27     {
28         $this->name = $name;
29         $this->fs = $fs ? $fs : FSTools::singleton();
30     }
31
32     /** Returns the filename of the file. */
33     public function getName() {return $this->name;}
34
35     /** Returns directory of the file without trailing slash */
36     public function getDirectory() {return $this->fs->dirname($this->name);}
37
38     /**
39      * Retrieves the contents of a file
40      * @todo Throw an exception if file doesn't exist
41      */
42     public function get()
43     {
44         return $this->fs->file_get_contents($this->name);
45     }
46
47     /** Writes contents to a file, creates new file if necessary */
48     public function write($contents)
49     {
50         return $this->fs->file_put_contents($this->name, $contents);
51     }
52
53     /** Deletes the file */
54     public function delete()
55     {
56         return $this->fs->unlink($this->name);
57     }
58
59     /** Returns true if file exists and is a file. */
60     public function exists()
61     {
62         return $this->fs->is_file($this->name);
63     }
64
65     /** Returns last file modification time */
66     public function getMTime()
67     {
68         return $this->fs->filemtime($this->name);
69     }
70
71     /**
72      * Chmod a file
73      * @note We ignore errors because of some weird owner trickery due
74      *       to SVN duality
75      */
76     public function chmod($octal_code)
77     {
78         return @$this->fs->chmod($this->name, $octal_code);
79     }
80
81     /** Opens file's handle */
82     public function open($mode)
83     {
84         if ($this->handle) $this->close();
85         $this->handle = $this->fs->fopen($this->name, $mode);
86         return true;
87     }
88
89     /** Closes file's handle */
90     public function close()
91     {
92         if (!$this->handle) return false;
93         $status = $this->fs->fclose($this->handle);
94         $this->handle = false;
95         return $status;
96     }
97
98     /** Retrieves a line from an open file, with optional max length $length */
99     public function getLine($length = null)
100     {
101         if (!$this->handle) $this->open('r');
102         if ($length === null) return $this->fs->fgets($this->handle);
103         else return $this->fs->fgets($this->handle, $length);
104     }
105
106     /** Retrieves a character from an open file */
107     public function getChar()
108     {
109         if (!$this->handle) $this->open('r');
110         return $this->fs->fgetc($this->handle);
111     }
112
113     /** Retrieves an $length bytes of data from an open data */
114     public function read($length)
115     {
116         if (!$this->handle) $this->open('r');
117         return $this->fs->fread($this->handle, $length);
118     }
119
120     /** Writes to an open file */
121     public function put($string)
122     {
123         if (!$this->handle) $this->open('a');
124         return $this->fs->fwrite($this->handle, $string);
125     }
126
127     /** Returns TRUE if the end of the file has been reached */
128     public function eof()
129     {
130         if (!$this->handle) return true;
131         return $this->fs->feof($this->handle);
132     }
133
134     public function __destruct()
135     {
136         if ($this->handle) $this->close();
137     }
138
139 }
140
141 // vim: et sw=4 sts=4