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