Chromium Code Reviews| Index: runtime/bin/directory.h |
| diff --git a/runtime/bin/directory.h b/runtime/bin/directory.h |
| index a19d27468628ec6da24b1823fffbdfbac805f919..570db1084e7216b3d57a6aa874708bb640a93764 100644 |
| --- a/runtime/bin/directory.h |
| +++ b/runtime/bin/directory.h |
| @@ -15,13 +15,143 @@ |
| namespace dart { |
| namespace bin { |
| +enum ListType { |
| + kListFile = 0, |
| + kListDirectory = 1, |
| + kListLink = 2, |
| + kListError = 3, |
| + kListDone = 4 |
| +}; |
| + |
| +class PathBuffer { |
| + public: |
| + PathBuffer(); |
| + ~PathBuffer() { |
| + free(data_); |
| + } |
| + |
| + bool Add(const char* name); |
| + bool AddW(const wchar_t* name); |
| + |
| + char* AsString() const; |
| + wchar_t* AsStringW() const; |
| + |
| + void Reset(int new_length); |
| + |
| + int length() const { |
| + return length_; |
| + } |
| + |
| + private: |
| + void* data_; |
| + int length_; |
| +}; |
| + |
| +class DirectoryListing; |
| + |
| +class DirectoryListingEntry { |
| + public: |
| + explicit DirectoryListingEntry(void* link); |
| + |
| + ~DirectoryListingEntry() { |
| + if (parent_ == NULL || parent_->link_ != link_) { |
| + free(link_); |
| + } |
| + } |
| + |
| + ListType Next(DirectoryListing* listing); |
| + |
| + DirectoryListingEntry* parent() const { |
| + return parent_; |
| + } |
| + |
| + void set_parent(DirectoryListingEntry* parent) { |
| + parent_ = parent; |
| + } |
| + |
| + void* link() { |
| + return link_; |
| + } |
| + |
| + private: |
| + DirectoryListingEntry* parent_; |
| + void* lister_; |
|
Søren Gjesse
2013/06/12 12:52:41
lister_, done_ and path_length_ are not used.
Anders Johnsen
2013/06/13 08:38:24
They are, in Next.
|
| + bool done_; |
| + int path_length_; |
| + void* link_; |
| +}; |
| + |
| class DirectoryListing { |
| public: |
| - virtual ~DirectoryListing() {} |
| + DirectoryListing(const char* dir_name, bool recursive, bool follow_links) |
| + : top_(NULL), |
| + error_(false), |
| + recursive_(recursive), |
| + follow_links_(follow_links) { |
| + if (!path_buffer_.Add(dir_name)) { |
| + error_ = true; |
| + } |
| + Push(new DirectoryListingEntry(NULL)); |
| + } |
| + |
| + virtual ~DirectoryListing() { |
| + while (!IsEmpty()) { |
| + Pop(); |
| + } |
| + } |
| + |
| virtual bool HandleDirectory(char* dir_name) = 0; |
| virtual bool HandleFile(char* file_name) = 0; |
| - virtual bool HandleLink(char* file_name) = 0; |
| + virtual bool HandleLink(char* link_name) = 0; |
| virtual bool HandleError(const char* dir_name) = 0; |
| + virtual void HandleDone() {} |
| + |
| + void Push(DirectoryListingEntry* directory) { |
| + directory->set_parent(top_); |
| + top_ = directory; |
| + } |
| + |
| + void Pop() { |
| + ASSERT(!IsEmpty()); |
| + DirectoryListingEntry* current = top_; |
| + top_ = top_->parent(); |
| + delete current; |
| + } |
| + |
| + bool IsEmpty() const { |
| + return top_ == NULL; |
| + } |
| + |
| + DirectoryListingEntry* top() const { |
| + return top_; |
| + } |
| + |
| + bool recursive() const { |
| + return recursive_; |
| + } |
| + |
| + bool follow_links() const { |
| + return follow_links_; |
| + } |
| + |
| + char* CurrentPath() { |
| + return path_buffer_.AsString(); |
| + } |
| + |
| + PathBuffer& path_buffer() { |
|
Søren Gjesse
2013/06/12 12:52:41
Why not PathBuffer* ?
Anders Johnsen
2013/06/13 08:38:24
A pointer would indicated that it could be kept on
|
| + return path_buffer_; |
| + } |
| + |
| + bool error() const { |
| + return error_; |
| + } |
| + |
| + private: |
| + PathBuffer path_buffer_; |
| + DirectoryListingEntry* top_; |
| + bool error_; |
| + bool recursive_; |
| + bool follow_links_; |
| }; |
| @@ -35,17 +165,34 @@ class AsyncDirectoryListing : public DirectoryListing { |
| kListDone = 4 |
| }; |
| - explicit AsyncDirectoryListing(Dart_Port response_port) |
| - : response_port_(response_port) {} |
| + explicit AsyncDirectoryListing(const char* dir_name, |
|
Søren Gjesse
2013/06/12 12:52:41
No need for explicit when there is more than one a
Anders Johnsen
2013/06/13 08:38:24
Done.
|
| + bool recursive, |
| + bool follow_links) |
| + : DirectoryListing(dir_name, recursive, follow_links) { |
| + } |
| + |
| virtual ~AsyncDirectoryListing() {} |
| virtual bool HandleDirectory(char* dir_name); |
| virtual bool HandleFile(char* file_name); |
| virtual bool HandleLink(char* file_name); |
| virtual bool HandleError(const char* dir_name); |
| + virtual void HandleDone(); |
| + |
| + void set_array(CObjectArray* array, intptr_t length) { |
|
Søren Gjesse
2013/06/12 12:52:41
This should be named SetArray as it does not just
Anders Johnsen
2013/06/13 08:38:24
Done.
|
| + array_ = array; |
| + index_ = 0; |
| + length_ = length; |
| + } |
| + |
| + intptr_t index() const { |
| + return index_; |
| + } |
| private: |
| - CObjectArray* NewResponse(Response response, char* arg); |
| - Dart_Port response_port_; |
| + bool NewResponse(Response response, char* arg); |
| + CObjectArray* array_; |
| + intptr_t index_; |
| + intptr_t length_; |
| DISALLOW_IMPLICIT_CONSTRUCTORS(AsyncDirectoryListing); |
| }; |
| @@ -53,8 +200,12 @@ class AsyncDirectoryListing : public DirectoryListing { |
| class SyncDirectoryListing: public DirectoryListing { |
| public: |
| - explicit SyncDirectoryListing(Dart_Handle results) |
| - : results_(results) { |
| + explicit SyncDirectoryListing(Dart_Handle results, |
|
Søren Gjesse
2013/06/12 12:52:41
No need for explicit when there is more than one a
Anders Johnsen
2013/06/13 08:38:24
Done.
|
| + const char* dir_name, |
| + bool recursive, |
| + bool follow_links) |
| + : DirectoryListing(dir_name, recursive, follow_links), |
| + results_(results) { |
| add_string_ = DartUtils::NewString("add"); |
| directory_class_ = |
| DartUtils::GetDartClass(DartUtils::kIOLibURL, "Directory"); |
| @@ -95,14 +246,13 @@ class Directory { |
| kDeleteRequest = 1, |
| kExistsRequest = 2, |
| kCreateTempRequest = 3, |
| - kListRequest = 4, |
| - kRenameRequest = 5 |
| + kListStartRequest = 4, |
| + kListNextRequest = 5, |
| + kListStopRequest = 6, |
| + kRenameRequest = 7 |
| }; |
| - static bool List(const char* path, |
| - bool recursive, |
| - bool follow_links, |
| - DirectoryListing* listing); |
| + static void List(DirectoryListing* listing); |
| static ExistsResult Exists(const char* path); |
| static char* Current(); |
| static bool SetCurrent(const char* path); |