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 <wx/wx.h>
38 #include <stdio.h>
39 
40 #include <ki_exception.h>
41 
42 
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 100000
74 #define LINE_READER_LINE_INITIAL_SIZE 5000
75 
82 {
83 protected:
84  unsigned length;
85  unsigned lineNum;
86 
87  char* line;
88  unsigned capacity;
89 
90  unsigned maxLineLength;
91 
92  wxString source;
93 
99  void expandCapacity( unsigned newsize );
100 
101 
102 public:
103 
109  LINE_READER( unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX );
110 
111  virtual ~LINE_READER();
112 
121  virtual char* ReadLine() throw( IO_ERROR ) = 0;
122 
130  virtual const wxString& GetSource() const
131  {
132  return source;
133  }
134 
139  char* Line() const
140  {
141  return line;
142  }
143 
149  operator char* () const
150  {
151  return Line();
152  }
153 
159  virtual unsigned LineNumber() const
160  {
161  return lineNum;
162  }
163 
168  unsigned Length() const
169  {
170  return length;
171  }
172 };
173 
174 
181 {
182 protected:
183 
184  bool iOwn;
185  FILE* fp;
186 
187 public:
188 
206  FILE_LINE_READER( const wxString& aFileName,
207  unsigned aStartingLineNumber = 0,
208  unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX ) throw( IO_ERROR );
209 
225  FILE_LINE_READER( FILE* aFile, const wxString& aFileName, bool doOwn = true,
226  unsigned aStartingLineNumber = 0,
227  unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX );
228 
233  ~FILE_LINE_READER();
234 
235  char* ReadLine() throw( IO_ERROR ) override;
236 
242  void Rewind()
243  {
244  rewind( fp );
245  lineNum = 0;
246  }
247 };
248 
249 
255 {
256 protected:
257  std::string lines;
258  size_t ndx;
259 
260 public:
261 
272  STRING_LINE_READER( const std::string& aString, const wxString& aSource );
273 
280  STRING_LINE_READER( const STRING_LINE_READER& aStartingPoint );
281 
282  char* ReadLine() throw( IO_ERROR ) override;
283 };
284 
285 
291 {
292 protected:
293  wxInputStream* m_stream; //< The input stream to read. No ownership of this pointer.
294 
295 public:
296 
303  INPUTSTREAM_LINE_READER( wxInputStream* aStream, const wxString& aSource );
304 
305  char* ReadLine() throw( IO_ERROR ) override;
306 };
307 
308 
309 #define OUTPUTFMTBUFZ 500
310 
311 
328 {
329  std::vector<char> buffer;
330  char quoteChar[2];
331 
332  int sprint( const char* fmt, ... ) throw( IO_ERROR );
333  int vprint( const char* fmt, va_list ap ) throw( IO_ERROR );
334 
335 
336 protected:
337  OUTPUTFORMATTER( int aReserve = OUTPUTFMTBUFZ, char aQuoteChar = '"' ) :
338  buffer( aReserve, '\0' )
339  {
340  quoteChar[0] = aQuoteChar;
341  quoteChar[1] = '\0';
342  }
343 
344  virtual ~OUTPUTFORMATTER() {}
345 
358  static const char* GetQuoteChar( const char* wrapee, const char* quote_char );
359 
368  virtual void write( const char* aOutBuf, int aCount ) throw( IO_ERROR ) = 0;
369 
370 #if defined(__GNUG__) // The GNU C++ compiler defines this
371 
372  // When used on a C++ function, we must account for the "this" pointer,
373  // so increase the STRING-INDEX and FIRST-TO_CHECK by one.
374  // See http://docs.freebsd.org/info/gcc/gcc.info.Function_Attributes.html
375  // Then to get format checking during the compile, compile with -Wall or -Wformat
376 #define PRINTF_FUNC __attribute__ ((format (printf, 3, 4)))
377 
378 #else
379 #define PRINTF_FUNC // nothing
380 #endif
381 
382 public:
383 
384  //-----<interface functions>------------------------------------------
385 
397  int PRINTF_FUNC Print( int nestLevel, const char* fmt, ... ) throw( IO_ERROR );
398 
415  virtual const char* GetQuoteChar( const char* wrapee );
416 
432  virtual std::string Quotes( const std::string& aWrapee ) throw( IO_ERROR );
433 
434  std::string Quotew( const wxString& aWrapee ) throw( IO_ERROR );
435 
436  //-----</interface functions>-----------------------------------------
437 };
438 
439 
446 {
447  std::string mystring;
448 
449 public:
450 
455  STRING_FORMATTER( int aReserve = OUTPUTFMTBUFZ, char aQuoteChar = '"' ) :
456  OUTPUTFORMATTER( aReserve, aQuoteChar )
457  {
458  }
459 
464  void Clear()
465  {
466  mystring.clear();
467  }
468 
473  void StripUseless();
474 
475  const std::string& GetString()
476  {
477  return mystring;
478  }
479 
480 protected:
481  //-----<OUTPUTFORMATTER>------------------------------------------------
482  void write( const char* aOutBuf, int aCount ) throw( IO_ERROR ) override;
483  //-----</OUTPUTFORMATTER>-----------------------------------------------
484 };
485 
486 
492 class FILE_OUTPUTFORMATTER : public OUTPUTFORMATTER
493 {
494 public:
495 
505  FILE_OUTPUTFORMATTER( const wxString& aFileName,
506  const wxChar* aMode = wxT( "wt" ),
507  char aQuoteChar = '"' )
508  throw( IO_ERROR );
509 
511 
512 protected:
513  //-----<OUTPUTFORMATTER>------------------------------------------------
514  void write( const char* aOutBuf, int aCount ) throw( IO_ERROR ) override;
515  //-----</OUTPUTFORMATTER>-----------------------------------------------
516 
517  FILE* m_fp;
518  wxString m_filename;
519 };
520 
521 
527 class STREAM_OUTPUTFORMATTER : public OUTPUTFORMATTER
528 {
529  wxOutputStream& os;
530 
531 public:
537  STREAM_OUTPUTFORMATTER( wxOutputStream& aStream, char aQuoteChar = '"' ) :
538  OUTPUTFORMATTER( OUTPUTFMTBUFZ, aQuoteChar ),
539  os( aStream )
540  {
541  }
542 
543 protected:
544  //-----<OUTPUTFORMATTER>------------------------------------------------
545  void write( const char* aOutBuf, int aCount ) throw( IO_ERROR ) override;
546  //-----</OUTPUTFORMATTER>-----------------------------------------------
547 };
548 
549 #endif // RICHIO_H_
std::string lines
Definition: richio.h:257
Class LINE_READER is an abstract class from which implementation specific LINE_READERs may be derived...
Definition: richio.h:81
virtual ~LINE_READER()
Definition: richio.cpp:126
char * ReadLine() override
Function ReadLine reads a line of text into the buffer and increments the line number counter...
Definition: richio.cpp:196
virtual unsigned LineNumber() const
Function Line Number returns the line number of the last line read from this LINE_READER.
Definition: richio.h:159
Class INPUTSTREAM_LINE_READER is a LINE_READER that reads from a wxInputStream object.
Definition: richio.h:290
virtual ~OUTPUTFORMATTER()
Definition: richio.h:344
Class STREAM_OUTPUTFORMATTER implements OUTPUTFORMATTER to a wxWidgets wxOutputStream.
Definition: richio.h:527
unsigned maxLineLength
maximum allowed capacity using resizing.
Definition: richio.h:90
Class OUTPUTFORMATTER is an important interface (abstract class) used to output 8 bit text in a conve...
Definition: richio.h:327
STREAM_OUTPUTFORMATTER(wxOutputStream &aStream, char aQuoteChar= '"' )
Constructor STREAM_OUTPUTFORMATTER can take any number of wxOutputStream derivations, so it can write to a file, socket, or zip file.
Definition: richio.h:537
wxInputStream * m_stream
Definition: richio.h:293
std::string mystring
Definition: richio.h:447
FILE * fp
I may own this file, but might not.
Definition: richio.h:185
char * ReadLine() override
Function ReadLine reads a line of text into the buffer and increments the line number counter...
Definition: richio.cpp:253
void Rewind()
Function Rewind rewinds the file and resets the line number back to zero.
Definition: richio.h:242
unsigned lineNum
Definition: richio.h:85
bool iOwn
if I own the file, I'll promise to close it, else not.
Definition: richio.h:184
char * line
the read line of UTF8 text
Definition: richio.h:87
Class FILE_LINE_READER is a LINE_READER that reads from an open file.
Definition: richio.h:180
wxString m_filename
Definition: richio.h:518
char * Line() const
Function Line returns a pointer to the last line that was read in.
Definition: richio.h:139
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:75
#define LINE_READER_LINE_DEFAULT_MAX
Definition: richio.h:73
STRING_FORMATTER(int aReserve=OUTPUTFMTBUFZ, char aQuoteChar= '"' )
Constructor STRING_FORMATTER reserves space in the buffer.
Definition: richio.h:455
std::vector< char > buffer
Definition: richio.h:329
void expandCapacity(unsigned newsize)
Function expandCapacity will expand the capacity of line up to maxLineLength but not greater...
Definition: richio.cpp:132
#define OUTPUTFMTBUFZ
default buffer size for any OUTPUT_FORMATTER
Definition: richio.h:309
const std::string & GetString()
Definition: richio.h:475
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:103
unsigned length
no. bytes in line before trailing nul.
Definition: richio.h:84
unsigned Length() const
Function Length returns the number of bytes in the last line read from this LINE_READER.
Definition: richio.h:168
FILE * m_fp
takes ownership
Definition: richio.h:517
wxOutputStream & os
Definition: richio.h:529
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:157
virtual const wxString & GetSource() const
Function GetSource returns the name of the source of the lines in an abstract sense.
Definition: richio.h:130
#define PRINTF_FUNC
Definition: richio.h:379
Class FILE_OUTPUTFORMATTER may be used for text file output.
Definition: richio.h:492
unsigned capacity
no. bytes allocated for line.
Definition: richio.h:88
static int vprint(std::string *result, const char *format, va_list ap)
Definition: richio.cpp:38
Class STRING_LINE_READER is a LINE_READER that reads from a multiline 8 bit wide std::string.
Definition: richio.h:254
Class STRING_FORMATTER implements OUTPUTFORMATTER to a memory buffer.
Definition: richio.h:445
wxString source
origin of text lines, e.g. filename or "clipboard"
Definition: richio.h:92
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:47
STRING_LINE_READER(const std::string &aString, const wxString &aSource)
Constructor STRING_LINE_READER( const std::string&, const wxString& )
Definition: richio.cpp:229
void Clear()
Function Clear clears the buffer and empties the internal string.
Definition: richio.h:464