FileEntry.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #ifndef _D_FILE_ENTRY_H_
  36. #define _D_FILE_ENTRY_H_
  37. #include "common.h"
  38. #include <string>
  39. #include <deque>
  40. #include <ostream>
  41. #include "SharedHandle.h"
  42. #include "File.h"
  43. namespace aria2 {
  44. class FileEntry {
  45. private:
  46. std::string path;
  47. std::deque<std::string> _uris;
  48. uint64_t length;
  49. off_t offset;
  50. bool extracted;
  51. bool requested;
  52. public:
  53. FileEntry():length(0), offset(0), extracted(false), requested(false) {}
  54. FileEntry(const std::string& path, uint64_t length, off_t offset,
  55. const std::deque<std::string>& uris = std::deque<std::string>());
  56. ~FileEntry();
  57. FileEntry& operator=(const FileEntry& entry);
  58. std::string getBasename() const
  59. {
  60. return File(path).getBasename();
  61. }
  62. std::string getDirname() const
  63. {
  64. return File(path).getDirname();
  65. }
  66. const std::string& getPath() const { return path; }
  67. void setPath(const std::string& path) { this->path = path; }
  68. uint64_t getLength() const { return length; }
  69. void setLength(uint64_t length) { this->length = length; }
  70. off_t getOffset() const { return offset; }
  71. void setOffset(off_t offset) { this->offset = offset; }
  72. bool isExtracted() const { return extracted; }
  73. void setExtracted(bool flag) { this->extracted = flag; }
  74. bool isRequested() const { return requested; }
  75. void setRequested(bool flag) { this->requested = flag; }
  76. void setupDir();
  77. const std::deque<std::string>& getAssociatedUris() const
  78. {
  79. return _uris;
  80. }
  81. bool operator<(const FileEntry& fileEntry) const;
  82. bool exists() const;
  83. };
  84. typedef SharedHandle<FileEntry> FileEntryHandle;
  85. typedef std::deque<FileEntryHandle> FileEntries;
  86. // Returns the first FileEntry which isRequested() method returns
  87. // true. If no such FileEntry exists, then returns
  88. // SharedHandle<FileEntry>().
  89. template<typename InputIterator>
  90. SharedHandle<FileEntry> getFirstRequestedFileEntry
  91. (InputIterator first, InputIterator last)
  92. {
  93. for(; first != last; ++first) {
  94. if((*first)->isRequested()) {
  95. return *first;
  96. }
  97. }
  98. return SharedHandle<FileEntry>();
  99. }
  100. // Counts the number of files selected in the given iterator range
  101. // [first, last).
  102. template<typename InputIterator>
  103. size_t countRequestedFileEntry(InputIterator first, InputIterator last)
  104. {
  105. size_t count = 0;
  106. for(; first != last; ++first) {
  107. if((*first)->isRequested()) {
  108. ++count;
  109. }
  110. }
  111. return count;
  112. }
  113. // Writes first filename to given o. If memory is true, the output is
  114. // "[MEMORY]" plus the basename of the first filename. If there is no
  115. // FileEntry, writes "n/a" to o. If more than 1 FileEntry are in the
  116. // iterator range [first, last), "(Nmore)" is written at the end where
  117. // N is the number of files in iterator range [first, last) minus 1.
  118. template<typename InputIterator>
  119. void writeFilePath
  120. (InputIterator first, InputIterator last, std::ostream& o, bool memory)
  121. {
  122. SharedHandle<FileEntry> e = getFirstRequestedFileEntry(first, last);
  123. if(e.isNull()) {
  124. o << "n/a";
  125. } else {
  126. if(e->getPath().empty()) {
  127. o << "n/a";
  128. } else {
  129. if(memory) {
  130. o << "[MEMORY]" << File(e->getPath()).getBasename();
  131. } else {
  132. o << e->getPath();
  133. }
  134. }
  135. size_t count = countRequestedFileEntry(first, last);
  136. if(count > 1) {
  137. o << " (" << count-1 << "more)";
  138. }
  139. }
  140. }
  141. }
  142. #endif // _D_FILE_ENTRY_H_