OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef BIN_DIRECTORY_H_ | 5 #ifndef BIN_DIRECTORY_H_ |
6 #define BIN_DIRECTORY_H_ | 6 #define BIN_DIRECTORY_H_ |
7 | 7 |
8 #include "bin/builtin.h" | 8 #include "bin/builtin.h" |
9 #include "bin/dartutils.h" | 9 #include "bin/dartutils.h" |
| 10 #include "bin/reference_counting.h" |
10 #include "bin/thread.h" | 11 #include "bin/thread.h" |
11 #include "platform/globals.h" | 12 #include "platform/globals.h" |
12 | 13 |
13 namespace dart { | 14 namespace dart { |
14 namespace bin { | 15 namespace bin { |
15 | 16 |
16 enum ListType { | 17 enum ListType { |
17 kListFile = 0, | 18 kListFile = 0, |
18 kListDirectory = 1, | 19 kListDirectory = 1, |
19 kListLink = 2, | 20 kListLink = 2, |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 error_(false), | 101 error_(false), |
101 recursive_(recursive), | 102 recursive_(recursive), |
102 follow_links_(follow_links) { | 103 follow_links_(follow_links) { |
103 if (!path_buffer_.Add(dir_name)) { | 104 if (!path_buffer_.Add(dir_name)) { |
104 error_ = true; | 105 error_ = true; |
105 } | 106 } |
106 Push(new DirectoryListingEntry(NULL)); | 107 Push(new DirectoryListingEntry(NULL)); |
107 } | 108 } |
108 | 109 |
109 virtual ~DirectoryListing() { | 110 virtual ~DirectoryListing() { |
110 while (!IsEmpty()) { | 111 PopAll(); |
111 Pop(); | |
112 } | |
113 } | 112 } |
114 | 113 |
115 virtual bool HandleDirectory(const char* dir_name) = 0; | 114 virtual bool HandleDirectory(const char* dir_name) = 0; |
116 virtual bool HandleFile(const char* file_name) = 0; | 115 virtual bool HandleFile(const char* file_name) = 0; |
117 virtual bool HandleLink(const char* link_name) = 0; | 116 virtual bool HandleLink(const char* link_name) = 0; |
118 virtual bool HandleError() = 0; | 117 virtual bool HandleError() = 0; |
119 virtual void HandleDone() {} | 118 virtual void HandleDone() {} |
120 | 119 |
121 void Push(DirectoryListingEntry* directory) { | 120 void Push(DirectoryListingEntry* directory) { |
122 top_ = directory; | 121 top_ = directory; |
123 } | 122 } |
124 | 123 |
125 void Pop() { | 124 void Pop() { |
126 ASSERT(!IsEmpty()); | 125 ASSERT(!IsEmpty()); |
127 DirectoryListingEntry* current = top_; | 126 DirectoryListingEntry* current = top_; |
128 top_ = top_->parent(); | 127 top_ = top_->parent(); |
129 delete current; | 128 delete current; |
130 } | 129 } |
131 | 130 |
132 bool IsEmpty() const { | 131 bool IsEmpty() const { |
133 return top_ == NULL; | 132 return top_ == NULL; |
134 } | 133 } |
135 | 134 |
| 135 void PopAll() { |
| 136 while (!IsEmpty()) { |
| 137 Pop(); |
| 138 } |
| 139 } |
| 140 |
136 DirectoryListingEntry* top() const { | 141 DirectoryListingEntry* top() const { |
137 return top_; | 142 return top_; |
138 } | 143 } |
139 | 144 |
140 bool recursive() const { | 145 bool recursive() const { |
141 return recursive_; | 146 return recursive_; |
142 } | 147 } |
143 | 148 |
144 bool follow_links() const { | 149 bool follow_links() const { |
145 return follow_links_; | 150 return follow_links_; |
(...skipping 13 matching lines...) Expand all Loading... |
159 | 164 |
160 private: | 165 private: |
161 PathBuffer path_buffer_; | 166 PathBuffer path_buffer_; |
162 DirectoryListingEntry* top_; | 167 DirectoryListingEntry* top_; |
163 bool error_; | 168 bool error_; |
164 bool recursive_; | 169 bool recursive_; |
165 bool follow_links_; | 170 bool follow_links_; |
166 }; | 171 }; |
167 | 172 |
168 | 173 |
169 class AsyncDirectoryListing : public DirectoryListing { | 174 class AsyncDirectoryListing : public ReferenceCounted<AsyncDirectoryListing>, |
| 175 public DirectoryListing { |
170 public: | 176 public: |
171 enum Response { | 177 enum Response { |
172 kListFile = 0, | 178 kListFile = 0, |
173 kListDirectory = 1, | 179 kListDirectory = 1, |
174 kListLink = 2, | 180 kListLink = 2, |
175 kListError = 3, | 181 kListError = 3, |
176 kListDone = 4 | 182 kListDone = 4 |
177 }; | 183 }; |
178 | 184 |
179 AsyncDirectoryListing(const char* dir_name, | 185 AsyncDirectoryListing(const char* dir_name, |
180 bool recursive, | 186 bool recursive, |
181 bool follow_links) | 187 bool follow_links) |
182 : DirectoryListing(dir_name, recursive, follow_links) {} | 188 : ReferenceCounted(), |
| 189 DirectoryListing(dir_name, recursive, follow_links), |
| 190 array_(NULL), |
| 191 index_(0), |
| 192 length_(0) {} |
183 | 193 |
184 virtual ~AsyncDirectoryListing() {} | |
185 virtual bool HandleDirectory(const char* dir_name); | 194 virtual bool HandleDirectory(const char* dir_name); |
186 virtual bool HandleFile(const char* file_name); | 195 virtual bool HandleFile(const char* file_name); |
187 virtual bool HandleLink(const char* file_name); | 196 virtual bool HandleLink(const char* file_name); |
188 virtual bool HandleError(); | 197 virtual bool HandleError(); |
189 virtual void HandleDone(); | 198 virtual void HandleDone(); |
190 | 199 |
191 void SetArray(CObjectArray* array, intptr_t length) { | 200 void SetArray(CObjectArray* array, intptr_t length) { |
192 ASSERT(length % 2 == 0); | 201 ASSERT(length % 2 == 0); |
193 array_ = array; | 202 array_ = array; |
194 index_ = 0; | 203 index_ = 0; |
195 length_ = length; | 204 length_ = length; |
196 } | 205 } |
197 | 206 |
198 intptr_t index() const { | 207 intptr_t index() const { |
199 return index_; | 208 return index_; |
200 } | 209 } |
201 | 210 |
202 private: | 211 private: |
| 212 virtual ~AsyncDirectoryListing() {} |
203 bool AddFileSystemEntityToResponse(Response response, const char* arg); | 213 bool AddFileSystemEntityToResponse(Response response, const char* arg); |
204 CObjectArray* array_; | 214 CObjectArray* array_; |
205 intptr_t index_; | 215 intptr_t index_; |
206 intptr_t length_; | 216 intptr_t length_; |
207 | 217 |
| 218 friend class ReferenceCounted<AsyncDirectoryListing>; |
208 DISALLOW_IMPLICIT_CONSTRUCTORS(AsyncDirectoryListing); | 219 DISALLOW_IMPLICIT_CONSTRUCTORS(AsyncDirectoryListing); |
209 }; | 220 }; |
210 | 221 |
211 | 222 |
212 class SyncDirectoryListing: public DirectoryListing { | 223 class SyncDirectoryListing: public DirectoryListing { |
213 public: | 224 public: |
214 SyncDirectoryListing(Dart_Handle results, | 225 SyncDirectoryListing(Dart_Handle results, |
215 const char* dir_name, | 226 const char* dir_name, |
216 bool recursive, | 227 bool recursive, |
217 bool follow_links) | 228 bool follow_links) |
(...skipping 13 matching lines...) Expand all Loading... |
231 virtual bool HandleLink(const char* file_name); | 242 virtual bool HandleLink(const char* file_name); |
232 virtual bool HandleError(); | 243 virtual bool HandleError(); |
233 | 244 |
234 private: | 245 private: |
235 Dart_Handle results_; | 246 Dart_Handle results_; |
236 Dart_Handle add_string_; | 247 Dart_Handle add_string_; |
237 Dart_Handle directory_type_; | 248 Dart_Handle directory_type_; |
238 Dart_Handle file_type_; | 249 Dart_Handle file_type_; |
239 Dart_Handle link_type_; | 250 Dart_Handle link_type_; |
240 | 251 |
| 252 DISALLOW_ALLOCATION() |
241 DISALLOW_IMPLICIT_CONSTRUCTORS(SyncDirectoryListing); | 253 DISALLOW_IMPLICIT_CONSTRUCTORS(SyncDirectoryListing); |
242 }; | 254 }; |
243 | 255 |
244 | 256 |
245 class Directory { | 257 class Directory { |
246 public: | 258 public: |
247 enum ExistsResult { | 259 enum ExistsResult { |
248 UNKNOWN, | 260 UNKNOWN, |
249 EXISTS, | 261 EXISTS, |
250 DOES_NOT_EXIST | 262 DOES_NOT_EXIST |
(...skipping 28 matching lines...) Expand all Loading... |
279 | 291 |
280 private: | 292 private: |
281 DISALLOW_ALLOCATION(); | 293 DISALLOW_ALLOCATION(); |
282 DISALLOW_IMPLICIT_CONSTRUCTORS(Directory); | 294 DISALLOW_IMPLICIT_CONSTRUCTORS(Directory); |
283 }; | 295 }; |
284 | 296 |
285 } // namespace bin | 297 } // namespace bin |
286 } // namespace dart | 298 } // namespace dart |
287 | 299 |
288 #endif // BIN_DIRECTORY_H_ | 300 #endif // BIN_DIRECTORY_H_ |
OLD | NEW |