author | Stefan Vogel <sv@exept.de> |
Wed, 08 May 2019 15:04:54 +0200 | |
changeset 24121 | d0148c842873 |
parent 23542 | 24f9687dbadc |
child 24234 | 524b83156e8e |
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 |
" |
|
7826 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
17287 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
1 | 16 |
FileStream subclass:#DirectoryStream |
1664 | 17 |
instanceVariableNames:'dirPointer readAheadEntry' |
1281
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1133
diff
changeset
|
18 |
classVariableNames:'' |
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1133
diff
changeset
|
19 |
poolDictionaries:'' |
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1133
diff
changeset
|
20 |
category:'Streams-External' |
1 | 21 |
! |
22 |
||
217 | 23 |
!DirectoryStream primitiveDefinitions! |
1 | 24 |
%{ |
17076 | 25 |
#include "stxOSDefs.h" |
26 |
||
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19651
diff
changeset
|
27 |
#if defined(__win32__) |
1631 | 28 |
# undef UNIX_LIKE |
16297 | 29 |
#else |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19651
diff
changeset
|
30 |
# if defined(__transputer__) |
16297 | 31 |
# undef UNIX_LIKE |
32 |
# else |
|
33 |
# define UNIX_LIKE |
|
34 |
# endif |
|
2896 | 35 |
#endif |
36 |
||
1631 | 37 |
#ifdef UNIX_LIKE |
7955
f3a24f9588ee
64bit readdir bug workaround
Claus Gittinger <cg@exept.de>
parents:
7826
diff
changeset
|
38 |
# if defined(__GLIBC__) && (__GLIBC__ == 2) |
f3a24f9588ee
64bit readdir bug workaround
Claus Gittinger <cg@exept.de>
parents:
7826
diff
changeset
|
39 |
# if defined(__GLIBC_MINOR__) && (__GLIBC_MINOR__ == 1) |
f3a24f9588ee
64bit readdir bug workaround
Claus Gittinger <cg@exept.de>
parents:
7826
diff
changeset
|
40 |
/* there is a bug there, preventing 64bit readdir. */ |
f3a24f9588ee
64bit readdir bug workaround
Claus Gittinger <cg@exept.de>
parents:
7826
diff
changeset
|
41 |
# undef __USE_FILE_OFFSET64 |
f3a24f9588ee
64bit readdir bug workaround
Claus Gittinger <cg@exept.de>
parents:
7826
diff
changeset
|
42 |
# undef __USE_LARGEFILE64 |
f3a24f9588ee
64bit readdir bug workaround
Claus Gittinger <cg@exept.de>
parents:
7826
diff
changeset
|
43 |
# endif |
f3a24f9588ee
64bit readdir bug workaround
Claus Gittinger <cg@exept.de>
parents:
7826
diff
changeset
|
44 |
# endif |
f3a24f9588ee
64bit readdir bug workaround
Claus Gittinger <cg@exept.de>
parents:
7826
diff
changeset
|
45 |
|
1631 | 46 |
# include <stdio.h> |
47 |
# define _STDIO_H_INCLUDED_ |
|
48 |
||
49 |
# include <errno.h> |
|
50 |
# define _ERRNO_H_INCLUDED_ |
|
10 | 51 |
|
2896 | 52 |
# include <sys/types.h> |
53 |
# include <sys/stat.h> |
|
1631 | 54 |
|
2896 | 55 |
# ifdef HAS_OPENDIR |
56 |
# include <sys/types.h> |
|
22578 | 57 |
# ifdef __NEXT__ |
2896 | 58 |
# include <sys/dir.h> |
8881
53e94478133f
STORE macro fix (gcc3.4 bug)
Claus Gittinger <cg@exept.de>
parents:
8775
diff
changeset
|
59 |
# define DIRENT_STRUCT struct direct |
2896 | 60 |
# else |
61 |
# include <dirent.h> |
|
8881
53e94478133f
STORE macro fix (gcc3.4 bug)
Claus Gittinger <cg@exept.de>
parents:
8775
diff
changeset
|
62 |
# define DIRENT_STRUCT struct dirent |
1 | 63 |
# endif |
64 |
# endif |
|
223 | 65 |
|
66 |
/* |
|
67 |
* on some systems errno is a macro ... check for it here |
|
68 |
*/ |
|
1631 | 69 |
# ifndef errno |
70 |
extern errno; |
|
71 |
# endif |
|
2896 | 72 |
|
1631 | 73 |
#endif /* UNIX_LIKE */ |
74 |
||
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19651
diff
changeset
|
75 |
#ifdef __win32__ |
1631 | 76 |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8881
diff
changeset
|
77 |
# ifdef __i386__ |
1631 | 78 |
# define _X86_ |
79 |
# endif |
|
80 |
||
81 |
# undef INT |
|
14644 | 82 |
# undef UINT |
1631 | 83 |
# undef Array |
84 |
# undef Number |
|
85 |
# undef Method |
|
86 |
# undef Point |
|
87 |
# undef Context |
|
88 |
# undef Rectangle |
|
1989 | 89 |
# undef Block |
4298 | 90 |
# undef Time |
91 |
# undef Date |
|
10342 | 92 |
# undef Message |
93 |
# undef Process |
|
94 |
# undef Processor |
|
95 |
# undef INT |
|
1631 | 96 |
|
2685 | 97 |
/* # include <types.h> /* */ |
1989 | 98 |
# include <stdarg.h> /* */ |
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4526
diff
changeset
|
99 |
# include <errno.h> |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4526
diff
changeset
|
100 |
# define _ERRNO_H_INCLUDED_ |
1989 | 101 |
# include <windef.h> /* */ |
102 |
# include <winbase.h> /* */ |
|
4298 | 103 |
/* # include <wingdi.h> /* */ |
104 |
/* # include <winuser.h> /* */ |
|
1631 | 105 |
|
106 |
# ifdef __DEF_Array |
|
107 |
# define Array __DEF_Array |
|
108 |
# endif |
|
109 |
# ifdef __DEF_Number |
|
110 |
# define Number __DEF_Number |
|
111 |
# endif |
|
112 |
# ifdef __DEF_Method |
|
113 |
# define Method __DEF_Method |
|
114 |
# endif |
|
115 |
# ifdef __DEF_Point |
|
116 |
# define Point __DEF_Point |
|
117 |
# endif |
|
1989 | 118 |
# ifdef __DEF_Block |
119 |
# define Block __DEF_Block |
|
120 |
# endif |
|
1631 | 121 |
# ifdef __DEF_Context |
122 |
# define Context __DEF_Context |
|
123 |
# endif |
|
4298 | 124 |
# ifdef __DEF_Date |
125 |
# define Date __DEF_Date |
|
126 |
# endif |
|
127 |
# ifdef __DEF_Time |
|
128 |
# define Time __DEF_Time |
|
129 |
# endif |
|
10342 | 130 |
# ifdef __DEF_Message |
131 |
# define Message __DEF_Message |
|
132 |
# endif |
|
133 |
# ifdef __DEF_Process |
|
134 |
# define Process __DEF_Process |
|
135 |
# endif |
|
136 |
# ifdef __DEF_Processor |
|
137 |
# define Processor __DEF_Processor |
|
138 |
# endif |
|
14644 | 139 |
|
140 |
# define INT STX_INT |
|
141 |
# define UINT STX_UINT |
|
1631 | 142 |
|
2999
b0e55440cf12
eliminate MKOBJ & MKCP (use MKEXTERNALADDRESS & externalAddressVal)
Claus Gittinger <cg@exept.de>
parents:
2982
diff
changeset
|
143 |
# define __HANDLEVal(o) (HANDLE)__externalAddressVal(o) |
19651
59a06f7e5116
avoid largeinteger arithmetic in osTime handling
Claus Gittinger <cg@exept.de>
parents:
18780
diff
changeset
|
144 |
// extern OBJ FileTimeToOsTime(); |
59a06f7e5116
avoid largeinteger arithmetic in osTime handling
Claus Gittinger <cg@exept.de>
parents:
18780
diff
changeset
|
145 |
extern OBJ FileTimeToOsTime1970(); |
20488 | 146 |
#endif /* __win32__ */ |
1631 | 147 |
|
12714 | 148 |
#include "stxOSDefs.h" |
1 | 149 |
%} |
206 | 150 |
! ! |
1 | 151 |
|
438 | 152 |
!DirectoryStream primitiveFunctions! |
153 |
%{ |
|
2896 | 154 |
|
155 |
#ifndef HAS_OPENDIR |
|
7955
f3a24f9588ee
64bit readdir bug workaround
Claus Gittinger <cg@exept.de>
parents:
7826
diff
changeset
|
156 |
|
2896 | 157 |
# if defined(__VMS__) |
158 |
||
3816
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
159 |
# define lib$find_file LIB$FIND_FILE |
438 | 160 |
|
161 |
/* |
|
162 |
** VMS readdir() routines. |
|
163 |
** Written by Rich $alz, <rsalz@bbn.com> in August, 1990. |
|
164 |
** This code has no copyright. |
|
165 |
*/ |
|
166 |
||
8881
53e94478133f
STORE macro fix (gcc3.4 bug)
Claus Gittinger <cg@exept.de>
parents:
8775
diff
changeset
|
167 |
/* 12-NOV-1990 added d_namlen field and special case "." name -GJC@MITECH.COM |
438 | 168 |
*/ |
169 |
||
2896 | 170 |
# ifndef _STDIO_H_INCLUDED_ |
171 |
# include <stdio.h> |
|
172 |
# define _STDIO_H_INCLUDED_ |
|
173 |
# endif |
|
438 | 174 |
|
2896 | 175 |
# ifndef _CTYPE_H_INCLUDED_ |
176 |
# include <ctype.h> |
|
177 |
# define _CTYPE_H_INCLUDED_ |
|
178 |
# endif |
|
438 | 179 |
|
2896 | 180 |
# ifndef _ERRNO_H_INCLUDED_ |
181 |
# include <errno.h> |
|
182 |
# define _ERRNO_H_INCLUDED_ |
|
183 |
# endif |
|
438 | 184 |
|
2896 | 185 |
# ifndef _DESCRIP_H_INCLUDED_ |
186 |
# include <descrip.h> |
|
187 |
# define _DESCRIP_H_INCLUDED_ |
|
188 |
# endif |
|
438 | 189 |
|
2896 | 190 |
# ifndef _RMSDEF_H_INCLUDED_ |
191 |
# include <rmsdef.h> |
|
192 |
# define _RMSDEF_H_INCLUDED_ |
|
193 |
# endif |
|
438 | 194 |
|
195 |
/* |
|
196 |
* actually, the following has to go into dirent.h ... |
|
197 |
*/ |
|
2896 | 198 |
/* BEGIN included dirent.h |
438 | 199 |
* |
200 |
** Header file for VMS readdir() routines. |
|
201 |
** Written by Rich $alz, <rsalz@bbn.com> in August, 1990. |
|
202 |
** This code has no copyright. |
|
203 |
** |
|
204 |
** You must #include <descrip.h> before this file. |
|
205 |
*/ |
|
206 |
||
207 |
/* 12-NOV-1990 added d_namlen field -GJC@MITECH.COM */ |
|
208 |
||
209 |
/* Data structure returned by READDIR(). */ |
|
210 |
struct dirent { |
|
211 |
char d_name[100]; /* File name */ |
|
212 |
int d_namlen; |
|
2957 | 213 |
int vms_verscount; /* Number of versions */ |
214 |
int vms_versions[20]; /* Version numbers */ |
|
438 | 215 |
}; |
216 |
||
217 |
/* Handle returned by opendir(), used by the other routines. You |
|
218 |
* are not supposed to care what's inside this structure. */ |
|
219 |
typedef struct _dirdesc { |
|
220 |
long context; |
|
2957 | 221 |
int vms_wantversions; |
438 | 222 |
char *pattern; |
223 |
struct dirent entry; |
|
224 |
struct dsc$descriptor_s pat; |
|
225 |
} DIR; |
|
226 |
||
227 |
||
228 |
#define rewinddir(dirp) seekdir((dirp), 0L) |
|
229 |
||
230 |
||
231 |
extern DIR *opendir(); |
|
232 |
extern struct dirent *readdir(); |
|
233 |
extern long telldir(); |
|
234 |
extern void seekdir(); |
|
235 |
extern void closedir(); |
|
236 |
extern void vmsreaddirversions(); |
|
237 |
/* |
|
238 |
* END dirent.h |
|
239 |
*/ |
|
2896 | 240 |
#define _DIRENT_H_INCLUDED_ |
438 | 241 |
|
242 |
||
243 |
/* Number of elements in vms_versions array */ |
|
244 |
#define VERSIZE(e) (sizeof e->vms_versions / sizeof e->vms_versions[0]) |
|
245 |
||
246 |
/* Linked in later. */ |
|
247 |
extern char *strrchr(); |
|
248 |
extern char *strcpy(); |
|
249 |
/* Don't need this when all these programs are lumped together. RLD |
|
250 |
extern char *malloc(); |
|
251 |
*/ |
|
252 |
||
253 |
/* |
|
254 |
** Open a directory, return a handle for later use. |
|
255 |
*/ |
|
256 |
DIR * |
|
257 |
opendir(name) |
|
258 |
char *name; |
|
259 |
{ |
|
260 |
DIR *dd; |
|
261 |
||
262 |
/* Get memory for the handle, and the pattern. */ |
|
263 |
if ((dd = (DIR *)malloc(sizeof *dd)) == NULL) { |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4526
diff
changeset
|
264 |
__threadErrno = ENOMEM; |
438 | 265 |
return NULL; |
266 |
} |
|
267 |
||
268 |
if (strcmp(".",name) == 0) name = ""; |
|
269 |
||
270 |
dd->pattern = malloc((unsigned int)(strlen(name) + sizeof "*.*" + 1)); |
|
271 |
if (dd->pattern == NULL) { |
|
272 |
free((char *)dd); |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4526
diff
changeset
|
273 |
__threadErrno = ENOMEM; |
438 | 274 |
return NULL; |
275 |
} |
|
276 |
||
277 |
/* Fill in the fields; mainly playing with the descriptor. */ |
|
278 |
(void)sprintf(dd->pattern, "%s*.*", name); |
|
279 |
dd->context = 0; |
|
280 |
dd->vms_wantversions = 0; |
|
281 |
dd->pat.dsc$a_pointer = dd->pattern; |
|
282 |
dd->pat.dsc$w_length = strlen(dd->pattern); |
|
283 |
dd->pat.dsc$b_dtype = DSC$K_DTYPE_T; |
|
284 |
dd->pat.dsc$b_class = DSC$K_CLASS_S; |
|
285 |
||
286 |
return dd; |
|
287 |
} |
|
288 |
||
289 |
/* |
|
290 |
** Set the flag to indicate we want versions or not. |
|
291 |
*/ |
|
292 |
void |
|
293 |
vmsreaddirversions(dd, flag) |
|
294 |
DIR *dd; |
|
295 |
int flag; |
|
296 |
{ |
|
297 |
dd->vms_wantversions = flag; |
|
298 |
} |
|
299 |
||
300 |
/* |
|
301 |
** Free up an opened directory. |
|
302 |
*/ |
|
303 |
void |
|
304 |
closedir(dd) |
|
305 |
DIR *dd; |
|
306 |
{ |
|
307 |
free(dd->pattern); |
|
308 |
free((char *)dd); |
|
309 |
} |
|
310 |
||
311 |
/* |
|
312 |
** Collect all the version numbers for the current file. |
|
313 |
*/ |
|
314 |
static void |
|
315 |
collectversions(dd) |
|
316 |
DIR *dd; |
|
317 |
{ |
|
318 |
struct dsc$descriptor_s pat; |
|
319 |
struct dsc$descriptor_s res; |
|
320 |
struct dirent *e; |
|
321 |
char *p; |
|
322 |
char buff[sizeof dd->entry.d_name]; |
|
323 |
int i; |
|
324 |
char *text; |
|
325 |
long context; |
|
326 |
||
327 |
/* Convenient shorthand. */ |
|
328 |
e = &dd->entry; |
|
329 |
||
330 |
/* Add the version wildcard, ignoring the "*.*" put on before */ |
|
331 |
i = strlen(dd->pattern); |
|
332 |
text = malloc((unsigned int)(i + strlen(e->d_name)+ 2 + 1)); |
|
333 |
if (text == NULL) |
|
334 |
return; |
|
335 |
(void)strcpy(text, dd->pattern); |
|
336 |
(void)sprintf(&text[i - 3], "%s;*", e->d_name); |
|
337 |
||
338 |
/* Set up the pattern descriptor. */ |
|
339 |
pat.dsc$a_pointer = text; |
|
340 |
pat.dsc$w_length = strlen(text); |
|
341 |
pat.dsc$b_dtype = DSC$K_DTYPE_T; |
|
342 |
pat.dsc$b_class = DSC$K_CLASS_S; |
|
343 |
||
344 |
/* Set up result descriptor. */ |
|
345 |
res.dsc$a_pointer = buff; |
|
346 |
res.dsc$w_length = sizeof buff - 2; |
|
347 |
res.dsc$b_dtype = DSC$K_DTYPE_T; |
|
348 |
res.dsc$b_class = DSC$K_CLASS_S; |
|
349 |
||
350 |
/* Read files, collecting versions. */ |
|
351 |
for (context = 0; e->vms_verscount < VERSIZE(e); e->vms_verscount++) { |
|
352 |
if (lib$find_file(&pat, &res, &context) == RMS$_NMF || context == 0) |
|
353 |
break; |
|
354 |
buff[sizeof buff - 1] = '\0'; |
|
355 |
if (p = strchr(buff, ';')) |
|
356 |
e->vms_versions[e->vms_verscount] = atoi(p + 1); |
|
357 |
else |
|
358 |
e->vms_versions[e->vms_verscount] = -1; |
|
359 |
} |
|
360 |
||
361 |
free(text); |
|
362 |
} |
|
363 |
||
364 |
/* |
|
365 |
** Read the next entry from the directory. |
|
366 |
*/ |
|
367 |
struct dirent * |
|
368 |
readdir(dd) |
|
2957 | 369 |
DIR *dd; |
438 | 370 |
{ |
2957 | 371 |
struct dsc$descriptor_s res; |
372 |
char *p; |
|
2982 | 373 |
char buff[sizeof dd->entry.d_name + 10]; |
2957 | 374 |
int i; |
438 | 375 |
|
376 |
/* Set up result descriptor, and get next file. */ |
|
377 |
res.dsc$a_pointer = buff; |
|
378 |
res.dsc$w_length = sizeof buff - 2; |
|
379 |
res.dsc$b_dtype = DSC$K_DTYPE_T; |
|
380 |
res.dsc$b_class = DSC$K_CLASS_S; |
|
381 |
if (lib$find_file(&dd->pat, &res, &dd->context) == RMS$_NMF |
|
382 |
|| dd->context == 0L) |
|
383 |
/* None left... */ |
|
384 |
return NULL; |
|
385 |
||
386 |
/* Force the buffer to end with a NUL. */ |
|
387 |
buff[sizeof buff - 1] = '\0'; |
|
388 |
for (p = buff; !isspace(*p); p++) |
|
389 |
; |
|
390 |
*p = '\0'; |
|
391 |
||
392 |
/* Skip any directory component and just copy the name. */ |
|
393 |
if (p = strchr(buff, ']')) |
|
394 |
(void)strcpy(dd->entry.d_name, p + 1); |
|
395 |
else |
|
396 |
(void)strcpy(dd->entry.d_name, buff); |
|
397 |
||
398 |
/* Clobber the version. */ |
|
399 |
if (p = strchr(dd->entry.d_name, ';')) |
|
400 |
*p = '\0'; |
|
401 |
||
2982 | 402 |
/* claus: empty dirs seems to leave *.* in the buffer ... */ |
403 |
if (strcmp(dd->entry.d_name, "*.*") == 0) { |
|
404 |
return NULL; |
|
405 |
} |
|
406 |
||
438 | 407 |
dd->entry.d_namlen = strlen(dd->entry.d_name); |
408 |
||
409 |
dd->entry.vms_verscount = 0; |
|
410 |
if (dd->vms_wantversions) |
|
411 |
collectversions(dd); |
|
412 |
return &dd->entry; |
|
413 |
} |
|
414 |
||
415 |
/* |
|
416 |
** Return something that can be used in a seekdir later. |
|
417 |
*/ |
|
418 |
long |
|
419 |
telldir(dd) |
|
2957 | 420 |
DIR *dd; |
438 | 421 |
{ |
422 |
return dd->context; |
|
423 |
} |
|
424 |
||
425 |
/* |
|
426 |
** Return to a spot where we used to be. |
|
427 |
*/ |
|
428 |
void |
|
429 |
seekdir(dd, pos) |
|
2957 | 430 |
DIR *dd; |
431 |
long pos; |
|
438 | 432 |
{ |
433 |
dd->context = pos; |
|
434 |
} |
|
435 |
||
2896 | 436 |
# define HAS_OPENDIR |
437 |
||
438 |
# endif /* __VMS__ */ |
|
439 |
#endif /* not HAS_OPENDIR */ |
|
438 | 440 |
%} |
441 |
! ! |
|
442 |
||
613 | 443 |
!DirectoryStream class methodsFor:'documentation'! |
444 |
||
445 |
copyright |
|
446 |
" |
|
447 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
448 |
All Rights Reserved |
|
1 | 449 |
|
613 | 450 |
This software is furnished under a license and may be used |
451 |
only in accordance with the terms of that license and with the |
|
452 |
inclusion of the above copyright notice. This software may not |
|
453 |
be provided or otherwise made available to, or used by, any |
|
454 |
other person. No title to or ownership of the software is |
|
455 |
hereby transferred. |
|
456 |
" |
|
457 |
! |
|
360 | 458 |
|
613 | 459 |
documentation |
460 |
" |
|
461 |
Instances of DirectoryStream allow reading a file-directory, |
|
462 |
as if it was a stream of filenames. |
|
463 |
Basically, its an interface to opendir, readdir and closedir. |
|
8881
53e94478133f
STORE macro fix (gcc3.4 bug)
Claus Gittinger <cg@exept.de>
parents:
8775
diff
changeset
|
464 |
Notice: |
53e94478133f
STORE macro fix (gcc3.4 bug)
Claus Gittinger <cg@exept.de>
parents:
8775
diff
changeset
|
465 |
DirectoryStream is an ST/X special; |
2957 | 466 |
for portability, we recommend the use of Filename protocol. |
1295 | 467 |
|
468 |
[author:] |
|
1631 | 469 |
Claus Gittinger |
2957 | 470 |
|
471 |
[see also:] |
|
472 |
Filename |
|
613 | 473 |
" |
1 | 474 |
! ! |
475 |
||
476 |
!DirectoryStream class methodsFor:'instance creation'! |
|
477 |
||
478 |
directoryNamed:dirName |
|
7826 | 479 |
"return a DirectoryStream for directory named dirName, aString |
480 |
Raises an openError, if the directory does not exists." |
|
1 | 481 |
|
482 |
|newStream| |
|
483 |
||
8775 | 484 |
newStream := (self basicNew) text; pathName:dirName. |
1 | 485 |
newStream openForReading isNil ifTrue:[^nil]. |
486 |
^ newStream |
|
7826 | 487 |
|
488 |
" |
|
8881
53e94478133f
STORE macro fix (gcc3.4 bug)
Claus Gittinger <cg@exept.de>
parents:
8775
diff
changeset
|
489 |
self directoryNamed:'adadasd' |
53e94478133f
STORE macro fix (gcc3.4 bug)
Claus Gittinger <cg@exept.de>
parents:
8775
diff
changeset
|
490 |
self directoryNamed:'.' |
7826 | 491 |
" |
1 | 492 |
! ! |
493 |
||
5245 | 494 |
!DirectoryStream methodsFor:'access-reading'! |
1 | 495 |
|
12909 | 496 |
contents |
497 |
"answer all of the directory entries as an OrderedCollection" |
|
498 |
||
499 |
|contents| |
|
500 |
||
501 |
contents := OrderedCollection new. |
|
13778 | 502 |
[self atEnd] whileFalse:[ |
503 |
|l| |
|
504 |
l := self nextLine. |
|
505 |
l isNil ifTrue:[ |
|
506 |
^ contents |
|
507 |
]. |
|
508 |
contents add:l |
|
12909 | 509 |
]. |
510 |
^ contents |
|
511 |
! |
|
512 |
||
1 | 513 |
nextLine |
17287 | 514 |
"return the next filename as a string or nil" |
17292 | 515 |
|
17287 | 516 |
|linkInfo| |
92 | 517 |
|
17287 | 518 |
linkInfo := self nextLinkInfo. |
519 |
linkInfo notNil ifTrue:[^ linkInfo sourcePath]. |
|
520 |
^ nil |
|
521 |
! |
|
522 |
||
523 |
nextLinkInfo |
|
524 |
"return the next FileStatusInfo or nil" |
|
92 | 525 |
|
17287 | 526 |
|nextResult resultInfo| |
6259
164d7efc1eb2
api call stuff (more calls wrapped; care for EINTR)
Claus Gittinger <cg@exept.de>
parents:
5245
diff
changeset
|
527 |
|
17302 | 528 |
(hitEOF or:[readAheadEntry isNil]) ifTrue:[ |
19651
59a06f7e5116
avoid largeinteger arithmetic in osTime handling
Claus Gittinger <cg@exept.de>
parents:
18780
diff
changeset
|
529 |
^ self pastEndRead |
17290 | 530 |
]. |
531 |
||
532 |
nextResult := OperatingSystem nextLinkInfoFrom:self dirPointer:dirPointer. |
|
533 |
nextResult isNil ifTrue:[ |
|
19651
59a06f7e5116
avoid largeinteger arithmetic in osTime handling
Claus Gittinger <cg@exept.de>
parents:
18780
diff
changeset
|
534 |
hitEOF := true. |
16297 | 535 |
]. |
17290 | 536 |
readAheadEntry notNil ifTrue:[ |
19651
59a06f7e5116
avoid largeinteger arithmetic in osTime handling
Claus Gittinger <cg@exept.de>
parents:
18780
diff
changeset
|
537 |
resultInfo := readAheadEntry. |
59a06f7e5116
avoid largeinteger arithmetic in osTime handling
Claus Gittinger <cg@exept.de>
parents:
18780
diff
changeset
|
538 |
readAheadEntry := nextResult. |
59a06f7e5116
avoid largeinteger arithmetic in osTime handling
Claus Gittinger <cg@exept.de>
parents:
18780
diff
changeset
|
539 |
^ resultInfo |
17290 | 540 |
]. |
541 |
nextResult isNil ifTrue:[ |
|
19651
59a06f7e5116
avoid largeinteger arithmetic in osTime handling
Claus Gittinger <cg@exept.de>
parents:
18780
diff
changeset
|
542 |
^ self pastEndRead |
17290 | 543 |
]. |
544 |
^ nextResult |
|
1 | 545 |
! ! |
546 |
||
8519 | 547 |
!DirectoryStream protectedMethodsFor:'instance release'! |
613 | 548 |
|
549 |
closeFile |
|
550 |
"low level close of a directoryStream" |
|
16299 | 551 |
|
552 |
|dp| |
|
553 |
||
554 |
dp := dirPointer. |
|
555 |
dp isNil ifTrue:[ |
|
17287 | 556 |
^ self errorNotOpen. |
16299 | 557 |
]. |
558 |
dirPointer := nil. |
|
613 | 559 |
%{ |
560 |
#ifdef HAS_OPENDIR |
|
16299 | 561 |
closedir( (DIR *)(__FILEVal(dp)) ); |
1631 | 562 |
#else |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19651
diff
changeset
|
563 |
# ifdef __win32__ |
16299 | 564 |
FindClose( __HANDLEVal(dp) ); |
1631 | 565 |
# endif |
613 | 566 |
#endif |
567 |
%} |
|
568 |
! ! |
|
569 |
||
1 | 570 |
!DirectoryStream methodsFor:'private'! |
571 |
||
572 |
openForReading |
|
573 |
"open the file for readonly" |
|
574 |
||
17287 | 575 |
|ok encodedPathName error fileSize osPathname osModTime osCrtTime osAccTime osFileAttributes | |
1 | 576 |
|
14581 | 577 |
encodedPathName := OperatingSystem encodePath:pathName. |
1 | 578 |
mode := #readonly. |
17290 | 579 |
hitEOF := false. |
22665 | 580 |
ok := false. |
17287 | 581 |
|
1 | 582 |
%{ |
22665 | 583 |
#if defined(HAS_OPENDIR) |
584 |
if (__INST(dirPointer) == nil && __isStringLike(encodedPathName)) { |
|
585 |
DIR *d; |
|
586 |
OBJ path, dp; |
|
1 | 587 |
|
22665 | 588 |
__threadErrno = 0; |
589 |
__BEGIN_INTERRUPTABLE__ |
|
590 |
do { |
|
591 |
d = opendir((char *) __stringVal(encodedPathName)); |
|
592 |
} while ((d == NULL) && (__threadErrno == EINTR)); |
|
593 |
__END_INTERRUPTABLE__ |
|
92 | 594 |
|
22665 | 595 |
if (d == NULL) { |
596 |
error = __mkSmallInteger(__threadErrno); |
|
597 |
} else { |
|
598 |
dp = __MKEXTERNALADDRESS(d); __INST(dirPointer) = dp; __STORE(self, dp); |
|
599 |
ok = true; |
|
600 |
} |
|
1 | 601 |
} |
22665 | 602 |
#elif defined(__win32__) |
1631 | 603 |
HANDLE d; |
604 |
OBJ path, dp; |
|
11294 | 605 |
union { |
22665 | 606 |
char pattern[MAXPATHLEN]; |
607 |
wchar_t wpattern[MAXPATHLEN]; |
|
11294 | 608 |
} uP; |
609 |
union { |
|
22665 | 610 |
WIN32_FIND_DATAA data; |
611 |
WIN32_FIND_DATAW wdata; |
|
11294 | 612 |
} uD; |
1631 | 613 |
|
614 |
if (__INST(dirPointer) == nil) { |
|
22665 | 615 |
path = __INST(pathName); |
616 |
if (__isStringLike(path)) { |
|
617 |
int l = __stringSize(path); |
|
17287 | 618 |
|
22665 | 619 |
if (l < (MAXPATHLEN-4)) { |
620 |
strncpy(uP.pattern, __stringVal(path), l); |
|
621 |
strcpy(uP.pattern+l, "\\*"); |
|
3164
4f37ae9c8961
use strncpy to avoid buffer-overruns
Claus Gittinger <cg@exept.de>
parents:
2999
diff
changeset
|
622 |
|
22665 | 623 |
do { |
624 |
__threadErrno = 0; |
|
625 |
d = STX_API_NOINT_CALL2( "FindFirstFileA", FindFirstFileA, uP.pattern, &uD.data ); |
|
626 |
} while ((d < 0) && (__threadErrno == EINTR)); |
|
11294 | 627 |
|
22665 | 628 |
if (d == INVALID_HANDLE_VALUE) { |
23542 | 629 |
error = __mkSmallInteger(__WIN32_ERR(GetLastError())); |
22665 | 630 |
} else { |
631 |
dp = __MKEXTERNALADDRESS(d); __INST(dirPointer) = dp; __STORE(self, dp); |
|
17287 | 632 |
|
22665 | 633 |
fileSize = __MKLARGEINT64(1, uD.data.nFileSizeLow, uD.data.nFileSizeHigh ); |
634 |
osPathname = __MKSTRING( uD.data.cFileName ); |
|
635 |
osFileAttributes = __mkSmallInteger( uD.data.dwFileAttributes ); |
|
6259
164d7efc1eb2
api call stuff (more calls wrapped; care for EINTR)
Claus Gittinger <cg@exept.de>
parents:
5245
diff
changeset
|
636 |
|
22665 | 637 |
osCrtTime = FileTimeToOsTime1970(&uD.data.ftCreationTime); |
638 |
osAccTime = FileTimeToOsTime1970(&uD.data.ftLastAccessTime); |
|
639 |
osModTime = FileTimeToOsTime1970(&uD.data.ftLastWriteTime); |
|
640 |
ok = true; |
|
641 |
} |
|
642 |
} |
|
643 |
} |
|
644 |
else if (__isUnicode16String(path)) { |
|
645 |
int l = __unicode16StringSize(path); |
|
646 |
int i; |
|
17287 | 647 |
|
22665 | 648 |
if (l < (MAXPATHLEN-4)) { |
649 |
for (i=0; i<l; i++) { |
|
650 |
uP.wpattern[i] = __unicode16StringVal(path)[i]; |
|
651 |
} |
|
652 |
uP.wpattern[i++] = '\\'; |
|
653 |
uP.wpattern[i++] = '*'; |
|
654 |
uP.wpattern[i] = 0; |
|
6259
164d7efc1eb2
api call stuff (more calls wrapped; care for EINTR)
Claus Gittinger <cg@exept.de>
parents:
5245
diff
changeset
|
655 |
|
22665 | 656 |
do { |
657 |
__threadErrno = 0; |
|
658 |
d = STX_API_NOINT_CALL2( "FindFirstFileW", FindFirstFileW, uP.wpattern, &uD.wdata ); |
|
659 |
} while ((d < 0) && (__threadErrno == EINTR)); |
|
17287 | 660 |
|
22665 | 661 |
if (d == INVALID_HANDLE_VALUE) { |
23542 | 662 |
error = __mkSmallInteger(__WIN32_ERR(GetLastError())); |
22665 | 663 |
} else { |
664 |
dp = __MKEXTERNALADDRESS(d); __INST(dirPointer) = dp; __STORE(self, dp); |
|
11294 | 665 |
|
22665 | 666 |
fileSize = __MKLARGEINT64(1, uD.wdata.nFileSizeLow, uD.wdata.nFileSizeHigh ); |
667 |
osPathname = __MKU16STRING( uD.wdata.cFileName ); |
|
668 |
osFileAttributes = __mkSmallInteger( uD.data.dwFileAttributes ); |
|
11294 | 669 |
|
22665 | 670 |
osCrtTime = FileTimeToOsTime1970(&uD.wdata.ftCreationTime); |
671 |
osAccTime = FileTimeToOsTime1970(&uD.wdata.ftLastAccessTime); |
|
672 |
osModTime = FileTimeToOsTime1970(&uD.wdata.ftLastWriteTime); |
|
673 |
ok = true; |
|
674 |
} |
|
675 |
} |
|
676 |
} |
|
1631 | 677 |
} |
22665 | 678 |
#endif // __win32__ |
155 | 679 |
%}. |
17287 | 680 |
|
681 |
ok == true ifTrue:[ |
|
22665 | 682 |
self registerForFinalization. |
683 |
osPathname isNil ifTrue:[ |
|
684 |
"UNIX: does not automatically provide the first entry" |
|
17291 | 685 |
|
22665 | 686 |
(StreamError,StreamIOError) handle:[:ex | |
687 |
self close. |
|
688 |
ex reject. |
|
689 |
] do:[ |
|
690 |
readAheadEntry := OperatingSystem nextLinkInfoFrom:self dirPointer:dirPointer. |
|
691 |
]. |
|
692 |
] ifFalse:[ |
|
693 |
"Windows already provides the first entry's info" |
|
17291 | 694 |
|
22665 | 695 |
readAheadEntry := OperatingSystem |
696 |
linkInfoFor:osPathname |
|
697 |
fileSize:fileSize |
|
698 |
fileAttributes:osFileAttributes |
|
699 |
osCrtTime:osCrtTime |
|
700 |
osAccTime:osAccTime |
|
701 |
osModTime:osModTime |
|
702 |
]. |
|
17287 | 703 |
|
22665 | 704 |
^ self |
1 | 705 |
]. |
155 | 706 |
|
17287 | 707 |
ok notNil ifTrue:[ |
22665 | 708 |
dirPointer notNil ifTrue:[^ self errorAlreadyOpen]. |
1 | 709 |
]. |
16297 | 710 |
error notNil ifTrue:[ |
23542 | 711 |
lastErrorNumber := error. |
712 |
^ self openError:error. |
|
16297 | 713 |
]. |
1 | 714 |
^ nil |
22665 | 715 |
|
716 |
"Modified: / 09-04-2018 / 19:03:18 / stefan" |
|
23542 | 717 |
"Modified: / 22-11-2018 / 15:49:37 / Stefan Vogel" |
1 | 718 |
! |
719 |
||
720 |
reOpen |
|
8604 | 721 |
"USERS WILL NEVER INVOKE THIS METHOD |
722 |
sent after snapin to reopen streams." |
|
38 | 723 |
|
2 | 724 |
dirPointer := nil. |
725 |
super reOpen |
|
1 | 726 |
! ! |
727 |
||
728 |
!DirectoryStream methodsFor:'testing'! |
|
729 |
||
730 |
atEnd |
|
731 |
"return true, if position is at end" |
|
732 |
||
17302 | 733 |
^ readAheadEntry == nil |
2945
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
734 |
! |
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
735 |
|
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
736 |
isEmpty |
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
737 |
"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
|
738 |
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
|
739 |
|
2957 | 740 |
|entry| |
2945
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
741 |
|
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
742 |
[self atEnd] whileFalse:[ |
3816
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
743 |
entry := self nextLine. |
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
744 |
entry asFilename isImplicit ifFalse:[ |
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
745 |
^ false. |
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
746 |
] |
2945
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
747 |
]. |
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
748 |
^ true |
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
749 |
|
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
750 |
|
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
751 |
" |
3816
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
752 |
(DirectoryStream directoryNamed:'/') isEmpty |
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
753 |
(DirectoryStream directoryNamed:'/var/tmp') isEmpty |
2945
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
754 |
" |
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
755 |
|
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
756 |
"Modified: 18.9.1997 / 18:05:31 / stefan" |
16299 | 757 |
! |
758 |
||
759 |
isOpen |
|
23019 | 760 |
"return true, if this stream is open" |
761 |
||
762 |
"executors are only used to finalize - they are not really open and are closed with #closeFile, |
|
763 |
which does not check for #isOpen" |
|
764 |
||
22695 | 765 |
self isExecutor ifTrue:[ |
766 |
^ false. |
|
767 |
]. |
|
16299 | 768 |
^ dirPointer notNil. |
22695 | 769 |
|
770 |
"Modified: / 23-04-2018 / 18:29:51 / stefan" |
|
23019 | 771 |
"Modified (comment): / 25-05-2018 / 08:21:57 / Claus Gittinger" |
1 | 772 |
! ! |
773 |
||
1281
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1133
diff
changeset
|
774 |
!DirectoryStream class methodsFor:'documentation'! |
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1133
diff
changeset
|
775 |
|
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1133
diff
changeset
|
776 |
version |
18780 | 777 |
^ '$Header$' |
12468
cc062d075d2a
__isByteArray() to __isByteArrayLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
12414
diff
changeset
|
778 |
! |
cc062d075d2a
__isByteArray() to __isByteArrayLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
12414
diff
changeset
|
779 |
|
cc062d075d2a
__isByteArray() to __isByteArrayLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
12414
diff
changeset
|
780 |
version_CVS |
18780 | 781 |
^ '$Header$' |
1281
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1133
diff
changeset
|
782 |
! ! |
22665 | 783 |