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.

78 lines
2.6 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2021 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 <unordered_map>
  22. #include <vector>
  23. #include <core/wx_stl_compat.h>
  24. /**
  25. * An asset archive represents a file containing data assets that are loaded from disk and then
  26. * cached in memory. For example, a set of bitmap images.
  27. *
  28. * The entire contents of the archive will be uncompressed and kept resident in memory in the
  29. * current implementation, so consider this before reusing this as-is for new use cases.
  30. */
  31. class ASSET_ARCHIVE
  32. {
  33. public:
  34. ASSET_ARCHIVE( const wxString& aFilePath, bool aLoadNow = true );
  35. ~ASSET_ARCHIVE() = default;
  36. bool Load();
  37. /**
  38. * Retrieves a file with the given path from the cached archive
  39. * @param aFilePath is the path within the archive to the requested file
  40. * @param aDest is the target byte array to copy into
  41. * @param aMaxLen is the maximum bytes that can be copied into aDest
  42. * @return the number of bytes copied, or -1 if the given file was not found
  43. */
  44. long GetFileContents( const wxString& aFilePath, const unsigned char* aDest, size_t aMaxLen );
  45. /**
  46. * Retrieves a pointer to a file with the given path from the cached archive
  47. * @param aFilePath is the path within the archive to the requested file
  48. * @param aDest will be set to point to the start of the file if the file was found
  49. * @return the file size, or -1 if the given file was not found
  50. */
  51. long GetFilePointer( const wxString& aFilePath, const unsigned char** aDest );
  52. private:
  53. struct FILE_INFO
  54. {
  55. size_t offset;
  56. size_t length;
  57. };
  58. /// Cache of file info for a given file path
  59. std::unordered_map<wxString, FILE_INFO> m_fileInfoCache;
  60. /// The full file contents
  61. std::vector<unsigned char> m_cache;
  62. /// Path to the source archive file
  63. wxString m_filePath;
  64. };
  65. #endif // KICAD_ASSET_ARCHIVE_H