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