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
331 virtual ~OUTPUTFORMATTER() {}
332
343 static const char* GetQuoteChar( const char* wrapee, const char* quote_char );
344
352 virtual void write( const char* aOutBuf, int aCount ) = 0;
353
354#if defined(__GNUG__) // The GNU C++ compiler defines this
355
356 // When used on a C++ function, we must account for the "this" pointer,
357 // so increase the STRING-INDEX and FIRST-TO_CHECK by one.
358 // See http://docs.freebsd.org/info/gcc/gcc.info.Function_Attributes.html
359 // Then to get format checking during the compile, compile with -Wall or -Wformat
360#define PRINTF_FUNC __attribute__( ( format( printf, 3, 4 ) ) )
361#else
362#define PRINTF_FUNC // nothing
363#endif
364
365public:
376 int PRINTF_FUNC Print( int nestLevel, const char* fmt, ... );
377
393 virtual const char* GetQuoteChar( const char* wrapee ) const;
394
407 virtual std::string Quotes( const std::string& aWrapee ) const;
408
409 std::string Quotew( const wxString& aWrapee ) const;
410
415 virtual bool Finish() { return true; }
416
417private:
418 std::vector<char> m_buffer;
419 char quoteChar[2];
420
421 int sprint( const char* fmt, ... );
422 int vprint( const char* fmt, va_list ap );
423
424};
425
426
433{
434public:
438 STRING_FORMATTER( int aReserve = OUTPUTFMTBUFZ, char aQuoteChar = '"' ) :
439 OUTPUTFORMATTER( aReserve, aQuoteChar )
440 {
441 }
442
446 void Clear()
447 {
448 m_mystring.clear();
449 }
450
454 void StripUseless();
455
456 const std::string& GetString()
457 {
458 return m_mystring;
459 }
460
461protected:
462 void write( const char* aOutBuf, int aCount ) override;
463
464private:
465 std::string m_mystring;
466};
467
468
475{
476public:
477
486 FILE_OUTPUTFORMATTER( const wxString& aFileName, const wxChar* aMode = wxT( "wt" ),
487 char aQuoteChar = '"' );
488
490
491protected:
492 void write( const char* aOutBuf, int aCount ) override;
493
494 FILE* m_fp;
495 wxString m_filename;
496};
497
498
500{
501public:
502 PRETTIFIED_FILE_OUTPUTFORMATTER( const wxString& aFileName, const wxChar* aMode = wxT( "wt" ),
503 char aQuoteChar = '"' );
504
506
511 bool Finish() override;
512
513protected:
514 void write( const char* aOutBuf, int aCount ) override;
515
516private:
517 FILE* m_fp;
518 std::string m_buf;
519};
520
521
522#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:475
FILE * m_fp
takes ownership
Definition: richio.h:494
wxString m_filename
Definition: richio.h:495
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:418
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:415
virtual ~OUTPUTFORMATTER()
Definition: richio.h:331
Implement an OUTPUTFORMATTER to a memory buffer.
Definition: richio.h:433
const std::string & GetString()
Definition: richio.h:456
std::string m_mystring
Definition: richio.h:465
STRING_FORMATTER(int aReserve=OUTPUTFMTBUFZ, char aQuoteChar='"' )
Reserve space in the buffer.
Definition: richio.h:438
void Clear()
Clear the buffer and empties the internal string.
Definition: richio.h:446
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 OUTPUTFMTBUFZ
default buffer size for any OUTPUT_FORMATTER
Definition: richio.h:304
#define LINE_READER_LINE_DEFAULT_MAX
Definition: richio.h:85