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 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 
53 int
54 #if defined(__GNUG__)
55  __attribute__ ((format (printf, 2, 3)))
56 #endif
57  StrPrintf( std::string* aResult, const char* aFormat, ... );
58 
59 
67 std::string
68 #if defined(__GNUG__)
69  __attribute__ ((format (printf, 1, 2)))
70 #endif
71  StrPrintf( const char* format, ... );
72 
73 
74 #define LINE_READER_LINE_DEFAULT_MAX 1000000
75 #define LINE_READER_LINE_INITIAL_SIZE 5000
76 
83 {
84 protected:
85  unsigned m_length;
86  unsigned m_lineNum;
87 
88  char* m_line;
89  unsigned m_capacity;
90 
91  unsigned m_maxLineLength;
92 
93  wxString m_source;
94 
100  void expandCapacity( unsigned aNewsize );
101 
102 
103 public:
104 
110  LINE_READER( unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX );
111 
112  virtual ~LINE_READER();
113 
122  virtual char* ReadLine() = 0;
123 
131  virtual const wxString& GetSource() const
132  {
133  return m_source;
134  }
135 
140  char* Line() const
141  {
142  return m_line;
143  }
144 
150  operator char* () const
151  {
152  return Line();
153  }
154 
160  virtual unsigned LineNumber() const
161  {
162  return m_lineNum;
163  }
164 
169  unsigned Length() const
170  {
171  return m_length;
172  }
173 };
174 
175 
182 {
183 protected:
184 
185  bool m_iOwn;
186  FILE* m_fp;
187 
188 public:
189 
207  FILE_LINE_READER( const wxString& aFileName,
208  unsigned aStartingLineNumber = 0,
209  unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX );
210 
226  FILE_LINE_READER( FILE* aFile, const wxString& aFileName, bool doOwn = true,
227  unsigned aStartingLineNumber = 0,
228  unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX );
229 
235 
236  char* ReadLine() override;
237 
243  void Rewind()
244  {
245  rewind( m_fp );
246  m_lineNum = 0;
247  }
248 };
249 
250 
256 {
257 protected:
258  std::string m_lines;
259  size_t m_ndx;
260 
261 public:
262 
273  STRING_LINE_READER( const std::string& aString, const wxString& aSource );
274 
281  STRING_LINE_READER( const STRING_LINE_READER& aStartingPoint );
282 
283  char* ReadLine() override;
284 };
285 
286 
292 {
293 protected:
294  wxInputStream* m_stream; //< The input stream to read. No ownership of this pointer.
295 
296 public:
297 
304  INPUTSTREAM_LINE_READER( wxInputStream* aStream, const wxString& aSource );
305 
306  char* ReadLine() override;
307 };
308 
309 
310 #define OUTPUTFMTBUFZ 500
311 
312 
329 {
330  std::vector<char> m_buffer;
331  char quoteChar[2];
332 
333  int sprint( const char* fmt, ... );
334  int vprint( const char* fmt, va_list ap );
335 
336 
337 protected:
338  OUTPUTFORMATTER( int aReserve = OUTPUTFMTBUFZ, char aQuoteChar = '"' ) :
339  m_buffer( aReserve, '\0' )
340  {
341  quoteChar[0] = aQuoteChar;
342  quoteChar[1] = '\0';
343  }
344 
345  virtual ~OUTPUTFORMATTER() {}
346 
359  static const char* GetQuoteChar( const char* wrapee, const char* quote_char );
360 
369  virtual void write( const char* aOutBuf, int aCount ) = 0;
370 
371 #if defined(__GNUG__) // The GNU C++ compiler defines this
372 
373  // When used on a C++ function, we must account for the "this" pointer,
374  // so increase the STRING-INDEX and FIRST-TO_CHECK by one.
375  // See http://docs.freebsd.org/info/gcc/gcc.info.Function_Attributes.html
376  // Then to get format checking during the compile, compile with -Wall or -Wformat
377 #define PRINTF_FUNC __attribute__ ((format (printf, 3, 4)))
378 
379 #else
380 #define PRINTF_FUNC // nothing
381 #endif
382 
383 public:
384 
385  //-----<interface functions>------------------------------------------
386 
398  int PRINTF_FUNC Print( int nestLevel, const char* fmt, ... );
399 
416  virtual const char* GetQuoteChar( const char* wrapee );
417 
433  virtual std::string Quotes( const std::string& aWrapee );
434 
435  std::string Quotew( const wxString& aWrapee );
436 
437  //-----</interface functions>-----------------------------------------
438 };
439 
440 
447 {
448  std::string m_mystring;
449 
450 public:
451 
456  STRING_FORMATTER( int aReserve = OUTPUTFMTBUFZ, char aQuoteChar = '"' ) :
457  OUTPUTFORMATTER( aReserve, aQuoteChar )
458  {
459  }
460 
465  void Clear()
466  {
467  m_mystring.clear();
468  }
469 
474  void StripUseless();
475 
476  const std::string& GetString()
477  {
478  return m_mystring;
479  }
480 
481 protected:
482  //-----<OUTPUTFORMATTER>------------------------------------------------
483  void write( const char* aOutBuf, int aCount ) override;
484  //-----</OUTPUTFORMATTER>-----------------------------------------------
485 };
486 
487 
494 {
495 public:
496 
506  FILE_OUTPUTFORMATTER( const wxString& aFileName,
507  const wxChar* aMode = wxT( "wt" ),
508  char aQuoteChar = '"' );
509 
511 
512 protected:
513  //-----<OUTPUTFORMATTER>------------------------------------------------
514  void write( const char* aOutBuf, int aCount ) override;
515  //-----</OUTPUTFORMATTER>-----------------------------------------------
516 
517  FILE* m_fp;
518  wxString m_filename;
519 };
520 
521 
528 {
529  wxOutputStream& m_os;
530 
531 public:
537  STREAM_OUTPUTFORMATTER( wxOutputStream& aStream, char aQuoteChar = '"' ) :
538  OUTPUTFORMATTER( OUTPUTFMTBUFZ, aQuoteChar ),
539  m_os( aStream )
540  {
541  }
542 
543 protected:
544  //-----<OUTPUTFORMATTER>------------------------------------------------
545  void write( const char* aOutBuf, int aCount ) override;
546  //-----</OUTPUTFORMATTER>-----------------------------------------------
547 };
548 
549 #endif // RICHIO_H_
int sprint(const char *fmt,...)
Definition: richio.cpp:396
wxOutputStream & m_os
Definition: richio.h:529
void write(const char *aOutBuf, int aCount) override
Function write should be coded in the interface implementation (derived) classes.
Definition: richio.cpp:530
LINE_READER is an abstract class from which implementation specific LINE_READERs may be derived to re...
Definition: richio.h:82
virtual ~LINE_READER()
Definition: richio.cpp:129
char * ReadLine() override
Function ReadLine reads a line of text into the buffer and increments the line number counter.
Definition: richio.cpp:198
char * Line() const
Function Line returns a pointer to the last line that was read in.
Definition: richio.h:140
char * m_line
the read line of UTF8 text
Definition: richio.h:88
virtual const wxString & GetSource() const
Function GetSource returns the name of the source of the lines in an abstract sense.
Definition: richio.h:131
INPUTSTREAM_LINE_READER is a LINE_READER that reads from a wxInputStream object.
Definition: richio.h:291
virtual ~OUTPUTFORMATTER()
Definition: richio.h:345
STREAM_OUTPUTFORMATTER implements OUTPUTFORMATTER to a wxWidgets wxOutputStream.
Definition: richio.h:527
~FILE_LINE_READER()
Destructor 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
Function write should be coded in the interface implementation (derived) classes.
INPUTSTREAM_LINE_READER(wxInputStream *aStream, const wxString &aSource)
Constructor WXINPUTSTREAM_LINE_READER.
Definition: richio.cpp:285
OUTPUTFORMATTER is an important interface (abstract class) used to output 8 bit text in a convenient ...
Definition: richio.h:328
wxInputStream * m_stream
Definition: richio.h:294
char * ReadLine() override
Function ReadLine reads a line of text into the buffer and increments the line number counter.
Definition: richio.cpp:255
void Rewind()
Function Rewind rewinds the file and resets the line number back to zero.
Definition: richio.h:243
unsigned m_lineNum
Definition: richio.h:86
FILE_LINE_READER is a LINE_READER that reads from an open file.
Definition: richio.h:181
wxString m_filename
Definition: richio.h:518
unsigned m_capacity
no. bytes allocated for line.
Definition: richio.h:89
std::string m_mystring
Definition: richio.h:448
virtual unsigned LineNumber() const
Function Line Number returns the line number of the last line read from this LINE_READER.
Definition: richio.h:160
int vprint(const char *fmt, va_list ap)
Definition: richio.cpp:370
OUTPUTFORMATTER(int aReserve=OUTPUTFMTBUFZ, char aQuoteChar='"' )
Definition: richio.h:338
int StrPrintf(std::string *aResult, const char *aFormat,...)
Function StrPrintf is like sprintf() but the output is appended to a std::string instead of to a char...
Definition: richio.cpp:78
void expandCapacity(unsigned aNewsize)
Function expandCapacity will expand the capacity of line up to maxLineLength but not greater,...
Definition: richio.cpp:135
#define LINE_READER_LINE_DEFAULT_MAX
Definition: richio.h:74
void write(const char *aOutBuf, int aCount) override
Function write 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:310
const std::string & GetString()
Definition: richio.h:476
std::string Quotew(const wxString &aWrapee)
Definition: richio.cpp:476
std::vector< char > m_buffer
Definition: richio.h:330
LINE_READER(unsigned aMaxLineLength=LINE_READER_LINE_DEFAULT_MAX)
Constructor LINE_READER builds a line reader and fixes the length of the maximum supported line lengt...
Definition: richio.cpp:106
wxString m_source
origin of text lines, e.g. filename or "clipboard"
Definition: richio.h:93
FILE * m_fp
I may own this file, but might not.
Definition: richio.h:186
static const char * GetQuoteChar(const char *wrapee, const char *quote_char)
Function GetQuoteChar performs quote character need determination according to the Specctra DSN speci...
Definition: richio.cpp:331
FILE_OUTPUTFORMATTER(const wxString &aFileName, const wxChar *aMode=wxT("wt"), char aQuoteChar='"' )
Constructor.
Definition: richio.cpp:511
unsigned Length() const
Function Length returns the number of bytes in the last line read from this LINE_READER.
Definition: richio.h:169
STRING_FORMATTER(int aReserve=OUTPUTFMTBUFZ, char aQuoteChar='"' )
Constructor STRING_FORMATTER reserves space in the buffer.
Definition: richio.h:456
virtual std::string Quotes(const std::string &aWrapee)
Function Quotes checks aWrapee input string for a need to be quoted (e.g.
Definition: richio.cpp:437
FILE * m_fp
takes ownership
Definition: richio.h:517
virtual char * ReadLine()=0
Function ReadLine reads 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)
Constructor FILE_LINE_READER takes aFileName and the size of the desired line buffer and opens the fi...
Definition: richio.cpp:162
#define PRINTF_FUNC
Definition: richio.h:380
FILE_OUTPUTFORMATTER may be used for text file output.
Definition: richio.h:493
STREAM_OUTPUTFORMATTER(wxOutputStream &aStream, char aQuoteChar='"' )
Constructor STREAM_OUTPUTFORMATTER can take any number of wxOutputStream derivations,...
Definition: richio.h:537
void write(const char *aOutBuf, int aCount) override
Function write should be coded in the interface implementation (derived) classes.
Definition: richio.cpp:489
void StripUseless()
Function StripUseless removes whitespace, '(', and ')' from the mystring.
Definition: richio.cpp:494
char * ReadLine() override
Function ReadLine reads 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,...)
Function Print formats and writes text to the output stream.
Definition: richio.cpp:408
char quoteChar[2]
Definition: richio.h:331
bool m_iOwn
if I own the file, I'll promise to close it, else not.
Definition: richio.h:185
unsigned m_length
no. bytes in line before trailing nul.
Definition: richio.h:85
std::string m_lines
Definition: richio.h:258
STRING_LINE_READER is a LINE_READER that reads from a multiline 8 bit wide std::string.
Definition: richio.h:255
STRING_FORMATTER implements OUTPUTFORMATTER to a memory buffer.
Definition: richio.h:446
STRING_LINE_READER(const std::string &aString, const wxString &aSource)
Constructor STRING_LINE_READER( const std::string&, const wxString& )
Definition: richio.cpp:232
unsigned m_maxLineLength
maximum allowed capacity using resizing.
Definition: richio.h:91
void Clear()
Function Clear clears the buffer and empties the internal string.
Definition: richio.h:465