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, 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 <core/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#include <kicommon.h>
44
45
55KICOMMON_API wxString SafeReadFile( const wxString& aFilePath, const wxString& aReadType );
56
57
58#define LINE_READER_LINE_DEFAULT_MAX 1000000
59#define LINE_READER_LINE_INITIAL_SIZE 5000
60
66{
67public:
68
73 LINE_READER( unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX );
74
75 virtual ~LINE_READER();
76
86 virtual char* ReadLine() = 0;
87
94 virtual const wxString& GetSource() const
95 {
96 return m_source;
97 }
98
102 char* Line() const
103 {
104 return m_line;
105 }
106
110 operator char* () const
111 {
112 return Line();
113 }
114
120 virtual unsigned LineNumber() const
121 {
122 return m_lineNum;
123 }
124
128 unsigned Length() const
129 {
130 return m_length;
131 }
132
133protected:
138 void expandCapacity( unsigned aNewsize );
139
140 unsigned m_length;
141 unsigned m_lineNum;
142
143 char* m_line;
144 unsigned m_capacity;
145
147
148 wxString m_source;
149};
150
151
158{
159public:
174 FILE_LINE_READER( const wxString& aFileName, unsigned aStartingLineNumber = 0,
175 unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX );
176
191 FILE_LINE_READER( FILE* aFile, const wxString& aFileName, bool doOwn = true,
192 unsigned aStartingLineNumber = 0,
193 unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX );
194
199
200 char* ReadLine() override;
201
207 void Rewind()
208 {
209 rewind( m_fp );
210 m_lineNum = 0;
211 }
212
213 long int FileLength();
214 long int CurPos();
215
216protected:
217 bool m_iOwn;
218 FILE* m_fp;
219};
220
221
226{
227protected:
228 std::string m_lines;
229 size_t m_ndx;
230
231public:
232
242 STRING_LINE_READER( const std::string& aString, const wxString& aSource );
243
250 STRING_LINE_READER( const STRING_LINE_READER& aStartingPoint );
251
252 char* ReadLine() override;
253};
254
255
260{
261public:
268 INPUTSTREAM_LINE_READER( wxInputStream* aStream, const wxString& aSource );
269
270 char* ReadLine() override;
271
272protected:
273 wxInputStream* m_stream;
274};
275
276
277#define OUTPUTFMTBUFZ 500
278
295{
296protected:
297 OUTPUTFORMATTER( int aReserve = OUTPUTFMTBUFZ, char aQuoteChar = '"' ) :
298 m_buffer( aReserve, '\0' )
299 {
300 quoteChar[0] = aQuoteChar;
301 quoteChar[1] = '\0';
302 }
303
314 static const char* GetQuoteChar( const char* wrapee, const char* quote_char );
315
323 virtual void write( const char* aOutBuf, int aCount ) = 0;
324
325#if defined(__GNUG__) // The GNU C++ compiler defines this
326
327 // When used on a C++ function, we must account for the "this" pointer,
328 // so increase the STRING-INDEX and FIRST-TO_CHECK by one.
329 // See http://docs.freebsd.org/info/gcc/gcc.info.Function_Attributes.html
330 // Then to get format checking during the compile, compile with -Wall or -Wformat
331#define PRINTF_FUNC_N __attribute__( ( format( printf, 3, 4 ) ) )
332#define PRINTF_FUNC __attribute__( ( format( printf, 2, 3 ) ) )
333#else
334#define PRINTF_FUNC_N // nothing
335#define PRINTF_FUNC // nothing
336#endif
337
338public:
342 virtual ~OUTPUTFORMATTER() {}
343
354 int PRINTF_FUNC_N Print( int nestLevel, const char* fmt, ... );
355
365 int PRINTF_FUNC Print( const char* fmt, ... );
366
382 virtual const char* GetQuoteChar( const char* wrapee ) const;
383
396 virtual std::string Quotes( const std::string& aWrapee ) const;
397
398 std::string Quotew( const wxString& aWrapee ) const;
399
404 virtual bool Finish() { return true; }
405
406private:
407 std::vector<char> m_buffer;
408 char quoteChar[2];
409
410 int sprint( const char* fmt, ... );
411 int vprint( const char* fmt, va_list ap );
412
413};
414
415
422{
423public:
427 STRING_FORMATTER( int aReserve = OUTPUTFMTBUFZ, char aQuoteChar = '"' ) :
428 OUTPUTFORMATTER( aReserve, aQuoteChar )
429 {
430 }
431
435 void Clear()
436 {
437 m_mystring.clear();
438 }
439
443 void StripUseless();
444
445 const std::string& GetString()
446 {
447 return m_mystring;
448 }
449
450 std::string& MutableString()
451 {
452 return m_mystring;
453 }
454
455protected:
456 void write( const char* aOutBuf, int aCount ) override;
457
458private:
459 std::string m_mystring;
460};
461
462
469{
470public:
471
480 FILE_OUTPUTFORMATTER( const wxString& aFileName, const wxChar* aMode = wxT( "wt" ),
481 char aQuoteChar = '"' );
482
484
485protected:
486 void write( const char* aOutBuf, int aCount ) override;
487
488 FILE* m_fp;
489 wxString m_filename;
490};
491
492
494{
495public:
496 PRETTIFIED_FILE_OUTPUTFORMATTER( const wxString& aFileName,
497 KICAD_FORMAT::FORMAT_MODE aFormatMode = KICAD_FORMAT::FORMAT_MODE::NORMAL,
498 const wxChar* aMode = wxT( "wt" ), char aQuoteChar = '"' );
499
501
506 bool Finish() override;
507
508protected:
509 void write( const char* aOutBuf, int aCount ) override;
510
511private:
512 FILE* m_fp;
513 std::string m_buf;
514 KICAD_FORMAT::FORMAT_MODE m_mode;
515};
516
517
518#endif // RICHIO_H_
void Rewind()
Rewind the file and resets the line number back to zero.
Definition richio.h:207
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:218
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:217
FILE * m_fp
takes ownership
Definition richio.h:488
FILE_OUTPUTFORMATTER(const wxString &aFileName, const wxChar *aMode=wxT("wt"), char aQuoteChar='"' )
Definition richio.cpp:542
void write(const char *aOutBuf, int aCount) override
Should be coded in the interface implementation (derived) classes.
Definition richio.cpp:561
wxString m_filename
Definition richio.h:489
wxInputStream * m_stream
The input stream to read. No ownership of this pointer.
Definition richio.h:273
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:94
unsigned m_maxLineLength
maximum allowed capacity using resizing.
Definition richio.h:146
unsigned m_length
no. bytes in line before trailing nul.
Definition richio.h:140
unsigned m_capacity
no. bytes allocated for line.
Definition richio.h:144
char * m_line
the read line of UTF8 text
Definition richio.h:143
unsigned m_lineNum
Definition richio.h:141
virtual unsigned LineNumber() const
Return the line number of the last line read from this LINE_READER.
Definition richio.h:120
wxString m_source
origin of text lines, e.g. filename or "clipboard"
Definition richio.h:148
unsigned Length() const
Return the number of bytes in the last line read from this LINE_READER.
Definition richio.h:128
char * Line() const
Return a pointer to the last line that was read in.
Definition richio.h:102
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:407
OUTPUTFORMATTER(int aReserve=OUTPUTFMTBUFZ, char aQuoteChar='"' )
Definition richio.h:297
virtual bool Finish()
Performs any cleanup needed at the end of a write.
Definition richio.h:404
char quoteChar[2]
Definition richio.h:408
virtual ~OUTPUTFORMATTER()
This is a polymorphic class that can validly be handled by a pointer to the base class.
Definition richio.h:342
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:613
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:568
bool Finish() override
Performs prettification and writes the stored buffer to the file.
Definition richio.cpp:596
KICAD_FORMAT::FORMAT_MODE m_mode
Definition richio.h:514
std::string & MutableString()
Definition richio.h:450
const std::string & GetString()
Definition richio.h:445
std::string m_mystring
Definition richio.h:459
STRING_FORMATTER(int aReserve=OUTPUTFMTBUFZ, char aQuoteChar='"' )
Reserve space in the buffer.
Definition richio.h:427
void Clear()
Clear the buffer and empties the internal string.
Definition richio.h:435
std::string m_lines
Definition richio.h:228
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:28
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:335
#define PRINTF_FUNC_N
Definition richio.h:334
#define OUTPUTFMTBUFZ
default buffer size for any OUTPUT_FORMATTER
Definition richio.h:277
#define LINE_READER_LINE_DEFAULT_MAX
Definition richio.h:58