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, you may find one here:
18 * http://www.gnu.org/licenses/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <streambuf>
25#include <istream>
26#include <string>
27#include <git2.h>
28#include <import_export.h>
29
30#include <richio.h>
31
32
33class APIEXPORT BLOB_BUFFER_STREAM : public std::streambuf
34{
35public:
36 BLOB_BUFFER_STREAM( git_blob* aBlob )
37 {
38 // Yay C++
39 setg( const_cast<char*>( static_cast<const char*>( git_blob_rawcontent( aBlob ) ) ),
40 const_cast<char*>( static_cast<const char*>( git_blob_rawcontent( aBlob ) ) ),
41 const_cast<char*>( static_cast<const char*>( git_blob_rawcontent( aBlob ) ) ) +
42 git_blob_rawsize( aBlob ) );
43 }
44
46 {
47 }
48
49 int overflow( int c ) override
50 {
51 return traits_type::eof();
52 }
53
54 std::streamsize xsputn( const char* s, std::streamsize n ) override
55 {
56 return 0;
57 }
58};
59
60// Build a class that implements LINE_READER for git_blobs
62{
63public:
64 BLOB_READER( git_blob* aBlob ) : m_blob( aBlob )
65 {
67 m_istream = new std::istream( m_stream );
68 m_line = nullptr;
69 m_lineNum = 0;
70 }
71
72 ~BLOB_READER() override
73 {
74 delete m_istream;
75 delete m_stream;
76 }
77
78 char* ReadLine() override
79 {
80 getline( *m_istream, m_buffer );
81
82 m_buffer.append( 1, '\n' );
83
84 m_length = m_buffer.size();
85 m_line = (char*) m_buffer.data(); //ew why no const??
86
87 // lineNum is incremented even if there was no line read, because this
88 // leads to better error reporting when we hit an end of file.
89 ++m_lineNum;
90
91 return m_istream->eof() ? nullptr : m_line;
92 }
93
94private:
95 git_blob* m_blob;
97 std::istream* m_istream;
98 std::string m_buffer;
99};
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:141
unsigned m_length
no. bytes in line before trailing nul.
Definition richio.h:167
char * m_line
the read line of UTF8 text
Definition richio.h:170
unsigned m_lineNum
Definition richio.h:168
#define APIEXPORT
Macros which export functions from a DLL/DSO.