| 1 | // | 
|---|---|
| 2 | // SortedDirectoryIterator.cpp | 
| 3 | // | 
| 4 | // Library: Foundation | 
| 5 | // Package: Filesystem | 
| 6 | // Module: DirectoryIterator | 
| 7 | // | 
| 8 | // Copyright (c) 2004-2012, Applied Informatics Software Engineering GmbH. | 
| 9 | // and Contributors. | 
| 10 | // | 
| 11 | // SPDX-License-Identifier: BSL-1.0 | 
| 12 | // | 
| 13 | |
| 14 | #include "Poco/SortedDirectoryIterator.h" | 
| 15 | #include <algorithm> | 
| 16 | |
| 17 | |
| 18 | namespace Poco { | 
| 19 | |
| 20 | |
| 21 | SortedDirectoryIterator::SortedDirectoryIterator() | 
| 22 | : DirectoryIterator(), _is_finished(true) | 
| 23 | { | 
| 24 | } | 
| 25 | |
| 26 | |
| 27 | SortedDirectoryIterator::SortedDirectoryIterator(const std::string& rPath) | 
| 28 | : DirectoryIterator(rPath), _is_finished(false) | 
| 29 | { | 
| 30 | scan(); | 
| 31 | next(); | 
| 32 | } | 
| 33 | |
| 34 | |
| 35 | SortedDirectoryIterator::SortedDirectoryIterator(const DirectoryIterator& iterator) | 
| 36 | : DirectoryIterator(iterator), _is_finished(false) | 
| 37 | { | 
| 38 | scan(); | 
| 39 | next(); | 
| 40 | } | 
| 41 | |
| 42 | |
| 43 | SortedDirectoryIterator::SortedDirectoryIterator(const File& file) | 
| 44 | : DirectoryIterator(file), _is_finished(false) | 
| 45 | { | 
| 46 | scan(); | 
| 47 | next(); | 
| 48 | } | 
| 49 | |
| 50 | |
| 51 | SortedDirectoryIterator::SortedDirectoryIterator(const Path& rPath) | 
| 52 | : DirectoryIterator(rPath), _is_finished(false) | 
| 53 | { | 
| 54 | scan(); | 
| 55 | next(); | 
| 56 | } | 
| 57 | |
| 58 | |
| 59 | SortedDirectoryIterator::~SortedDirectoryIterator() | 
| 60 | { | 
| 61 | } | 
| 62 | |
| 63 | |
| 64 | SortedDirectoryIterator& SortedDirectoryIterator::operator ++() | 
| 65 | { | 
| 66 | if (!_is_finished) | 
| 67 | { | 
| 68 | next(); | 
| 69 | } | 
| 70 | return *this; | 
| 71 | } | 
| 72 | |
| 73 | |
| 74 | void SortedDirectoryIterator::scan() | 
| 75 | { | 
| 76 | DirectoryIterator end_it; | 
| 77 | while (*this != end_it) | 
| 78 | { | 
| 79 | bool isDir = false; | 
| 80 | try | 
| 81 | { | 
| 82 | isDir = (*this)->isDirectory(); | 
| 83 | } | 
| 84 | catch (...) { } | 
| 85 | if (isDir) | 
| 86 | _directories.push_back(_path.toString()); | 
| 87 | else | 
| 88 | _files.push_back(_path.toString()); | 
| 89 | |
| 90 | DirectoryIterator::operator++(); | 
| 91 | } | 
| 92 | |
| 93 | std::sort(_directories.begin(), _directories.end()); | 
| 94 | std::sort(_files.begin(), _files.end()); | 
| 95 | } | 
| 96 | |
| 97 | |
| 98 | void SortedDirectoryIterator::next() | 
| 99 | { | 
| 100 | DirectoryIterator end_it; | 
| 101 | if (!_directories.empty()) | 
| 102 | { | 
| 103 | _path.assign(_directories.front()); | 
| 104 | _directories.pop_front(); | 
| 105 | _file = _path; | 
| 106 | } | 
| 107 | else if (!_files.empty()) | 
| 108 | { | 
| 109 | _path.assign(_files.front()); | 
| 110 | _files.pop_front(); | 
| 111 | _file = _path; | 
| 112 | } | 
| 113 | else | 
| 114 | { | 
| 115 | _is_finished = true; | 
| 116 | _path = end_it.path(); | 
| 117 | _file = _path; | 
| 118 | } | 
| 119 | } | 
| 120 | |
| 121 | |
| 122 | } // namespace Poco | 
| 123 | 
