1
0

unarchiver.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * unarchiver.cpp
  3. * --------------
  4. * Purpose: archive loader
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #include "stdafx.h"
  10. #include "unarchiver.h"
  11. #include "../common/FileReader.h"
  12. OPENMPT_NAMESPACE_BEGIN
  13. CUnarchiver::CUnarchiver(FileReader &file)
  14. : impl(nullptr)
  15. , inFile(file)
  16. , emptyArchive(inFile)
  17. #if (defined(MPT_WITH_ZLIB) && defined(MPT_WITH_MINIZIP)) || defined(MPT_WITH_MINIZ)
  18. , zipArchive(inFile)
  19. #endif
  20. #ifdef MPT_WITH_LHASA
  21. , lhaArchive(inFile)
  22. #endif
  23. #if defined(MPT_WITH_ZLIB) || defined(MPT_WITH_MINIZ)
  24. , gzipArchive(inFile)
  25. #endif
  26. #ifdef MPT_WITH_UNRAR
  27. , rarArchive(inFile)
  28. #endif
  29. #ifdef MPT_WITH_ANCIENT
  30. , ancientArchive(inFile)
  31. #endif
  32. {
  33. inFile.Rewind();
  34. #if (defined(MPT_WITH_ZLIB) && defined(MPT_WITH_MINIZIP)) || defined(MPT_WITH_MINIZ)
  35. if(zipArchive.IsArchive()) { impl = &zipArchive; return; }
  36. #endif
  37. #ifdef MPT_WITH_LHASA
  38. if(lhaArchive.IsArchive()) { impl = &lhaArchive; return; }
  39. #endif
  40. #if defined(MPT_WITH_ZLIB) || defined(MPT_WITH_MINIZ)
  41. if(gzipArchive.IsArchive()) { impl = &gzipArchive; return; }
  42. #endif
  43. #ifdef MPT_WITH_UNRAR
  44. if(rarArchive.IsArchive()) { impl = &rarArchive; return; }
  45. #endif
  46. #ifdef MPT_WITH_ANCIENT
  47. if(ancientArchive.IsArchive()) { impl = &ancientArchive; return; }
  48. #endif
  49. impl = &emptyArchive;
  50. }
  51. CUnarchiver::~CUnarchiver()
  52. {
  53. return;
  54. }
  55. static inline std::string GetExtension(const std::string &filename)
  56. {
  57. if(filename.find_last_of(".") != std::string::npos)
  58. {
  59. return mpt::ToLowerCaseAscii(filename.substr(filename.find_last_of(".") + 1));
  60. }
  61. return std::string();
  62. }
  63. std::size_t CUnarchiver::FindBestFile(const std::vector<const char *> &extensions)
  64. {
  65. if(!IsArchive())
  66. {
  67. return failIndex;
  68. }
  69. uint64 biggestSize = 0;
  70. std::size_t bestIndex = failIndex;
  71. for(std::size_t i = 0; i < size(); ++i)
  72. {
  73. if(operator[](i).type != ArchiveFileType::Normal)
  74. {
  75. continue;
  76. }
  77. const std::string ext = GetExtension(operator[](i).name.ToUTF8());
  78. if(ext == "diz" || ext == "nfo" || ext == "txt")
  79. {
  80. // we do not want these
  81. continue;
  82. }
  83. // Compare with list of preferred extensions
  84. if(mpt::contains(extensions, ext))
  85. {
  86. bestIndex = i;
  87. break;
  88. }
  89. if(operator[](i).size >= biggestSize)
  90. {
  91. biggestSize = operator[](i).size;
  92. bestIndex = i;
  93. }
  94. }
  95. return bestIndex;
  96. }
  97. bool CUnarchiver::ExtractBestFile(const std::vector<const char *> &extensions)
  98. {
  99. std::size_t bestFile = FindBestFile(extensions);
  100. if(bestFile == failIndex)
  101. {
  102. return false;
  103. }
  104. return ExtractFile(bestFile);
  105. }
  106. bool CUnarchiver::IsArchive() const
  107. {
  108. return impl->IsArchive();
  109. }
  110. mpt::ustring CUnarchiver::GetComment() const
  111. {
  112. return impl->GetComment();
  113. }
  114. bool CUnarchiver::ExtractFile(std::size_t index)
  115. {
  116. return impl->ExtractFile(index);
  117. }
  118. FileReader CUnarchiver::GetOutputFile() const
  119. {
  120. return impl->GetOutputFile();
  121. }
  122. std::size_t CUnarchiver::size() const
  123. {
  124. return impl->size();
  125. }
  126. IArchive::const_iterator CUnarchiver::begin() const
  127. {
  128. return impl->begin();
  129. }
  130. IArchive::const_iterator CUnarchiver::end() const
  131. {
  132. return impl->end();
  133. }
  134. const ArchiveFileInfo & CUnarchiver::operator [] (std::size_t index) const
  135. {
  136. return impl->operator[](index);
  137. }
  138. OPENMPT_NAMESPACE_END