KiCad PCB EDA Suite
Loading...
Searching...
No Matches
io_utils.cpp
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#include "io_utils.h"
21
22#include <wx/wfstream.h>
23#include <wx/txtstrm.h>
24
25#include <mmh3_hash.h>
26
27namespace IO_UTILS
28{
29 const std::vector<uint8_t> COMPOUND_FILE_HEADER = { 0xD0, 0xCF, 0x11, 0xE0,
30 0xA1, 0xB1, 0x1A, 0xE1 };
31
32bool fileStartsWithPrefix( const wxString& aFilePath, const wxString& aPrefix,
33 bool aIgnoreWhitespace )
34{
35 wxFFileInputStream input( aFilePath );
36
37 if( input.IsOk() && !input.Eof() )
38 {
39 // Find first non-empty line
40 wxTextInputStream text( input );
41 wxString line = text.ReadLine();
42
43 if( aIgnoreWhitespace )
44 {
45 while( !input.Eof() && line.IsEmpty() )
46 line = text.ReadLine().Trim( false /*trim from left*/ );
47 }
48
49 if( line.StartsWith( aPrefix ) )
50 return true;
51 }
52
53 return false;
54}
55
56
57bool fileHasBinaryHeader( const wxString& aFilePath, const std::vector<uint8_t>& aHeader,
58 size_t aOffset )
59{
60 wxFFileInputStream input( aFilePath );
61
62 if( input.IsOk() && !input.Eof() )
63 {
64 if( static_cast<size_t>( input.GetLength() ) < aOffset + aHeader.size() )
65 return false;
66
67 // Move the stream to the offset
68 if( aOffset )
69 {
70 if( !input.SeekI( aOffset, wxFromStart ) )
71 return false;
72 }
73
74 std::vector<uint8_t> parsedHeader( aHeader.size() );
75
76 if( !input.ReadAll( parsedHeader.data(), parsedHeader.size() ) )
77 return false;
78
79 return parsedHeader == aHeader;
80 }
81
82 return false;
83}
84
85
86std::optional<wxString> fileHashMMH3( const wxString& aFilePath )
87{
88 constexpr size_t c_BufSize = 1024;
89 wxFFileInputStream input( aFilePath );
90 MMH3_HASH hash( 0x68AF835D ); // Arbitrary seed
91
92 if( input.IsOk() )
93 {
94 uint8_t buf[ c_BufSize ];
95 while ( !input.Eof() )
96 {
97 input.Read( buf, c_BufSize);
98 size_t byteCount = input.LastRead();
99 if( byteCount > 0 )
100 {
101 hash.addData( buf, byteCount );
102 }
103 else
104 {
105 break;
106 }
107 }
108
109 return hash.digest().ToString();
110 }
111
112 return std::optional<wxString>();
113}
114
115
116}
A streaming C++ equivalent for MurmurHash3_x64_128.
Definition mmh3_hash.h:56
FORCE_INLINE void addData(const uint8_t *data, size_t length)
Definition mmh3_hash.h:69
FORCE_INLINE HASH_128 digest()
Definition mmh3_hash.h:136
std::optional< wxString > fileHashMMH3(const wxString &aFilePath)
Calculates an MMH3 hash of a given file.
Definition io_utils.cpp:86
bool fileStartsWithPrefix(const wxString &aFilePath, const wxString &aPrefix, bool aIgnoreWhitespace)
Check if a file starts with a defined string.
Definition io_utils.cpp:32
const std::vector< uint8_t > COMPOUND_FILE_HEADER
Definition io_utils.cpp:29
bool fileHasBinaryHeader(const wxString &aFilePath, const std::vector< uint8_t > &aHeader, size_t aOffset)
Check if a file starts with a defined binary header.
Definition io_utils.cpp:57
std::string ToString() const
Definition hash_128.h:43