author | Claus Gittinger <cg@exept.de> |
Thu, 11 Mar 1999 00:14:46 +0100 | |
changeset 4042 | 526c79cadbf8 |
parent 3980 | 8f9443e37693 |
child 4298 | 1889f8051f69 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
155 | 3 |
All Rights Reserved |
1 | 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 |
FileStream subclass:#DirectoryStream |
|
1664 | 14 |
instanceVariableNames:'dirPointer readAheadEntry' |
1281
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1133
diff
changeset
|
15 |
classVariableNames:'' |
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1133
diff
changeset
|
16 |
poolDictionaries:'' |
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1133
diff
changeset
|
17 |
category:'Streams-External' |
1 | 18 |
! |
19 |
||
217 | 20 |
!DirectoryStream primitiveDefinitions! |
1 | 21 |
%{ |
437 | 22 |
|
1631 | 23 |
#define UNIX_LIKE |
2609 | 24 |
|
1631 | 25 |
#if defined(WIN32) |
26 |
# undef UNIX_LIKE |
|
27 |
#endif |
|
437 | 28 |
|
2896 | 29 |
#if defined(transputer) |
30 |
# undef UNIX_LIKE |
|
31 |
#endif |
|
32 |
||
1631 | 33 |
#ifdef UNIX_LIKE |
34 |
# include <stdio.h> |
|
35 |
# define _STDIO_H_INCLUDED_ |
|
36 |
||
37 |
# include <errno.h> |
|
38 |
# define _ERRNO_H_INCLUDED_ |
|
10 | 39 |
|
2896 | 40 |
# include <sys/types.h> |
41 |
# include <sys/stat.h> |
|
1631 | 42 |
|
2896 | 43 |
# ifdef HAS_OPENDIR |
44 |
# include <sys/types.h> |
|
45 |
# ifdef NEXT |
|
46 |
# include <sys/dir.h> |
|
47 |
# else |
|
48 |
# include <dirent.h> |
|
1 | 49 |
# endif |
50 |
# endif |
|
223 | 51 |
|
52 |
/* |
|
53 |
* on some systems errno is a macro ... check for it here |
|
54 |
*/ |
|
1631 | 55 |
# ifndef errno |
56 |
extern errno; |
|
57 |
# endif |
|
2896 | 58 |
|
1631 | 59 |
#endif /* UNIX_LIKE */ |
60 |
||
2896 | 61 |
|
1631 | 62 |
#ifdef WIN32 |
63 |
||
64 |
# ifdef i386 |
|
65 |
# define _X86_ |
|
66 |
# endif |
|
67 |
||
68 |
# undef INT |
|
69 |
# undef Array |
|
70 |
# undef Number |
|
71 |
# undef Method |
|
72 |
# undef Point |
|
73 |
# undef Context |
|
74 |
# undef Rectangle |
|
1989 | 75 |
# undef Block |
1631 | 76 |
|
2685 | 77 |
/* # include <types.h> /* */ |
1989 | 78 |
# include <stdarg.h> /* */ |
79 |
# include <windef.h> /* */ |
|
80 |
# include <winbase.h> /* */ |
|
81 |
# include <wingdi.h> /* */ |
|
82 |
# include <winuser.h> /* */ |
|
1631 | 83 |
|
84 |
# ifdef __DEF_Array |
|
85 |
# define Array __DEF_Array |
|
86 |
# endif |
|
87 |
# ifdef __DEF_Number |
|
88 |
# define Number __DEF_Number |
|
89 |
# endif |
|
90 |
# ifdef __DEF_Method |
|
91 |
# define Method __DEF_Method |
|
92 |
# endif |
|
93 |
# ifdef __DEF_Point |
|
94 |
# define Point __DEF_Point |
|
95 |
# endif |
|
1989 | 96 |
# ifdef __DEF_Block |
97 |
# define Block __DEF_Block |
|
98 |
# endif |
|
1631 | 99 |
# ifdef __DEF_Context |
100 |
# define Context __DEF_Context |
|
101 |
# endif |
|
102 |
||
2999
b0e55440cf12
eliminate MKOBJ & MKCP (use MKEXTERNALADDRESS & externalAddressVal)
Claus Gittinger <cg@exept.de>
parents:
2982
diff
changeset
|
103 |
# define __HANDLEVal(o) (HANDLE)__externalAddressVal(o) |
1631 | 104 |
|
105 |
#endif /* WIN32 */ |
|
223 | 106 |
|
1 | 107 |
%} |
206 | 108 |
! ! |
1 | 109 |
|
438 | 110 |
!DirectoryStream primitiveFunctions! |
111 |
%{ |
|
2896 | 112 |
|
113 |
#ifndef HAS_OPENDIR |
|
114 |
# if defined(__VMS__) |
|
115 |
||
3816
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
116 |
# define lib$find_file LIB$FIND_FILE |
438 | 117 |
|
118 |
/* |
|
119 |
** VMS readdir() routines. |
|
120 |
** Written by Rich $alz, <rsalz@bbn.com> in August, 1990. |
|
121 |
** This code has no copyright. |
|
122 |
*/ |
|
123 |
||
124 |
/* 12-NOV-1990 added d_namlen field and special case "." name -GJC@MITECH.COM |
|
125 |
*/ |
|
126 |
||
2896 | 127 |
# ifndef _STDIO_H_INCLUDED_ |
128 |
# include <stdio.h> |
|
129 |
# define _STDIO_H_INCLUDED_ |
|
130 |
# endif |
|
438 | 131 |
|
2896 | 132 |
# ifndef _CTYPE_H_INCLUDED_ |
133 |
# include <ctype.h> |
|
134 |
# define _CTYPE_H_INCLUDED_ |
|
135 |
# endif |
|
438 | 136 |
|
2896 | 137 |
# ifndef _ERRNO_H_INCLUDED_ |
138 |
# include <errno.h> |
|
139 |
# define _ERRNO_H_INCLUDED_ |
|
140 |
# endif |
|
438 | 141 |
|
2896 | 142 |
# ifndef _DESCRIP_H_INCLUDED_ |
143 |
# include <descrip.h> |
|
144 |
# define _DESCRIP_H_INCLUDED_ |
|
145 |
# endif |
|
438 | 146 |
|
2896 | 147 |
# ifndef _RMSDEF_H_INCLUDED_ |
148 |
# include <rmsdef.h> |
|
149 |
# define _RMSDEF_H_INCLUDED_ |
|
150 |
# endif |
|
438 | 151 |
|
152 |
/* |
|
153 |
* actually, the following has to go into dirent.h ... |
|
154 |
*/ |
|
2896 | 155 |
/* BEGIN included dirent.h |
438 | 156 |
* |
157 |
** Header file for VMS readdir() routines. |
|
158 |
** Written by Rich $alz, <rsalz@bbn.com> in August, 1990. |
|
159 |
** This code has no copyright. |
|
160 |
** |
|
161 |
** You must #include <descrip.h> before this file. |
|
162 |
*/ |
|
163 |
||
164 |
/* 12-NOV-1990 added d_namlen field -GJC@MITECH.COM */ |
|
165 |
||
166 |
/* Data structure returned by READDIR(). */ |
|
167 |
struct dirent { |
|
168 |
char d_name[100]; /* File name */ |
|
169 |
int d_namlen; |
|
2957 | 170 |
int vms_verscount; /* Number of versions */ |
171 |
int vms_versions[20]; /* Version numbers */ |
|
438 | 172 |
}; |
173 |
||
174 |
/* Handle returned by opendir(), used by the other routines. You |
|
175 |
* are not supposed to care what's inside this structure. */ |
|
176 |
typedef struct _dirdesc { |
|
177 |
long context; |
|
2957 | 178 |
int vms_wantversions; |
438 | 179 |
char *pattern; |
180 |
struct dirent entry; |
|
181 |
struct dsc$descriptor_s pat; |
|
182 |
} DIR; |
|
183 |
||
184 |
||
185 |
#define rewinddir(dirp) seekdir((dirp), 0L) |
|
186 |
||
187 |
||
188 |
extern DIR *opendir(); |
|
189 |
extern struct dirent *readdir(); |
|
190 |
extern long telldir(); |
|
191 |
extern void seekdir(); |
|
192 |
extern void closedir(); |
|
193 |
extern void vmsreaddirversions(); |
|
194 |
/* |
|
195 |
* END dirent.h |
|
196 |
*/ |
|
2896 | 197 |
#define _DIRENT_H_INCLUDED_ |
438 | 198 |
|
199 |
||
200 |
/* Number of elements in vms_versions array */ |
|
201 |
#define VERSIZE(e) (sizeof e->vms_versions / sizeof e->vms_versions[0]) |
|
202 |
||
203 |
/* Linked in later. */ |
|
204 |
extern char *strrchr(); |
|
205 |
extern char *strcpy(); |
|
206 |
/* Don't need this when all these programs are lumped together. RLD |
|
207 |
extern char *malloc(); |
|
208 |
*/ |
|
209 |
||
210 |
/* |
|
211 |
** Open a directory, return a handle for later use. |
|
212 |
*/ |
|
213 |
DIR * |
|
214 |
opendir(name) |
|
215 |
char *name; |
|
216 |
{ |
|
217 |
DIR *dd; |
|
218 |
||
219 |
/* Get memory for the handle, and the pattern. */ |
|
220 |
if ((dd = (DIR *)malloc(sizeof *dd)) == NULL) { |
|
221 |
errno = ENOMEM; |
|
222 |
return NULL; |
|
223 |
} |
|
224 |
||
225 |
if (strcmp(".",name) == 0) name = ""; |
|
226 |
||
227 |
dd->pattern = malloc((unsigned int)(strlen(name) + sizeof "*.*" + 1)); |
|
228 |
if (dd->pattern == NULL) { |
|
229 |
free((char *)dd); |
|
230 |
errno = ENOMEM; |
|
231 |
return NULL; |
|
232 |
} |
|
233 |
||
234 |
/* Fill in the fields; mainly playing with the descriptor. */ |
|
235 |
(void)sprintf(dd->pattern, "%s*.*", name); |
|
236 |
dd->context = 0; |
|
237 |
dd->vms_wantversions = 0; |
|
238 |
dd->pat.dsc$a_pointer = dd->pattern; |
|
239 |
dd->pat.dsc$w_length = strlen(dd->pattern); |
|
240 |
dd->pat.dsc$b_dtype = DSC$K_DTYPE_T; |
|
241 |
dd->pat.dsc$b_class = DSC$K_CLASS_S; |
|
242 |
||
243 |
return dd; |
|
244 |
} |
|
245 |
||
246 |
/* |
|
247 |
** Set the flag to indicate we want versions or not. |
|
248 |
*/ |
|
249 |
void |
|
250 |
vmsreaddirversions(dd, flag) |
|
251 |
DIR *dd; |
|
252 |
int flag; |
|
253 |
{ |
|
254 |
dd->vms_wantversions = flag; |
|
255 |
} |
|
256 |
||
257 |
/* |
|
258 |
** Free up an opened directory. |
|
259 |
*/ |
|
260 |
void |
|
261 |
closedir(dd) |
|
262 |
DIR *dd; |
|
263 |
{ |
|
264 |
free(dd->pattern); |
|
265 |
free((char *)dd); |
|
266 |
} |
|
267 |
||
268 |
/* |
|
269 |
** Collect all the version numbers for the current file. |
|
270 |
*/ |
|
271 |
static void |
|
272 |
collectversions(dd) |
|
273 |
DIR *dd; |
|
274 |
{ |
|
275 |
struct dsc$descriptor_s pat; |
|
276 |
struct dsc$descriptor_s res; |
|
277 |
struct dirent *e; |
|
278 |
char *p; |
|
279 |
char buff[sizeof dd->entry.d_name]; |
|
280 |
int i; |
|
281 |
char *text; |
|
282 |
long context; |
|
283 |
||
284 |
/* Convenient shorthand. */ |
|
285 |
e = &dd->entry; |
|
286 |
||
287 |
/* Add the version wildcard, ignoring the "*.*" put on before */ |
|
288 |
i = strlen(dd->pattern); |
|
289 |
text = malloc((unsigned int)(i + strlen(e->d_name)+ 2 + 1)); |
|
290 |
if (text == NULL) |
|
291 |
return; |
|
292 |
(void)strcpy(text, dd->pattern); |
|
293 |
(void)sprintf(&text[i - 3], "%s;*", e->d_name); |
|
294 |
||
295 |
/* Set up the pattern descriptor. */ |
|
296 |
pat.dsc$a_pointer = text; |
|
297 |
pat.dsc$w_length = strlen(text); |
|
298 |
pat.dsc$b_dtype = DSC$K_DTYPE_T; |
|
299 |
pat.dsc$b_class = DSC$K_CLASS_S; |
|
300 |
||
301 |
/* Set up result descriptor. */ |
|
302 |
res.dsc$a_pointer = buff; |
|
303 |
res.dsc$w_length = sizeof buff - 2; |
|
304 |
res.dsc$b_dtype = DSC$K_DTYPE_T; |
|
305 |
res.dsc$b_class = DSC$K_CLASS_S; |
|
306 |
||
307 |
/* Read files, collecting versions. */ |
|
308 |
for (context = 0; e->vms_verscount < VERSIZE(e); e->vms_verscount++) { |
|
309 |
if (lib$find_file(&pat, &res, &context) == RMS$_NMF || context == 0) |
|
310 |
break; |
|
311 |
buff[sizeof buff - 1] = '\0'; |
|
312 |
if (p = strchr(buff, ';')) |
|
313 |
e->vms_versions[e->vms_verscount] = atoi(p + 1); |
|
314 |
else |
|
315 |
e->vms_versions[e->vms_verscount] = -1; |
|
316 |
} |
|
317 |
||
318 |
free(text); |
|
319 |
} |
|
320 |
||
321 |
/* |
|
322 |
** Read the next entry from the directory. |
|
323 |
*/ |
|
324 |
struct dirent * |
|
325 |
readdir(dd) |
|
2957 | 326 |
DIR *dd; |
438 | 327 |
{ |
2957 | 328 |
struct dsc$descriptor_s res; |
329 |
char *p; |
|
2982 | 330 |
char buff[sizeof dd->entry.d_name + 10]; |
2957 | 331 |
int i; |
438 | 332 |
|
333 |
/* Set up result descriptor, and get next file. */ |
|
334 |
res.dsc$a_pointer = buff; |
|
335 |
res.dsc$w_length = sizeof buff - 2; |
|
336 |
res.dsc$b_dtype = DSC$K_DTYPE_T; |
|
337 |
res.dsc$b_class = DSC$K_CLASS_S; |
|
338 |
if (lib$find_file(&dd->pat, &res, &dd->context) == RMS$_NMF |
|
339 |
|| dd->context == 0L) |
|
340 |
/* None left... */ |
|
341 |
return NULL; |
|
342 |
||
343 |
/* Force the buffer to end with a NUL. */ |
|
344 |
buff[sizeof buff - 1] = '\0'; |
|
345 |
for (p = buff; !isspace(*p); p++) |
|
346 |
; |
|
347 |
*p = '\0'; |
|
348 |
||
349 |
/* Skip any directory component and just copy the name. */ |
|
350 |
if (p = strchr(buff, ']')) |
|
351 |
(void)strcpy(dd->entry.d_name, p + 1); |
|
352 |
else |
|
353 |
(void)strcpy(dd->entry.d_name, buff); |
|
354 |
||
355 |
/* Clobber the version. */ |
|
356 |
if (p = strchr(dd->entry.d_name, ';')) |
|
357 |
*p = '\0'; |
|
358 |
||
2982 | 359 |
/* claus: empty dirs seems to leave *.* in the buffer ... */ |
360 |
if (strcmp(dd->entry.d_name, "*.*") == 0) { |
|
361 |
return NULL; |
|
362 |
} |
|
363 |
||
438 | 364 |
dd->entry.d_namlen = strlen(dd->entry.d_name); |
365 |
||
366 |
dd->entry.vms_verscount = 0; |
|
367 |
if (dd->vms_wantversions) |
|
368 |
collectversions(dd); |
|
369 |
return &dd->entry; |
|
370 |
} |
|
371 |
||
372 |
/* |
|
373 |
** Return something that can be used in a seekdir later. |
|
374 |
*/ |
|
375 |
long |
|
376 |
telldir(dd) |
|
2957 | 377 |
DIR *dd; |
438 | 378 |
{ |
379 |
return dd->context; |
|
380 |
} |
|
381 |
||
382 |
/* |
|
383 |
** Return to a spot where we used to be. |
|
384 |
*/ |
|
385 |
void |
|
386 |
seekdir(dd, pos) |
|
2957 | 387 |
DIR *dd; |
388 |
long pos; |
|
438 | 389 |
{ |
390 |
dd->context = pos; |
|
391 |
} |
|
392 |
||
2896 | 393 |
# define HAS_OPENDIR |
394 |
||
395 |
# endif /* __VMS__ */ |
|
396 |
#endif /* not HAS_OPENDIR */ |
|
438 | 397 |
%} |
398 |
! ! |
|
399 |
||
613 | 400 |
!DirectoryStream class methodsFor:'documentation'! |
401 |
||
402 |
copyright |
|
403 |
" |
|
404 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
405 |
All Rights Reserved |
|
1 | 406 |
|
613 | 407 |
This software is furnished under a license and may be used |
408 |
only in accordance with the terms of that license and with the |
|
409 |
inclusion of the above copyright notice. This software may not |
|
410 |
be provided or otherwise made available to, or used by, any |
|
411 |
other person. No title to or ownership of the software is |
|
412 |
hereby transferred. |
|
413 |
" |
|
414 |
! |
|
360 | 415 |
|
613 | 416 |
documentation |
417 |
" |
|
418 |
Instances of DirectoryStream allow reading a file-directory, |
|
419 |
as if it was a stream of filenames. |
|
420 |
Basically, its an interface to opendir, readdir and closedir. |
|
2957 | 421 |
Notice: |
422 |
DirectoryStream is an ST/X special; |
|
423 |
for portability, we recommend the use of Filename protocol. |
|
1295 | 424 |
|
425 |
[author:] |
|
1631 | 426 |
Claus Gittinger |
2957 | 427 |
|
428 |
[see also:] |
|
429 |
Filename |
|
613 | 430 |
" |
1 | 431 |
! ! |
432 |
||
433 |
!DirectoryStream class methodsFor:'instance creation'! |
|
434 |
||
435 |
directoryNamed:dirName |
|
436 |
"return a DirectoryStream for directory named dirName, aString" |
|
437 |
||
438 |
|newStream| |
|
439 |
||
440 |
newStream := (self basicNew) pathName:dirName. |
|
441 |
newStream openForReading isNil ifTrue:[^nil]. |
|
442 |
^ newStream |
|
443 |
! ! |
|
444 |
||
445 |
!DirectoryStream methodsFor:'access reading'! |
|
446 |
||
447 |
nextLine |
|
448 |
"return the next filename as a string" |
|
449 |
||
450 |
|prevEntry nextEntry| |
|
451 |
%{ |
|
77 | 452 |
#ifdef HAS_OPENDIR |
1 | 453 |
DIR *d; |
1631 | 454 |
# ifdef NEXT |
1 | 455 |
struct direct *dp; |
1631 | 456 |
# else |
1 | 457 |
struct dirent *dp; |
1631 | 458 |
# endif |
92 | 459 |
OBJ dirP; |
1 | 460 |
|
1281
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1133
diff
changeset
|
461 |
if (__INST(hitEOF) != true && (dirP = __INST(dirPointer)) != nil) { |
1631 | 462 |
__INST(lastErrorNumber) = nil; |
463 |
d = (DIR *)__FILEVal(dirP); |
|
92 | 464 |
|
1631 | 465 |
__BEGIN_INTERRUPTABLE__ |
466 |
do { |
|
2957 | 467 |
do { |
3816
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
468 |
errno = 0; |
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
469 |
dp = readdir(d); |
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
470 |
/* |
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
471 |
* for compatibility with ST-80, |
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
472 |
* skip entries for '.' and '..'. |
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
473 |
* If wanted, these must be added synthetically. |
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
474 |
*/ |
2957 | 475 |
} while (dp && ((strcmp(dp->d_name, ".")==0) || (strcmp(dp->d_name, "..")==0))); |
1631 | 476 |
} while ((dp == NULL) && (errno == EINTR)); |
477 |
__END_INTERRUPTABLE__ |
|
92 | 478 |
|
1631 | 479 |
if (dp != NULL) { |
1688
8a42db1eea60
removed all COMMA_CON / CON_COMMA uses
Claus Gittinger <cg@exept.de>
parents:
1664
diff
changeset
|
480 |
nextEntry = __MKSTRING((char *)(dp->d_name)); |
1631 | 481 |
} else { |
482 |
if (errno) { |
|
483 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
484 |
} else { |
|
485 |
__INST(hitEOF) = true; |
|
486 |
} |
|
1281
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1133
diff
changeset
|
487 |
} |
1 | 488 |
} |
1631 | 489 |
#else /* no HAS_OPENDIR */ |
490 |
# ifdef WIN32 |
|
491 |
HANDLE d; |
|
492 |
WIN32_FIND_DATA data; |
|
493 |
OBJ dirP; |
|
3816
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
494 |
int rslt; |
1631 | 495 |
|
496 |
if (__INST(hitEOF) != true && (dirP = __INST(dirPointer)) != nil) { |
|
497 |
__INST(lastErrorNumber) = nil; |
|
498 |
d = __HANDLEVal(dirP); |
|
499 |
||
3816
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
500 |
# ifdef DO_WRAP_CALLS |
3825 | 501 |
rslt = __STX_API_CALL2( (void*)FindNextFile, (void *)d, (void *)&data ); |
3816
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
502 |
# else |
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
503 |
rslt = FindNextFile(d, &data); |
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
504 |
# endif |
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
505 |
if (rslt) { |
1631 | 506 |
nextEntry = __MKSTRING( data.cFileName ); |
507 |
} else { |
|
508 |
__INST(hitEOF) = true; |
|
509 |
} |
|
510 |
} |
|
511 |
# endif /* WIN32 */ |
|
512 |
#endif /* HAS_OPENDIR */ |
|
155 | 513 |
%}. |
514 |
lastErrorNumber notNil ifTrue:[^ self ioError]. |
|
1664 | 515 |
prevEntry := readAheadEntry. |
516 |
readAheadEntry := nextEntry. |
|
1281
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1133
diff
changeset
|
517 |
prevEntry isNil ifTrue:[^ self pastEnd]. |
1 | 518 |
^ prevEntry |
519 |
! ! |
|
520 |
||
613 | 521 |
!DirectoryStream methodsFor:'closing'! |
522 |
||
523 |
close |
|
524 |
"close the stream - tell operating system" |
|
525 |
||
526 |
dirPointer notNil ifTrue:[ |
|
527 |
Lobby unregister:self. |
|
528 |
self closeFile. |
|
529 |
dirPointer := nil |
|
530 |
] |
|
531 |
! ! |
|
532 |
||
533 |
!DirectoryStream methodsFor:'instance release'! |
|
534 |
||
535 |
closeFile |
|
536 |
"low level close of a directoryStream" |
|
537 |
%{ |
|
538 |
#ifdef HAS_OPENDIR |
|
539 |
OBJ dp; |
|
540 |
||
1133 | 541 |
if ((dp = __INST(dirPointer)) != nil) { |
542 |
__INST(dirPointer) = nil; |
|
613 | 543 |
closedir( (DIR *)(__FILEVal(dp)) ); |
544 |
} |
|
1631 | 545 |
#else |
546 |
# ifdef WIN32 |
|
547 |
OBJ dp; |
|
548 |
||
549 |
if ((dp = __INST(dirPointer)) != nil) { |
|
550 |
__INST(dirPointer) = nil; |
|
551 |
FindClose( __HANDLEVal(dp) ); |
|
552 |
} |
|
553 |
# endif |
|
613 | 554 |
#endif |
555 |
%} |
|
556 |
! ! |
|
557 |
||
1 | 558 |
!DirectoryStream methodsFor:'private'! |
559 |
||
560 |
openForReading |
|
561 |
"open the file for readonly" |
|
562 |
||
1631 | 563 |
|ok entry| |
1 | 564 |
|
565 |
mode := #readonly. |
|
566 |
%{ |
|
77 | 567 |
#ifdef HAS_OPENDIR |
1 | 568 |
DIR *d; |
477
8710aba7876b
oops - making id's real objects requires a store macro
Claus Gittinger <cg@exept.de>
parents:
475
diff
changeset
|
569 |
OBJ path, dp; |
1 | 570 |
|
155 | 571 |
ok = false; |
1133 | 572 |
if (__INST(dirPointer) == nil) { |
573 |
path = __INST(pathName); |
|
155 | 574 |
if (__isString(path)) { |
362 | 575 |
__BEGIN_INTERRUPTABLE__ |
92 | 576 |
errno = 0; |
155 | 577 |
do { |
1133 | 578 |
d = opendir((char *) __stringVal(path)); |
155 | 579 |
} while ((d == NULL) && (errno == EINTR)); |
362 | 580 |
__END_INTERRUPTABLE__ |
92 | 581 |
|
155 | 582 |
if (d == NULL) { |
1133 | 583 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
155 | 584 |
} else { |
2999
b0e55440cf12
eliminate MKOBJ & MKCP (use MKEXTERNALADDRESS & externalAddressVal)
Claus Gittinger <cg@exept.de>
parents:
2982
diff
changeset
|
585 |
__INST(dirPointer) = dp = __MKEXTERNALADDRESS(d); __STORE(self, dp); |
155 | 586 |
ok = true; |
587 |
} |
|
588 |
} |
|
1 | 589 |
} |
1631 | 590 |
#else |
591 |
# ifdef WIN32 |
|
592 |
HANDLE d; |
|
593 |
OBJ path, dp; |
|
594 |
char pattern[512]; |
|
595 |
WIN32_FIND_DATA data; |
|
596 |
||
597 |
ok = false; |
|
598 |
if (__INST(dirPointer) == nil) { |
|
599 |
path = __INST(pathName); |
|
600 |
if (__isString(path)) { |
|
3164
4f37ae9c8961
use strncpy to avoid buffer-overruns
Claus Gittinger <cg@exept.de>
parents:
2999
diff
changeset
|
601 |
int l = __stringSize(path); |
4f37ae9c8961
use strncpy to avoid buffer-overruns
Claus Gittinger <cg@exept.de>
parents:
2999
diff
changeset
|
602 |
|
4f37ae9c8961
use strncpy to avoid buffer-overruns
Claus Gittinger <cg@exept.de>
parents:
2999
diff
changeset
|
603 |
if (l < (sizeof(pattern)-4)) { |
3816
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
604 |
strncpy(pattern, __stringVal(path), l); |
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
605 |
strcpy(pattern+l, "\\*"); |
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
606 |
# ifdef DO_WRAP_CALLS |
3825 | 607 |
d = __STX_API_CALL2( (void*)FindFirstFile, (void *)pattern, (void *)&data ); |
3816
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
608 |
# else |
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
609 |
d = FindFirstFile(pattern, &data); |
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
610 |
# endif |
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
611 |
if (d == INVALID_HANDLE_VALUE) { |
3164
4f37ae9c8961
use strncpy to avoid buffer-overruns
Claus Gittinger <cg@exept.de>
parents:
2999
diff
changeset
|
612 |
__INST(lastErrorNumber) = __MKSMALLINT(GetLastError()); |
3816
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
613 |
} else { |
3164
4f37ae9c8961
use strncpy to avoid buffer-overruns
Claus Gittinger <cg@exept.de>
parents:
2999
diff
changeset
|
614 |
__INST(dirPointer) = dp = __MKEXTERNALADDRESS(d); __STORE(self, dp); |
4f37ae9c8961
use strncpy to avoid buffer-overruns
Claus Gittinger <cg@exept.de>
parents:
2999
diff
changeset
|
615 |
entry = __MKSTRING( data.cFileName ); |
4f37ae9c8961
use strncpy to avoid buffer-overruns
Claus Gittinger <cg@exept.de>
parents:
2999
diff
changeset
|
616 |
ok = true; |
4f37ae9c8961
use strncpy to avoid buffer-overruns
Claus Gittinger <cg@exept.de>
parents:
2999
diff
changeset
|
617 |
} |
1631 | 618 |
} |
619 |
} |
|
620 |
} |
|
621 |
# endif |
|
1 | 622 |
#endif |
155 | 623 |
%}. |
624 |
ok isNil ifTrue:[ |
|
625 |
" |
|
626 |
opendir not avalable - use slower pipe |
|
627 |
" |
|
628 |
^ PipeStream readingFrom:('cd ' , pathName , '; ls -a') |
|
1 | 629 |
]. |
155 | 630 |
|
631 |
(ok == true) ifTrue:[ |
|
632 |
Lobby register:self. |
|
1631 | 633 |
entry isNil ifTrue:[ |
1664 | 634 |
self nextLine. "read 1st entry into readAheadEntry buffer" |
1631 | 635 |
] ifFalse:[ |
1664 | 636 |
readAheadEntry := entry. |
1631 | 637 |
]. |
155 | 638 |
^ self |
1 | 639 |
]. |
155 | 640 |
dirPointer notNil ifTrue:[^ self errorOpen]. |
641 |
lastErrorNumber notNil ifTrue:[^ self openError]. |
|
1 | 642 |
^ nil |
643 |
! |
|
644 |
||
645 |
reOpen |
|
38 | 646 |
"reOpen the stream after image restart" |
647 |
||
2 | 648 |
dirPointer := nil. |
649 |
super reOpen |
|
1 | 650 |
! ! |
651 |
||
652 |
!DirectoryStream methodsFor:'testing'! |
|
653 |
||
654 |
atEnd |
|
655 |
"return true, if position is at end" |
|
656 |
||
1664 | 657 |
^ readAheadEntry == nil |
2945
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
658 |
! |
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
659 |
|
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
660 |
isEmpty |
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
661 |
"test for if the unread portion of the directory stream is empty. |
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
662 |
This query changes the readPointer of the DirectoryStream" |
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
663 |
|
2957 | 664 |
|entry| |
2945
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
665 |
|
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
666 |
[self atEnd] whileFalse:[ |
3816
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
667 |
entry := self nextLine. |
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
668 |
entry asFilename isImplicit ifFalse:[ |
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
669 |
^ false. |
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
670 |
] |
2945
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
671 |
]. |
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
672 |
^ true |
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
673 |
|
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
674 |
|
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
675 |
" |
3816
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
676 |
(DirectoryStream directoryNamed:'/') isEmpty |
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
677 |
(DirectoryStream directoryNamed:'/var/tmp') isEmpty |
2945
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
678 |
" |
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
679 |
|
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
680 |
"Modified: 18.9.1997 / 18:05:31 / stefan" |
1 | 681 |
! ! |
682 |
||
1281
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1133
diff
changeset
|
683 |
!DirectoryStream class methodsFor:'documentation'! |
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1133
diff
changeset
|
684 |
|
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1133
diff
changeset
|
685 |
version |
3980
8f9443e37693
renamed __new() to __STX___new()
Claus Gittinger <cg@exept.de>
parents:
3956
diff
changeset
|
686 |
^ '$Header: /cvs/stx/stx/libbasic/DirectoryStream.st,v 1.46 1999-02-12 14:54:59 cg Exp $' |
1281
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1133
diff
changeset
|
687 |
! ! |