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 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 <richio.h>
30 #include <errno.h>
31 
32 #include <wx/file.h>
33 #include <wx/translation.h>
34 
35 
36 // Fall back to getc() when getc_unlocked() is not available on the target platform.
37 #if !defined( HAVE_FGETC_NOLOCK )
38 #define getc_unlocked getc
39 #endif
40 
41 
42 static int vprint( std::string* result, const char* format, va_list ap )
43 {
44  char msg[512];
45  // This function can call vsnprintf twice.
46  // But internally, vsnprintf retrieves arguments from the va_list identified by arg as if
47  // va_arg was used on it, and thus the state of the va_list is likely to be altered by the call.
48  // see: www.cplusplus.com/reference/cstdio/vsnprintf
49  // we make a copy of va_list ap for the second call, if happens
50  va_list tmp;
51  va_copy( tmp, ap );
52 
53  size_t len = vsnprintf( msg, sizeof(msg), format, ap );
54 
55  if( len < sizeof(msg) ) // the output fit into msg
56  {
57  result->append( msg, msg + len );
58  }
59  else
60  {
61  // output was too big, so now incur the expense of allocating
62  // a buf for holding suffient characters.
63 
64  std::vector<char> buf;
65  buf.reserve( len+1 ); // reserve(), not resize() which writes. +1 for trailing nul.
66 
67  len = vsnprintf( &buf[0], len+1, format, tmp );
68 
69  result->append( &buf[0], &buf[0] + len );
70  }
71 
72  va_end( tmp ); // Release the temporary va_list, initialised from ap
73 
74  return len;
75 }
76 
77 
78 int StrPrintf( std::string* result, const char* format, ... )
79 {
80  va_list args;
81 
82  va_start( args, format );
83  int ret = vprint( result, format, args );
84  va_end( args );
85 
86  return ret;
87 }
88 
89 
90 std::string StrPrintf( const char* format, ... )
91 {
92  std::string ret;
93  va_list args;
94 
95  va_start( args, format );
96  int ignore = vprint( &ret, format, args );
97  (void) ignore;
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( NULL ),
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 lenght 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 lenght. 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,
163  unsigned aStartingLineNumber, 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(
171  _( "Unable to open filename \"%s\" for reading" ), 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  m_length = 0;
201 
202  for(;;)
203  {
204  if( m_length >= m_maxLineLength )
205  THROW_IO_ERROR( _( "Maximum line length exceeded" ) );
206 
207  if( m_length >= m_capacity )
208  expandCapacity( m_capacity * 2 );
209 
210  // faster, POSIX compatible fgetc(), no locking.
211  int cc = getc_unlocked( m_fp );
212 
213  if( cc == EOF )
214  break;
215 
216  m_line[ m_length++ ] = (char) cc;
217 
218  if( cc == '\n' )
219  break;
220  }
221 
222  m_line[ m_length ] = 0;
223 
224  // m_lineNum is incremented even if there was no line read, because this
225  // leads to better error reporting when we hit an end of file.
226  ++m_lineNum;
227 
228  return m_length ? m_line : NULL;
229 }
230 
231 
232 STRING_LINE_READER::STRING_LINE_READER( const std::string& aString, const wxString& aSource ):
234  m_lines( aString ), m_ndx( 0 )
235 {
236  // Clipboard text should be nice and _use multiple lines_ so that
237  // we can report _line number_ oriented error messages when parsing.
238  m_source = aSource;
239 }
240 
241 
244  m_lines( aStartingPoint.m_lines ),
245  m_ndx( aStartingPoint.m_ndx )
246 {
247  // since we are keeping the same "source" name, for error reporting purposes
248  // we need to have the same notion of line number and offset.
249 
250  m_source = aStartingPoint.m_source;
251  m_lineNum = aStartingPoint.m_lineNum;
252 }
253 
254 
256 {
257  size_t nlOffset = m_lines.find( '\n', m_ndx );
258 
259  if( nlOffset == std::string::npos )
260  m_length = m_lines.length() - m_ndx;
261  else
262  m_length = nlOffset - m_ndx + 1; // include the newline, so +1
263 
264  if( m_length )
265  {
266  if( m_length >= m_maxLineLength )
267  THROW_IO_ERROR( _("Line length exceeded") );
268 
269  if( m_length+1 > m_capacity ) // +1 for terminating nul
271 
272  wxASSERT( m_ndx + m_length <= m_lines.length() );
273 
274  memcpy( m_line, &m_lines[m_ndx], m_length );
275  m_ndx += m_length;
276  }
277 
278  ++m_lineNum; // this gets incremented even if no bytes were read
279  m_line[m_length] = 0;
280 
281  return m_length ? m_line : NULL;
282 }
283 
284 
285 INPUTSTREAM_LINE_READER::INPUTSTREAM_LINE_READER( wxInputStream* aStream, const wxString& aSource ) :
287  m_stream( aStream )
288 {
289  m_source = aSource;
290 }
291 
292 
294 {
295  m_length = 0;
296 
297  for(;;)
298  {
299  if( m_length >= m_maxLineLength )
300  THROW_IO_ERROR( _( "Maximum line length exceeded" ) );
301 
302  if( m_length + 1 > m_capacity )
303  expandCapacity( m_capacity * 2 );
304 
305  // this read may fail, docs say to test LastRead() before trusting cc.
306  char cc = m_stream->GetC();
307 
308  if( !m_stream->LastRead() )
309  break;
310 
311  m_line[ m_length++ ] = cc;
312 
313  if( cc == '\n' )
314  break;
315  }
316 
317  m_line[ m_length ] = 0;
318 
319  // m_lineNum is incremented even if there was no line read, because this
320  // leads to better error reporting when we hit an end of file.
321  ++m_lineNum;
322 
323  return m_length ? m_line : NULL;
324 }
325 
326 
327 //-----<OUTPUTFORMATTER>----------------------------------------------------
328 
329 // factor out a common GetQuoteChar
330 
331 const char* OUTPUTFORMATTER::GetQuoteChar( const char* wrapee, const char* quote_char )
332 {
333  // Include '#' so a symbol is not confused with a comment. We intend
334  // to wrap any symbol starting with a '#'.
335  // Our LEXER class handles comments, and comments appear to be an extension
336  // to the SPECCTRA DSN specification.
337  if( *wrapee == '#' )
338  return quote_char;
339 
340  if( strlen( wrapee ) == 0 )
341  return quote_char;
342 
343  bool isFirst = true;
344 
345  for( ; *wrapee; ++wrapee, isFirst = false )
346  {
347  static const char quoteThese[] = "\t ()"
348  "%" // per Alfons of freerouting.net, he does not like this unquoted as of 1-Feb-2008
349  "{}" // guessing that these are problems too
350  ;
351 
352  // if the string to be wrapped (wrapee) has a delimiter in it,
353  // return the quote_char so caller wraps the wrapee.
354  if( strchr( quoteThese, *wrapee ) )
355  return quote_char;
356 
357  if( !isFirst && '-' == *wrapee )
358  return quote_char;
359  }
360 
361  return ""; // caller does not need to wrap, can use an unwrapped string.
362 }
363 
364 
365 const char* OUTPUTFORMATTER::GetQuoteChar( const char* wrapee ) const
366 {
367  return GetQuoteChar( wrapee, quoteChar );
368 }
369 
370 int OUTPUTFORMATTER::vprint( const char* fmt, va_list ap )
371 {
372  // This function can call vsnprintf twice.
373  // But internally, vsnprintf retrieves arguments from the va_list identified by arg as if
374  // va_arg was used on it, and thus the state of the va_list is likely to be altered by the call.
375  // see: www.cplusplus.com/reference/cstdio/vsnprintf
376  // we make a copy of va_list ap for the second call, if happens
377  va_list tmp;
378  va_copy( tmp, ap );
379  int ret = vsnprintf( &m_buffer[0], m_buffer.size(), fmt, ap );
380 
381  if( ret >= (int) m_buffer.size() )
382  {
383  m_buffer.resize( ret + 1000 );
384  ret = vsnprintf( &m_buffer[0], m_buffer.size(), fmt, tmp );
385  }
386 
387  va_end( tmp ); // Release the temporary va_list, initialised from ap
388 
389  if( ret > 0 )
390  write( &m_buffer[0], ret );
391 
392  return ret;
393 }
394 
395 
396 int OUTPUTFORMATTER::sprint( const char* fmt, ... )
397 {
398  va_list args;
399 
400  va_start( args, fmt );
401  int ret = vprint( fmt, args);
402  va_end( args );
403 
404  return ret;
405 }
406 
407 
408 int OUTPUTFORMATTER::Print( int nestLevel, const char* fmt, ... )
409 {
410 #define NESTWIDTH 2
411 
412  va_list args;
413 
414  va_start( args, fmt );
415 
416  int result = 0;
417  int total = 0;
418 
419  for( int i = 0; i < nestLevel; ++i )
420  {
421  // no error checking needed, an exception indicates an error.
422  result = sprint( "%*c", NESTWIDTH, ' ' );
423 
424  total += result;
425  }
426 
427  // no error checking needed, an exception indicates an error.
428  result = vprint( fmt, args );
429 
430  va_end( args );
431 
432  total += result;
433  return total;
434 }
435 
436 
437 std::string OUTPUTFORMATTER::Quotes( const std::string& aWrapee ) const
438 {
439  std::string ret;
440 
441  ret.reserve( aWrapee.size() * 2 + 2 );
442 
443  ret += '"';
444 
445  for( std::string::const_iterator it = aWrapee.begin(); it != aWrapee.end(); ++it )
446  {
447  switch( *it )
448  {
449  case '\n':
450  ret += '\\';
451  ret += 'n';
452  break;
453  case '\r':
454  ret += '\\';
455  ret += 'r';
456  break;
457  case '\\':
458  ret += '\\';
459  ret += '\\';
460  break;
461  case '"':
462  ret += '\\';
463  ret += '"';
464  break;
465  default:
466  ret += *it;
467  }
468  }
469 
470  ret += '"';
471 
472  return ret;
473 }
474 
475 
476 std::string OUTPUTFORMATTER::Quotew( const wxString& aWrapee ) const
477 {
478  // wxStrings are always encoded as UTF-8 as we convert to a byte sequence.
479  // The non-virutal function calls the virtual workhorse function, and if
480  // a different quoting or escaping strategy is desired from the standard,
481  // a derived class can overload Quotes() above, but
482  // should never be a reason to overload this Quotew() here.
483  return Quotes( (const char*) aWrapee.utf8_str() );
484 }
485 
486 
487 //-----<STRING_FORMATTER>----------------------------------------------------
488 
489 void STRING_FORMATTER::write( const char* aOutBuf, int aCount )
490 {
491  m_mystring.append( aOutBuf, aCount );
492 }
493 
495 {
496  std::string copy = m_mystring;
497 
498  m_mystring.clear();
499 
500  for( std::string::iterator i = copy.begin(); i != copy.end(); ++i )
501  {
502  if( !isspace( *i ) && *i != ')' && *i != '(' && *i != '"' )
503  {
504  m_mystring += *i;
505  }
506  }
507 }
508 
509 //-----<FILE_OUTPUTFORMATTER>----------------------------------------
510 
511 FILE_OUTPUTFORMATTER::FILE_OUTPUTFORMATTER( const wxString& aFileName, const wxChar* aMode,
512  char aQuoteChar ):
513  OUTPUTFORMATTER( OUTPUTFMTBUFZ, aQuoteChar ),
514  m_filename( aFileName )
515 {
516  m_fp = wxFopen( aFileName, aMode );
517 
518  if( !m_fp )
519  THROW_IO_ERROR( strerror( errno ) );
520 }
521 
522 
524 {
525  if( m_fp )
526  fclose( m_fp );
527 }
528 
529 
530 void FILE_OUTPUTFORMATTER::write( const char* aOutBuf, int aCount )
531 {
532  if( fwrite( aOutBuf, (unsigned) aCount, 1, m_fp ) != 1 )
533  THROW_IO_ERROR( strerror( errno ) );
534 }
535 
536 
537 //-----<STREAM_OUTPUTFORMATTER>--------------------------------------
538 
539 void STREAM_OUTPUTFORMATTER::write( const char* aOutBuf, int aCount )
540 {
541  int lastWrite;
542 
543  // This might delay awhile if you were writing to say a socket, but for
544  // a file it should only go through the loop once.
545  for( int total = 0; total<aCount; total += lastWrite )
546  {
547  lastWrite = m_os.Write( aOutBuf, aCount ).LastWrite();
548 
549  if( !m_os.IsOk() )
550  {
551  THROW_IO_ERROR( _( "OUTPUTSTREAM_OUTPUTFORMATTER write error" ) );
552  }
553  }
554 }
555 
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
#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: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
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:78
unsigned m_lineNum
Definition: richio.h:156
#define NULL
unsigned m_capacity
no. bytes allocated for line.
Definition: richio.h:159
std::string m_mystring
Definition: richio.h:444
int vprint(const char *fmt, va_list ap)
Definition: richio.cpp:370
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 getc_unlocked
Definition: richio.cpp:38
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
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
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:473
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 _(s)
Definition: 3d_actions.cpp:33
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
static int vprint(std::string *result, const char *format, va_list ap)
Definition: richio.cpp:42
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
#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:232
unsigned m_maxLineLength
maximum allowed capacity using resizing.
Definition: richio.h:161
std::string Quotew(const wxString &aWrapee) const
Definition: richio.cpp:476