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
54#if defined(__GNUG__)
55 __attribute__ ((format (printf, 2, 3)))
56#endif
57 StrPrintf( std::string* aResult, const char* aFormat, ... );
58
59
67KICOMMON_API std::string
68#if defined(__GNUG__)
69 __attribute__ ((format (printf, 1, 2)))
70#endif
71 StrPrintf( const char* format, ... );
72
73
83KICOMMON_API wxString SafeReadFile( const wxString& aFilePath, const wxString& aReadType );
84
85
86#define LINE_READER_LINE_DEFAULT_MAX 1000000
87#define LINE_READER_LINE_INITIAL_SIZE 5000
88
94{
95public:
96
101 LINE_READER( unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX );
102
103 virtual ~LINE_READER();
104
114 virtual char* ReadLine() = 0;
115
122 virtual const wxString& GetSource() const
123 {
124 return m_source;
125 }
126
130 char* Line() const
131 {
132 return m_line;
133 }
134
138 operator char* () const
139 {
140 return Line();
141 }
142
148 virtual unsigned LineNumber() const
149 {
150 return m_lineNum;
151 }
152
156 unsigned Length() const
157 {
158 return m_length;
159 }
160
161protected:
166 void expandCapacity( unsigned aNewsize );
167
168 unsigned m_length;
169 unsigned m_lineNum;
170
171 char* m_line;
172 unsigned m_capacity;
173
175
176 wxString m_source;
177};
178
179
186{
187public:
202 FILE_LINE_READER( const wxString& aFileName, unsigned aStartingLineNumber = 0,
203 unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX );
204
219 FILE_LINE_READER( FILE* aFile, const wxString& aFileName, bool doOwn = true,
220 unsigned aStartingLineNumber = 0,
221 unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX );
222
227
228 char* ReadLine() override;
229
235 void Rewind()
236 {
237 rewind( m_fp );
238 m_lineNum = 0;
239 }
240
241 long int FileLength();
242 long int CurPos();
243
244protected:
245 bool m_iOwn;
246 FILE* m_fp;
247};
248
249
254{
255protected:
256 std::string m_lines;
257 size_t m_ndx;
258
259public:
260
270 STRING_LINE_READER( const std::string& aString, const wxString& aSource );
271
278 STRING_LINE_READER( const STRING_LINE_READER& aStartingPoint );
279
280 char* ReadLine() override;
281};
282
283
288{
289public:
296 INPUTSTREAM_LINE_READER( wxInputStream* aStream, const wxString& aSource );
297
298 char* ReadLine() override;
299
300protected:
301 wxInputStream* m_stream;
302};
303
304
305#define OUTPUTFMTBUFZ 500
306
323{
324protected:
325 OUTPUTFORMATTER( int aReserve = OUTPUTFMTBUFZ, char aQuoteChar = '"' ) :
326 m_buffer( aReserve, '\0' )
327 {
328 quoteChar[0] = aQuoteChar;
329 quoteChar[1] = '\0';
330 }
331
342 static const char* GetQuoteChar( const char* wrapee, const char* quote_char );
343
351 virtual void write( const char* aOutBuf, int aCount ) = 0;
352
353#if defined(__GNUG__) // The GNU C++ compiler defines this
354
355 // When used on a C++ function, we must account for the "this" pointer,
356 // so increase the STRING-INDEX and FIRST-TO_CHECK by one.
357 // See http://docs.freebsd.org/info/gcc/gcc.info.Function_Attributes.html
358 // Then to get format checking during the compile, compile with -Wall or -Wformat
359#define PRINTF_FUNC_N __attribute__( ( format( printf, 3, 4 ) ) )
360#define PRINTF_FUNC __attribute__( ( format( printf, 2, 3 ) ) )
361#else
362#define PRINTF_FUNC_N // nothing
363#define PRINTF_FUNC // nothing
364#endif
365
366public:
370 virtual ~OUTPUTFORMATTER() {}
371
382 int PRINTF_FUNC_N Print( int nestLevel, const char* fmt, ... );
383
393 int PRINTF_FUNC Print( const char* fmt, ... );
394
410 virtual const char* GetQuoteChar( const char* wrapee ) const;
411
424 virtual std::string Quotes( const std::string& aWrapee ) const;
425
426 std::string Quotew( const wxString& aWrapee ) const;
427
432 virtual bool Finish() { return true; }
433
434private:
435 std::vector<char> m_buffer;
436 char quoteChar[2];
437
438 int sprint( const char* fmt, ... );
439 int vprint( const char* fmt, va_list ap );
440
441};
442
443
450{
451public:
455 STRING_FORMATTER( int aReserve = OUTPUTFMTBUFZ, char aQuoteChar = '"' ) :
456 OUTPUTFORMATTER( aReserve, aQuoteChar )
457 {
458 }
459
463 void Clear()
464 {
465 m_mystring.clear();
466 }
467
471 void StripUseless();
472
473 const std::string& GetString()
474 {
475 return m_mystring;
476 }
477
478 std::string& MutableString()
479 {
480 return m_mystring;
481 }
482
483protected:
484 void write( const char* aOutBuf, int aCount ) override;
485
486private:
487 std::string m_mystring;
488};
489
490
497{
498public:
499
508 FILE_OUTPUTFORMATTER( const wxString& aFileName, const wxChar* aMode = wxT( "wt" ),
509 char aQuoteChar = '"' );
510
512
513protected:
514 void write( const char* aOutBuf, int aCount ) override;
515
516 FILE* m_fp;
517 wxString m_filename;
518};
519
520
522{
523public:
524 PRETTIFIED_FILE_OUTPUTFORMATTER( const wxString& aFileName,
525 KICAD_FORMAT::FORMAT_MODE aFormatMode = KICAD_FORMAT::FORMAT_MODE::NORMAL,
526 const wxChar* aMode = wxT( "wt" ), char aQuoteChar = '"' );
527
529
534 bool Finish() override;
535
536protected:
537 void write( const char* aOutBuf, int aCount ) override;
538
539private:
540 FILE* m_fp;
541 std::string m_buf;
542 KICAD_FORMAT::FORMAT_MODE m_mode;
543};
544
545
546#endif // RICHIO_H_
void Rewind()
Rewind the file and resets the line number back to zero.
Definition richio.h:235
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:199
FILE * m_fp
I may own this file, but might not.
Definition richio.h:246
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition richio.cpp:251
bool m_iOwn
if I own the file, I'll promise to close it, else not.
Definition richio.h:245
FILE * m_fp
takes ownership
Definition richio.h:516
FILE_OUTPUTFORMATTER(const wxString &aFileName, const wxChar *aMode=wxT("wt"), char aQuoteChar='"' )
Definition richio.cpp:585
void write(const char *aOutBuf, int aCount) override
Should be coded in the interface implementation (derived) classes.
Definition richio.cpp:604
wxString m_filename
Definition richio.h:517
wxInputStream * m_stream
The input stream to read. No ownership of this pointer.
Definition richio.h:301
INPUTSTREAM_LINE_READER(wxInputStream *aStream, const wxString &aSource)
Construct a LINE_READER from a wxInputStream object.
Definition richio.cpp:340
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition richio.cpp:349
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:143
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:122
unsigned m_maxLineLength
maximum allowed capacity using resizing.
Definition richio.h:174
unsigned m_length
no. bytes in line before trailing nul.
Definition richio.h:168
unsigned m_capacity
no. bytes allocated for line.
Definition richio.h:172
char * m_line
the read line of UTF8 text
Definition richio.h:171
unsigned m_lineNum
Definition richio.h:169
virtual unsigned LineNumber() const
Return the line number of the last line read from this LINE_READER.
Definition richio.h:148
wxString m_source
origin of text lines, e.g. filename or "clipboard"
Definition richio.h:176
unsigned Length() const
Return the number of bytes in the last line read from this LINE_READER.
Definition richio.h:156
char * Line() const
Return a pointer to the last line that was read in.
Definition richio.h:130
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:453
std::vector< char > m_buffer
Definition richio.h:435
OUTPUTFORMATTER(int aReserve=OUTPUTFMTBUFZ, char aQuoteChar='"' )
Definition richio.h:325
virtual bool Finish()
Performs any cleanup needed at the end of a write.
Definition richio.h:432
char quoteChar[2]
Definition richio.h:436
virtual ~OUTPUTFORMATTER()
This is a polymorphic class that can validly be handled by a pointer to the base class.
Definition richio.h:370
void write(const char *aOutBuf, int aCount) override
Should be coded in the interface implementation (derived) classes.
Definition richio.cpp:656
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:611
bool Finish() override
Performs prettification and writes the stored buffer to the file.
Definition richio.cpp:639
KICAD_FORMAT::FORMAT_MODE m_mode
Definition richio.h:542
std::string & MutableString()
Definition richio.h:478
const std::string & GetString()
Definition richio.h:473
std::string m_mystring
Definition richio.h:487
STRING_FORMATTER(int aReserve=OUTPUTFMTBUFZ, char aQuoteChar='"' )
Reserve space in the buffer.
Definition richio.h:455
void Clear()
Clear the buffer and empties the internal string.
Definition richio.h:463
std::string m_lines
Definition richio.h:256
STRING_LINE_READER(const std::string &aString, const wxString &aSource)
Construct a string line reader.
Definition richio.cpp:285
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition richio.cpp:308
#define KICOMMON_API
Definition kicommon.h:28
static int vprint(std::string *result, const char *format, va_list ap)
Definition richio.cpp:53
KICOMMON_API wxString SafeReadFile(const wxString &aFilePath, const wxString &aReadType)
Nominally opens a file and reads it into a string.
Definition richio.cpp:96
#define PRINTF_FUNC
Definition richio.h:363
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:71
#define PRINTF_FUNC_N
Definition richio.h:362
#define OUTPUTFMTBUFZ
default buffer size for any OUTPUT_FORMATTER
Definition richio.h:305
#define LINE_READER_LINE_DEFAULT_MAX
Definition richio.h:86