1
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1989-93 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
|
13 |
FileStream subclass:#DirectoryStream
|
|
14 |
instanceVariableNames:'dirPointer readAhead'
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Streams-External'
|
|
18 |
!
|
|
19 |
|
|
20 |
DirectoryStream comment:'
|
|
21 |
|
|
22 |
COPYRIGHT (c) 1989-93 by Claus Gittinger
|
|
23 |
All Rights Reserved
|
|
24 |
|
|
25 |
%W% %E%
|
|
26 |
'!
|
|
27 |
|
|
28 |
%{
|
|
29 |
#include <stdio.h>
|
|
30 |
#ifndef transputer
|
|
31 |
# include <sys/types.h>
|
|
32 |
# include <sys/stat.h>
|
|
33 |
# ifdef OPENDIR
|
|
34 |
# include <sys/types.h>
|
|
35 |
# ifdef NEXT
|
|
36 |
# include <sys/dir.h>
|
|
37 |
# else
|
|
38 |
# include <dirent.h>
|
|
39 |
# endif
|
|
40 |
# endif
|
|
41 |
#endif
|
|
42 |
%}
|
|
43 |
|
|
44 |
!DirectoryStream methodsFor:'instance release'!
|
|
45 |
|
|
46 |
closeFile
|
|
47 |
"a directoryStream has been collected - close it"
|
|
48 |
%{
|
|
49 |
#ifdef OPENDIR
|
|
50 |
closedir((DIR *)MKFD(_INST(dirPointer)));
|
|
51 |
#endif
|
|
52 |
%}
|
|
53 |
! !
|
|
54 |
|
|
55 |
!DirectoryStream class methodsFor:'instance creation'!
|
|
56 |
|
|
57 |
directoryNamed:dirName
|
|
58 |
"return a DirectoryStream for directory named dirName, aString"
|
|
59 |
|
|
60 |
|newStream|
|
|
61 |
|
|
62 |
newStream := (self basicNew) pathName:dirName.
|
|
63 |
newStream openForReading isNil ifTrue:[^nil].
|
|
64 |
^ newStream
|
|
65 |
! !
|
|
66 |
|
|
67 |
!DirectoryStream methodsFor:'access reading'!
|
|
68 |
|
|
69 |
nextLine
|
|
70 |
"return the next filename as a string"
|
|
71 |
|
|
72 |
|prevEntry nextEntry|
|
|
73 |
%{
|
|
74 |
#ifdef OPENDIR
|
|
75 |
DIR *d;
|
|
76 |
#ifdef NEXT
|
|
77 |
struct direct *dp;
|
|
78 |
#else
|
|
79 |
struct dirent *dp;
|
|
80 |
#endif
|
|
81 |
|
|
82 |
if (_INST(dirPointer) != nil) {
|
|
83 |
d = (DIR *)MKFD(_INST(dirPointer));
|
|
84 |
dp = readdir(d);
|
|
85 |
if (dp != NULL) {
|
|
86 |
nextEntry = _MKSTRING((char *)(dp->d_name) COMMA_CON);
|
|
87 |
}
|
|
88 |
}
|
|
89 |
#endif
|
|
90 |
%}
|
|
91 |
.
|
|
92 |
prevEntry := readAhead.
|
|
93 |
readAhead := nextEntry.
|
|
94 |
^ prevEntry
|
|
95 |
! !
|
|
96 |
|
|
97 |
!DirectoryStream methodsFor:'private'!
|
|
98 |
|
|
99 |
openForReading
|
|
100 |
"open the file for readonly"
|
|
101 |
|
|
102 |
|retVal|
|
|
103 |
|
|
104 |
mode := #readonly.
|
|
105 |
%{
|
|
106 |
#ifdef OPENDIR
|
|
107 |
DIR *d;
|
|
108 |
OBJ path;
|
|
109 |
extern OBJ ErrorNumber, ErrorString;
|
|
110 |
extern errno;
|
|
111 |
|
|
112 |
retVal = false;
|
|
113 |
if (_INST(dirPointer) == nil) {
|
|
114 |
path = _INST(pathName);
|
|
115 |
if (_isString(path)) {
|
|
116 |
d = opendir((char *) _stringVal(path));
|
|
117 |
if (d == NULL) {
|
|
118 |
/* ErrorString = _MKSTRING(perror("popen:") COMMA_CON); */
|
|
119 |
ErrorNumber = _MKSMALLINT(errno);
|
|
120 |
} else {
|
|
121 |
_INST(dirPointer) = MKOBJ(d);
|
|
122 |
retVal = true;
|
|
123 |
}
|
|
124 |
}
|
|
125 |
}
|
|
126 |
#endif
|
|
127 |
%}
|
|
128 |
.
|
|
129 |
retVal isNil ifTrue:[
|
|
130 |
"opendir not avalable - use slow pipe"
|
|
131 |
^ PipeStream readingFrom:('cd ' , pathName , '; ls -a')
|
|
132 |
].
|
|
133 |
(retVal == true) ifTrue:[
|
|
134 |
lobby register:self.
|
|
135 |
self nextLine. "read 1st entry into readAhead buffer"
|
|
136 |
^ self
|
|
137 |
].
|
|
138 |
^ nil
|
|
139 |
!
|
|
140 |
|
|
141 |
reOpen
|
|
142 |
"sent after snapin to reopen streams"
|
|
143 |
|
|
144 |
filePointer notNil ifTrue:[
|
|
145 |
"it was open, when snapped-out"
|
|
146 |
filePointer := nil.
|
|
147 |
'cannot reopen directorystream' printNewline
|
|
148 |
]
|
|
149 |
! !
|
|
150 |
|
|
151 |
!DirectoryStream methodsFor:'testing'!
|
|
152 |
|
|
153 |
atEnd
|
|
154 |
"return true, if position is at end"
|
|
155 |
|
|
156 |
^ readAhead == nil
|
|
157 |
! !
|
|
158 |
|
|
159 |
!DirectoryStream methodsFor:'closing'!
|
|
160 |
|
|
161 |
close
|
|
162 |
"close the stream - tell operating system"
|
|
163 |
|
|
164 |
dirPointer notNil ifTrue:[
|
|
165 |
lobby unregister:self.
|
|
166 |
self closeFile.
|
|
167 |
dirPointer := nil
|
|
168 |
]
|
|
169 |
! !
|