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