KiCad PCB EDA Suite
Loading...
Searching...
No Matches
richio.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) 2007-2010 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#ifndef RICHIO_H_
22#define RICHIO_H_
23
24// This file defines 3 classes useful for working with DSN text files and is named
25// "richio" after its author, Richard Hollenbeck, aka Dick Hollenbeck.
26
27
28#include <memory>
29#include <vector>
30#include <core/utf8.h>
31
32// I really did not want to be dependent on wxWidgets in richio
33// but the errorText needs to be wide char so wxString rules.
34#include <cstdio>
35#include <wx/string.h>
36#include <wx/stream.h>
37
38#include <ki_exception.h>
39#include <kicommon.h>
41
42
44
54KICOMMON_API wxString SafeReadFile( const wxString& aFilePath, const wxString& aReadType );
55
56
57#define LINE_READER_LINE_DEFAULT_MAX 16000000
58#define LINE_READER_LINE_INITIAL_SIZE 5000
59
65{
66public:
67
72 LINE_READER( unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX );
73
74 virtual ~LINE_READER();
75
85 virtual char* ReadLine() = 0;
86
93 virtual const wxString& GetSource() const
94 {
95 return m_source;
96 }
97
101 char* Line() const
102 {
103 return m_line;
104 }
105
109 operator char* () const
110 {
111 return Line();
112 }
113
119 virtual unsigned LineNumber() const
120 {
121 return m_lineNum;
122 }
123
127 unsigned Length() const
128 {
129 return m_length;
130 }
131
132protected:
137 void expandCapacity( unsigned aNewsize );
138
139 unsigned m_length;
140 unsigned m_lineNum;
141
142 char* m_line;
143 unsigned m_capacity;
144
146
147 wxString m_source;
148};
149
150
157{
158public:
173 FILE_LINE_READER( const wxString& aFileName, unsigned aStartingLineNumber = 0,
174 unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX );
175
190 FILE_LINE_READER( FILE* aFile, const wxString& aFileName, bool doOwn = true,
191 unsigned aStartingLineNumber = 0,
192 unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX );
193
198
199 char* ReadLine() override;
200
206 void Rewind()
207 {
208 rewind( m_fp );
209 m_lineNum = 0;
210 }
211
212 long int FileLength();
213 long int CurPos();
214
215protected:
216 bool m_iOwn;
217 FILE* m_fp;
218};
219
220
225{
226protected:
227 std::string m_lines;
228 size_t m_ndx;
229
230public:
231
241 STRING_LINE_READER( const std::string& aString, const wxString& aSource );
242
249 STRING_LINE_READER( const STRING_LINE_READER& aStartingPoint );
250
251 char* ReadLine() override;
252};
253
254
259{
260public:
267 INPUTSTREAM_LINE_READER( wxInputStream* aStream, const wxString& aSource );
268
269 char* ReadLine() override;
270
271protected:
272 wxInputStream* m_stream;
273};
274
275
276#define OUTPUTFMTBUFZ 500
277
294{
295protected:
296 OUTPUTFORMATTER( int aReserve = OUTPUTFMTBUFZ, char aQuoteChar = '"' ) :
297 m_buffer( aReserve, '\0' )
298 {
299 quoteChar[0] = aQuoteChar;
300 quoteChar[1] = '\0';
301 }
302
313 static const char* GetQuoteChar( const char* wrapee, const char* quote_char );
314
322 virtual void write( const char* aOutBuf, int aCount ) = 0;
323
324#if defined(__GNUG__) // The GNU C++ compiler defines this
325
326 // When used on a C++ function, we must account for the "this" pointer,
327 // so increase the STRING-INDEX and FIRST-TO_CHECK by one.
328 // See http://docs.freebsd.org/info/gcc/gcc.info.Function_Attributes.html
329 // Then to get format checking during the compile, compile with -Wall or -Wformat
330#define PRINTF_FUNC_N __attribute__( ( format( printf, 3, 4 ) ) )
331#define PRINTF_FUNC __attribute__( ( format( printf, 2, 3 ) ) )
332#else
333#define PRINTF_FUNC_N // nothing
334#define PRINTF_FUNC // nothing
335#endif
336
337public:
341 virtual ~OUTPUTFORMATTER() {}
342
353 int PRINTF_FUNC_N Print( int nestLevel, const char* fmt, ... );
354
364 int PRINTF_FUNC Print( const char* fmt, ... );
365
373 int Indent( int aNestLevel );
374
390 virtual const char* GetQuoteChar( const char* wrapee ) const;
391
404 virtual std::string Quotes( const std::string& aWrapee ) const;
405
406 std::string Quotew( const wxString& aWrapee ) const;
407
412 virtual bool Finish() { return true; }
413
414private:
415 std::vector<char> m_buffer;
416 char quoteChar[2];
417
418 int sprint( const char* fmt, ... );
419 int vprint( const char* fmt, va_list ap );
420
421};
422
423
430{
431public:
435 STRING_FORMATTER( int aReserve = OUTPUTFMTBUFZ, char aQuoteChar = '"' ) :
436 OUTPUTFORMATTER( aReserve, aQuoteChar )
437 {
438 }
439
443 void Clear()
444 {
445 m_mystring.clear();
446 }
447
451 void StripUseless();
452
453 const std::string& GetString()
454 {
455 return m_mystring;
456 }
457
458 std::string& MutableString()
459 {
460 return m_mystring;
461 }
462
463protected:
464 void write( const char* aOutBuf, int aCount ) override;
465
466private:
467 std::string m_mystring;
468};
469
470
483{
484public:
485
494 FILE_OUTPUTFORMATTER( const wxString& aFileName, const wxChar* aMode = wxT( "wt" ),
495 char aQuoteChar = '"' );
496
498
512 bool Finish() override;
513
514protected:
515 void write( const char* aOutBuf, int aCount ) override;
516
518 std::unique_ptr<SIBLING_TEMP_FILE> m_tempFile;
519};
520
521
523{
524public:
525 PRETTIFIED_FILE_OUTPUTFORMATTER( const wxString& aFileName,
526 KICAD_FORMAT::FORMAT_MODE aFormatMode = KICAD_FORMAT::FORMAT_MODE::NORMAL,
527 const wxChar* aMode = wxT( "wt" ), char aQuoteChar = '"' );
528
530
546 bool Finish() override;
547
548protected:
549 void write( const char* aOutBuf, int aCount ) override;
550
551private:
553 std::unique_ptr<SIBLING_TEMP_FILE> m_tempFile;
554 std::string m_buf;
555 KICAD_FORMAT::FORMAT_MODE m_mode;
556};
557
558
559#endif // RICHIO_H_
void Rewind()
Rewind the file and resets the line number back to zero.
Definition richio.h:206
FILE_LINE_READER(const wxString &aFileName, unsigned aStartingLineNumber=0, unsigned aMaxLineLength=LINE_READER_LINE_DEFAULT_MAX)
Take aFileName and the size of the desired line buffer and opens the file and assumes the obligation ...
Definition richio.cpp:154
FILE * m_fp
I may own this file, but might not.
Definition richio.h:217
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition richio.cpp:202
bool m_iOwn
if I own the file, I'll promise to close it, else not.
Definition richio.h:216
std::unique_ptr< SIBLING_TEMP_FILE > m_tempFile
Definition richio.h:518
FILE_OUTPUTFORMATTER(const wxString &aFileName, const wxChar *aMode=wxT("wt"), char aQuoteChar='"' )
Definition richio.cpp:672
void write(const char *aOutBuf, int aCount) override
sibling temp file, committed by Finish()
Definition richio.cpp:688
bool Finish() override
Flushes the temp file to disk and atomically renames it over the final target path.
Definition richio.cpp:682
wxInputStream * m_stream
The input stream to read. No ownership of this pointer.
Definition richio.h:272
INPUTSTREAM_LINE_READER(wxInputStream *aStream, const wxString &aSource)
Construct a LINE_READER from a wxInputStream object.
Definition richio.cpp:291
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition richio.cpp:300
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:98
virtual char * ReadLine()=0
Read a line of text into the buffer and increments the line number counter.
virtual const wxString & GetSource() const
Returns the name of the source of the lines in an abstract sense.
Definition richio.h:93
unsigned m_maxLineLength
maximum allowed capacity using resizing.
Definition richio.h:145
unsigned m_length
no. bytes in line before trailing nul.
Definition richio.h:139
unsigned m_capacity
no. bytes allocated for line.
Definition richio.h:143
char * m_line
the read line of UTF8 text
Definition richio.h:142
unsigned m_lineNum
Definition richio.h:140
virtual unsigned LineNumber() const
Return the line number of the last line read from this LINE_READER.
Definition richio.h:119
wxString m_source
origin of text lines, e.g. filename or "clipboard"
Definition richio.h:147
unsigned Length() const
Return the number of bytes in the last line read from this LINE_READER.
Definition richio.h:127
char * Line() const
Return a pointer to the last line that was read in.
Definition richio.h:101
virtual void write(const char *aOutBuf, int aCount)=0
Should be coded in the interface implementation (derived) classes.
int sprint(const char *fmt,...)
Definition richio.cpp:404
std::vector< char > m_buffer
Definition richio.h:415
OUTPUTFORMATTER(int aReserve=OUTPUTFMTBUFZ, char aQuoteChar='"' )
Definition richio.h:296
virtual bool Finish()
Performs any cleanup needed at the end of a write.
Definition richio.h:412
char quoteChar[2]
Definition richio.h:416
virtual ~OUTPUTFORMATTER()
This is a polymorphic class that can validly be handled by a pointer to the base class.
Definition richio.h:341
int vprint(const char *fmt, va_list ap)
Definition richio.cpp:378
void write(const char *aOutBuf, int aCount) override
Should be coded in the interface implementation (derived) classes.
Definition richio.cpp:733
std::unique_ptr< SIBLING_TEMP_FILE > m_tempFile
< sibling temp file, committed by Finish()
Definition richio.h:553
PRETTIFIED_FILE_OUTPUTFORMATTER(const wxString &aFileName, KICAD_FORMAT::FORMAT_MODE aFormatMode=KICAD_FORMAT::FORMAT_MODE::NORMAL, const wxChar *aMode=wxT("wt"), char aQuoteChar='"' )
Definition richio.cpp:695
bool Finish() override
Runs prettification over the buffered bytes, writes them to the sibling temp file,...
Definition richio.cpp:710
KICAD_FORMAT::FORMAT_MODE m_mode
Definition richio.h:555
A class that owns a sibling temp file, which is created next to the target at construction.
Definition richio.cpp:552
std::string & MutableString()
Definition richio.h:458
const std::string & GetString()
Definition richio.h:453
std::string m_mystring
Definition richio.h:467
STRING_FORMATTER(int aReserve=OUTPUTFMTBUFZ, char aQuoteChar='"' )
Reserve space in the buffer.
Definition richio.h:435
void Clear()
Clear the buffer and empties the internal string.
Definition richio.h:443
std::string m_lines
Definition richio.h:227
STRING_LINE_READER(const std::string &aString, const wxString &aSource)
Construct a string line reader.
Definition richio.cpp:236
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition richio.cpp:259
#define KICOMMON_API
Definition kicommon.h:27
KICOMMON_API wxString SafeReadFile(const wxString &aFilePath, const wxString &aReadType)
Nominally opens a file and reads it into a string.
Definition richio.cpp:51
#define PRINTF_FUNC
Definition richio.h:334
#define PRINTF_FUNC_N
Definition richio.h:333
#define OUTPUTFMTBUFZ
default buffer size for any OUTPUT_FORMATTER
Definition richio.h:276
#define LINE_READER_LINE_DEFAULT_MAX
Definition richio.h:57