KiCad PCB EDA Suite
Loading...
Searching...
No Matches
diptrace_binary_reader.h
Go to the documentation of this file.
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
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#ifndef DIPTRACE_BINARY_READER_H
21#define DIPTRACE_BINARY_READER_H
22
23#include <cstddef>
24#include <cstdint>
25#include <vector>
26
27#include <wx/string.h>
28
29
30namespace DIPTRACE
31{
32
39
42constexpr int INT3_BIAS = 1000000;
43
46constexpr int INT4_BIAS = 1000000000;
47
51constexpr double DIPTRACE_COORD_TO_MM = 1.0 / 30000.0;
52
54constexpr double DIPTRACE_ANGLE_TO_DEG = 0.00001;
55
64constexpr int LEGACY_STRING_VERSION = 37;
65
68constexpr int MAX_STRING_CHARS = 10000;
69
70
82{
83public:
90 BINARY_READER( const wxString& aFileName );
91
93
94 // -- Position management -------------------------------------------------
95
97 size_t GetOffset() const { return m_offset; }
98
105 void SetOffset( size_t aOffset );
106
113 void Skip( size_t aBytes );
114
116 size_t GetFileSize() const { return m_data.size(); }
117
119 bool IsEOF() const { return m_offset >= m_data.size(); }
120
122 const uint8_t* GetData() const { return m_data.data(); }
123
124 // -- Version management --------------------------------------------------
125
135 void SetVersion( int aVersion ) { m_version = aVersion; }
136
138 int GetVersion() const { return m_version; }
139
144 void SetStringEncoding( STRING_ENCODING aEncoding ) { m_stringEncoding = aEncoding; }
145
158 void DetectStringEncoding( size_t aProbeOffset );
159
160 // -- Primitive readers ---------------------------------------------------
161
167 uint8_t ReadByte();
168
178 int ReadInt3();
179
190 int ReadInt4();
191
201 wxString ReadString();
202
211 void ReadColor( uint8_t& r, uint8_t& g, uint8_t& b );
212
220 void ReadBytes( uint8_t* aDst, size_t aCount );
221
222 // -- Peek methods (do not advance position) ------------------------------
223
229 int PeekInt3() const;
230
236 int PeekInt4() const;
237
243 uint8_t PeekByte() const;
244
245 // -- Coordinate conversion helpers ---------------------------------------
246
253 static int DipTraceToKiCadNm( int aDipTraceCoord );
254
261 static double DipTraceToMM( int aDipTraceCoord );
262
263 // -- Search helpers ------------------------------------------------------
264
276 size_t FindPattern( const uint8_t* aPattern, size_t aPatternLen,
277 size_t aStart, size_t aEnd ) const;
278
290 size_t FindString( const wxString& aStr, size_t aStart, size_t aEnd ) const;
291
292 // -- Try-read methods (return false instead of throwing) ------------------
293
307 bool TryReadString( wxString& aResult );
308
309private:
313 wxString ReadStringUTF16();
314
318 wxString ReadStringASCII();
319
324 bool TryReadStringUTF16( wxString& aResult );
325
330 bool TryReadStringASCII( wxString& aResult );
331
336 static bool IsPrintableString( const wxString& aStr );
337
343 void ThrowEOFError( size_t aBytesNeeded ) const;
344
345 std::vector<uint8_t> m_data;
346 size_t m_offset;
349};
350
351} // namespace DIPTRACE
352
353#endif // DIPTRACE_BINARY_READER_H
bool TryReadStringUTF16(wxString &aResult)
Attempt to read a UTF-16-BE string with validation.
const uint8_t * GetData() const
Return a pointer to the raw file data buffer.
int GetVersion() const
Return the currently configured format version.
size_t FindString(const wxString &aStr, size_t aStart, size_t aEnd) const
Search for a UTF-16-BE encoded string in the file data, including its two-byte length prefix.
int PeekInt3() const
Peek at the next 3-byte biased integer without advancing the position.
wxString ReadStringASCII()
Read a v37 legacy ASCII string: int3(byte_count) + raw ASCII bytes.
bool TryReadStringASCII(wxString &aResult)
Attempt to read a legacy ASCII string with validation.
void ReadBytes(uint8_t *aDst, size_t aCount)
Read a block of raw bytes into the caller's buffer.
int m_version
DipTrace format version.
void Skip(size_t aBytes)
Advance the read position by the given number of bytes.
uint8_t ReadByte()
Read a single unsigned byte and advance the position by 1.
void SetVersion(int aVersion)
Set the DipTrace format version.
size_t m_offset
Current read position (byte offset).
uint8_t PeekByte() const
Peek at the next byte without advancing the position.
void ThrowEOFError(size_t aBytesNeeded) const
Throw IO_ERROR with a message indicating a read past end of file.
void DetectStringEncoding(size_t aProbeOffset)
Detect the string encoding from the bytes at aProbeOffset, which must sit at the start of a non-empty...
STRING_ENCODING m_stringEncoding
Explicit string encoding override.
static bool IsPrintableString(const wxString &aStr)
Verify that all characters in aStr are printable or common whitespace (space, tab,...
void ReadColor(uint8_t &r, uint8_t &g, uint8_t &b)
Read a 3-byte RGB color value.
int ReadInt4()
Read a 4-byte big-endian biased integer (bias 1,000,000,000) and advance the position by 4.
int ReadInt3()
Read a 3-byte big-endian biased integer (bias 1,000,000) and advance the position by 3.
size_t GetFileSize() const
Return the total size of the loaded file in bytes.
static int DipTraceToKiCadNm(int aDipTraceCoord)
Convert a DipTrace coordinate value (10 nm units) to KiCad nanometers.
size_t GetOffset() const
Return the current byte offset within the file.
bool IsEOF() const
Return true if the read position has reached or passed the end of file.
size_t FindPattern(const uint8_t *aPattern, size_t aPatternLen, size_t aStart, size_t aEnd) const
Search for a byte pattern in the file data.
void SetStringEncoding(STRING_ENCODING aEncoding)
Override DipTrace string decoding when a file family has a deterministic string-encoding cutover that...
void SetOffset(size_t aOffset)
Set the read position to an absolute byte offset.
bool TryReadString(wxString &aResult)
Attempt to read a string at the current position.
wxString ReadStringUTF16()
Read a v39+ UTF-16-BE string: uint16-BE char count + UTF-16-BE data.
BINARY_READER(const wxString &aFileName)
Construct a reader by loading the given file into memory.
int PeekInt4() const
Peek at the next 4-byte biased integer without advancing the position.
static double DipTraceToMM(int aDipTraceCoord)
Convert a DipTrace coordinate value (10 nm units) to millimeters.
wxString ReadString()
Read a string using the configured encoding.
std::vector< uint8_t > m_data
Entire file contents loaded into memory.
constexpr double DIPTRACE_COORD_TO_MM
DipTrace uses 762 units per mil (30 000 units per mm).
constexpr int MAX_STRING_CHARS
Maximum sane string length (in characters) accepted by the reader.
constexpr int INT4_BIAS
Bias value added to stored 4-byte unsigned integers.
constexpr int LEGACY_STRING_VERSION
Format version at or below which strings use the legacy ASCII encoding (int3 byte-count + raw ASCII b...
constexpr int INT3_BIAS
Bias value added to stored 3-byte unsigned integers.
constexpr double DIPTRACE_ANGLE_TO_DEG
DipTrace stores angles with 100 000 units per degree.