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