/* */ #ifndef _D_FILE_ENTRY_H_ #define _D_FILE_ENTRY_H_ #include "common.h" #include #include #include #include "SharedHandle.h" #include "File.h" namespace aria2 { class FileEntry { private: std::string path; std::deque _uris; uint64_t length; off_t offset; bool extracted; bool requested; public: FileEntry():length(0), offset(0), extracted(false), requested(false) {} FileEntry(const std::string& path, uint64_t length, off_t offset, const std::deque& uris = std::deque()); ~FileEntry(); FileEntry& operator=(const FileEntry& entry); std::string getBasename() const { return File(path).getBasename(); } std::string getDirname() const { return File(path).getDirname(); } const std::string& getPath() const { return path; } void setPath(const std::string& path) { this->path = path; } uint64_t getLength() const { return length; } void setLength(uint64_t length) { this->length = length; } off_t getOffset() const { return offset; } void setOffset(off_t offset) { this->offset = offset; } bool isExtracted() const { return extracted; } void setExtracted(bool flag) { this->extracted = flag; } bool isRequested() const { return requested; } void setRequested(bool flag) { this->requested = flag; } void setupDir(); const std::deque& getAssociatedUris() const { return _uris; } bool operator<(const FileEntry& fileEntry) const; bool exists() const; }; typedef SharedHandle FileEntryHandle; typedef std::deque FileEntries; // Returns the first FileEntry which isRequested() method returns // true. If no such FileEntry exists, then returns // SharedHandle(). template SharedHandle getFirstRequestedFileEntry (InputIterator first, InputIterator last) { for(; first != last; ++first) { if((*first)->isRequested()) { return *first; } } return SharedHandle(); } // Counts the number of files selected in the given iterator range // [first, last). template 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 void writeFilePath (InputIterator first, InputIterator last, std::ostream& o, bool memory) { SharedHandle 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_