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 __attribute__( ( format( printf, 3, 4 ) ) )
359#else
360#define PRINTF_FUNC // nothing
361#endif
362
363public:
367 virtual ~OUTPUTFORMATTER() {}
368
379 int PRINTF_FUNC Print( int nestLevel, const char* fmt, ... );
380
396 virtual const char* GetQuoteChar( const char* wrapee ) const;
397
410 virtual std::string Quotes( const std::string& aWrapee ) const;
411
412 std::string Quotew( const wxString& aWrapee ) const;
413
418 virtual bool Finish() { return true; }
419
420private:
421 std::vector<char> m_buffer;
422 char quoteChar[2];
423
424 int sprint( const char* fmt, ... );
425 int vprint( const char* fmt, va_list ap );
426
427};
428
429
436{
437public:
441 STRING_FORMATTER( int aReserve = OUTPUTFMTBUFZ, char aQuoteChar = '"' ) :
442 OUTPUTFORMATTER( aReserve, aQuoteChar )
443 {
444 }
445
449 void Clear()
450 {
451 m_mystring.clear();
452 }
453
457 void StripUseless();
458
459 const std::string& GetString()
460 {
461 return m_mystring;
462 }
463
464protected:
465 void write( const char* aOutBuf, int aCount ) override;
466
467private:
468 std::string m_mystring;
469};
470
471
478{
479public:
480
489 FILE_OUTPUTFORMATTER( const wxString& aFileName, const wxChar* aMode = wxT( "wt" ),
490 char aQuoteChar = '"' );
491
493
494protected:
495 void write( const char* aOutBuf, int aCount ) override;
496
497 FILE* m_fp;
498 wxString m_filename;
499};
500
501
503{
504public:
505 PRETTIFIED_FILE_OUTPUTFORMATTER( const wxString& aFileName, const wxChar* aMode = wxT( "wt" ),
506 char aQuoteChar = '"' );
507
509
514 bool Finish() override;
515
516protected:
517 void write( const char* aOutBuf, int aCount ) override;
518
519private:
520 FILE* m_fp;
521 std::string m_buf;
522};
523
524
525#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:478
FILE * m_fp
takes ownership
Definition: richio.h:497
wxString m_filename
Definition: richio.h:498
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:421
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:418
virtual ~OUTPUTFORMATTER()
This is a polymorphic class that can validly be handled by a pointer to the base class.
Definition: richio.h:367
Implement an OUTPUTFORMATTER to a memory buffer.
Definition: richio.h:436
const std::string & GetString()
Definition: richio.h:459
std::string m_mystring
Definition: richio.h:468
STRING_FORMATTER(int aReserve=OUTPUTFMTBUFZ, char aQuoteChar='"' )
Reserve space in the buffer.
Definition: richio.h:441
void Clear()
Clear the buffer and empties the internal string.
Definition: richio.h:449
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:360
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 OUTPUTFMTBUFZ
default buffer size for any OUTPUT_FORMATTER
Definition: richio.h:304
#define LINE_READER_LINE_DEFAULT_MAX
Definition: richio.h:85