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