|
@@ -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_
|