KiCad PCB EDA Suite
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 <dick@softplc.com>
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 <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 
43 
52 int
53 #if defined(__GNUG__)
54  __attribute__ ((format (printf, 2, 3)))
55 #endif
56  StrPrintf( std::string* aResult, const char* aFormat, ... );
57 
58 
66 std::string
67 #if defined(__GNUG__)
68  __attribute__ ((format (printf, 1, 2)))
69 #endif
70  StrPrintf( const char* format, ... );
71 
72 
73 #define LINE_READER_LINE_DEFAULT_MAX 1000000
74 #define LINE_READER_LINE_INITIAL_SIZE 5000
75 
81 {
82 public:
83 
88  LINE_READER( unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX );
89 
90  virtual ~LINE_READER();
91 
101  virtual char* ReadLine() = 0;
102 
109  virtual const wxString& GetSource() const
110  {
111  return m_source;
112  }
113 
117  char* Line() const
118  {
119  return m_line;
120  }
121 
125  operator char* () const
126  {
127  return Line();
128  }
129 
135  virtual unsigned LineNumber() const
136  {
137  return m_lineNum;
138  }
139 
143  unsigned Length() const
144  {
145  return m_length;
146  }
147 
148 protected:
153  void expandCapacity( unsigned aNewsize );
154 
155  unsigned m_length;
156  unsigned m_lineNum;
157 
158  char* m_line;
159  unsigned m_capacity;
160 
161  unsigned m_maxLineLength;
162 
163  wxString m_source;
164 };
165 
166 
173 {
174 public:
189  FILE_LINE_READER( const wxString& aFileName, unsigned aStartingLineNumber = 0,
190  unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX );
191 
206  FILE_LINE_READER( FILE* aFile, const wxString& aFileName, bool doOwn = true,
207  unsigned aStartingLineNumber = 0,
208  unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX );
209 
214 
215  char* ReadLine() override;
216 
222  void Rewind()
223  {
224  rewind( m_fp );
225  m_lineNum = 0;
226  }
227 
228 protected:
229  bool m_iOwn;
230  FILE* m_fp;
231 };
232 
233 
238 {
239 protected:
240  std::string m_lines;
241  size_t m_ndx;
242 
243 public:
244 
254  STRING_LINE_READER( const std::string& aString, const wxString& aSource );
255 
262  STRING_LINE_READER( const STRING_LINE_READER& aStartingPoint );
263 
264  char* ReadLine() override;
265 };
266 
267 
272 {
273 public:
280  INPUTSTREAM_LINE_READER( wxInputStream* aStream, const wxString& aSource );
281 
282  char* ReadLine() override;
283 
284 protected:
285  wxInputStream* m_stream; //< The input stream to read. No ownership of this pointer.
286 };
287 
288 
289 #define OUTPUTFMTBUFZ 500
290 
291 
307 {
308 protected:
309  OUTPUTFORMATTER( int aReserve = OUTPUTFMTBUFZ, char aQuoteChar = '"' ) :
310  m_buffer( aReserve, '\0' )
311  {
312  quoteChar[0] = aQuoteChar;
313  quoteChar[1] = '\0';
314  }
315 
316  virtual ~OUTPUTFORMATTER() {}
317 
328  static const char* GetQuoteChar( const char* wrapee, const char* quote_char );
329 
337  virtual void write( const char* aOutBuf, int aCount ) = 0;
338 
339 #if defined(__GNUG__) // The GNU C++ compiler defines this
340 
341  // When used on a C++ function, we must account for the "this" pointer,
342  // so increase the STRING-INDEX and FIRST-TO_CHECK by one.
343  // See http://docs.freebsd.org/info/gcc/gcc.info.Function_Attributes.html
344  // Then to get format checking during the compile, compile with -Wall or -Wformat
345 #define PRINTF_FUNC __attribute__( ( format( printf, 3, 4 ) ) )
346 #else
347 #define PRINTF_FUNC // nothing
348 #endif
349 
350 public:
361  int PRINTF_FUNC Print( int nestLevel, const char* fmt, ... );
362 
378  virtual const char* GetQuoteChar( const char* wrapee ) const;
379 
392  virtual std::string Quotes( const std::string& aWrapee ) const;
393 
394  std::string Quotew( const wxString& aWrapee ) const;
395 
396 private:
397  std::vector<char> m_buffer;
398  char quoteChar[2];
399 
400  int sprint( const char* fmt, ... );
401  int vprint( const char* fmt, va_list ap );
402 
403 };
404 
405 
412 {
413 public:
417  STRING_FORMATTER( int aReserve = OUTPUTFMTBUFZ, char aQuoteChar = '"' ) :
418  OUTPUTFORMATTER( aReserve, aQuoteChar )
419  {
420  }
421 
425  void Clear()
426  {
427  m_mystring.clear();
428  }
429 
433  void StripUseless();
434 
435  const std::string& GetString()
436  {
437  return m_mystring;
438  }
439 
440 protected:
441  void write( const char* aOutBuf, int aCount ) override;
442 
443 private:
444  std::string m_mystring;
445 };
446 
447 
454 {
455 public:
456 
465  FILE_OUTPUTFORMATTER( const wxString& aFileName, const wxChar* aMode = wxT( "wt" ),
466  char aQuoteChar = '"' );
467 
469 
470 protected:
471  void write( const char* aOutBuf, int aCount ) override;
472 
473  FILE* m_fp;
474  wxString m_filename;
475 };
476 
477 
484 {
485  wxOutputStream& m_os;
486 
487 public:
492  STREAM_OUTPUTFORMATTER( wxOutputStream& aStream, char aQuoteChar = '"' ) :
493  OUTPUTFORMATTER( OUTPUTFMTBUFZ, aQuoteChar ),
494  m_os( aStream )
495  {
496  }
497 
498 protected:
499  void write( const char* aOutBuf, int aCount ) override;
500 };
501 
502 #endif // RICHIO_H_
int sprint(const char *fmt,...)
Definition: richio.cpp:396
wxOutputStream & m_os
Definition: richio.h:485
void write(const char *aOutBuf, int aCount) override
Should be coded in the interface implementation (derived) classes.
Definition: richio.cpp:530
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition: richio.h:80
virtual ~LINE_READER()
Definition: richio.cpp:129
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition: richio.cpp:198
char * Line() const
Return a pointer to the last line that was read in.
Definition: richio.h:117
char * m_line
the read line of UTF8 text
Definition: richio.h:158
virtual const wxString & GetSource() const
Returns the name of the source of the lines in an abstract sense.
Definition: richio.h:109
A LINE_READER that reads from a wxInputStream object.
Definition: richio.h:271
virtual ~OUTPUTFORMATTER()
Definition: richio.h:316
Implement an OUTPUTFORMATTER to a wxWidgets wxOutputStream.
Definition: richio.h:483
~FILE_LINE_READER()
May or may not close the open file, depending on doOwn in constructor.
Definition: richio.cpp:191
virtual void write(const char *aOutBuf, int aCount)=0
Should be coded in the interface implementation (derived) classes.
INPUTSTREAM_LINE_READER(wxInputStream *aStream, const wxString &aSource)
Construct a LINE_READER from a wxInputStream object.
Definition: richio.cpp:285
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:306
virtual std::string Quotes(const std::string &aWrapee) const
Check aWrapee input string for a need to be quoted (e.g.
Definition: richio.cpp:437
wxInputStream * m_stream
Definition: richio.h:285
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition: richio.cpp:255
void Rewind()
Rewind the file and resets the line number back to zero.
Definition: richio.h:222
unsigned m_lineNum
Definition: richio.h:156
A LINE_READER that reads from an open file.
Definition: richio.h:172
wxString m_filename
Definition: richio.h:474
unsigned m_capacity
no. bytes allocated for line.
Definition: richio.h:159
std::string m_mystring
Definition: richio.h:444
virtual unsigned LineNumber() const
Return the line number of the last line read from this LINE_READER.
Definition: richio.h:135
int vprint(const char *fmt, va_list ap)
Definition: richio.cpp:370
OUTPUTFORMATTER(int aReserve=OUTPUTFMTBUFZ, char aQuoteChar='"' )
Definition: richio.h:309
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:78
void expandCapacity(unsigned aNewsize)
Will expand the capacity of line up to maxLineLength but not greater, so be careful about making assu...
Definition: richio.cpp:135
#define LINE_READER_LINE_DEFAULT_MAX
Definition: richio.h:73
void write(const char *aOutBuf, int aCount) override
Should be coded in the interface implementation (derived) classes.
Definition: richio.cpp:539
#define OUTPUTFMTBUFZ
default buffer size for any OUTPUT_FORMATTER
Definition: richio.h:289
const std::string & GetString()
Definition: richio.h:435
std::vector< char > m_buffer
Definition: richio.h:397
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:106
wxString m_source
origin of text lines, e.g. filename or "clipboard"
Definition: richio.h:163
FILE * m_fp
I may own this file, but might not.
Definition: richio.h:230
static const char * GetQuoteChar(const char *wrapee, const char *quote_char)
Perform quote character need determination according to the Specctra DSN specification.
Definition: richio.cpp:331
FILE_OUTPUTFORMATTER(const wxString &aFileName, const wxChar *aMode=wxT("wt"), char aQuoteChar='"' )
Definition: richio.cpp:511
unsigned Length() const
Return the number of bytes in the last line read from this LINE_READER.
Definition: richio.h:143
STRING_FORMATTER(int aReserve=OUTPUTFMTBUFZ, char aQuoteChar='"' )
Reserve space in the buffer.
Definition: richio.h:417
FILE * m_fp
takes ownership
Definition: richio.h:473
virtual char * ReadLine()=0
Read a line of text into the buffer and increments the line number counter.
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:162
#define PRINTF_FUNC
Definition: richio.h:347
Used for text file output.
Definition: richio.h:453
STREAM_OUTPUTFORMATTER(wxOutputStream &aStream, char aQuoteChar='"' )
This can take any number of wxOutputStream derivations, so it can write to a file,...
Definition: richio.h:492
void write(const char *aOutBuf, int aCount) override
Should be coded in the interface implementation (derived) classes.
Definition: richio.cpp:489
void StripUseless()
Removes whitespace, '(', and ')' from the string.
Definition: richio.cpp:494
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition: richio.cpp:293
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:408
char quoteChar[2]
Definition: richio.h:398
bool m_iOwn
if I own the file, I'll promise to close it, else not.
Definition: richio.h:229
unsigned m_length
no. bytes in line before trailing nul.
Definition: richio.h:155
std::string m_lines
Definition: richio.h:240
Is a LINE_READER that reads from a multiline 8 bit wide std::string.
Definition: richio.h:237
Implement an OUTPUTFORMATTER to a memory buffer.
Definition: richio.h:411
STRING_LINE_READER(const std::string &aString, const wxString &aSource)
Construct a string line reader.
Definition: richio.cpp:232
unsigned m_maxLineLength
maximum allowed capacity using resizing.
Definition: richio.h:161
std::string Quotew(const wxString &aWrapee) const
Definition: richio.cpp:476
void Clear()
Clear the buffer and empties the internal string.
Definition: richio.h:425