33#include <wx/filename.h>
34#include <wx/translation.h>
39#if !defined( HAVE_FGETC_NOLOCK )
44#define getc_unlocked _fgetc_nolock
46#define getc_unlocked getc
51wxString
SafeReadFile(
const wxString& aFilePath,
const wxString& aReadType )
56 if( !wxFileExists( aFilePath ) )
60 wxFFile ff( aFilePath );
68 bool utf16le = bytes[1] == 0;
75 readOk = ff.ReadAll( &contents, wxMBConvUTF16LE() );
77 readOk = ff.ReadAll( &contents, wxMBConvUTF8() );
79 if( !readOk || contents.empty() )
82 ff.ReadAll( &contents, wxConvAuto( wxFONTENCODING_CP1252 ) );
85 if( contents.empty() )
90 contents.Replace( wxS(
"\r\r\n" ), wxS(
"\n" ) );
102 if( aMaxLineLength != 0 )
155 unsigned aMaxLineLength ):
161 THROW_IO_ERRORF(
_(
"Unable to open %s for reading." ), aFileName.GetData() );
170 unsigned aStartingLineNumber,
171 unsigned aMaxLineLength ) :
188 fseek(
m_fp, 0, SEEK_END );
189 long int fileLength = ftell(
m_fp );
198 return ftell(
m_fp );
264 if( nlOffset == std::string::npos )
267 new_length = nlOffset -
m_ndx + 1;
292 const wxString& aSource ) :
347 if( strlen( wrapee ) == 0 )
352 for( ; *wrapee; ++wrapee, isFirst = false )
354 static const char quoteThese[] =
"\t ()"
361 if( strchr( quoteThese, *wrapee ) )
364 if( !isFirst &&
'-' == *wrapee )
408 va_start( args, fmt );
409 int ret =
vprint( fmt, args );
422 for(
int i = 0; i < aNestLevel; ++i )
436 va_start( args, fmt );
438 int total =
Indent( nestLevel );
441 total +=
vprint( fmt, args );
453 va_start( args, fmt );
470 ret.reserve( aWrapee.size() * 2 + 2 );
474 for( std::string::const_iterator it = aWrapee.begin(); it != aWrapee.end(); ++it )
512 return Quotes( (
const char*) aWrapee.utf8_str() );
530 for( std::string::iterator i =
copy.begin(); i !=
copy.end(); ++i )
532 if( !isspace( *i ) && *i !=
')' && *i !=
'(' && *i !=
'"' )
612 ret = fclose(
m_fp );
635 wxCHECK_MSG(
m_fp,
false, wxT(
"Commit() called on an already-used temp file (committed or abandoned)" ) );
646 wxString::FromUTF8( strerror( err ) ) );
655 wxString commitError;
690 if( fwrite( aOutBuf, (
unsigned) aCount, 1,
m_tempFile->File() ) != 1 )
696 KICAD_FORMAT::FORMAT_MODE aFormatMode,
697 const wxChar* aMode,
char aQuoteChar ) :
703 m_mode = KICAD_FORMAT::FORMAT_MODE::COMPACT_TEXT_PROPERTIES;
725 wxString::FromUTF8( strerror( err ) ) );
735 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"
A class that owns a sibling temp file, which is created next to the target at construction.
SIBLING_TEMP_FILE(const wxString &aTargetPath, const wxChar *aMode)
SIBLING_TEMP_FILE & operator=(const SIBLING_TEMP_FILE &)=delete
Ditto for assignment: the temp file cannot be shared.
const wxString & Path() const
The temp file's path on disk.
bool Commit()
Flush, close and atomically rename the temp file over the target.
int Abandon()
Abandons the in-progress save: closes the temp file handle without committing, leaving the file on di...
FILE * File()
The open temp file to write to. Null once Commit() or Abandon() has run.
SIBLING_TEMP_FILE(const SIBLING_TEMP_FILE &)=delete
Copy is meaningless: the temp file cannot be shared.
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.