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 (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 <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>
43
53#if defined(__GNUG__)
54 __attribute__ ((format (printf, 2, 3)))
55#endif
56 StrPrintf( std::string* aResult, const char* aFormat, ... );
57
58
66KICOMMON_API std::string
67#if defined(__GNUG__)
68 __attribute__ ((format (printf, 1, 2)))
69#endif
70 StrPrintf( const char* format, ... );
71
72
82KICOMMON_API wxString SafeReadFile( const wxString& aFilePath, const wxString& aReadType );
83
84
85#define LINE_READER_LINE_DEFAULT_MAX 1000000
86#define LINE_READER_LINE_INITIAL_SIZE 5000
87
93{
94public:
95
100 LINE_READER( unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX );
101
102 virtual ~LINE_READER();
103
113 virtual char* ReadLine() = 0;
114
121 virtual const wxString& GetSource() const
122 {
123 return m_source;
124 }
125
129 char* Line() const
130 {
131 return m_line;
132 }
133
137 operator char* () const
138 {
139 return Line();
140 }
141
147 virtual unsigned LineNumber() const
148 {
149 return m_lineNum;
150 }
151
155 unsigned Length() const
156 {
157 return m_length;
158 }
159
160protected:
165 void expandCapacity( unsigned aNewsize );
166
167 unsigned m_length;
168 unsigned m_lineNum;
169
170 char* m_line;
171 unsigned m_capacity;
172
174
175 wxString m_source;
176};
177
178
185{
186public:
201 FILE_LINE_READER( const wxString& aFileName, unsigned aStartingLineNumber = 0,
202 unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX );
203
218 FILE_LINE_READER( FILE* aFile, const wxString& aFileName, bool doOwn = true,
219 unsigned aStartingLineNumber = 0,
220 unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX );
221
226
227 char* ReadLine() override;
228
234 void Rewind()
235 {
236 rewind( m_fp );
237 m_lineNum = 0;
238 }
239
240 long int FileLength();
241 long int CurPos();
242
243protected:
244 bool m_iOwn;
245 FILE* m_fp;
246};
247
248
253{
254protected:
255 std::string m_lines;
256 size_t m_ndx;
257
258public:
259
269 STRING_LINE_READER( const std::string& aString, const wxString& aSource );
270
277 STRING_LINE_READER( const STRING_LINE_READER& aStartingPoint );
278
279 char* ReadLine() override;
280};
281
282
287{
288public:
295 INPUTSTREAM_LINE_READER( wxInputStream* aStream, const wxString& aSource );
296
297 char* ReadLine() override;
298
299protected:
300 wxInputStream* m_stream; //< The input stream to read. No ownership of this pointer.
301};
302
303
304#define OUTPUTFMTBUFZ 500
305
322{
323protected:
324 OUTPUTFORMATTER( int aReserve = OUTPUTFMTBUFZ, char aQuoteChar = '"' ) :
325 m_buffer( aReserve, '\0' )
326 {
327 quoteChar[0] = aQuoteChar;
328 quoteChar[1] = '\0';
329 }
330
341 static const char* GetQuoteChar( const char* wrapee, const char* quote_char );
342
350 virtual void write( const char* aOutBuf, int aCount ) = 0;
351
352#if defined(__GNUG__) // The GNU C++ compiler defines this
353
354 // When used on a C++ function, we must account for the "this" pointer,
355 // so increase the STRING-INDEX and FIRST-TO_CHECK by one.
356 // See http://docs.freebsd.org/info/gcc/gcc.info.Function_Attributes.html
357 // Then to get format checking during the compile, compile with -Wall or -Wformat
358#define PRINTF_FUNC_N __attribute__( ( format( printf, 3, 4 ) ) )
359#define PRINTF_FUNC __attribute__( ( format( printf, 2, 3 ) ) )
360#else
361#define PRINTF_FUNC_N // nothing
362#define PRINTF_FUNC // nothing
363#endif
364
365public:
369 virtual ~OUTPUTFORMATTER() {}
370
381 int PRINTF_FUNC_N Print( int nestLevel, const char* fmt, ... );
382
392 int PRINTF_FUNC Print( const char* fmt, ... );
393
409 virtual const char* GetQuoteChar( const char* wrapee ) const;
410
423 virtual std::string Quotes( const std::string& aWrapee ) const;
424
425 std::string Quotew( const wxString& aWrapee ) const;
426
431 virtual bool Finish() { return true; }
432
433private:
434 std::vector<char> m_buffer;
435 char quoteChar[2];
436
437 int sprint( const char* fmt, ... );
438 int vprint( const char* fmt, va_list ap );
439
440};
441
442
449{
450public:
454 STRING_FORMATTER( int aReserve = OUTPUTFMTBUFZ, char aQuoteChar = '"' ) :
455 OUTPUTFORMATTER( aReserve, aQuoteChar )
456 {
457 }
458
462 void Clear()
463 {
464 m_mystring.clear();
465 }
466
470 void StripUseless();
471
472 const std::string& GetString()
473 {
474 return m_mystring;
475 }
476
477protected:
478 void write( const char* aOutBuf, int aCount ) override;
479
480private:
481 std::string m_mystring;
482};
483
484
491{
492public:
493
502 FILE_OUTPUTFORMATTER( const wxString& aFileName, const wxChar* aMode = wxT( "wt" ),
503 char aQuoteChar = '"' );
504
506
507protected:
508 void write( const char* aOutBuf, int aCount ) override;
509
510 FILE* m_fp;
511 wxString m_filename;
512};
513
514
516{
517public:
518 PRETTIFIED_FILE_OUTPUTFORMATTER( const wxString& aFileName, const wxChar* aMode = wxT( "wt" ),
519 char aQuoteChar = '"' );
520
522
527 bool Finish() override;
528
529protected:
530 void write( const char* aOutBuf, int aCount ) override;
531
532private:
533 FILE* m_fp;
534 std::string m_buf;
535};
536
537
538#endif // RICHIO_H_
A LINE_READER that reads from an open file.
Definition: richio.h:185
void Rewind()
Rewind the file and resets the line number back to zero.
Definition: richio.h:234
FILE * m_fp
I may own this file, but might not.
Definition: richio.h:245
bool m_iOwn
if I own the file, I'll promise to close it, else not.
Definition: richio.h:244
Used for text file output.
Definition: richio.h:491
FILE * m_fp
takes ownership
Definition: richio.h:510
wxString m_filename
Definition: richio.h:511
A LINE_READER that reads from a wxInputStream object.
Definition: richio.h:287
wxInputStream * m_stream
Definition: richio.h:300
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition: richio.h:93
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:121
unsigned m_maxLineLength
maximum allowed capacity using resizing.
Definition: richio.h:173
unsigned m_length
no. bytes in line before trailing nul.
Definition: richio.h:167
unsigned m_capacity
no. bytes allocated for line.
Definition: richio.h:171
char * m_line
the read line of UTF8 text
Definition: richio.h:170
unsigned m_lineNum
Definition: richio.h:168
virtual unsigned LineNumber() const
Return the line number of the last line read from this LINE_READER.
Definition: richio.h:147
wxString m_source
origin of text lines, e.g. filename or "clipboard"
Definition: richio.h:175
unsigned Length() const
Return the number of bytes in the last line read from this LINE_READER.
Definition: richio.h:155
char * Line() const
Return a pointer to the last line that was read in.
Definition: richio.h:129
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:322
virtual void write(const char *aOutBuf, int aCount)=0
Should be coded in the interface implementation (derived) classes.
std::vector< char > m_buffer
Definition: richio.h:434
OUTPUTFORMATTER(int aReserve=OUTPUTFMTBUFZ, char aQuoteChar='"' )
Definition: richio.h:324
virtual bool Finish()
Performs any cleanup needed at the end of a write.
Definition: richio.h:431
virtual ~OUTPUTFORMATTER()
This is a polymorphic class that can validly be handled by a pointer to the base class.
Definition: richio.h:369
Implement an OUTPUTFORMATTER to a memory buffer.
Definition: richio.h:449
const std::string & GetString()
Definition: richio.h:472
std::string m_mystring
Definition: richio.h:481
STRING_FORMATTER(int aReserve=OUTPUTFMTBUFZ, char aQuoteChar='"' )
Reserve space in the buffer.
Definition: richio.h:454
void Clear()
Clear the buffer and empties the internal string.
Definition: richio.h:462
Is a LINE_READER that reads from a multiline 8 bit wide std::string.
Definition: richio.h:253
std::string m_lines
Definition: richio.h:255
#define KICOMMON_API
Definition: kicommon.h:28
static int vprint(std::string *result, const char *format, va_list ap)
Definition: richio.cpp:50
KICOMMON_API wxString SafeReadFile(const wxString &aFilePath, const wxString &aReadType)
Nominally opens a file and reads it into a string.
Definition: richio.cpp:93
#define PRINTF_FUNC
Definition: richio.h:362
KICOMMON_API 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:68
#define PRINTF_FUNC_N
Definition: richio.h:361
#define OUTPUTFMTBUFZ
default buffer size for any OUTPUT_FORMATTER
Definition: richio.h:304
#define LINE_READER_LINE_DEFAULT_MAX
Definition: richio.h:85