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 ) )
59 THROW_IO_ERROR( wxString::Format(
_(
"File '%s' does not exist." ), aFilePath ) );
62 wxFFile ff( aFilePath );
65 THROW_IO_ERROR( wxString::Format(
_(
"Cannot open file '%s'." ), 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() )
88 THROW_IO_ERROR( wxString::Format(
_(
"Unable to read file '%s'." ), aFilePath ) );
92 contents.Replace( wxS(
"\r\r\n" ), wxS(
"\n" ) );
104 if( aMaxLineLength != 0 )
157 unsigned aMaxLineLength ):
164 wxString msg = wxString::Format(
_(
"Unable to open %s for reading." ),
165 aFileName.GetData() );
176 unsigned aStartingLineNumber,
177 unsigned aMaxLineLength ) :
194 fseek(
m_fp, 0, SEEK_END );
195 long int fileLength = ftell(
m_fp );
204 return ftell(
m_fp );
270 if( nlOffset == std::string::npos )
273 new_length = nlOffset -
m_ndx + 1;
298 const wxString& aSource ) :
353 if( strlen( wrapee ) == 0 )
358 for( ; *wrapee; ++wrapee, isFirst = false )
360 static const char quoteThese[] =
"\t ()"
367 if( strchr( quoteThese, *wrapee ) )
370 if( !isFirst &&
'-' == *wrapee )
414 va_start( args, fmt );
415 int ret =
vprint( fmt, args );
428 va_start( args, fmt );
433 for(
int i = 0; i < nestLevel; ++i )
455 va_start( args, fmt );
472 ret.reserve( aWrapee.size() * 2 + 2 );
476 for( std::string::const_iterator it = aWrapee.begin(); it != aWrapee.end(); ++it )
514 return Quotes( (
const char*) aWrapee.utf8_str() );
532 for( std::string::iterator i =
copy.begin(); i !=
copy.end(); ++i )
534 if( !isspace( *i ) && *i !=
')' && *i !=
'(' && *i !=
'"' )
547 m_fp = wxFopen( aFileName, aMode );
563 if( fwrite( aOutBuf, (
unsigned) aCount, 1,
m_fp ) != 1 )
569 KICAD_FORMAT::FORMAT_MODE aFormatMode,
576 m_mode = KICAD_FORMAT::FORMAT_MODE::COMPACT_TEXT_PROPERTIES;
578 m_fp = wxFopen( aFileName, aMode );
615 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.