KiCad PCB EDA Suite
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 (C) 2016-2020 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, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#ifndef RICHIO_H_
26#define RICHIO_H_
27
28// This file defines 3 classes useful for working with DSN text files and is named
29// "richio" after its author, Richard Hollenbeck, aka Dick Hollenbeck.
30
31
32#include <vector>
33#include <utf8.h>
34
35// I really did not want to be dependent on wxWidgets in richio
36// but the errorText needs to be wide char so wxString rules.
37#include <cstdio>
38#include <wx/string.h>
39#include <wx/stream.h>
40
41#include <ki_exception.h>
42
43
52int
53#if defined(__GNUG__)
54 __attribute__ ((format (printf, 2, 3)))
55#endif
56 StrPrintf( std::string* aResult, const char* aFormat, ... );
57
58
66std::string
67#if defined(__GNUG__)
68 __attribute__ ((format (printf, 1, 2)))
69#endif
70 StrPrintf( const char* format, ... );
71
72
73#define LINE_READER_LINE_DEFAULT_MAX 1000000
74#define LINE_READER_LINE_INITIAL_SIZE 5000
75
81{
82public:
83
88 LINE_READER( unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX );
89
90 virtual ~LINE_READER();
91
101 virtual char* ReadLine() = 0;
102
109 virtual const wxString& GetSource() const
110 {
111 return m_source;
112 }
113
117 char* Line() const
118 {
119 return m_line;
120 }
121
125 operator char* () const
126 {
127 return Line();
128 }
129
135 virtual unsigned LineNumber() const
136 {
137 return m_lineNum;
138 }
139
143 unsigned Length() const
144 {
145 return m_length;
146 }
147
148protected:
153 void expandCapacity( unsigned aNewsize );
154
155 unsigned m_length;
156 unsigned m_lineNum;
157
158 char* m_line;
159 unsigned m_capacity;
160
162
163 wxString m_source;
164};
165
166
173{
174public:
189 FILE_LINE_READER( const wxString& aFileName, unsigned aStartingLineNumber = 0,
190 unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX );
191
206 FILE_LINE_READER( FILE* aFile, const wxString& aFileName, bool doOwn = true,
207 unsigned aStartingLineNumber = 0,
208 unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX );
209
214
215 char* ReadLine() override;
216
222 void Rewind()
223 {
224 rewind( m_fp );
225 m_lineNum = 0;
226 }
227
228 long int FileLength();
229 long int CurPos();
230
231protected:
232 bool m_iOwn;
233 FILE* m_fp;
234};
235
236
241{
242protected:
243 std::string m_lines;
244 size_t m_ndx;
245
246public:
247
257 STRING_LINE_READER( const std::string& aString, const wxString& aSource );
258
265 STRING_LINE_READER( const STRING_LINE_READER& aStartingPoint );
266
267 char* ReadLine() override;
268};
269
270
275{
276public:
283 INPUTSTREAM_LINE_READER( wxInputStream* aStream, const wxString& aSource );
284
285 char* ReadLine() override;
286
287protected:
288 wxInputStream* m_stream; //< The input stream to read. No ownership of this pointer.
289};
290
291
292#define OUTPUTFMTBUFZ 500
293
310{
311protected:
312 OUTPUTFORMATTER( int aReserve = OUTPUTFMTBUFZ, char aQuoteChar = '"' ) :
313 m_buffer( aReserve, '\0' )
314 {
315 quoteChar[0] = aQuoteChar;
316 quoteChar[1] = '\0';
317 }
318
319 virtual ~OUTPUTFORMATTER() {}
320
331 static const char* GetQuoteChar( const char* wrapee, const char* quote_char );
332
340 virtual void write( const char* aOutBuf, int aCount ) = 0;
341
342#if defined(__GNUG__) // The GNU C++ compiler defines this
343
344 // When used on a C++ function, we must account for the "this" pointer,
345 // so increase the STRING-INDEX and FIRST-TO_CHECK by one.
346 // See http://docs.freebsd.org/info/gcc/gcc.info.Function_Attributes.html
347 // Then to get format checking during the compile, compile with -Wall or -Wformat
348#define PRINTF_FUNC __attribute__( ( format( printf, 3, 4 ) ) )
349#else
350#define PRINTF_FUNC // nothing
351#endif
352
353public:
364 int PRINTF_FUNC Print( int nestLevel, const char* fmt, ... );
365
381 virtual const char* GetQuoteChar( const char* wrapee ) const;
382
395 virtual std::string Quotes( const std::string& aWrapee ) const;
396
397 std::string Quotew( const wxString& aWrapee ) const;
398
399private:
400 std::vector<char> m_buffer;
401 char quoteChar[2];
402
403 int sprint( const char* fmt, ... );
404 int vprint( const char* fmt, va_list ap );
405
406};
407
408
415{
416public:
420 STRING_FORMATTER( int aReserve = OUTPUTFMTBUFZ, char aQuoteChar = '"' ) :
421 OUTPUTFORMATTER( aReserve, aQuoteChar )
422 {
423 }
424
428 void Clear()
429 {
430 m_mystring.clear();
431 }
432
436 void StripUseless();
437
438 const std::string& GetString()
439 {
440 return m_mystring;
441 }
442
443protected:
444 void write( const char* aOutBuf, int aCount ) override;
445
446private:
447 std::string m_mystring;
448};
449
450
457{
458public:
459
468 FILE_OUTPUTFORMATTER( const wxString& aFileName, const wxChar* aMode = wxT( "wt" ),
469 char aQuoteChar = '"' );
470
472
473protected:
474 void write( const char* aOutBuf, int aCount ) override;
475
476 FILE* m_fp;
477 wxString m_filename;
478};
479
480
487{
488 wxOutputStream& m_os;
489
490public:
495 STREAM_OUTPUTFORMATTER( wxOutputStream& aStream, char aQuoteChar = '"' ) :
496 OUTPUTFORMATTER( OUTPUTFMTBUFZ, aQuoteChar ),
497 m_os( aStream )
498 {
499 }
500
501protected:
502 void write( const char* aOutBuf, int aCount ) override;
503};
504
505#endif // RICHIO_H_
A LINE_READER that reads from an open file.
Definition: richio.h:173
~FILE_LINE_READER()
May or may not close the open file, depending on doOwn in constructor.
Definition: richio.cpp:196
void Rewind()
Rewind the file and resets the line number back to zero.
Definition: richio.h:222
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:167
FILE * m_fp
I may own this file, but might not.
Definition: richio.h:233
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition: richio.cpp:219
bool m_iOwn
if I own the file, I'll promise to close it, else not.
Definition: richio.h:232
long int FileLength()
Definition: richio.cpp:203
long int CurPos()
Definition: richio.cpp:213
Used for text file output.
Definition: richio.h:457
FILE * m_fp
takes ownership
Definition: richio.h:476
FILE_OUTPUTFORMATTER(const wxString &aFileName, const wxChar *aMode=wxT("wt"), char aQuoteChar='"' )
Definition: richio.cpp:533
void write(const char *aOutBuf, int aCount) override
Should be coded in the interface implementation (derived) classes.
Definition: richio.cpp:552
wxString m_filename
Definition: richio.h:477
A LINE_READER that reads from a wxInputStream object.
Definition: richio.h:275
wxInputStream * m_stream
Definition: richio.h:288
INPUTSTREAM_LINE_READER(wxInputStream *aStream, const wxString &aSource)
Construct a LINE_READER from a wxInputStream object.
Definition: richio.cpp:306
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition: richio.cpp:315
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition: richio.h:81
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:111
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:109
unsigned m_maxLineLength
maximum allowed capacity using resizing.
Definition: richio.h:161
unsigned m_length
no. bytes in line before trailing nul.
Definition: richio.h:155
unsigned m_capacity
no. bytes allocated for line.
Definition: richio.h:159
void expandCapacity(unsigned aNewsize)
Will expand the capacity of line up to maxLineLength but not greater, so be careful about making assu...
Definition: richio.cpp:140
char * m_line
the read line of UTF8 text
Definition: richio.h:158
unsigned m_lineNum
Definition: richio.h:156
virtual unsigned LineNumber() const
Return the line number of the last line read from this LINE_READER.
Definition: richio.h:135
virtual ~LINE_READER()
Definition: richio.cpp:134
wxString m_source
origin of text lines, e.g. filename or "clipboard"
Definition: richio.h:163
unsigned Length() const
Return the number of bytes in the last line read from this LINE_READER.
Definition: richio.h:143
char * Line() const
Return a pointer to the last line that was read in.
Definition: richio.h:117
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:310
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:419
std::vector< char > m_buffer
Definition: richio.h:400
OUTPUTFORMATTER(int aReserve=OUTPUTFMTBUFZ, char aQuoteChar='"' )
Definition: richio.h:312
char quoteChar[2]
Definition: richio.h:401
virtual ~OUTPUTFORMATTER()
Definition: richio.h:319
int vprint(const char *fmt, va_list ap)
Definition: richio.cpp:393
std::string Quotew(const wxString &aWrapee) const
Definition: richio.cpp:499
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:431
static const char * GetQuoteChar(const char *wrapee, const char *quote_char)
Perform quote character need determination according to the Specctra DSN specification.
Definition: richio.cpp:353
virtual std::string Quotes(const std::string &aWrapee) const
Check aWrapee input string for a need to be quoted (e.g.
Definition: richio.cpp:460
Implement an OUTPUTFORMATTER to a wxWidgets wxOutputStream.
Definition: richio.h:487
STREAM_OUTPUTFORMATTER(wxOutputStream &aStream, char aQuoteChar='"' )
This can take any number of wxOutputStream derivations, so it can write to a file,...
Definition: richio.h:495
void write(const char *aOutBuf, int aCount) override
Should be coded in the interface implementation (derived) classes.
Definition: richio.cpp:559
wxOutputStream & m_os
Definition: richio.h:488
Implement an OUTPUTFORMATTER to a memory buffer.
Definition: richio.h:415
void write(const char *aOutBuf, int aCount) override
Should be coded in the interface implementation (derived) classes.
Definition: richio.cpp:512
void StripUseless()
Removes whitespace, '(', and ')' from the string.
Definition: richio.cpp:517
const std::string & GetString()
Definition: richio.h:438
std::string m_mystring
Definition: richio.h:447
STRING_FORMATTER(int aReserve=OUTPUTFMTBUFZ, char aQuoteChar='"' )
Reserve space in the buffer.
Definition: richio.h:420
void Clear()
Clear the buffer and empties the internal string.
Definition: richio.h:428
Is a LINE_READER that reads from a multiline 8 bit wide std::string.
Definition: richio.h:241
std::string m_lines
Definition: richio.h:243
STRING_LINE_READER(const std::string &aString, const wxString &aSource)
Construct a string line reader.
Definition: richio.cpp:253
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition: richio.cpp:276
int StrPrintf(std::string *aResult, const char *aFormat,...)
This is like sprintf() but the output is appended to a std::string instead of to a character array.
Definition: richio.cpp:84
#define PRINTF_FUNC
Definition: richio.h:350
#define OUTPUTFMTBUFZ
default buffer size for any OUTPUT_FORMATTER
Definition: richio.h:292
#define LINE_READER_LINE_DEFAULT_MAX
Definition: richio.h:73