36#include <wx/translation.h>
41#if !defined( HAVE_FGETC_NOLOCK )
46#define getc_unlocked _fgetc_nolock
48#define getc_unlocked getc
53static int vprint( std::string* result,
const char* format, va_list ap )
57 size_t len = vsnprintf(
nullptr, 0, format, tmp );
61 size_t size = result->size();
62 result->resize( size + len );
65 len = vsnprintf( result->data() + size, len + 1, format, ap );
71int StrPrintf( std::string* result,
const char* format, ... )
75 va_start( args, format );
76 int ret =
vprint( result, format, args );
88 va_start( args, format );
96wxString
SafeReadFile(
const wxString& aFilePath,
const wxString& aReadType )
101 if( !wxFileExists( aFilePath ) )
102 THROW_IO_ERROR( wxString::Format(
_(
"File '%s' does not exist." ), aFilePath ) );
105 wxFFile ff( aFilePath );
108 THROW_IO_ERROR( wxString::Format(
_(
"Cannot open file '%s'." ), aFilePath ) );
113 bool utf16le = bytes[1] == 0;
118 ff.ReadAll( &contents, wxMBConvUTF16LE() );
120 ff.ReadAll( &contents, wxMBConvUTF8() );
122 if( contents.empty() )
125 ff.ReadAll( &contents, wxConvAuto( wxFONTENCODING_CP1252 ) );
128 if( contents.empty() )
129 THROW_IO_ERROR( wxString::Format(
_(
"Unable to read file '%s'." ), aFilePath ) );
133 contents.Replace( wxS(
"\r\r\n" ), wxS(
"\n" ) );
142 m_length( 0 ), m_lineNum( 0 ), m_line( nullptr ),
143 m_capacity( 0 ), m_maxLineLength( aMaxLineLength )
145 if( aMaxLineLength != 0 )
198 unsigned aMaxLineLength ):
205 wxString msg = wxString::Format(
_(
"Unable to open %s for reading." ),
206 aFileName.GetData() );
217 unsigned aStartingLineNumber,
218 unsigned aMaxLineLength ) :
219 LINE_READER( aMaxLineLength ), m_iOwn( doOwn ), m_fp( aFile )
235 fseek(
m_fp, 0, SEEK_END );
236 long int fileLength = ftell(
m_fp );
245 return ftell(
m_fp );
285 m_lines( aString ), m_ndx( 0 )
295 m_lines( aStartingPoint.m_lines ),
296 m_ndx( aStartingPoint.m_ndx )
311 if( nlOffset == std::string::npos )
314 new_length = nlOffset -
m_ndx + 1;
339 const wxString& aSource ) :
394 if( strlen( wrapee ) == 0 )
399 for( ; *wrapee; ++wrapee, isFirst = false )
401 static const char quoteThese[] =
"\t ()"
408 if( strchr( quoteThese, *wrapee ) )
411 if( !isFirst &&
'-' == *wrapee )
455 va_start( args, fmt );
456 int ret =
vprint( fmt, args );
469 va_start( args, fmt );
474 for(
int i = 0; i < nestLevel; ++i )
483 result =
vprint( fmt, args );
496 va_start( args, fmt );
501 result =
vprint( fmt, args );
513 ret.reserve( aWrapee.size() * 2 + 2 );
517 for( std::string::const_iterator it = aWrapee.begin(); it != aWrapee.end(); ++it )
555 return Quotes( (
const char*) aWrapee.utf8_str() );
573 for( std::string::iterator i =
copy.begin(); i !=
copy.end(); ++i )
575 if( !isspace( *i ) && *i !=
')' && *i !=
'(' && *i !=
'"' )
586 m_filename( aFileName )
588 m_fp = wxFopen( aFileName, aMode );
604 if( fwrite( aOutBuf, (
unsigned) aCount, 1,
m_fp ) != 1 )
614 m_fp = wxFopen( aFileName, aMode );
651 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.
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
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"
Is a LINE_READER that reads from a multiline 8 bit wide std::string.
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.
void ignore_unused(const T &)
#define THROW_IO_ERROR(msg)
static int vprint(std::string *result, const char *format, va_list ap)
int StrPrintf(std::string *result, const char *format,...)
This is like sprintf() but the output is appended to a std::string instead of to a character array.
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