KiCad PCB EDA Suite
ASSET_ARCHIVE Class Reference

An asset archive represents a file containing data assets that are loaded from disk and then cached in memory. More...

#include <asset_archive.h>

Classes

struct  FILE_INFO
 

Public Member Functions

 ASSET_ARCHIVE (const wxString &aFilePath, bool aLoadNow=true)
 
 ~ASSET_ARCHIVE ()=default
 
bool Load ()
 
long GetFileContents (const wxString &aFilePath, const unsigned char *aDest, size_t aMaxLen)
 Retrieves a file with the given path from the cached archive. More...
 
long GetFilePointer (const wxString &aFilePath, const unsigned char **aDest)
 Retrieves a pointer to a file with the given path from the cached archive. More...
 

Private Attributes

std::unordered_map< wxString, FILE_INFOm_fileInfoCache
 Cache of file info for a given file path. More...
 
std::vector< unsigned char > m_cache
 The full file contents. More...
 
wxString m_filePath
 Path to the source archive file. More...
 

Detailed Description

An asset archive represents a file containing data assets that are loaded from disk and then cached in memory.

For example, a set of bitmap images.

The entire contents of the archive will be uncompressed and kept resident in memory in the current implementation, so consider this before reusing this as-is for new use cases.

Definition at line 35 of file asset_archive.h.

Constructor & Destructor Documentation

◆ ASSET_ARCHIVE()

ASSET_ARCHIVE::ASSET_ARCHIVE ( const wxString &  aFilePath,
bool  aLoadNow = true 
)

Definition at line 27 of file asset_archive.cpp.

27  :
28  m_filePath( aFilePath )
29 {
30  if( aLoadNow )
31  Load();
32 }
wxString m_filePath
Path to the source archive file.
Definition: asset_archive.h:75

References Load().

◆ ~ASSET_ARCHIVE()

ASSET_ARCHIVE::~ASSET_ARCHIVE ( )
default

Member Function Documentation

◆ GetFileContents()

long ASSET_ARCHIVE::GetFileContents ( const wxString &  aFilePath,
const unsigned char *  aDest,
size_t  aMaxLen 
)

Retrieves a file with the given path from the cached archive.

Parameters
aFilePathis the path within the archive to the requested file
aDestis the target byte array to copy into
aMaxLenis the maximum bytes that can be copied into aDest
Returns
the number of bytes copied, or -1 if the given file was not found

Definition at line 87 of file asset_archive.cpp.

89 {
90  wxFAIL_MSG( "Unimplemented" );
91  return 0;
92 }

◆ GetFilePointer()

long ASSET_ARCHIVE::GetFilePointer ( const wxString &  aFilePath,
const unsigned char **  aDest 
)

Retrieves a pointer to a file with the given path from the cached archive.

Parameters
aFilePathis the path within the archive to the requested file
aDestwill be set to point to the start of the file if the file was found
Returns
the file size, or -1 if the given file was not found

Definition at line 95 of file asset_archive.cpp.

96 {
97  if( aFilePath.IsEmpty() )
98  return -1;
99 
100  wxASSERT( aDest );
101 
102  if( !m_fileInfoCache.count( aFilePath ) )
103  return -1;
104 
105  const FILE_INFO& fi = m_fileInfoCache.at( aFilePath );
106 
107  *aDest = &m_cache[fi.offset];
108 
109  return fi.length;
110 }
std::unordered_map< wxString, FILE_INFO > m_fileInfoCache
Cache of file info for a given file path.
Definition: asset_archive.h:69
std::vector< unsigned char > m_cache
The full file contents.
Definition: asset_archive.h:72

References ASSET_ARCHIVE::FILE_INFO::length, m_cache, m_fileInfoCache, and ASSET_ARCHIVE::FILE_INFO::offset.

◆ Load()

bool ASSET_ARCHIVE::Load ( )

Definition at line 35 of file asset_archive.cpp.

36 {
37  // We don't support hot-reloading yet
38  if( !m_fileInfoCache.empty() )
39  return false;
40 
41  wxFFileInputStream zipFile( m_filePath );
42 
43  if( !zipFile.IsOk() )
44  return false;
45 
46  wxZlibInputStream stream( zipFile, wxZLIB_GZIP );
47  wxTarInputStream tarStream( stream );
48  wxTarEntry* entry;
49 
50  // Avoid realloc while reading: we're not going to get better than 2:1 compression
51  m_cache.resize( 2 * zipFile.GetLength() );
52 
53  size_t offset = 0;
54 
55  while( ( entry = tarStream.GetNextEntry() ) )
56  {
57  if( entry->IsDir() )
58  {
59  delete entry;
60  continue;
61  }
62 
63  size_t length = entry->GetSize();
64 
65  FILE_INFO fi;
66  fi.offset = offset;
67  fi.length = length;
68 
69  if( offset + length > m_cache.size() )
70  m_cache.resize( m_cache.size() * 2 );
71 
72  tarStream.Read( &m_cache[offset], length );
73 
74  m_fileInfoCache[entry->GetName()] = fi;
75 
76  offset += length;
77 
78  delete entry;
79  }
80 
81  m_cache.resize( offset );
82 
83  return true;
84 }
wxString m_filePath
Path to the source archive file.
Definition: asset_archive.h:75
std::unordered_map< wxString, FILE_INFO > m_fileInfoCache
Cache of file info for a given file path.
Definition: asset_archive.h:69
std::vector< unsigned char > m_cache
The full file contents.
Definition: asset_archive.h:72

References ASSET_ARCHIVE::FILE_INFO::length, m_cache, m_fileInfoCache, m_filePath, and ASSET_ARCHIVE::FILE_INFO::offset.

Referenced by ASSET_ARCHIVE().

Member Data Documentation

◆ m_cache

std::vector<unsigned char> ASSET_ARCHIVE::m_cache
private

The full file contents.

Definition at line 72 of file asset_archive.h.

Referenced by GetFilePointer(), and Load().

◆ m_fileInfoCache

std::unordered_map<wxString, FILE_INFO> ASSET_ARCHIVE::m_fileInfoCache
private

Cache of file info for a given file path.

Definition at line 69 of file asset_archive.h.

Referenced by GetFilePointer(), and Load().

◆ m_filePath

wxString ASSET_ARCHIVE::m_filePath
private

Path to the source archive file.

Definition at line 75 of file asset_archive.h.

Referenced by Load().


The documentation for this class was generated from the following files: