You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.7 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef KICAD_ASSET_ARCHIVE_H
  20. #define KICAD_ASSET_ARCHIVE_H
  21. #include <kicommon.h>
  22. #include <unordered_map>
  23. #include <vector>
  24. #include <core/wx_stl_compat.h>
  25. /**
  26. * An asset archive represents a file containing data assets that are loaded from disk and then
  27. * cached in memory. For example, a set of bitmap images.
  28. *
  29. * The entire contents of the archive will be uncompressed and kept resident in memory in the
  30. * current implementation, so consider this before reusing this as-is for new use cases.
  31. */
  32. class KICOMMON_API ASSET_ARCHIVE
  33. {
  34. public:
  35. ASSET_ARCHIVE( const wxString& aFilePath, bool aLoadNow = true );
  36. ~ASSET_ARCHIVE() = default;
  37. bool Load();
  38. /**
  39. * Retrieves a file with the given path from the cached archive
  40. * @param aFilePath is the path within the archive to the requested file
  41. * @param aDest is the target byte array to copy into
  42. * @param aMaxLen is the maximum bytes that can be copied into aDest
  43. * @return the number of bytes copied, or -1 if the given file was not found
  44. */
  45. long GetFileContents( const wxString& aFilePath, const unsigned char* aDest, size_t aMaxLen );
  46. /**
  47. * Retrieves a pointer to a file with the given path from the cached archive
  48. * @param aFilePath is the path within the archive to the requested file
  49. * @param aDest will be set to point to the start of the file if the file was found
  50. * @return the file size, or -1 if the given file was not found
  51. */
  52. long GetFilePointer( const wxString& aFilePath, const unsigned char** aDest );
  53. private:
  54. struct FILE_INFO
  55. {
  56. size_t offset;
  57. size_t length;
  58. };
  59. /// Cache of file info for a given file path
  60. std::unordered_map<wxString, FILE_INFO> m_fileInfoCache;
  61. /// The full file contents
  62. std::vector<unsigned char> m_cache;
  63. /// Path to the source archive file
  64. wxString m_filePath;
  65. };
  66. #endif // KICAD_ASSET_ARCHIVE_H