KiCad PCB EDA Suite
richio.cpp
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-2011 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
5  * Copyright (C) 2017-2021 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 
26 #include <cstdarg>
27 #include <config.h> // HAVE_FGETC_NOLOCK
28 
29 #include <ignore.h>
30 #include <richio.h>
31 #include <errno.h>
32 
33 #include <wx/file.h>
34 #include <wx/translation.h>
35 
36 
37 // Fall back to getc() when getc_unlocked() is not available on the target platform.
38 #if !defined( HAVE_FGETC_NOLOCK )
39 #define getc_unlocked getc
40 #endif
41 
42 
43 static int vprint( std::string* result, const char* format, va_list ap )
44 {
45  char msg[512];
46  // This function can call vsnprintf twice.
47  // But internally, vsnprintf retrieves arguments from the va_list identified by arg as if
48  // va_arg was used on it, and thus the state of the va_list is likely to be altered by the call.
49  // see: www.cplusplus.com/reference/cstdio/vsnprintf
50  // we make a copy of va_list ap for the second call, if happens
51  va_list tmp;
52  va_copy( tmp, ap );
53 
54  size_t len = vsnprintf( msg, sizeof(msg), format, ap );
55 
56  if( len < sizeof(msg) ) // the output fit into msg
57  {
58  result->append( msg, msg + len );
59  }
60  else
61  {
62  // output was too big, so now incur the expense of allocating
63  // a buf for holding suffient characters.
64 
65  std::vector<char> buf;
66  buf.reserve( len+1 ); // reserve(), not resize() which writes. +1 for trailing nul.
67 
68  len = vsnprintf( &buf[0], len+1, format, tmp );
69 
70  result->append( &buf[0], &buf[0] + len );
71  }
72 
73  va_end( tmp ); // Release the temporary va_list, initialised from ap
74 
75  return len;
76 }
77 
78 
79 int StrPrintf( std::string* result, const char* format, ... )
80 {
81  va_list args;
82 
83  va_start( args, format );
84  int ret = vprint( result, format, args );
85  va_end( args );
86 
87  return ret;
88 }
89 
90 
91 std::string StrPrintf( const char* format, ... )
92 {
93  std::string ret;
94  va_list args;
95 
96  va_start( args, format );
97  ignore_unused( vprint( &ret, format, args ) );
98  va_end( args );
99 
100  return ret;
101 }
102 
103 
104 //-----<LINE_READER>------------------------------------------------------
105 
106 LINE_READER::LINE_READER( unsigned aMaxLineLength ) :
107  m_length( 0 ), m_lineNum( 0 ), m_line( nullptr ),
108  m_capacity( 0 ), m_maxLineLength( aMaxLineLength )
109 {
110  if( aMaxLineLength != 0 )
111  {
112  // start at the INITIAL size, expand as needed up to the MAX size in maxLineLength
114 
115  // but never go above user's aMaxLineLength, and leave space for trailing nul
116  if( m_capacity > aMaxLineLength+1 )
117  m_capacity = aMaxLineLength+1;
118 
119  // Be sure there is room for a null EOL char, so reserve at least capacity+1 bytes
120  // to ensure capacity line length and avoid corner cases
121  // Use capacity+5 to cover and corner case
122  m_line = new char[m_capacity+5];
123 
124  m_line[0] = '\0';
125  }
126 }
127 
128 
130 {
131  delete[] m_line;
132 }
133 
134 
135 void LINE_READER::expandCapacity( unsigned aNewsize )
136 {
137  // m_length can equal maxLineLength and nothing breaks, there's room for
138  // the terminating nul. cannot go over this.
139  if( aNewsize > m_maxLineLength+1 )
140  aNewsize = m_maxLineLength+1;
141 
142  if( aNewsize > m_capacity )
143  {
144  m_capacity = aNewsize;
145 
146  // resize the buffer, and copy the original data
147  // Be sure there is room for the null EOL char, so reserve capacity+1 bytes
148  // to ensure capacity line length. Use capacity+5 to cover and corner case
149  char* bigger = new char[m_capacity+5];
150 
151  wxASSERT( m_capacity >= m_length+1 );
152 
153  memcpy( bigger, m_line, m_length );
154  bigger[m_length] = 0;
155 
156  delete[] m_line;
157  m_line = bigger;
158  }
159 }
160 
161 
162 FILE_LINE_READER::FILE_LINE_READER( const wxString& aFileName, unsigned aStartingLineNumber,
163  unsigned aMaxLineLength ):
164  LINE_READER( aMaxLineLength ), m_iOwn( true )
165 {
166  m_fp = wxFopen( aFileName, wxT( "rt" ) );
167 
168  if( !m_fp )
169  {
170  wxString msg = wxString::Format( _( "Unable to open %s for reading." ),
171  aFileName.GetData() );
172  THROW_IO_ERROR( msg );
173  }
174 
175  m_source = aFileName;
176  m_lineNum = aStartingLineNumber;
177 }
178 
179 
180 FILE_LINE_READER::FILE_LINE_READER( FILE* aFile, const wxString& aFileName,
181  bool doOwn,
182  unsigned aStartingLineNumber,
183  unsigned aMaxLineLength ) :
184  LINE_READER( aMaxLineLength ), m_iOwn( doOwn ), m_fp( aFile )
185 {
186  m_source = aFileName;
187  m_lineNum = aStartingLineNumber;
188 }
189 
190 
192 {
193  if( m_iOwn && m_fp )
194  fclose( m_fp );
195 }
196 
197 
199 {
200  fseek( m_fp, 0, SEEK_END );
201  long int fileLength = ftell( m_fp );
202  rewind( m_fp );
203 
204  return fileLength;
205 }
206 
207 
209 {
210  return ftell( m_fp );
211 }
212 
213 
215 {
216  m_length = 0;
217 
218  for( ;; )
219  {
220  if( m_length >= m_maxLineLength )
221  THROW_IO_ERROR( _( "Maximum line length exceeded" ) );
222 
223  if( m_length >= m_capacity )
224  expandCapacity( m_capacity * 2 );
225 
226  // faster, POSIX compatible fgetc(), no locking.
227  int cc = getc_unlocked( m_fp );
228 
229  if( cc == EOF )
230  break;
231 
232  m_line[ m_length++ ] = (char) cc;
233 
234  if( cc == '\n' )
235  break;
236  }
237 
238  m_line[ m_length ] = 0;
239 
240  // m_lineNum is incremented even if there was no line read, because this
241  // leads to better error reporting when we hit an end of file.
242  ++m_lineNum;
243 
244  return m_length ? m_line : nullptr;
245 }
246 
247 
248 STRING_LINE_READER::STRING_LINE_READER( const std::string& aString, const wxString& aSource ):
250  m_lines( aString ), m_ndx( 0 )
251 {
252  // Clipboard text should be nice and _use multiple lines_ so that
253  // we can report _line number_ oriented error messages when parsing.
254  m_source = aSource;
255 }
256 
257 
260  m_lines( aStartingPoint.m_lines ),
261  m_ndx( aStartingPoint.m_ndx )
262 {
263  // since we are keeping the same "source" name, for error reporting purposes
264  // we need to have the same notion of line number and offset.
265 
266  m_source = aStartingPoint.m_source;
267  m_lineNum = aStartingPoint.m_lineNum;
268 }
269 
270 
272 {
273  size_t nlOffset = m_lines.find( '\n', m_ndx );
274 
275  if( nlOffset == std::string::npos )
276  m_length = m_lines.length() - m_ndx;
277  else
278  m_length = nlOffset - m_ndx + 1; // include the newline, so +1
279 
280  if( m_length )
281  {
282  if( m_length >= m_maxLineLength )
283  THROW_IO_ERROR( _("Line length exceeded") );
284 
285  if( m_length+1 > m_capacity ) // +1 for terminating nul
287 
288  wxASSERT( m_ndx + m_length <= m_lines.length() );
289 
290  memcpy( m_line, &m_lines[m_ndx], m_length );
291  m_ndx += m_length;
292  }
293 
294  ++m_lineNum; // this gets incremented even if no bytes were read
295  m_line[m_length] = 0;
296 
297  return m_length ? m_line : nullptr;
298 }
299 
300 
302  const wxString& aSource ) :
304  m_stream( aStream )
305 {
306  m_source = aSource;
307 }
308 
309 
311 {
312  m_length = 0;
313 
314  for( ;; )
315  {
316  if( m_length >= m_maxLineLength )
317  THROW_IO_ERROR( _( "Maximum line length exceeded" ) );
318 
319  if( m_length + 1 > m_capacity )
320  expandCapacity( m_capacity * 2 );
321 
322  // this read may fail, docs say to test LastRead() before trusting cc.
323  char cc = m_stream->GetC();
324 
325  if( !m_stream->LastRead() )
326  break;
327 
328  m_line[ m_length++ ] = cc;
329 
330  if( cc == '\n' )
331  break;
332  }
333 
334  m_line[ m_length ] = 0;
335 
336  // m_lineNum is incremented even if there was no line read, because this
337  // leads to better error reporting when we hit an end of file.
338  ++m_lineNum;
339 
340  return m_length ? m_line : nullptr;
341 }
342 
343 
344 //-----<OUTPUTFORMATTER>----------------------------------------------------
345 
346 // factor out a common GetQuoteChar
347 
348 const char* OUTPUTFORMATTER::GetQuoteChar( const char* wrapee, const char* quote_char )
349 {
350  // Include '#' so a symbol is not confused with a comment. We intend
351  // to wrap any symbol starting with a '#'.
352  // Our LEXER class handles comments, and comments appear to be an extension
353  // to the SPECCTRA DSN specification.
354  if( *wrapee == '#' )
355  return quote_char;
356 
357  if( strlen( wrapee ) == 0 )
358  return quote_char;
359 
360  bool isFirst = true;
361 
362  for( ; *wrapee; ++wrapee, isFirst = false )
363  {
364  static const char quoteThese[] = "\t ()"
365  "%" // per Alfons of freerouting.net, he does not like this unquoted as of 1-Feb-2008
366  "{}" // guessing that these are problems too
367  ;
368 
369  // if the string to be wrapped (wrapee) has a delimiter in it,
370  // return the quote_char so caller wraps the wrapee.
371  if( strchr( quoteThese, *wrapee ) )
372  return quote_char;
373 
374  if( !isFirst && '-' == *wrapee )
375  return quote_char;
376  }
377 
378  return ""; // caller does not need to wrap, can use an unwrapped string.
379 }
380 
381 
382 const char* OUTPUTFORMATTER::GetQuoteChar( const char* wrapee ) const
383 {
384  return GetQuoteChar( wrapee, quoteChar );
385 }
386 
387 
388 int OUTPUTFORMATTER::vprint( const char* fmt, va_list ap )
389 {
390  // This function can call vsnprintf twice.
391  // But internally, vsnprintf retrieves arguments from the va_list identified by arg as if
392  // va_arg was used on it, and thus the state of the va_list is likely to be altered by the call.
393  // see: www.cplusplus.com/reference/cstdio/vsnprintf
394  // we make a copy of va_list ap for the second call, if happens
395  va_list tmp;
396  va_copy( tmp, ap );
397  int ret = vsnprintf( &m_buffer[0], m_buffer.size(), fmt, ap );
398 
399  if( ret >= (int) m_buffer.size() )
400  {
401  m_buffer.resize( ret + 1000 );
402  ret = vsnprintf( &m_buffer[0], m_buffer.size(), fmt, tmp );
403  }
404 
405  va_end( tmp ); // Release the temporary va_list, initialised from ap
406 
407  if( ret > 0 )
408  write( &m_buffer[0], ret );
409 
410  return ret;
411 }
412 
413 
414 int OUTPUTFORMATTER::sprint( const char* fmt, ... )
415 {
416  va_list args;
417 
418  va_start( args, fmt );
419  int ret = vprint( fmt, args);
420  va_end( args );
421 
422  return ret;
423 }
424 
425 
426 int OUTPUTFORMATTER::Print( int nestLevel, const char* fmt, ... )
427 {
428 #define NESTWIDTH 2
429 
430  va_list args;
431 
432  va_start( args, fmt );
433 
434  int result = 0;
435  int total = 0;
436 
437  for( int i = 0; i < nestLevel; ++i )
438  {
439  // no error checking needed, an exception indicates an error.
440  result = sprint( "%*c", NESTWIDTH, ' ' );
441 
442  total += result;
443  }
444 
445  // no error checking needed, an exception indicates an error.
446  result = vprint( fmt, args );
447 
448  va_end( args );
449 
450  total += result;
451  return total;
452 }
453 
454 
455 std::string OUTPUTFORMATTER::Quotes( const std::string& aWrapee ) const
456 {
457  std::string ret;
458 
459  ret.reserve( aWrapee.size() * 2 + 2 );
460 
461  ret += '"';
462 
463  for( std::string::const_iterator it = aWrapee.begin(); it != aWrapee.end(); ++it )
464  {
465  switch( *it )
466  {
467  case '\n':
468  ret += '\\';
469  ret += 'n';
470  break;
471  case '\r':
472  ret += '\\';
473  ret += 'r';
474  break;
475  case '\\':
476  ret += '\\';
477  ret += '\\';
478  break;
479  case '"':
480  ret += '\\';
481  ret += '"';
482  break;
483  default:
484  ret += *it;
485  }
486  }
487 
488  ret += '"';
489 
490  return ret;
491 }
492 
493 
494 std::string OUTPUTFORMATTER::Quotew( const wxString& aWrapee ) const
495 {
496  // wxStrings are always encoded as UTF-8 as we convert to a byte sequence.
497  // The non-virtual function calls the virtual workhorse function, and if
498  // a different quoting or escaping strategy is desired from the standard,
499  // a derived class can overload Quotes() above, but
500  // should never be a reason to overload this Quotew() here.
501  return Quotes( (const char*) aWrapee.utf8_str() );
502 }
503 
504 
505 //-----<STRING_FORMATTER>----------------------------------------------------
506 
507 void STRING_FORMATTER::write( const char* aOutBuf, int aCount )
508 {
509  m_mystring.append( aOutBuf, aCount );
510 }
511 
513 {
514  std::string copy = m_mystring;
515 
516  m_mystring.clear();
517 
518  for( std::string::iterator i = copy.begin(); i != copy.end(); ++i )
519  {
520  if( !isspace( *i ) && *i != ')' && *i != '(' && *i != '"' )
521  {
522  m_mystring += *i;
523  }
524  }
525 }
526 
527 
528 FILE_OUTPUTFORMATTER::FILE_OUTPUTFORMATTER( const wxString& aFileName, const wxChar* aMode,
529  char aQuoteChar ):
530  OUTPUTFORMATTER( OUTPUTFMTBUFZ, aQuoteChar ),
531  m_filename( aFileName )
532 {
533  m_fp = wxFopen( aFileName, aMode );
534 
535  if( !m_fp )
536  THROW_IO_ERROR( strerror( errno ) );
537 }
538 
539 
541 {
542  if( m_fp )
543  fclose( m_fp );
544 }
545 
546 
547 void FILE_OUTPUTFORMATTER::write( const char* aOutBuf, int aCount )
548 {
549  if( fwrite( aOutBuf, (unsigned) aCount, 1, m_fp ) != 1 )
550  THROW_IO_ERROR( strerror( errno ) );
551 }
552 
553 
554 void STREAM_OUTPUTFORMATTER::write( const char* aOutBuf, int aCount )
555 {
556  int lastWrite;
557 
558  // This might delay awhile if you were writing to say a socket, but for
559  // a file it should only go through the loop once.
560  for( int total = 0; total<aCount; total += lastWrite )
561  {
562  lastWrite = m_os.Write( aOutBuf, aCount ).LastWrite();
563 
564  if( !m_os.IsOk() )
565  {
566  THROW_IO_ERROR( _( "OUTPUTSTREAM_OUTPUTFORMATTER write error" ) );
567  }
568  }
569 }
570 
int sprint(const char *fmt,...)
Definition: richio.cpp:414
wxOutputStream & m_os
Definition: richio.h:488
void write(const char *aOutBuf, int aCount) override
Should be coded in the interface implementation (derived) classes.
Definition: richio.cpp:547
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:214
#define LINE_READER_LINE_INITIAL_SIZE
Definition: richio.h:74
#define NESTWIDTH
char * m_line
the read line of UTF8 text
Definition: richio.h:158
~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:301
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:309
long int CurPos()
Definition: richio.cpp:208
virtual std::string Quotes(const std::string &aWrapee) const
Check aWrapee input string for a need to be quoted (e.g.
Definition: richio.cpp:455
wxInputStream * m_stream
Definition: richio.h:288
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition: richio.cpp:271
int StrPrintf(std::string *result, const char *format,...)
This is like sprintf() but the output is appended to a std::string instead of to a character array.
Definition: richio.cpp:79
unsigned m_lineNum
Definition: richio.h:156
unsigned m_capacity
no. bytes allocated for line.
Definition: richio.h:159
std::string m_mystring
Definition: richio.h:447
int vprint(const char *fmt, va_list ap)
Definition: richio.cpp:388
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
#define _(s)
#define getc_unlocked
Definition: richio.cpp:39
void write(const char *aOutBuf, int aCount) override
Should be coded in the interface implementation (derived) classes.
Definition: richio.cpp:554
#define OUTPUTFMTBUFZ
default buffer size for any OUTPUT_FORMATTER
Definition: richio.h:292
std::vector< char > m_buffer
Definition: richio.h:400
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:233
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:348
FILE_OUTPUTFORMATTER(const wxString &aFileName, const wxChar *aMode=wxT("wt"), char aQuoteChar='"' )
Definition: richio.cpp:528
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
FILE * m_fp
takes ownership
Definition: richio.h:476
long int FileLength()
Definition: richio.cpp:198
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
void ignore_unused(const T &)
Definition: ignore.h:24
void write(const char *aOutBuf, int aCount) override
Should be coded in the interface implementation (derived) classes.
Definition: richio.cpp:507
void StripUseless()
Removes whitespace, '(', and ')' from the string.
Definition: richio.cpp:512
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition: richio.cpp:310
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:426
static int vprint(std::string *result, const char *format, va_list ap)
Definition: richio.cpp:43
char quoteChar[2]
Definition: richio.h:401
bool m_iOwn
if I own the file, I'll promise to close it, else not.
Definition: richio.h:232
unsigned m_length
no. bytes in line before trailing nul.
Definition: richio.h:155
std::string m_lines
Definition: richio.h:243
Is a LINE_READER that reads from a multiline 8 bit wide std::string.
Definition: richio.h:240
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
STRING_LINE_READER(const std::string &aString, const wxString &aSource)
Construct a string line reader.
Definition: richio.cpp:248
unsigned m_maxLineLength
maximum allowed capacity using resizing.
Definition: richio.h:161
std::string Quotew(const wxString &aWrapee) const
Definition: richio.cpp:494