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