| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* $Id: PathTokenizer.php 123 2006-09-14 20:19:08Z mrook $
|
|
|
4 |
*
|
|
|
5 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
6 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
7 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
8 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
9 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
10 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
11 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
12 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
13 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
14 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
15 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
16 |
*
|
|
|
17 |
* This software consists of voluntary contributions made by many individuals
|
|
|
18 |
* and is licensed under the LGPL. For more information please see
|
|
|
19 |
* <http://phing.info>.
|
|
|
20 |
*/
|
|
|
21 |
|
|
|
22 |
|
|
|
23 |
|
|
|
24 |
include_once 'phing/util/StringHelper.php';
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
|
|
|
30 |
* A Path tokenizer takes a path and returns the components that make up
|
|
|
31 |
|
|
|
32 |
* that path.
|
|
|
33 |
|
|
|
34 |
*
|
|
|
35 |
|
|
|
36 |
* The path can use path separators of either ':' or ';' and file separators
|
|
|
37 |
|
|
|
38 |
* of either '/' or '\'.
|
|
|
39 |
|
|
|
40 |
*
|
|
|
41 |
|
|
|
42 |
* @author Hans Lellelid <hans@xmpl.org> (Phing)
|
|
|
43 |
|
|
|
44 |
* @author Conor MacNeill (Ant)
|
|
|
45 |
|
|
|
46 |
* @author Jeff Tulley <jtulley@novell.com> (Ant)
|
|
|
47 |
|
|
|
48 |
* @pacakge phing.util
|
|
|
49 |
|
|
|
50 |
*/
|
|
|
51 |
|
|
|
52 |
class PathTokenizer {
|
|
|
53 |
|
|
|
54 |
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
|
|
|
58 |
* A array of tokens, created by preg_split().
|
|
|
59 |
|
|
|
60 |
*/
|
|
|
61 |
|
|
|
62 |
private $tokens = array();
|
|
|
63 |
|
|
|
64 |
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
|
|
|
68 |
* A string which stores any path components which have been read ahead
|
|
|
69 |
|
|
|
70 |
* due to DOS filesystem compensation.
|
|
|
71 |
|
|
|
72 |
* @var string
|
|
|
73 |
|
|
|
74 |
*/
|
|
|
75 |
|
|
|
76 |
private $lookahead;
|
|
|
77 |
|
|
|
78 |
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
|
|
|
82 |
* Flag to indicate whether or not we are running on a platform with a
|
|
|
83 |
|
|
|
84 |
* DOS style filesystem
|
|
|
85 |
|
|
|
86 |
* @var boolean
|
|
|
87 |
|
|
|
88 |
*/
|
|
|
89 |
|
|
|
90 |
private $dosStyleFilesystem;
|
|
|
91 |
|
|
|
92 |
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
|
|
|
96 |
* Constructs a path tokenizer for the specified path.
|
|
|
97 |
|
|
|
98 |
*
|
|
|
99 |
|
|
|
100 |
* @param path The path to tokenize. Must not be <code>null</code>.
|
|
|
101 |
|
|
|
102 |
*/
|
|
|
103 |
|
|
|
104 |
public function __construct($path) {
|
|
|
105 |
|
|
|
106 |
// on Windows and Unix, we can ignore delimiters and still have
|
|
|
107 |
|
|
|
108 |
// enough information to tokenize correctly.
|
|
|
109 |
|
|
|
110 |
$this->tokens = preg_split("/[;:]/", $path, -1, PREG_SPLIT_NO_EMPTY);
|
|
|
111 |
|
|
|
112 |
$this->dosStyleFilesystem = ( PATH_SEPARATOR == ';');
|
|
|
113 |
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
|
|
|
117 |
|
|
|
118 |
/**
|
|
|
119 |
|
|
|
120 |
* Tests if there are more path elements available from this tokenizer's
|
|
|
121 |
|
|
|
122 |
* path. If this method returns <code>true</code>, then a subsequent call
|
|
|
123 |
|
|
|
124 |
* to nextToken will successfully return a token.
|
|
|
125 |
|
|
|
126 |
*
|
|
|
127 |
|
|
|
128 |
* @return <code>true</code> if and only if there is at least one token
|
|
|
129 |
|
|
|
130 |
* in the string after the current position; <code>false</code> otherwise.
|
|
|
131 |
|
|
|
132 |
*/
|
|
|
133 |
|
|
|
134 |
public function hasMoreTokens() {
|
|
|
135 |
|
|
|
136 |
if ($this->lookahead !== null) {
|
|
|
137 |
|
|
|
138 |
return true;
|
|
|
139 |
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
return !empty($this->tokens);
|
|
|
143 |
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
|
|
|
147 |
|
|
|
148 |
/**
|
|
|
149 |
|
|
|
150 |
* Returns the next path element from this tokenizer.
|
|
|
151 |
|
|
|
152 |
*
|
|
|
153 |
|
|
|
154 |
* @return the next path element from this tokenizer.
|
|
|
155 |
|
|
|
156 |
*
|
|
|
157 |
|
|
|
158 |
* @throws Exception if there are no more elements in this tokenizer's path.
|
|
|
159 |
|
|
|
160 |
*/
|
|
|
161 |
|
|
|
162 |
public function nextToken() {
|
|
|
163 |
|
|
|
164 |
|
|
|
165 |
|
|
|
166 |
if ($this->lookahead !== null) {
|
|
|
167 |
|
|
|
168 |
$token = $this->lookahead;
|
|
|
169 |
|
|
|
170 |
$this->lookahead = null;
|
|
|
171 |
|
|
|
172 |
} else {
|
|
|
173 |
|
|
|
174 |
$token = trim(array_shift($this->tokens));
|
|
|
175 |
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
|
|
|
179 |
|
|
|
180 |
|
|
|
181 |
|
|
|
182 |
if (strlen($token) === 1 && Character::isLetter($token{0})
|
|
|
183 |
|
|
|
184 |
&& $this->dosStyleFilesystem
|
|
|
185 |
|
|
|
186 |
&& !empty($this->tokens)) {
|
|
|
187 |
|
|
|
188 |
// we are on a dos style system so this path could be a drive
|
|
|
189 |
|
|
|
190 |
// spec. We look at the next token
|
|
|
191 |
|
|
|
192 |
$nextToken = trim(array_shift($this->tokens));
|
|
|
193 |
|
|
|
194 |
if (StringHelper::startsWith('\\', $nextToken) || StringHelper::startsWith('/', $nextToken)) {
|
|
|
195 |
|
|
|
196 |
// we know we are on a DOS style platform and the next path
|
|
|
197 |
|
|
|
198 |
// starts with a slash or backslash, so we know this is a
|
|
|
199 |
|
|
|
200 |
// drive spec
|
|
|
201 |
|
|
|
202 |
$token .= ':' . $nextToken;
|
|
|
203 |
|
|
|
204 |
} else {
|
|
|
205 |
|
|
|
206 |
// store the token just read for next time
|
|
|
207 |
|
|
|
208 |
$this->lookahead = $nextToken;
|
|
|
209 |
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
|
|
|
215 |
|
|
|
216 |
return $token;
|
|
|
217 |
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
|
|
|
221 |
|
|
|
222 |
/**
|
|
|
223 |
|
|
|
224 |
* Non StringTokenizer function, that indicates whether the specified path is contained in loaded tokens.
|
|
|
225 |
|
|
|
226 |
* We can do this easily because in PHP implimentation we're using arrays.
|
|
|
227 |
|
|
|
228 |
* @param string $path path to search for.
|
|
|
229 |
|
|
|
230 |
* @return boolean
|
|
|
231 |
|
|
|
232 |
*/
|
|
|
233 |
|
|
|
234 |
public function contains($path) {
|
|
|
235 |
|
|
|
236 |
return in_array($path, $this->tokens, true);
|
|
|
237 |
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
|
|
|
241 |
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
|
|
|
245 |
|