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 <vector>
29#include <core/utf8.h>
30
31// I really did not want to be dependent on wxWidgets in richio
32// but the errorText needs to be wide char so wxString rules.
33#include <cstdio>
34#include <wx/string.h>
35#include <wx/stream.h>
36
37#include <ki_exception.h>
38#include <kicommon.h>
40
41
51KICOMMON_API wxString SafeReadFile( const wxString& aFilePath, const wxString& aReadType );
52
53
54#define LINE_READER_LINE_DEFAULT_MAX 16000000
55#define LINE_READER_LINE_INITIAL_SIZE 5000
56
62{
63public:
64
69 LINE_READER( unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX );
70
71 virtual ~LINE_READER();
72
82 virtual char* ReadLine() = 0;
83
90 virtual const wxString& GetSource() const
91 {
92 return m_source;
93 }
94
98 char* Line() const
99 {
100 return m_line;
101 }
102
106 operator char* () const
107 {
108 return Line();
109 }
110
116 virtual unsigned LineNumber() const
117 {
118 return m_lineNum;
119 }
120
124 unsigned Length() const
125 {
126 return m_length;
127 }
128
129protected:
134 void expandCapacity( unsigned aNewsize );
135
136 unsigned m_length;
137 unsigned m_lineNum;
138
139 char* m_line;
140 unsigned m_capacity;
141
143
144 wxString m_source;
145};
146
147
154{
155public:
170 FILE_LINE_READER( const wxString& aFileName, unsigned aStartingLineNumber = 0,
171 unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX );
172
187 FILE_LINE_READER( FILE* aFile, const wxString& aFileName, bool doOwn = true,
188 unsigned aStartingLineNumber = 0,
189 unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX );
190
195
196 char* ReadLine() override;
197
203 void Rewind()
204 {
205 rewind( m_fp );
206 m_lineNum = 0;
207 }
208
209 long int FileLength();
210 long int CurPos();
211
212protected:
213 bool m_iOwn;
214 FILE* m_fp;
215};
216
217
222{
223protected:
224 std::string m_lines;
225 size_t m_ndx;
226
227public:
228
238 STRING_LINE_READER( const std::string& aString, const wxString& aSource );
239
246 STRING_LINE_READER( const STRING_LINE_READER& aStartingPoint );
247
248 char* ReadLine() override;
249};
250
251
256{
257public:
264 INPUTSTREAM_LINE_READER( wxInputStream* aStream, const wxString& aSource );
265
266 char* ReadLine() override;
267
268protected:
269 wxInputStream* m_stream;
270};
271
272
273#define OUTPUTFMTBUFZ 500
274
291{
292protected:
293 OUTPUTFORMATTER( int aReserve = OUTPUTFMTBUFZ, char aQuoteChar = '"' ) :
294 m_buffer( aReserve, '\0' )
295 {
296 quoteChar[0] = aQuoteChar;
297 quoteChar[1] = '\0';
298 }
299
310 static const char* GetQuoteChar( const char* wrapee, const char* quote_char );
311
319 virtual void write( const char* aOutBuf, int aCount ) = 0;
320
321#if defined(__GNUG__) // The GNU C++ compiler defines this
322
323 // When used on a C++ function, we must account for the "this" pointer,
324 // so increase the STRING-INDEX and FIRST-TO_CHECK by one.
325 // See http://docs.freebsd.org/info/gcc/gcc.info.Function_Attributes.html
326 // Then to get format checking during the compile, compile with -Wall or -Wformat
327#define PRINTF_FUNC_N __attribute__( ( format( printf, 3, 4 ) ) )
328#define PRINTF_FUNC __attribute__( ( format( printf, 2, 3 ) ) )
329#else
330#define PRINTF_FUNC_N // nothing
331#define PRINTF_FUNC // nothing
332#endif
333
334public:
338 virtual ~OUTPUTFORMATTER() {}
339
350 int PRINTF_FUNC_N Print( int nestLevel, const char* fmt, ... );
351
361 int PRINTF_FUNC Print( const char* fmt, ... );
362
378 virtual const char* GetQuoteChar( const char* wrapee ) const;
379
392 virtual std::string Quotes( const std::string& aWrapee ) const;
393
394 std::string Quotew( const wxString& aWrapee ) const;
395
400 virtual bool Finish() { return true; }
401
402private:
403 std::vector<char> m_buffer;
404 char quoteChar[2];
405
406 int sprint( const char* fmt, ... );
407 int vprint( const char* fmt, va_list ap );
408
409};
410
411
418{
419public:
423 STRING_FORMATTER( int aReserve = OUTPUTFMTBUFZ, char aQuoteChar = '"' ) :
424 OUTPUTFORMATTER( aReserve, aQuoteChar )
425 {
426 }
427
431 void Clear()
432 {
433 m_mystring.clear();
434 }
435
439 void StripUseless();
440
441 const std::string& GetString()
442 {
443 return m_mystring;
444 }
445
446 std::string& MutableString()
447 {
448 return m_mystring;
449 }
450
451protected:
452 void write( const char* aOutBuf, int aCount ) override;
453
454private:
455 std::string m_mystring;
456};
457
458
470{
471public:
472
481 FILE_OUTPUTFORMATTER( const wxString& aFileName, const wxChar* aMode = wxT( "wt" ),
482 char aQuoteChar = '"' );
483
485
494 bool Finish() override;
495
496protected:
497 void write( const char* aOutBuf, int aCount ) override;
498
499 FILE* m_fp;
500 wxString m_filename;
501 wxString m_tempPath;
503};
504
505
507{
508public:
509 PRETTIFIED_FILE_OUTPUTFORMATTER( const wxString& aFileName,
510 KICAD_FORMAT::FORMAT_MODE aFormatMode = KICAD_FORMAT::FORMAT_MODE::NORMAL,
511 const wxChar* aMode = wxT( "wt" ), char aQuoteChar = '"' );
512
514
524 bool Finish() override;
525
526protected:
527 void write( const char* aOutBuf, int aCount ) override;
528
529private:
530 FILE* m_fp;
531 wxString m_filename;
532 wxString m_tempPath;
534 std::string m_buf;
535 KICAD_FORMAT::FORMAT_MODE m_mode;
536};
537
538
539#endif // RICHIO_H_
void Rewind()
Rewind the file and resets the line number back to zero.
Definition richio.h:203
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:156
FILE * m_fp
I may own this file, but might not.
Definition richio.h:214
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition richio.cpp:208
bool m_iOwn
if I own the file, I'll promise to close it, else not.
Definition richio.h:213
FILE * m_fp
takes ownership; points at the temp file
Definition richio.h:499
bool m_committed
set true once Finish() has renamed into place
Definition richio.h:502
FILE_OUTPUTFORMATTER(const wxString &aFileName, const wxChar *aMode=wxT("wt"), char aQuoteChar='"' )
Definition richio.cpp:621
void write(const char *aOutBuf, int aCount) override
Should be coded in the interface implementation (derived) classes.
Definition richio.cpp:661
wxString m_filename
final destination path
Definition richio.h:500
wxString m_tempPath
sibling temp file being written
Definition richio.h:501
bool Finish() override
Flushes the temp file to disk and atomically renames it over the final target path.
Definition richio.cpp:642
wxInputStream * m_stream
The input stream to read. No ownership of this pointer.
Definition richio.h:269
INPUTSTREAM_LINE_READER(wxInputStream *aStream, const wxString &aSource)
Construct a LINE_READER from a wxInputStream object.
Definition richio.cpp:297
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition richio.cpp:306
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
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:90
unsigned m_maxLineLength
maximum allowed capacity using resizing.
Definition richio.h:142
unsigned m_length
no. bytes in line before trailing nul.
Definition richio.h:136
unsigned m_capacity
no. bytes allocated for line.
Definition richio.h:140
char * m_line
the read line of UTF8 text
Definition richio.h:139
unsigned m_lineNum
Definition richio.h:137
virtual unsigned LineNumber() const
Return the line number of the last line read from this LINE_READER.
Definition richio.h:116
wxString m_source
origin of text lines, e.g. filename or "clipboard"
Definition richio.h:144
unsigned Length() const
Return the number of bytes in the last line read from this LINE_READER.
Definition richio.h:124
char * Line() const
Return a pointer to the last line that was read in.
Definition richio.h:98
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:410
std::vector< char > m_buffer
Definition richio.h:403
OUTPUTFORMATTER(int aReserve=OUTPUTFMTBUFZ, char aQuoteChar='"' )
Definition richio.h:293
virtual bool Finish()
Performs any cleanup needed at the end of a write.
Definition richio.h:400
char quoteChar[2]
Definition richio.h:404
virtual ~OUTPUTFORMATTER()
This is a polymorphic class that can validly be handled by a pointer to the base class.
Definition richio.h:338
int vprint(const char *fmt, va_list ap)
Definition richio.cpp:384
void write(const char *aOutBuf, int aCount) override
Should be coded in the interface implementation (derived) classes.
Definition richio.cpp:727
wxString m_filename
final destination path
Definition richio.h:531
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:668
wxString m_tempPath
sibling temp file being written
Definition richio.h:532
bool m_committed
set true once rename has landed
Definition richio.h:533
bool Finish() override
Runs prettification over the buffered bytes, writes them to the sibling temp file,...
Definition richio.cpp:696
KICAD_FORMAT::FORMAT_MODE m_mode
Definition richio.h:535
std::string & MutableString()
Definition richio.h:446
const std::string & GetString()
Definition richio.h:441
std::string m_mystring
Definition richio.h:455
STRING_FORMATTER(int aReserve=OUTPUTFMTBUFZ, char aQuoteChar='"' )
Reserve space in the buffer.
Definition richio.h:423
void Clear()
Clear the buffer and empties the internal string.
Definition richio.h:431
std::string m_lines
Definition richio.h:224
STRING_LINE_READER(const std::string &aString, const wxString &aSource)
Construct a string line reader.
Definition richio.cpp:242
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition richio.cpp:265
#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:53
#define PRINTF_FUNC
Definition richio.h:331
#define PRINTF_FUNC_N
Definition richio.h:330
#define OUTPUTFMTBUFZ
default buffer size for any OUTPUT_FORMATTER
Definition richio.h:273
#define LINE_READER_LINE_DEFAULT_MAX
Definition richio.h:54