diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2009-06-14 09:45:44 +0000 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2009-06-14 09:45:44 +0000 |
| commit | 9120e79ff1d4c1c1b9ec80cb4081d132e253c2c3 (patch) | |
| tree | 91546251b513c0ba309accb7fa9d522d1a22a0ff /src/FileEntry.h | |
| parent | 1c070ed66e0db2e8db60e334f9fed31a6470df0b (diff) | |
2009-06-14 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Moved getFirstRequestedFileEntry(), countRequestedFileEntry() and
writeFilePath() function template from RequestGroupMan.cc to
FileEntry.h
* src/FileEntry.h
* src/RequestGroupMan.cc
Diffstat (limited to 'src/FileEntry.h')
| -rw-r--r-- | src/FileEntry.h | 65 |
1 files changed, 63 insertions, 2 deletions
diff --git a/src/FileEntry.h b/src/FileEntry.h index 5d036a70..7cc27619 100644 --- a/src/FileEntry.h +++ b/src/FileEntry.h @@ -36,10 +36,13 @@ #define _D_FILE_ENTRY_H_ #include "common.h" -#include "SharedHandle.h" -#include "File.h" + #include <string> #include <deque> +#include <ostream> + +#include "SharedHandle.h" +#include "File.h" namespace aria2 { @@ -106,6 +109,64 @@ public: typedef SharedHandle<FileEntry> FileEntryHandle; typedef std::deque<FileEntryHandle> FileEntries; +// Returns the first FileEntry which isRequested() method returns +// true. If no such FileEntry exists, then returns +// SharedHandle<FileEntry>(). +template<typename InputIterator> +SharedHandle<FileEntry> getFirstRequestedFileEntry +(InputIterator first, InputIterator last) +{ + for(; first != last; ++first) { + if((*first)->isRequested()) { + return *first; + } + } + return SharedHandle<FileEntry>(); +} + +// Counts the number of files selected in the given iterator range +// [first, last). +template<typename InputIterator> +size_t countRequestedFileEntry(InputIterator first, InputIterator last) +{ + size_t count = 0; + for(; first != last; ++first) { + if((*first)->isRequested()) { + ++count; + } + } + return count; +} + +// Writes first filename to given o. If memory is true, the output is +// "[MEMORY]" plus the basename of the first filename. If there is no +// FileEntry, writes "n/a" to o. If more than 1 FileEntry are in the +// iterator range [first, last), "(Nmore)" is written at the end where +// N is the number of files in iterator range [first, last) minus 1. +template<typename InputIterator> +void writeFilePath +(InputIterator first, InputIterator last, std::ostream& o, bool memory) +{ + SharedHandle<FileEntry> e = getFirstRequestedFileEntry(first, last); + if(e.isNull()) { + o << "n/a"; + } else { + if(e->getPath().empty()) { + o << "n/a"; + } else { + if(memory) { + o << "[MEMORY]" << File(e->getPath()).getBasename(); + } else { + o << e->getPath(); + } + } + size_t count = countRequestedFileEntry(first, last); + if(count > 1) { + o << " (" << count-1 << "more)"; + } + } +} + } #endif // _D_FILE_ENTRY_H_ |
