38#include <wx/filename.h>
40#include <wx/translation.h>
45#if !defined( HAVE_FGETC_NOLOCK )
50#define getc_unlocked _fgetc_nolock
52#define getc_unlocked getc
57wxString
SafeReadFile(
const wxString& aFilePath,
const wxString& aReadType )
62 if( !wxFileExists( aFilePath ) )
63 THROW_IO_ERROR( wxString::Format(
_(
"File '%s' does not exist." ), aFilePath ) );
66 wxFFile ff( aFilePath );
69 THROW_IO_ERROR( wxString::Format(
_(
"Cannot open file '%s'." ), aFilePath ) );
74 bool utf16le = bytes[1] == 0;
81 readOk = ff.ReadAll( &contents, wxMBConvUTF16LE() );
83 readOk = ff.ReadAll( &contents, wxMBConvUTF8() );
85 if( !readOk || contents.empty() )
88 ff.ReadAll( &contents, wxConvAuto( wxFONTENCODING_CP1252 ) );
91 if( contents.empty() )
92 THROW_IO_ERROR( wxString::Format(
_(
"Unable to read file '%s'." ), aFilePath ) );
96 contents.Replace( wxS(
"\r\r\n" ), wxS(
"\n" ) );
108 if( aMaxLineLength != 0 )
161 unsigned aMaxLineLength ):
168 wxString msg = wxString::Format(
_(
"Unable to open %s for reading." ),
169 aFileName.GetData() );
180 unsigned aStartingLineNumber,
181 unsigned aMaxLineLength ) :
198 fseek(
m_fp, 0, SEEK_END );
199 long int fileLength = ftell(
m_fp );
208 return ftell(
m_fp );
274 if( nlOffset == std::string::npos )
277 new_length = nlOffset -
m_ndx + 1;
302 const wxString& aSource ) :
357 if( strlen( wrapee ) == 0 )
362 for( ; *wrapee; ++wrapee, isFirst = false )
364 static const char quoteThese[] =
"\t ()"
371 if( strchr( quoteThese, *wrapee ) )
374 if( !isFirst &&
'-' == *wrapee )
418 va_start( args, fmt );
419 int ret =
vprint( fmt, args );
432 va_start( args, fmt );
437 for(
int i = 0; i < nestLevel; ++i )
459 va_start( args, fmt );
476 ret.reserve( aWrapee.size() * 2 + 2 );
480 for( std::string::const_iterator it = aWrapee.begin(); it != aWrapee.end(); ++it )
518 return Quotes( (
const char*) aWrapee.utf8_str() );
536 for( std::string::iterator i =
copy.begin(); i !=
copy.end(); ++i )
538 if( !isspace( *i ) && *i !=
')' && *i !=
'(' && *i !=
'"' )
552void atomicCommit( FILE*& aFp,
const wxString& aTempPath,
const wxString& aFinalPath )
559 wxRemoveFile( aTempPath );
560 THROW_IO_ERROR( wxString::Format(
_(
"Cannot flush '%s' to disk: %s" ), aTempPath,
561 wxString::FromUTF8( strerror( err ) ) ) );
567 wxString commitError;
571 wxRemoveFile( aTempPath );
577void discardTempFile( FILE*& aFp,
const wxString& aTempPath )
585 if( !aTempPath.IsEmpty() )
586 wxRemoveFile( aTempPath );
597template <
typename FinishFn>
598void finalizeFormatter( FILE*& aFp,
const wxString& aTempPath,
const wxString& aFilename,
599 bool aCommitted, FinishFn aFinish )
604 if( std::uncaught_exceptions() > 0 )
606 discardTempFile( aFp, aTempPath );
614 catch(
const std::exception& e )
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 );
667 if( fwrite( aOutBuf, (
unsigned) aCount, 1,
m_fp ) != 1 )
673 KICAD_FORMAT::FORMAT_MODE aFormatMode,
683 m_mode = KICAD_FORMAT::FORMAT_MODE::COMPACT_TEXT_PROPERTIES;
722 wxString::FromUTF8( strerror( err ) ) ) );
733 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
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.