25#ifndef _ALTIUM_BINARY_PARSER_H
26#define _ALTIUM_BINARY_PARSER_H
34#include <wx/mstream.h>
35#include <wx/zstream.h>
44class CompoundFileReader;
45struct COMPOUND_FILE_ENTRY;
53std::string
FormatPath(
const std::vector<std::string>& aVectorPath );
81 std::unique_ptr<ALTIUM_COMPOUND_FILE>
DecodeIntLibStream(
const CFB::COMPOUND_FILE_ENTRY& cfe );
85 std::tuple<wxString, const CFB::COMPOUND_FILE_ENTRY*>
FindLibFootprintDirName(
const wxString& aFpUnicodeName );
87 const CFB::COMPOUND_FILE_ENTRY*
FindStream(
const std::vector<std::string>& aStreamPath )
const;
89 const CFB::COMPOUND_FILE_ENTRY*
FindStream(
const CFB::COMPOUND_FILE_ENTRY* aStart,
const std::vector<std::string>& aStreamPath )
const;
92 const std::string aName,
93 const bool aIsStream )
const;
95 std::map<wxString, const CFB::COMPOUND_FILE_ENTRY*>
EnumDir(
const std::wstring& aDir )
const;
97 std::map<wxString, const CFB::COMPOUND_FILE_ENTRY*>
GetLibSymbols(
const CFB::COMPOUND_FILE_ENTRY* aStart )
const;
118 template <
typename Type>
122 if( remainingBytes >=
sizeof( Type ) )
124 Type val = *(Type*) (
m_pos );
125 m_pos +=
sizeof( Type );
130 m_pos += remainingBytes;
136 template <
typename Type>
139 char*
const oldPos =
m_pos;
141 Type result = Read<Type>();
149 uint8_t len = Read<uint8_t>();
153 char* buf =
static_cast<char*
>( malloc( len ) );
154 memcpy( buf,
m_pos, len );
157 return wxScopedCharBuffer::CreateOwned( buf, len );
162 return wxScopedCharBuffer();
175 std::map<uint32_t, wxString> table;
178 while( remaining >= 8 )
180 uint32_t index = Read<uint32_t>();
181 uint32_t length = Read<uint32_t>();
189 if( length > remaining )
192 str = wxString(
m_pos, wxMBConvUTF16LE(), length - 2 );
195 table.emplace( index, str );
212 std::vector<char> data(
m_pos,
m_pos + aSize );
227 memcpy( aOut,
m_pos, aSize );
264 uint32_t length = Read<uint32_t>();
270 std::function<std::map<wxString, wxString>(
const std::string& )> handleBinaryData =
271 [](
const std::string& )
273 return std::map<wxString, wxString>();
335 throw std::out_of_range(
"ALTIUM_BINARY_READER: out of range" );
345 throw std::out_of_range(
"ALTIUM_BINARY_READER: out of range" );
355 throw std::out_of_range(
"ALTIUM_BINARY_READER: out of range" );
367 throw std::out_of_range(
"ALTIUM_BINARY_READER: out of range" );
394 throw std::runtime_error(
"ALTIUM_COMPRESSED_READER: invalid compressed string" );
398 id = std::stoi( str );
405 return std::make_pair(
id, result );
412 wxMemoryInputStream memStream( (
void*) aData.data(), aData.length() );
415 wxZlibInputStream zStream( memStream );
418 std::string decompressedData;
421 while( !zStream.Eof() )
424 zStream.Read( buffer,
sizeof( buffer ) );
425 size_t bytesRead = zStream.LastRead();
426 decompressedData.append( buffer, bytesRead );
429 return decompressedData;
std::string FormatPath(const std::vector< std::string > &aVectorPath)
Helper for debug logging (vector -> string)
size_t GetRemainingBytes() const
~ALTIUM_BINARY_PARSER()=default
void Skip(size_t aLength)
std::vector< char > ReadVector(size_t aSize)
std::unique_ptr< char[]> m_content
wxScopedCharBuffer ReadCharBuffer()
std::map< uint32_t, wxString > ReadWideStringTable()
size_t ReadAndSetSubrecordLength()
VECTOR2I ReadVector2ISize()
VECTOR2I ReadVector2IPos()
size_t GetRemainingSubrecordBytes() const
std::map< wxString, wxString > ReadProperties(std::function< std::map< wxString, wxString >(const std::string &)> handleBinaryData=[](const std::string &) { return std::map< wxString, wxString >();})
int ReadBytes(char *aOut, size_t aSize)
const std::string & m_data
ALTIUM_BINARY_READER(const std::string &binaryData)
std::string ReadPascalString()
std::map< wxString, wxString > ListLibFootprints()
const CFB::CompoundFileReader & GetCompoundFileReader() const
std::unique_ptr< ALTIUM_COMPOUND_FILE > DecodeIntLibStream(const CFB::COMPOUND_FILE_ENTRY &cfe)
const CFB::COMPOUND_FILE_ENTRY * FindStreamSingleLevel(const CFB::COMPOUND_FILE_ENTRY *aEntry, const std::string aName, const bool aIsStream) const
~ALTIUM_COMPOUND_FILE()=default
std::map< wxString, const CFB::COMPOUND_FILE_ENTRY * > EnumDir(const std::wstring &aDir) const
ALTIUM_COMPOUND_FILE & operator=(const ALTIUM_COMPOUND_FILE &temp_obj)=delete
std::vector< char > m_buffer
std::unique_ptr< CFB::CompoundFileReader > m_reader
std::map< wxString, const CFB::COMPOUND_FILE_ENTRY * > GetLibSymbols(const CFB::COMPOUND_FILE_ENTRY *aStart) const
std::map< wxString, const CFB::COMPOUND_FILE_ENTRY * > m_libFootprintNameCache
std::tuple< wxString, const CFB::COMPOUND_FILE_ENTRY * > FindLibFootprintDirName(const wxString &aFpUnicodeName)
void cacheLibFootprintNames()
const CFB::COMPOUND_FILE_ENTRY * FindStream(const std::vector< std::string > &aStreamPath) const
std::map< wxString, wxString > m_libFootprintDirNameCache
ALTIUM_COMPOUND_FILE(const ALTIUM_COMPOUND_FILE &temp_obj)=delete
std::pair< int, std::string > ReadCompressedString()
ALTIUM_COMPRESSED_READER(const std::string &aData)
std::string decompressData(std::string &aData)
static int32_t ConvertToKicadUnit(const double aValue)