author | Claus Gittinger <cg@exept.de> |
Mon, 17 Aug 1998 10:06:01 +0200 | |
changeset 679 | ce7aed726aff |
parent 620 | 2a40beccf337 |
child 695 | 368ddb719976 |
permissions | -rw-r--r-- |
583 | 1 |
" |
2 |
COPYRIGHT (c) 1997 by eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
13 |
||
14 |
||
15 |
Object subclass:#DirectoryContents |
|
16 |
instanceVariableNames:'directory timeStamp names files types' |
|
17 |
classVariableNames:'List' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'System-Support' |
|
20 |
! |
|
21 |
||
22 |
!DirectoryContents class methodsFor:'documentation'! |
|
23 |
||
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1997 by eXept Software AG |
|
27 |
All Rights Reserved |
|
28 |
||
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
||
37 |
||
38 |
! |
|
39 |
||
40 |
documentation |
|
41 |
" |
|
42 |
DirectoryContents provides a cached view onto a fileDirectory. |
|
43 |
||
44 |
||
45 |
Notice: |
|
46 |
This class is not available in other ST-systems; |
|
47 |
Applications using it may not be portable. |
|
48 |
||
49 |
[author:] |
|
50 |
Claus Atzkern |
|
51 |
||
52 |
[see also:] |
|
53 |
Filename |
|
54 |
FileStream DirectoryStream OperatingSystem |
|
55 |
" |
|
56 |
||
57 |
! ! |
|
58 |
||
59 |
!DirectoryContents class methodsFor:'accessing'! |
|
60 |
||
61 |
directoryNamed:aDirectoryName |
|
62 |
"returns the DirectoryContents for a directory named |
|
63 |
aDirectoryName, aString or Filename |
|
64 |
" |
|
65 |
|directory dir max| |
|
66 |
||
67 |
directory := aDirectoryName asFilename. |
|
68 |
||
620 | 69 |
directory exists ifFalse:[ |
70 |
^ nil |
|
71 |
]. |
|
72 |
||
583 | 73 |
List isNil ifTrue:[ |
74 |
List := OrderedCollection new |
|
75 |
] ifFalse:[ |
|
76 |
dir := self directoryAt:directory. |
|
77 |
||
78 |
dir notNil ifTrue:[ |
|
79 |
^ dir |
|
80 |
]. |
|
81 |
max := self maxCachedDirectories. |
|
82 |
||
83 |
List size > max ifTrue:[ |
|
84 |
List := List select:[:aDir| |
|
85 |
(aDir size > 32 and:[aDir isObsolete not]) |
|
86 |
]. |
|
87 |
List size > max ifTrue:[ |
|
88 |
List removeFirst |
|
89 |
] |
|
90 |
] |
|
91 |
]. |
|
92 |
^ List add:(self new directory:directory) |
|
620 | 93 |
|
583 | 94 |
! ! |
95 |
||
616
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
96 |
!DirectoryContents class methodsFor:'cache flushing'! |
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
97 |
|
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
98 |
preSnapshot |
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
99 |
"flush list of rememebred directory contents' before saving an image" |
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
100 |
|
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
101 |
List := nil |
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
102 |
|
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
103 |
"Created: / 18.2.1998 / 18:17:05 / cg" |
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
104 |
! ! |
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
105 |
|
583 | 106 |
!DirectoryContents class methodsFor:'constants'! |
107 |
||
108 |
maxCachedDirectories |
|
109 |
"returns number of maximum cached directories |
|
110 |
" |
|
616
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
111 |
^ 20 |
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
112 |
|
cb0377b446bd
flush cachedDirectory list when saving a snapshot
Claus Gittinger <cg@exept.de>
parents:
583
diff
changeset
|
113 |
"Modified: / 25.2.1998 / 19:56:24 / cg" |
583 | 114 |
! ! |
115 |
||
116 |
!DirectoryContents class methodsFor:'private'! |
|
117 |
||
118 |
directoryAt:aFilename |
|
119 |
"checks whether directory already exists and is valid. |
|
120 |
If true the directory is returned otherwise nil |
|
121 |
" |
|
122 |
|index directory| |
|
123 |
||
124 |
List notNil ifTrue:[ |
|
125 |
index := List findFirst:[:d| d directory = aFilename ]. |
|
126 |
||
127 |
index ~~ 0 ifTrue:[ |
|
128 |
directory := List at:index. |
|
129 |
||
130 |
directory isObsolete ifFalse:[ |
|
131 |
^ directory |
|
132 |
]. |
|
133 |
List removeAtIndex:index |
|
134 |
] |
|
135 |
]. |
|
136 |
^ nil |
|
137 |
! ! |
|
138 |
||
139 |
!DirectoryContents class methodsFor:'queries'! |
|
140 |
||
141 |
directoryNamed:aDirectoryName detect:aTwoArgBlock |
|
142 |
"evaluate the block, [:filename :isDirectory] on the directory |
|
143 |
contents of a directory named aDirectoryName, until the block |
|
144 |
returns true. If nothing detected false is returned |
|
145 |
" |
|
146 |
|directory name dir| |
|
147 |
||
148 |
directory := aDirectoryName asFilename. |
|
149 |
||
620 | 150 |
directory exists ifFalse:[ |
151 |
^ false |
|
152 |
]. |
|
153 |
||
583 | 154 |
(dir := self directoryAt:directory) notNil ifTrue:[ |
155 |
dir contentsDo:[:aFile :isDir| |
|
156 |
(aTwoArgBlock value:aFile value:isDir) ifTrue:[^ true] |
|
157 |
]. |
|
158 |
^ false |
|
159 |
]. |
|
160 |
dir := DirectoryStream directoryNamed:(directory pathName). |
|
161 |
||
162 |
dir isNil ifFalse:[ |
|
163 |
[ |
|
164 |
[dir atEnd] whileFalse:[ |
|
165 |
(name := dir nextLine) notNil ifTrue:[ |
|
166 |
name := directory construct:name. |
|
167 |
||
168 |
(aTwoArgBlock value:name value:(name isDirectory)) ifTrue:[ |
|
169 |
^ true |
|
170 |
] |
|
171 |
] |
|
172 |
]. |
|
173 |
] valueNowOrOnUnwindDo:[ |
|
174 |
dir close. |
|
175 |
] |
|
176 |
]. |
|
177 |
^ false |
|
178 |
! ! |
|
179 |
||
180 |
!DirectoryContents methodsFor:'accessing'! |
|
181 |
||
182 |
directory |
|
183 |
"returns the directoy name as Filename |
|
184 |
" |
|
185 |
^ directory |
|
186 |
! |
|
187 |
||
188 |
modificationTime |
|
189 |
"get the last modification time of the directory |
|
190 |
" |
|
191 |
^ timeStamp |
|
192 |
! |
|
193 |
||
194 |
size |
|
195 |
"get number of files including directories in the directory |
|
196 |
" |
|
197 |
^ names size |
|
198 |
! ! |
|
199 |
||
200 |
!DirectoryContents methodsFor:'enumerating'! |
|
201 |
||
202 |
contentsAndBaseNamesDo:aThreeArgBlock |
|
203 |
"evaluate the block on each file; the argument to the block is the |
|
204 |
filename, the baseName and true in case of a directory |
|
205 |
block arguments: [:fileName :aBaseName :isDirectory| |
|
206 |
" |
|
207 |
self indiciesDo:[:i| |
|
208 |
aThreeArgBlock value:(files at:i) value:(names at:i) value:(types at:i) |
|
209 |
]. |
|
210 |
! |
|
211 |
||
212 |
contentsDo:aTwoArgBlock |
|
213 |
"evaluate the block on each file; the argument to the block is the |
|
214 |
filename and true in case of a directory |
|
215 |
block arguments: [:fileName :isDirectory| |
|
216 |
" |
|
217 |
self indiciesDo:[:i| |
|
218 |
aTwoArgBlock value:(files at:i) value:(types at:i) |
|
219 |
]. |
|
220 |
! |
|
221 |
||
222 |
directoriesAndBasenamesDo:aTwoArgBlock |
|
223 |
"evaluate block on each directory; a Filename and Basename. |
|
224 |
The directories are sorted |
|
225 |
" |
|
226 |
self indiciesDo:[:i| |
|
227 |
(types at:i) ifTrue:[ |
|
228 |
aTwoArgBlock value:(files at:i) value:(names at:i) |
|
229 |
] |
|
230 |
] |
|
231 |
! |
|
232 |
||
233 |
directoriesDo:aOneArgBlock |
|
234 |
"evaluate block on each directory; a Filename. The directories are sorted |
|
235 |
" |
|
236 |
self indiciesDo:[:i| |
|
237 |
(types at:i) ifTrue:[ |
|
238 |
aOneArgBlock value:(files at:i) |
|
239 |
] |
|
240 |
] |
|
241 |
! |
|
242 |
||
243 |
filesAndBasenamesDo:aTwoArgBlock |
|
244 |
"evaluate block on each file; a Filename and a Basename. |
|
245 |
The files are sorted. |
|
246 |
" |
|
247 |
self indiciesDo:[:i| |
|
248 |
(types at:i) ifFalse:[ |
|
249 |
aTwoArgBlock value:(files at:i) value:(names at:i) |
|
250 |
] |
|
251 |
] |
|
252 |
! |
|
253 |
||
254 |
filesDo:aOneArgBlock |
|
255 |
"evaluate block on each file; a Filename. The files are sorted. |
|
256 |
" |
|
257 |
self indiciesDo:[:i| |
|
258 |
(types at:i) ifFalse:[ |
|
259 |
aOneArgBlock value:(files at:i) |
|
260 |
] |
|
261 |
] |
|
262 |
! ! |
|
263 |
||
264 |
!DirectoryContents methodsFor:'instance creation'! |
|
265 |
||
266 |
directory:aFilename |
|
267 |
"instance creation; setup attributes |
|
268 |
" |
|
620 | 269 |
|contents| |
583 | 270 |
|
271 |
directory := aFilename asFilename. |
|
272 |
timeStamp := directory modificationTime. |
|
620 | 273 |
contents := directory directoryContents. |
274 |
names := contents notNil ifTrue:[contents sort] ifFalse:[#()]. |
|
583 | 275 |
|
276 |
! ! |
|
277 |
||
278 |
!DirectoryContents methodsFor:'private'! |
|
279 |
||
280 |
indiciesDo:aOneArgBlock |
|
281 |
"check whether files and types are set; than run from |
|
282 |
1 to size evaluating the block with the index |
|
283 |
" |
|
284 |
|file |
|
285 |
size "{ Class:SmallInteger }"| |
|
286 |
||
287 |
size := names size. |
|
288 |
||
289 |
types isNil ifTrue:[ |
|
290 |
types := Array new:size. |
|
291 |
files := Array new:size. |
|
292 |
||
293 |
1 to:size do:[:i| |
|
294 |
file := directory construct:(names at:i). |
|
295 |
types at:i put:(file isDirectory). |
|
296 |
files at:i put:file |
|
297 |
] |
|
298 |
]. |
|
299 |
1 to:size do:[:i| aOneArgBlock value:i] |
|
300 |
! ! |
|
301 |
||
302 |
!DirectoryContents methodsFor:'queries'! |
|
303 |
||
304 |
isEmpty |
|
305 |
"retuirns true if directory is empty |
|
306 |
" |
|
307 |
^ names size == 0 |
|
308 |
! |
|
309 |
||
310 |
isObsolete |
|
311 |
"returns true if directory contents is obsolete |
|
312 |
" |
|
313 |
^ ( directory exists not |
|
679 | 314 |
or:[timeStamp notNil and:[timeStamp < directory modificationTime]] |
583 | 315 |
) |
679 | 316 |
|
317 |
"Modified: / 17.8.1998 / 10:05:23 / cg" |
|
583 | 318 |
! |
319 |
||
320 |
notEmpty |
|
321 |
"returns true if directory is not empty |
|
322 |
" |
|
323 |
^ names size ~~ 0 |
|
324 |
! ! |
|
325 |
||
326 |
!DirectoryContents class methodsFor:'documentation'! |
|
327 |
||
328 |
version |
|
679 | 329 |
^ '$Header: /cvs/stx/stx/libbasic2/DirectoryContents.st,v 1.4 1998-08-17 08:06:01 cg Exp $' |
583 | 330 |
! ! |