34#include <wx/filename.h>
36#include <wx/translation.h>
41#if !defined( HAVE_FGETC_NOLOCK )
46#define getc_unlocked _fgetc_nolock
48#define getc_unlocked getc
53wxString
SafeReadFile(
const wxString& aFilePath,
const wxString& aReadType )
58 if( !wxFileExists( aFilePath ) )
62 wxFFile ff( aFilePath );
70 bool utf16le = bytes[1] == 0;
77 readOk = ff.ReadAll( &contents, wxMBConvUTF16LE() );
79 readOk = ff.ReadAll( &contents, wxMBConvUTF8() );
81 if( !readOk || contents.empty() )
84 ff.ReadAll( &contents, wxConvAuto( wxFONTENCODING_CP1252 ) );
87 if( contents.empty() )
92 contents.Replace( wxS(
"\r\r\n" ), wxS(
"\n" ) );
104 if( aMaxLineLength != 0 )
157 unsigned aMaxLineLength ):
163 THROW_IO_ERRORF(
_(
"Unable to open %s for reading." ), aFileName.GetData() );
172 unsigned aStartingLineNumber,
173 unsigned aMaxLineLength ) :
190 fseek(
m_fp, 0, SEEK_END );
191 long int fileLength = ftell(
m_fp );
200 return ftell(
m_fp );
266 if( nlOffset == std::string::npos )
269 new_length = nlOffset -
m_ndx + 1;
294 const wxString& aSource ) :
349 if( strlen( wrapee ) == 0 )
354 for( ; *wrapee; ++wrapee, isFirst = false )
356 static const char quoteThese[] =
"\t ()"
363 if( strchr( quoteThese, *wrapee ) )
366 if( !isFirst &&
'-' == *wrapee )
410 va_start( args, fmt );
411 int ret =
vprint( fmt, args );
424 va_start( args, fmt );
429 for(
int i = 0; i < nestLevel; ++i )
451 va_start( args, fmt );
468 ret.reserve( aWrapee.size() * 2 + 2 );
472 for( std::string::const_iterator it = aWrapee.begin(); it != aWrapee.end(); ++it )
510 return Quotes( (
const char*) aWrapee.utf8_str() );
528 for( std::string::iterator i =
copy.begin(); i !=
copy.end(); ++i )
530 if( !isspace( *i ) && *i !=
')' && *i !=
'(' && *i !=
'"' )
544void atomicCommit( FILE*& aFp,
const wxString& aTempPath,
const wxString& aFinalPath )
551 wxRemoveFile( aTempPath );
552 THROW_IO_ERRORF(
_(
"Cannot flush '%s' to disk: %s" ), aTempPath, wxString::FromUTF8( strerror( err ) ) );
558 wxString commitError;
562 wxRemoveFile( aTempPath );
568void discardTempFile( FILE*& aFp,
const wxString& aTempPath )
576 if( !aTempPath.IsEmpty() )
577 wxRemoveFile( aTempPath );
588template <
typename FinishFn>
589void finalizeFormatter( FILE*& aFp,
const wxString& aTempPath,
const wxString& aFilename,
590 bool aCommitted, FinishFn aFinish )
595 if( std::uncaught_exceptions() > 0 )
597 discardTempFile( aFp, aTempPath );
605 catch(
const std::exception& e )
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 );
657 if( fwrite( aOutBuf, (
unsigned) aCount, 1,
m_fp ) != 1 )
663 KICAD_FORMAT::FORMAT_MODE aFormatMode,
673 m_mode = KICAD_FORMAT::FORMAT_MODE::COMPACT_TEXT_PROPERTIES;
722 m_buf.append( aOutBuf, aCount );
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.
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 ...
FILE * m_fp
I may own this file, but might not.
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
bool m_iOwn
if I own the file, I'll promise to close it, else not.
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.
unsigned m_maxLineLength
maximum allowed capacity using resizing.
unsigned m_length
no. bytes in line before trailing nul.
unsigned m_capacity
no. bytes allocated for line.
void expandCapacity(unsigned aNewsize)
Will expand the capacity of line up to maxLineLength but not greater, so be careful about making assu...
char * m_line
the read line of UTF8 text
wxString m_source
origin of text lines, e.g. filename or "clipboard"
STRING_LINE_READER(const std::string &aString, const wxString &aSource)
Construct a string line reader.
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
#define THROW_IO_ERRORF(msg,...)
wxString SafeReadFile(const wxString &aFilePath, const wxString &aReadType)
Nominally opens a file and reads it into a string.
#define OUTPUTFMTBUFZ
default buffer size for any OUTPUT_FORMATTER
#define LINE_READER_LINE_INITIAL_SIZE
#define LINE_READER_LINE_DEFAULT_MAX
wxString result
Test unit parsing edge cases and error handling.