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