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 <exception>
28#include <config.h> // HAVE_FGETC_NOLOCK
29
30#include <kiplatform/io.h>
31#include <core/ignore.h>
32#include <richio.h>
33#include <errno.h>
34#include <string.h>
35#include <advanced_config.h>
37
38#include <wx/filename.h>
39#include <wx/log.h>
40#include <wx/translation.h>
41#include <wx/ffile.h>
42
43
44// Fall back to getc() when getc_unlocked() is not available on the target platform.
45#if !defined( HAVE_FGETC_NOLOCK )
46#ifdef _MSC_VER
47
48// getc is not a macro on windows and adds a tiny overhead for the indirection to eventually
49// calling fgetc
50#define getc_unlocked _fgetc_nolock
51#else
52#define getc_unlocked getc
53#endif
54#endif
55
56
57wxString SafeReadFile( const wxString& aFilePath, const wxString& aReadType )
58{
59 // Check the path exists as a file first
60 // the IsOpened check would be logical, but on linux you can fopen (in read mode) a directory
61 // And then everything else in here will barf
62 if( !wxFileExists( aFilePath ) )
63 THROW_IO_ERROR( wxString::Format( _( "File '%s' does not exist." ), aFilePath ) );
64
65 wxString contents;
66 wxFFile ff( aFilePath );
67
68 if( !ff.IsOpened() )
69 THROW_IO_ERROR( wxString::Format( _( "Cannot open file '%s'." ), aFilePath ) );
70
71 // Try to determine encoding
72 char bytes[2]{ 0 };
73 ff.Read( bytes, 2 );
74 bool utf16le = bytes[1] == 0;
75
76 ff.Seek( 0 );
77
78 bool readOk = false;
79
80 if( utf16le )
81 readOk = ff.ReadAll( &contents, wxMBConvUTF16LE() );
82 else
83 readOk = ff.ReadAll( &contents, wxMBConvUTF8() );
84
85 if( !readOk || contents.empty() )
86 {
87 ff.Seek( 0 );
88 ff.ReadAll( &contents, wxConvAuto( wxFONTENCODING_CP1252 ) );
89 }
90
91 if( contents.empty() )
92 THROW_IO_ERROR( wxString::Format( _( "Unable to read file '%s'." ), aFilePath ) );
93
94 // I'm not sure what the source of this style of line-endings is, but it can be
95 // found in some Fairchild Semiconductor SPICE files.
96 contents.Replace( wxS( "\r\r\n" ), wxS( "\n" ) );
97
98 return contents;
99}
100
101
102//-----<LINE_READER>------------------------------------------------------
103
104LINE_READER::LINE_READER( unsigned aMaxLineLength ) :
105 m_length( 0 ), m_lineNum( 0 ), m_line( nullptr ),
106 m_capacity( 0 ), m_maxLineLength( aMaxLineLength )
107{
108 if( aMaxLineLength != 0 )
109 {
110 // start at the INITIAL size, expand as needed up to the MAX size in maxLineLength
112
113 // but never go above user's aMaxLineLength, and leave space for trailing nul
114 if( m_capacity > aMaxLineLength+1 )
115 m_capacity = aMaxLineLength+1;
116
117 // Be sure there is room for a null EOL char, so reserve at least capacity+1 bytes
118 // to ensure capacity line length and avoid corner cases
119 // Use capacity+5 to cover and corner case
120 m_line = new char[m_capacity+5];
121
122 m_line[0] = '\0';
123 }
124}
125
126
128{
129 delete[] m_line;
130}
131
132
133void LINE_READER::expandCapacity( unsigned aNewsize )
134{
135 // m_length can equal maxLineLength and nothing breaks, there's room for
136 // the terminating nul. cannot go over this.
137 if( aNewsize > m_maxLineLength+1 )
138 aNewsize = m_maxLineLength+1;
139
140 if( aNewsize > m_capacity )
141 {
142 m_capacity = aNewsize;
143
144 // resize the buffer, and copy the original data
145 // Be sure there is room for the null EOL char, so reserve capacity+1 bytes
146 // to ensure capacity line length. Use capacity+5 to cover and corner case
147 char* bigger = new char[m_capacity+5];
148
149 wxASSERT( m_capacity >= m_length+1 );
150
151 memcpy( bigger, m_line, m_length );
152 bigger[m_length] = 0;
153
154 delete[] m_line;
155 m_line = bigger;
156 }
157}
158
159
160FILE_LINE_READER::FILE_LINE_READER( const wxString& aFileName, unsigned aStartingLineNumber,
161 unsigned aMaxLineLength ):
162 LINE_READER( aMaxLineLength ), m_iOwn( true )
163{
164 m_fp = KIPLATFORM::IO::SeqFOpen( aFileName, wxT( "rt" ) );
165
166 if( !m_fp )
167 {
168 wxString msg = wxString::Format( _( "Unable to open %s for reading." ),
169 aFileName.GetData() );
170 THROW_IO_ERROR( msg );
171 }
172
173 m_source = aFileName;
174 m_lineNum = aStartingLineNumber;
175}
176
177
178FILE_LINE_READER::FILE_LINE_READER( FILE* aFile, const wxString& aFileName,
179 bool doOwn,
180 unsigned aStartingLineNumber,
181 unsigned aMaxLineLength ) :
182 LINE_READER( aMaxLineLength ), m_iOwn( doOwn ), m_fp( aFile )
183{
184 m_source = aFileName;
185 m_lineNum = aStartingLineNumber;
186}
187
188
190{
191 if( m_iOwn && m_fp )
192 fclose( m_fp );
193}
194
195
197{
198 fseek( m_fp, 0, SEEK_END );
199 long int fileLength = ftell( m_fp );
200 rewind( m_fp );
201
202 return fileLength;
203}
204
205
207{
208 return ftell( m_fp );
209}
210
211
213{
214 m_length = 0;
215
216 for( ;; )
217 {
219 THROW_IO_ERROR( _( "Maximum line length exceeded" ) );
220
221 if( m_length >= m_capacity )
223
224 // faster, POSIX compatible fgetc(), no locking.
225 int cc = getc_unlocked( m_fp );
226
227 if( cc == EOF )
228 break;
229
230 m_line[ m_length++ ] = (char) cc;
231
232 if( cc == '\n' )
233 break;
234 }
235
236 m_line[ m_length ] = 0;
237
238 // m_lineNum is incremented even if there was no line read, because this
239 // leads to better error reporting when we hit an end of file.
240 ++m_lineNum;
241
242 return m_length ? m_line : nullptr;
243}
244
245
246STRING_LINE_READER::STRING_LINE_READER( const std::string& aString, const wxString& aSource ):
248 m_lines( aString ), m_ndx( 0 )
249{
250 // Clipboard text should be nice and _use multiple lines_ so that
251 // we can report _line number_ oriented error messages when parsing.
252 m_source = aSource;
253}
254
255
258 m_lines( aStartingPoint.m_lines ),
259 m_ndx( aStartingPoint.m_ndx )
260{
261 // since we are keeping the same "source" name, for error reporting purposes
262 // we need to have the same notion of line number and offset.
263
264 m_source = aStartingPoint.m_source;
265 m_lineNum = aStartingPoint.m_lineNum;
266}
267
268
270{
271 size_t nlOffset = m_lines.find( '\n', m_ndx );
272 unsigned new_length;
273
274 if( nlOffset == std::string::npos )
275 new_length = m_lines.length() - m_ndx;
276 else
277 new_length = nlOffset - m_ndx + 1; // include the newline, so +1
278
279 if( new_length )
280 {
281 if( new_length >= m_maxLineLength )
282 THROW_IO_ERROR( _("Line length exceeded") );
283
284 if( new_length+1 > m_capacity ) // +1 for terminating nul
285 expandCapacity( new_length+1 );
286
287 wxASSERT( m_ndx + new_length <= m_lines.length() );
288
289 memcpy( m_line, &m_lines[m_ndx], new_length );
290 m_ndx += new_length;
291 }
292
293 m_length = new_length;
294 ++m_lineNum; // this gets incremented even if no bytes were read
295 m_line[m_length] = 0;
296
297 return m_length ? m_line : nullptr;
298}
299
300
302 const wxString& aSource ) :
304 m_stream( aStream )
305{
306 m_source = aSource;
307}
308
309
311{
312 m_length = 0;
313
314 for( ;; )
315 {
317 THROW_IO_ERROR( _( "Maximum line length exceeded" ) );
318
319 if( m_length + 1 > m_capacity )
321
322 // this read may fail, docs say to test LastRead() before trusting cc.
323 char cc = m_stream->GetC();
324
325 if( !m_stream->LastRead() )
326 break;
327
328 m_line[ m_length++ ] = cc;
329
330 if( cc == '\n' )
331 break;
332 }
333
334 m_line[ m_length ] = 0;
335
336 // m_lineNum is incremented even if there was no line read, because this
337 // leads to better error reporting when we hit an end of file.
338 ++m_lineNum;
339
340 return m_length ? m_line : nullptr;
341}
342
343
344//-----<OUTPUTFORMATTER>----------------------------------------------------
345
346// factor out a common GetQuoteChar
347
348const char* OUTPUTFORMATTER::GetQuoteChar( const char* wrapee, const char* quote_char )
349{
350 // Include '#' so a symbol is not confused with a comment. We intend
351 // to wrap any symbol starting with a '#'.
352 // Our LEXER class handles comments, and comments appear to be an extension
353 // to the SPECCTRA DSN specification.
354 if( *wrapee == '#' )
355 return quote_char;
356
357 if( strlen( wrapee ) == 0 )
358 return quote_char;
359
360 bool isFirst = true;
361
362 for( ; *wrapee; ++wrapee, isFirst = false )
363 {
364 static const char quoteThese[] = "\t ()"
365 "%" // per Alfons of freerouting.net, he does not like this unquoted as of 1-Feb-2008
366 "{}" // guessing that these are problems too
367 ;
368
369 // if the string to be wrapped (wrapee) has a delimiter in it,
370 // return the quote_char so caller wraps the wrapee.
371 if( strchr( quoteThese, *wrapee ) )
372 return quote_char;
373
374 if( !isFirst && '-' == *wrapee )
375 return quote_char;
376 }
377
378 return ""; // caller does not need to wrap, can use an unwrapped string.
379}
380
381
382const char* OUTPUTFORMATTER::GetQuoteChar( const char* wrapee ) const
383{
384 return GetQuoteChar( wrapee, quoteChar );
385}
386
387
388int OUTPUTFORMATTER::vprint( const char* fmt, va_list ap )
389{
390 // This function can call vsnprintf twice.
391 // But internally, vsnprintf retrieves arguments from the va_list identified by arg as if
392 // va_arg was used on it, and thus the state of the va_list is likely to be altered by the call.
393 // see: www.cplusplus.com/reference/cstdio/vsnprintf
394 // we make a copy of va_list ap for the second call, if happens
395 va_list tmp;
396 va_copy( tmp, ap );
397 int ret = vsnprintf( &m_buffer[0], m_buffer.size(), fmt, ap );
398
399 if( ret >= (int) m_buffer.size() )
400 {
401 m_buffer.resize( ret + 1000 );
402 ret = vsnprintf( &m_buffer[0], m_buffer.size(), fmt, tmp );
403 }
404
405 va_end( tmp ); // Release the temporary va_list, initialised from ap
406
407 if( ret > 0 )
408 write( &m_buffer[0], ret );
409
410 return ret;
411}
412
413
414int OUTPUTFORMATTER::sprint( const char* fmt, ... )
415{
416 va_list args;
417
418 va_start( args, fmt );
419 int ret = vprint( fmt, args );
420 va_end( args );
421
422 return ret;
423}
424
425
426int OUTPUTFORMATTER::Print( int nestLevel, const char* fmt, ... )
427{
428#define NESTWIDTH 2
429
430 va_list args;
431
432 va_start( args, fmt );
433
434 int result = 0;
435 int total = 0;
436
437 for( int i = 0; i < nestLevel; ++i )
438 {
439 // no error checking needed, an exception indicates an error.
440 result = sprint( "%*c", NESTWIDTH, ' ' );
441
442 total += result;
443 }
444
445 // no error checking needed, an exception indicates an error.
446 result = vprint( fmt, args );
447
448 va_end( args );
449
450 total += result;
451 return total;
452}
453
454
455int OUTPUTFORMATTER::Print( const char* fmt, ... )
456{
457 va_list args;
458
459 va_start( args, fmt );
460
461 int result = 0;
462
463 // no error checking needed, an exception indicates an error.
464 result = vprint( fmt, args );
465
466 va_end( args );
467
468 return result;
469}
470
471
472std::string OUTPUTFORMATTER::Quotes( const std::string& aWrapee ) const
473{
474 std::string ret;
475
476 ret.reserve( aWrapee.size() * 2 + 2 );
477
478 ret += '"';
479
480 for( std::string::const_iterator it = aWrapee.begin(); it != aWrapee.end(); ++it )
481 {
482 switch( *it )
483 {
484 case '\n':
485 ret += '\\';
486 ret += 'n';
487 break;
488 case '\r':
489 ret += '\\';
490 ret += 'r';
491 break;
492 case '\\':
493 ret += '\\';
494 ret += '\\';
495 break;
496 case '"':
497 ret += '\\';
498 ret += '"';
499 break;
500 default:
501 ret += *it;
502 }
503 }
504
505 ret += '"';
506
507 return ret;
508}
509
510
511std::string OUTPUTFORMATTER::Quotew( const wxString& aWrapee ) const
512{
513 // wxStrings are always encoded as UTF-8 as we convert to a byte sequence.
514 // The non-virtual function calls the virtual workhorse function, and if
515 // a different quoting or escaping strategy is desired from the standard,
516 // a derived class can overload Quotes() above, but
517 // should never be a reason to overload this Quotew() here.
518 return Quotes( (const char*) aWrapee.utf8_str() );
519}
520
521
522//-----<STRING_FORMATTER>----------------------------------------------------
523
524void STRING_FORMATTER::write( const char* aOutBuf, int aCount )
525{
526 m_mystring.append( aOutBuf, aCount );
527}
528
529
531{
532 std::string copy = m_mystring;
533
534 m_mystring.clear();
535
536 for( std::string::iterator i = copy.begin(); i != copy.end(); ++i )
537 {
538 if( !isspace( *i ) && *i != ')' && *i != '(' && *i != '"' )
539 {
540 m_mystring += *i;
541 }
542 }
543}
544
545
546// Both file-output formatters below write to a sibling temp file and atomically rename
547// over the target on Finish(). A crash, throw, or power loss before commit leaves the
548// final target byte-identical to its prior contents.
549
550namespace
551{
552void atomicCommit( FILE*& aFp, const wxString& aTempPath, const wxString& aFinalPath )
553{
554 if( !KIPLATFORM::IO::FlushToDisk( aFp ) )
555 {
556 int err = errno;
557 fclose( aFp );
558 aFp = nullptr;
559 wxRemoveFile( aTempPath );
560 THROW_IO_ERROR( wxString::Format( _( "Cannot flush '%s' to disk: %s" ), aTempPath,
561 wxString::FromUTF8( strerror( err ) ) ) );
562 }
563
564 fclose( aFp );
565 aFp = nullptr;
566
567 wxString commitError;
568
569 if( !KIPLATFORM::IO::CommitTempFile( aTempPath, aFinalPath, &commitError ) )
570 {
571 wxRemoveFile( aTempPath );
572 THROW_IO_ERROR( commitError );
573 }
574}
575
576
577void discardTempFile( FILE*& aFp, const wxString& aTempPath )
578{
579 if( aFp )
580 {
581 fclose( aFp );
582 aFp = nullptr;
583 }
584
585 if( !aTempPath.IsEmpty() )
586 wxRemoveFile( aTempPath );
587}
588
589
590// Shared destructor body for the atomic-commit formatters. Throwing from a destructor
591// while another exception is in flight calls std::terminate, so during stack unwinding
592// we discard the temp and let the original exception propagate. When no exception is in
593// flight we fall back to a best-effort commit for callers that have not been migrated to
594// explicit Finish() yet. Explicit Finish() is the contract for anything that cares about
595// data-loss detection; destructor-path failures are surfaced as wxLogError because we
596// cannot throw safely from here.
597template <typename FinishFn>
598void finalizeFormatter( FILE*& aFp, const wxString& aTempPath, const wxString& aFilename,
599 bool aCommitted, FinishFn aFinish )
600{
601 if( aCommitted )
602 return;
603
604 if( std::uncaught_exceptions() > 0 )
605 {
606 discardTempFile( aFp, aTempPath );
607 return;
608 }
609
610 try
611 {
612 aFinish();
613 }
614 catch( const std::exception& e )
615 {
616 wxLogError( _( "Failed to commit save of '%s': %s. "
617 "The file on disk has not been modified." ),
618 aFilename, wxString::FromUTF8( e.what() ) );
619 discardTempFile( aFp, aTempPath );
620 }
621}
622} // anonymous namespace
623
624
625FILE_OUTPUTFORMATTER::FILE_OUTPUTFORMATTER( const wxString& aFileName, const wxChar* aMode,
626 char aQuoteChar ):
627 OUTPUTFORMATTER( OUTPUTFMTBUFZ, aQuoteChar ),
628 m_fp( nullptr ),
629 m_filename( KIPLATFORM::IO::ResolveSymlinkTarget( aFileName ) ),
630 m_committed( false )
631{
632 wxString err;
634
635 if( !m_fp )
636 THROW_IO_ERROR( err );
637}
638
639
641{
642 finalizeFormatter( m_fp, m_tempPath, m_filename, m_committed, [this] { Finish(); } );
643}
644
645
647{
648 if( m_committed )
649 return true;
650
651 if( !m_fp )
652 {
653 if( !m_tempPath.IsEmpty() )
654 wxRemoveFile( m_tempPath );
655
656 return false;
657 }
658
659 atomicCommit( m_fp, m_tempPath, m_filename );
660 m_committed = true;
661 return true;
662}
663
664
665void FILE_OUTPUTFORMATTER::write( const char* aOutBuf, int aCount )
666{
667 if( fwrite( aOutBuf, (unsigned) aCount, 1, m_fp ) != 1 )
668 THROW_IO_ERROR( strerror( errno ) );
669}
670
671
673 KICAD_FORMAT::FORMAT_MODE aFormatMode,
674 const wxChar* aMode,
675 char aQuoteChar ) :
676 OUTPUTFORMATTER( OUTPUTFMTBUFZ, aQuoteChar ),
677 m_fp( nullptr ),
678 m_filename( KIPLATFORM::IO::ResolveSymlinkTarget( aFileName ) ),
679 m_committed( false ),
680 m_mode( aFormatMode )
681{
682 if( ADVANCED_CFG::GetCfg().m_CompactSave && m_mode == KICAD_FORMAT::FORMAT_MODE::NORMAL )
683 m_mode = KICAD_FORMAT::FORMAT_MODE::COMPACT_TEXT_PROPERTIES;
684
685 wxString err;
687
688 if( !m_fp )
689 THROW_IO_ERROR( err );
690}
691
692
698
699
701{
702 if( m_committed )
703 return true;
704
705 if( !m_fp )
706 {
707 if( !m_tempPath.IsEmpty() )
708 wxRemoveFile( m_tempPath );
709
710 return false;
711 }
712
714
715 if( !m_buf.empty() && fwrite( m_buf.c_str(), m_buf.length(), 1, m_fp ) != 1 )
716 {
717 int err = errno;
718 fclose( m_fp );
719 m_fp = nullptr;
720 wxRemoveFile( m_tempPath );
721 THROW_IO_ERROR( wxString::Format( _( "Write failed to '%s': %s" ), m_tempPath,
722 wxString::FromUTF8( strerror( err ) ) ) );
723 }
724
725 atomicCommit( m_fp, m_tempPath, m_filename );
726 m_committed = true;
727 return true;
728}
729
730
731void PRETTIFIED_FILE_OUTPUTFORMATTER::write( const char* aOutBuf, int aCount )
732{
733 m_buf.append( aOutBuf, aCount );
734}
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
~FILE_LINE_READER()
May or may not close the open file, depending on doOwn in constructor.
Definition richio.cpp:189
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:160
FILE * m_fp
I may own this file, but might not.
Definition richio.h:218
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition richio.cpp:212
bool m_iOwn
if I own the file, I'll promise to close it, else not.
Definition richio.h:217
long int FileLength()
Definition richio.cpp:196
long int CurPos()
Definition richio.cpp:206
FILE * m_fp
takes ownership; points at the temp file
Definition richio.h:503
bool m_committed
set true once Finish() has renamed into place
Definition richio.h:506
FILE_OUTPUTFORMATTER(const wxString &aFileName, const wxChar *aMode=wxT("wt"), char aQuoteChar='"' )
Definition richio.cpp:625
void write(const char *aOutBuf, int aCount) override
Should be coded in the interface implementation (derived) classes.
Definition richio.cpp:665
wxString m_filename
final destination path
Definition richio.h:504
wxString m_tempPath
sibling temp file being written
Definition richio.h:505
bool Finish() override
Flushes the temp file to disk and atomically renames it over the final target path.
Definition richio.cpp:646
wxInputStream * m_stream
The input stream to read. No ownership of this pointer.
Definition richio.h:273
INPUTSTREAM_LINE_READER(wxInputStream *aStream, const wxString &aSource)
Construct a LINE_READER from a wxInputStream object.
Definition richio.cpp:301
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition richio.cpp:310
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:104
unsigned m_maxLineLength
maximum allowed capacity using resizing.
Definition richio.h:146
unsigned m_length
no. bytes in line before trailing nul.
Definition richio.h:140
unsigned m_capacity
no. bytes allocated for line.
Definition richio.h:144
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:133
char * m_line
the read line of UTF8 text
Definition richio.h:143
unsigned m_lineNum
Definition richio.h:141
virtual ~LINE_READER()
Definition richio.cpp:127
wxString m_source
origin of text lines, e.g. filename or "clipboard"
Definition richio.h:148
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:414
std::vector< char > m_buffer
Definition richio.h:407
OUTPUTFORMATTER(int aReserve=OUTPUTFMTBUFZ, char aQuoteChar='"' )
Definition richio.h:297
char quoteChar[2]
Definition richio.h:408
int vprint(const char *fmt, va_list ap)
Definition richio.cpp:388
std::string Quotew(const wxString &aWrapee) const
Definition richio.cpp:511
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition richio.cpp:426
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:348
virtual std::string Quotes(const std::string &aWrapee) const
Check aWrapee input string for a need to be quoted (e.g.
Definition richio.cpp:472
void write(const char *aOutBuf, int aCount) override
Should be coded in the interface implementation (derived) classes.
Definition richio.cpp:731
wxString m_filename
final destination path
Definition richio.h:535
PRETTIFIED_FILE_OUTPUTFORMATTER(const wxString &aFileName, KICAD_FORMAT::FORMAT_MODE aFormatMode=KICAD_FORMAT::FORMAT_MODE::NORMAL, const wxChar *aMode=wxT("wt"), char aQuoteChar='"' )
Definition richio.cpp:672
wxString m_tempPath
sibling temp file being written
Definition richio.h:536
bool m_committed
set true once rename has landed
Definition richio.h:537
bool Finish() override
Runs prettification over the buffered bytes, writes them to the sibling temp file,...
Definition richio.cpp:700
KICAD_FORMAT::FORMAT_MODE m_mode
Definition richio.h:539
void write(const char *aOutBuf, int aCount) override
Should be coded in the interface implementation (derived) classes.
Definition richio.cpp:524
void StripUseless()
Removes whitespace, '(', and ')' from the string.
Definition richio.cpp:530
std::string m_mystring
Definition richio.h:459
std::string m_lines
Definition richio.h:228
STRING_LINE_READER(const std::string &aString, const wxString &aSource)
Construct a string line reader.
Definition richio.cpp:246
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition richio.cpp:269
#define _(s)
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
void Prettify(std::string &aSource, FORMAT_MODE aMode)
Pretty-prints s-expression text according to KiCad format rules.
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:39
FILE * OpenUniqueSiblingTempFile(const wxString &aTargetPath, const wxString &aMode, wxString *aTempPathOut, wxString *aError=nullptr)
Opens a fresh sibling temp file next to aTargetPath with exclusive-create semantics (POSIX O_CREAT|O_...
Definition common/io.cpp:66
bool CommitTempFile(const wxString &aTempPath, const wxString &aTargetPath, wxString *aError=nullptr)
Completes an atomic save.
bool FlushToDisk(FILE *aFp)
Flushes user-space buffers for aFp and forces the kernel/filesystem to commit the file's data blocks ...
Definition unix/io.cpp:182
#define NESTWIDTH
#define getc_unlocked
Definition richio.cpp:52
wxString SafeReadFile(const wxString &aFilePath, const wxString &aReadType)
Nominally opens a file and reads it into a string.
Definition richio.cpp:57
#define OUTPUTFMTBUFZ
default buffer size for any OUTPUT_FORMATTER
Definition richio.h:277
#define LINE_READER_LINE_INITIAL_SIZE
Definition richio.h:59
#define LINE_READER_LINE_DEFAULT_MAX
Definition richio.h:58
wxString result
Test unit parsing edge cases and error handling.