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 (C) 2023 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
29#include <richio.h>
30
31
32class BLOB_BUFFER_STREAM : public std::streambuf
33{
34 public:
35 BLOB_BUFFER_STREAM( git_blob* aBlob )
36 {
37 // Yay C++
38 setg( const_cast<char*>( static_cast<const char*>( git_blob_rawcontent( aBlob ) ) ),
39 const_cast<char*>( static_cast<const char*>( git_blob_rawcontent( aBlob ) ) ),
40 const_cast<char*>( static_cast<const char*>( git_blob_rawcontent( aBlob ) ) ) + git_blob_rawsize( aBlob ) );
41 }
42
44 {
45 }
46
47 int overflow( int c ) override
48 {
49 return traits_type::eof();
50 }
51
52 std::streamsize xsputn( const char* s, std::streamsize n ) override
53 {
54 return 0;
55 }
56};
57
58// Build a class that implements LINE_READER for git_blobs
60{
61 public:
62 BLOB_READER( git_blob* aBlob ) : m_blob( aBlob )
63 {
65 m_istream = new std::istream( m_stream );
66 m_line = nullptr;
67 m_lineNum = 0;
68 }
69
70 ~BLOB_READER() override
71 {
72 delete m_istream;
73 delete m_stream;
74 }
75
76 char* ReadLine() override
77 {
78 getline( *m_istream, m_buffer );
79
80 m_buffer.append( 1, '\n' );
81
82 m_length = m_buffer.size();
83 m_line = (char*) m_buffer.data(); //ew why no const??
84
85 // lineNum is incremented even if there was no line read, because this
86 // leads to better error reporting when we hit an end of file.
87 ++m_lineNum;
88
89 return m_istream->eof() ? nullptr : m_line;
90 }
91
92 private:
93 git_blob* m_blob;
95 std::istream* m_istream;
96 std::string m_buffer;
97};
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::string m_buffer
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.
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition: richio.h:93
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