36#include <wx/translation.h>
40#if !defined( HAVE_FGETC_NOLOCK )
45#define getc_unlocked _fgetc_nolock
47#define getc_unlocked getc
52static int vprint( std::string* result,
const char* format, va_list ap )
56 size_t len = vsnprintf(
nullptr, 0, format, tmp );
60 size_t size = result->size();
61 result->resize( size + len );
64 len = vsnprintf( result->data() + size, len + 1, format, ap );
70int StrPrintf( std::string* result,
const char* format, ... )
74 va_start( args, format );
75 int ret =
vprint( result, format, args );
87 va_start( args, format );
95wxString
SafeReadFile(
const wxString& aFilePath,
const wxString& aReadType )
98 [](
const char* cstring )
100 wxString line = wxString::FromUTF8( cstring );
103 line = wxConvCurrent->cMB2WC( cstring );
110 line = wxSafeConvertMB2WX( cstring );
114 line.Replace( wxS(
"\r\r\n" ), wxS(
"\n" ) );
120 FILE* fp = wxFopen( aFilePath, aReadType );
123 THROW_IO_ERROR( wxString::Format(
_(
"Cannot open file '%s'." ), aFilePath ) );
130 contents += From_UTF8_WINE( fileReader.
Line() );
139 m_length( 0 ), m_lineNum( 0 ), m_line( nullptr ),
140 m_capacity( 0 ), m_maxLineLength( aMaxLineLength )
142 if( aMaxLineLength != 0 )
195 unsigned aMaxLineLength ):
202 wxString msg = wxString::Format(
_(
"Unable to open %s for reading." ),
203 aFileName.GetData() );
214 unsigned aStartingLineNumber,
215 unsigned aMaxLineLength ) :
216 LINE_READER( aMaxLineLength ), m_iOwn( doOwn ), m_fp( aFile )
232 fseek(
m_fp, 0, SEEK_END );
233 long int fileLength = ftell(
m_fp );
242 return ftell(
m_fp );
282 m_lines( aString ), m_ndx( 0 )
292 m_lines( aStartingPoint.m_lines ),
293 m_ndx( aStartingPoint.m_ndx )
308 if( nlOffset == std::string::npos )
311 new_length = nlOffset -
m_ndx + 1;
336 const wxString& aSource ) :
391 if( strlen( wrapee ) == 0 )
396 for( ; *wrapee; ++wrapee, isFirst = false )
398 static const char quoteThese[] =
"\t ()"
405 if( strchr( quoteThese, *wrapee ) )
408 if( !isFirst &&
'-' == *wrapee )
452 va_start( args, fmt );
453 int ret =
vprint( fmt, args );
466 va_start( args, fmt );
471 for(
int i = 0; i < nestLevel; ++i )
480 result =
vprint( fmt, args );
493 va_start( args, fmt );
498 result =
vprint( fmt, args );
510 ret.reserve( aWrapee.size() * 2 + 2 );
514 for( std::string::const_iterator it = aWrapee.begin(); it != aWrapee.end(); ++it )
552 return Quotes( (
const char*) aWrapee.utf8_str() );
570 for( std::string::iterator i =
copy.begin(); i !=
copy.end(); ++i )
572 if( !isspace( *i ) && *i !=
')' && *i !=
'(' && *i !=
'"' )
583 m_filename( aFileName )
585 m_fp = wxFopen( aFileName, aMode );
601 if( fwrite( aOutBuf, (
unsigned) aCount, 1,
m_fp ) != 1 )
611 m_fp = wxFopen( aFileName, aMode );
648 m_buf.append( aOutBuf, aCount );
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
A LINE_READER that reads from an open file.
~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"
char * Line() const
Return a pointer to the last line that was read in.
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