KiCad PCB EDA Suite
Loading...
Searching...
No Matches
kicad_git_blob_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 3
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 <streambuf>
21#include <istream>
22#include <string>
23#include <git2.h>
24#include <import_export.h>
25
26#include <richio.h>
27
28
29class APIEXPORT BLOB_BUFFER_STREAM : public std::streambuf
30{
31public:
32 BLOB_BUFFER_STREAM( git_blob* aBlob )
33 {
34 // Yay C++
35 setg( const_cast<char*>( static_cast<const char*>( git_blob_rawcontent( aBlob ) ) ),
36 const_cast<char*>( static_cast<const char*>( git_blob_rawcontent( aBlob ) ) ),
37 const_cast<char*>( static_cast<const char*>( git_blob_rawcontent( aBlob ) ) ) +
38 git_blob_rawsize( aBlob ) );
39 }
40
42 {
43 }
44
45 int overflow( int c ) override
46 {
47 return traits_type::eof();
48 }
49
50 std::streamsize xsputn( const char* s, std::streamsize n ) override
51 {
52 return 0;
53 }
54};
55
56// Build a class that implements LINE_READER for git_blobs
58{
59public:
60 BLOB_READER( git_blob* aBlob ) : m_blob( aBlob )
61 {
63 m_istream = new std::istream( m_stream );
64 m_line = nullptr;
65 m_lineNum = 0;
66 }
67
68 ~BLOB_READER() override
69 {
70 delete m_istream;
71 delete m_stream;
72 }
73
74 char* ReadLine() override
75 {
76 getline( *m_istream, m_buffer );
77
78 m_buffer.append( 1, '\n' );
79
80 m_length = m_buffer.size();
81 m_line = (char*) m_buffer.data(); //ew why no const??
82
83 // lineNum is incremented even if there was no line read, because this
84 // leads to better error reporting when we hit an end of file.
85 ++m_lineNum;
86
87 return m_istream->eof() ? nullptr : m_line;
88 }
89
90private:
91 git_blob* m_blob;
93 std::istream* m_istream;
94 std::string m_buffer;
95};
BLOB_BUFFER_STREAM(git_blob *aBlob)
std::streamsize xsputn(const char *s, std::streamsize n) override
int overflow(int c) override
BLOB_BUFFER_STREAM * m_stream
std::istream * m_istream
BLOB_READER(git_blob *aBlob)
~BLOB_READER() override
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
LINE_READER(unsigned aMaxLineLength=LINE_READER_LINE_DEFAULT_MAX)
Build a line reader and fixes the length of the maximum supported line length to aMaxLineLength.
Definition richio.cpp:100
unsigned m_length
no. bytes in line before trailing nul.
Definition richio.h:136
char * m_line
the read line of UTF8 text
Definition richio.h:139
unsigned m_lineNum
Definition richio.h:137
#define APIEXPORT
Macros which export functions from a DLL/DSO.