1
0

archive.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * archive.h
  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. #pragma once
  10. #include "openmpt/all/BuildSettings.hpp"
  11. #include "../common/FileReader.h"
  12. #include <string>
  13. #include <vector>
  14. OPENMPT_NAMESPACE_BEGIN
  15. enum class ArchiveFileType
  16. {
  17. Invalid,
  18. Normal,
  19. Special,
  20. };
  21. struct ArchiveFileInfo
  22. {
  23. mpt::PathString name;
  24. ArchiveFileType type = ArchiveFileType::Invalid;
  25. uint64 size = 0;
  26. mpt::ustring comment;
  27. uint64 cookie1 = 0;
  28. uint64 cookie2 = 0;
  29. };
  30. class IArchive
  31. {
  32. public:
  33. using const_iterator = std::vector<ArchiveFileInfo>::const_iterator;
  34. protected:
  35. IArchive() {}
  36. public:
  37. virtual ~IArchive() {}
  38. public:
  39. virtual bool IsArchive() const = 0;
  40. virtual mpt::ustring GetComment() const = 0;
  41. virtual bool ExtractFile(std::size_t index) = 0;
  42. virtual FileReader GetOutputFile() const = 0;
  43. virtual std::size_t size() const = 0;
  44. virtual IArchive::const_iterator begin() const = 0;
  45. virtual IArchive::const_iterator end() const = 0;
  46. virtual const ArchiveFileInfo & operator [] (std::size_t index) const = 0;
  47. };
  48. class ArchiveBase
  49. : public IArchive
  50. {
  51. protected:
  52. FileReader inFile;
  53. mpt::ustring comment;
  54. std::vector<ArchiveFileInfo> contents;
  55. std::vector<char> data;
  56. public:
  57. ArchiveBase(const FileReader &inFile)
  58. : inFile(inFile)
  59. {
  60. return;
  61. }
  62. ~ArchiveBase() override
  63. {
  64. return;
  65. }
  66. bool ExtractFile(std::size_t index) override { MPT_UNREFERENCED_PARAMETER(index); return false; } // overwrite this
  67. public:
  68. bool IsArchive() const override
  69. {
  70. return !contents.empty();
  71. }
  72. mpt::ustring GetComment() const override
  73. {
  74. return comment;
  75. }
  76. FileReader GetOutputFile() const override
  77. {
  78. return FileReader(mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(data)));
  79. }
  80. std::size_t size() const override { return contents.size(); }
  81. IArchive::const_iterator begin() const override { return contents.begin(); }
  82. IArchive::const_iterator end() const override { return contents.end(); }
  83. const ArchiveFileInfo & operator [] (std::size_t index) const override { return contents[index]; }
  84. };
  85. OPENMPT_NAMESPACE_END