2009-12-11 04:55:24 +00:00
|
|
|
/*
|
2011-09-30 18:15:37 +00:00
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
2009-12-11 04:55:24 +00:00
|
|
|
*
|
2011-02-02 15:31:48 +00:00
|
|
|
* Copyright (C) 2007-2011 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
|
2017-11-12 17:29:56 +00:00
|
|
|
* Copyright (C) 2017 KiCad Developers, see AUTHORS.txt for contributors.
|
2009-12-11 04:55:24 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <cstdarg>
|
2016-01-12 02:41:33 +00:00
|
|
|
#include <config.h> // HAVE_FGETC_NOLOCK
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <richio.h>
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2012-10-09 14:31:36 +00:00
|
|
|
|
|
|
|
// Fall back to getc() when getc_unlocked() is not available on the target platform.
|
2012-11-10 06:39:18 +00:00
|
|
|
#if !defined( HAVE_FGETC_NOLOCK )
|
2012-10-09 14:31:36 +00:00
|
|
|
#define getc_unlocked getc
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2013-09-26 15:02:46 +00:00
|
|
|
static int vprint( std::string* result, const char* format, va_list ap )
|
2013-09-26 05:29:54 +00:00
|
|
|
{
|
|
|
|
char msg[512];
|
2015-06-24 13:53:03 +00:00
|
|
|
// This function can call vsnprintf twice.
|
|
|
|
// But internally, vsnprintf retrieves arguments from the va_list identified by arg as if
|
|
|
|
// va_arg was used on it, and thus the state of the va_list is likely to be altered by the call.
|
|
|
|
// see: www.cplusplus.com/reference/cstdio/vsnprintf
|
|
|
|
// we make a copy of va_list ap for the second call, if happens
|
|
|
|
va_list tmp;
|
|
|
|
va_copy( tmp, ap );
|
|
|
|
|
2013-09-26 05:29:54 +00:00
|
|
|
size_t len = vsnprintf( msg, sizeof(msg), format, ap );
|
|
|
|
|
|
|
|
if( len < sizeof(msg) ) // the output fit into msg
|
|
|
|
{
|
|
|
|
result->append( msg, msg + len );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// output was too big, so now incur the expense of allocating
|
|
|
|
// a buf for holding suffient characters.
|
|
|
|
|
|
|
|
std::vector<char> buf;
|
|
|
|
buf.reserve( len+1 ); // reserve(), not resize() which writes. +1 for trailing nul.
|
|
|
|
|
2015-06-24 13:53:03 +00:00
|
|
|
len = vsnprintf( &buf[0], len+1, format, tmp );
|
2013-09-26 05:29:54 +00:00
|
|
|
|
|
|
|
result->append( &buf[0], &buf[0] + len );
|
|
|
|
}
|
|
|
|
|
2015-06-24 13:53:03 +00:00
|
|
|
va_end( tmp ); // Release the temporary va_list, initialised from ap
|
|
|
|
|
2013-09-26 05:29:54 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-26 15:02:46 +00:00
|
|
|
int StrPrintf( std::string* result, const char* format, ... )
|
2013-09-26 05:29:54 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start( args, format );
|
|
|
|
int ret = vprint( result, format, args );
|
|
|
|
va_end( args );
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-26 15:02:46 +00:00
|
|
|
std::string StrPrintf( const char* format, ... )
|
2013-09-26 05:29:54 +00:00
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start( args, format );
|
|
|
|
int ignore = vprint( &ret, format, args );
|
|
|
|
(void) ignore;
|
|
|
|
va_end( args );
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-11 04:55:24 +00:00
|
|
|
//-----<LINE_READER>------------------------------------------------------
|
|
|
|
|
2015-03-11 16:04:20 +00:00
|
|
|
LINE_READER::LINE_READER( unsigned aMaxLineLength ) :
|
2017-11-12 17:29:56 +00:00
|
|
|
m_length( 0 ), m_lineNum( 0 ), m_line( NULL ),
|
|
|
|
m_capacity( 0 ), m_maxLineLength( aMaxLineLength )
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
2015-03-11 16:04:20 +00:00
|
|
|
if( aMaxLineLength != 0 )
|
2012-12-08 23:58:03 +00:00
|
|
|
{
|
|
|
|
// start at the INITIAL size, expand as needed up to the MAX size in maxLineLength
|
2017-11-12 17:29:56 +00:00
|
|
|
m_capacity = LINE_READER_LINE_INITIAL_SIZE;
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2012-12-08 23:58:03 +00:00
|
|
|
// but never go above user's aMaxLineLength, and leave space for trailing nul
|
2017-11-12 17:29:56 +00:00
|
|
|
if( m_capacity > aMaxLineLength+1 )
|
|
|
|
m_capacity = aMaxLineLength+1;
|
2010-10-20 22:47:34 +00:00
|
|
|
|
2017-11-08 11:52:14 +00:00
|
|
|
// Be sure there is room for a null EOL char, so reserve at least capacity+1 bytes
|
|
|
|
// to ensure capacity line lenght and avoid corner cases
|
|
|
|
// Use capacity+5 to cover and corner case
|
2017-11-12 17:29:56 +00:00
|
|
|
m_line = new char[m_capacity+5];
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2017-11-12 17:29:56 +00:00
|
|
|
m_line[0] = '\0';
|
2012-12-08 23:58:03 +00:00
|
|
|
}
|
2009-12-11 04:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-27 13:50:01 +00:00
|
|
|
LINE_READER::~LINE_READER()
|
|
|
|
{
|
2017-11-12 17:29:56 +00:00
|
|
|
delete[] m_line;
|
2012-11-27 13:50:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-12 17:29:56 +00:00
|
|
|
void LINE_READER::expandCapacity( unsigned aNewsize )
|
2010-10-20 22:47:34 +00:00
|
|
|
{
|
2017-11-12 17:29:56 +00:00
|
|
|
// m_length can equal maxLineLength and nothing breaks, there's room for
|
2010-10-20 22:47:34 +00:00
|
|
|
// the terminating nul. cannot go over this.
|
2017-11-12 17:29:56 +00:00
|
|
|
if( aNewsize > m_maxLineLength+1 )
|
|
|
|
aNewsize = m_maxLineLength+1;
|
2010-10-20 22:47:34 +00:00
|
|
|
|
2017-11-12 17:29:56 +00:00
|
|
|
if( aNewsize > m_capacity )
|
2010-10-20 22:47:34 +00:00
|
|
|
{
|
2017-11-12 17:29:56 +00:00
|
|
|
m_capacity = aNewsize;
|
2010-10-20 22:47:34 +00:00
|
|
|
|
|
|
|
// resize the buffer, and copy the original data
|
2017-11-08 11:52:14 +00:00
|
|
|
// Be sure there is room for the null EOL char, so reserve capacity+1 bytes
|
|
|
|
// to ensure capacity line lenght. Use capacity+5 to cover and corner case
|
2017-11-12 17:29:56 +00:00
|
|
|
char* bigger = new char[m_capacity+5];
|
2010-10-20 22:47:34 +00:00
|
|
|
|
2017-11-12 17:29:56 +00:00
|
|
|
wxASSERT( m_capacity >= m_length+1 );
|
2010-10-20 23:11:00 +00:00
|
|
|
|
2017-11-12 17:29:56 +00:00
|
|
|
memcpy( bigger, m_line, m_length );
|
|
|
|
bigger[m_length] = 0;
|
2010-10-20 22:47:34 +00:00
|
|
|
|
2017-11-12 17:29:56 +00:00
|
|
|
delete[] m_line;
|
|
|
|
m_line = bigger;
|
2010-10-20 22:47:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-27 12:33:12 +00:00
|
|
|
FILE_LINE_READER::FILE_LINE_READER( const wxString& aFileName,
|
2017-11-08 11:52:14 +00:00
|
|
|
unsigned aStartingLineNumber, unsigned aMaxLineLength ):
|
2017-11-12 17:29:56 +00:00
|
|
|
LINE_READER( aMaxLineLength ), m_iOwn( true )
|
2012-11-27 12:33:12 +00:00
|
|
|
{
|
2017-11-12 17:29:56 +00:00
|
|
|
m_fp = wxFopen( aFileName, wxT( "rt" ) );
|
2017-11-08 11:52:14 +00:00
|
|
|
|
2017-11-12 17:29:56 +00:00
|
|
|
if( !m_fp )
|
2012-11-27 12:33:12 +00:00
|
|
|
{
|
|
|
|
wxString msg = wxString::Format(
|
2017-12-15 11:37:46 +00:00
|
|
|
_( "Unable to open filename \"%s\" for reading" ), aFileName.GetData() );
|
2012-11-27 12:33:12 +00:00
|
|
|
THROW_IO_ERROR( msg );
|
|
|
|
}
|
|
|
|
|
2017-11-12 17:29:56 +00:00
|
|
|
m_source = aFileName;
|
|
|
|
m_lineNum = aStartingLineNumber;
|
2012-11-27 12:33:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-28 02:44:30 +00:00
|
|
|
FILE_LINE_READER::FILE_LINE_READER( FILE* aFile, const wxString& aFileName,
|
|
|
|
bool doOwn,
|
|
|
|
unsigned aStartingLineNumber,
|
|
|
|
unsigned aMaxLineLength ) :
|
2017-11-12 17:29:56 +00:00
|
|
|
LINE_READER( aMaxLineLength ), m_iOwn( doOwn ), m_fp( aFile )
|
2010-03-03 06:13:46 +00:00
|
|
|
{
|
2017-11-12 17:29:56 +00:00
|
|
|
m_source = aFileName;
|
|
|
|
m_lineNum = aStartingLineNumber;
|
2010-12-28 02:44:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FILE_LINE_READER::~FILE_LINE_READER()
|
|
|
|
{
|
2017-11-12 17:29:56 +00:00
|
|
|
if( m_iOwn && m_fp )
|
|
|
|
fclose( m_fp );
|
2010-03-03 06:13:46 +00:00
|
|
|
}
|
|
|
|
|
2012-10-08 19:34:46 +00:00
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
char* FILE_LINE_READER::ReadLine()
|
2012-10-08 19:34:46 +00:00
|
|
|
{
|
2017-11-12 17:29:56 +00:00
|
|
|
m_length = 0;
|
2012-10-08 19:34:46 +00:00
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
2017-11-12 17:29:56 +00:00
|
|
|
if( m_length >= m_maxLineLength )
|
2012-10-08 19:34:46 +00:00
|
|
|
THROW_IO_ERROR( _( "Maximum line length exceeded" ) );
|
|
|
|
|
2017-11-12 17:29:56 +00:00
|
|
|
if( m_length >= m_capacity )
|
|
|
|
expandCapacity( m_capacity * 2 );
|
2012-10-08 19:34:46 +00:00
|
|
|
|
|
|
|
// faster, POSIX compatible fgetc(), no locking.
|
2017-11-12 17:29:56 +00:00
|
|
|
int cc = getc_unlocked( m_fp );
|
2017-11-08 11:52:14 +00:00
|
|
|
|
2012-10-08 19:34:46 +00:00
|
|
|
if( cc == EOF )
|
|
|
|
break;
|
|
|
|
|
2017-11-12 17:29:56 +00:00
|
|
|
m_line[ m_length++ ] = (char) cc;
|
2012-10-08 19:34:46 +00:00
|
|
|
|
|
|
|
if( cc == '\n' )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-11-12 17:29:56 +00:00
|
|
|
m_line[ m_length ] = 0;
|
2012-10-08 19:34:46 +00:00
|
|
|
|
2017-11-12 17:29:56 +00:00
|
|
|
// m_lineNum is incremented even if there was no line read, because this
|
2012-10-08 19:34:46 +00:00
|
|
|
// leads to better error reporting when we hit an end of file.
|
2017-11-12 17:29:56 +00:00
|
|
|
++m_lineNum;
|
2012-10-08 19:34:46 +00:00
|
|
|
|
2017-11-12 17:29:56 +00:00
|
|
|
return m_length ? m_line : NULL;
|
2012-10-08 19:34:46 +00:00
|
|
|
}
|
|
|
|
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
STRING_LINE_READER::STRING_LINE_READER( const std::string& aString, const wxString& aSource ):
|
2010-12-28 02:44:30 +00:00
|
|
|
LINE_READER( LINE_READER_LINE_DEFAULT_MAX ),
|
2017-11-12 17:29:56 +00:00
|
|
|
m_lines( aString ), m_ndx( 0 )
|
2010-12-28 02:44:30 +00:00
|
|
|
{
|
|
|
|
// Clipboard text should be nice and _use multiple lines_ so that
|
|
|
|
// we can report _line number_ oriented error messages when parsing.
|
2017-11-12 17:29:56 +00:00
|
|
|
m_source = aSource;
|
2010-12-28 02:44:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
STRING_LINE_READER::STRING_LINE_READER( const STRING_LINE_READER& aStartingPoint ):
|
2010-12-28 02:44:30 +00:00
|
|
|
LINE_READER( LINE_READER_LINE_DEFAULT_MAX ),
|
2017-11-12 17:29:56 +00:00
|
|
|
m_lines( aStartingPoint.m_lines ),
|
|
|
|
m_ndx( aStartingPoint.m_ndx )
|
2010-12-28 02:44:30 +00:00
|
|
|
{
|
|
|
|
// since we are keeping the same "source" name, for error reporting purposes
|
|
|
|
// we need to have the same notion of line number and offset.
|
|
|
|
|
2017-11-12 17:29:56 +00:00
|
|
|
m_source = aStartingPoint.m_source;
|
|
|
|
m_lineNum = aStartingPoint.m_lineNum;
|
2010-12-28 02:44:30 +00:00
|
|
|
}
|
|
|
|
|
2012-11-27 13:50:01 +00:00
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
char* STRING_LINE_READER::ReadLine()
|
2010-03-03 06:13:46 +00:00
|
|
|
{
|
2017-11-12 17:29:56 +00:00
|
|
|
size_t nlOffset = m_lines.find( '\n', m_ndx );
|
2010-03-03 06:13:46 +00:00
|
|
|
|
|
|
|
if( nlOffset == std::string::npos )
|
2017-11-12 17:29:56 +00:00
|
|
|
m_length = m_lines.length() - m_ndx;
|
2010-03-03 06:13:46 +00:00
|
|
|
else
|
2017-11-12 17:29:56 +00:00
|
|
|
m_length = nlOffset - m_ndx + 1; // include the newline, so +1
|
2010-03-03 06:13:46 +00:00
|
|
|
|
2017-11-12 17:29:56 +00:00
|
|
|
if( m_length )
|
2010-03-03 06:13:46 +00:00
|
|
|
{
|
2017-11-12 17:29:56 +00:00
|
|
|
if( m_length >= m_maxLineLength )
|
2010-12-30 22:15:53 +00:00
|
|
|
THROW_IO_ERROR( _("Line length exceeded") );
|
2010-03-03 06:13:46 +00:00
|
|
|
|
2017-11-12 17:29:56 +00:00
|
|
|
if( m_length+1 > m_capacity ) // +1 for terminating nul
|
|
|
|
expandCapacity( m_length+1 );
|
2010-03-03 06:13:46 +00:00
|
|
|
|
2017-11-12 17:29:56 +00:00
|
|
|
wxASSERT( m_ndx + m_length <= m_lines.length() );
|
2010-10-20 22:47:34 +00:00
|
|
|
|
2017-11-12 17:29:56 +00:00
|
|
|
memcpy( m_line, &m_lines[m_ndx], m_length );
|
|
|
|
m_ndx += m_length;
|
2010-03-03 06:13:46 +00:00
|
|
|
}
|
|
|
|
|
2017-11-12 17:29:56 +00:00
|
|
|
++m_lineNum; // this gets incremented even if no bytes were read
|
|
|
|
m_line[m_length] = 0;
|
2010-03-03 06:26:48 +00:00
|
|
|
|
2017-11-12 17:29:56 +00:00
|
|
|
return m_length ? m_line : NULL;
|
2010-03-03 06:13:46 +00:00
|
|
|
}
|
|
|
|
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2014-07-05 20:42:59 +00:00
|
|
|
INPUTSTREAM_LINE_READER::INPUTSTREAM_LINE_READER( wxInputStream* aStream, const wxString& aSource ) :
|
2012-10-07 15:37:25 +00:00
|
|
|
LINE_READER( LINE_READER_LINE_DEFAULT_MAX ),
|
|
|
|
m_stream( aStream )
|
|
|
|
{
|
2017-11-12 17:29:56 +00:00
|
|
|
m_source = aSource;
|
2012-10-07 15:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
char* INPUTSTREAM_LINE_READER::ReadLine()
|
2012-10-07 15:37:25 +00:00
|
|
|
{
|
2017-11-12 17:29:56 +00:00
|
|
|
m_length = 0;
|
2012-10-07 15:37:25 +00:00
|
|
|
|
2012-10-07 21:26:41 +00:00
|
|
|
for(;;)
|
2012-10-07 15:37:25 +00:00
|
|
|
{
|
2017-11-12 17:29:56 +00:00
|
|
|
if( m_length >= m_maxLineLength )
|
2012-10-07 15:37:25 +00:00
|
|
|
THROW_IO_ERROR( _( "Maximum line length exceeded" ) );
|
|
|
|
|
2017-11-12 17:29:56 +00:00
|
|
|
if( m_length + 1 > m_capacity )
|
|
|
|
expandCapacity( m_capacity * 2 );
|
2012-10-07 15:37:25 +00:00
|
|
|
|
2012-10-07 21:26:41 +00:00
|
|
|
// this read may fail, docs say to test LastRead() before trusting cc.
|
|
|
|
char cc = m_stream->GetC();
|
|
|
|
|
|
|
|
if( !m_stream->LastRead() )
|
|
|
|
break;
|
|
|
|
|
2017-11-12 17:29:56 +00:00
|
|
|
m_line[ m_length++ ] = cc;
|
2012-10-07 15:37:25 +00:00
|
|
|
|
2012-10-07 21:26:41 +00:00
|
|
|
if( cc == '\n' )
|
2012-10-07 15:37:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-11-12 17:29:56 +00:00
|
|
|
m_line[ m_length ] = 0;
|
2012-10-07 15:37:25 +00:00
|
|
|
|
2017-11-12 17:29:56 +00:00
|
|
|
// m_lineNum is incremented even if there was no line read, because this
|
2012-10-07 15:37:25 +00:00
|
|
|
// leads to better error reporting when we hit an end of file.
|
2017-11-12 17:29:56 +00:00
|
|
|
++m_lineNum;
|
2012-10-07 15:37:25 +00:00
|
|
|
|
2017-11-12 17:29:56 +00:00
|
|
|
return m_length ? m_line : NULL;
|
2012-10-07 15:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-11 04:55:24 +00:00
|
|
|
//-----<OUTPUTFORMATTER>----------------------------------------------------
|
|
|
|
|
|
|
|
// factor out a common GetQuoteChar
|
|
|
|
|
|
|
|
const char* OUTPUTFORMATTER::GetQuoteChar( const char* wrapee, const char* quote_char )
|
|
|
|
{
|
|
|
|
// Include '#' so a symbol is not confused with a comment. We intend
|
|
|
|
// to wrap any symbol starting with a '#'.
|
|
|
|
// Our LEXER class handles comments, and comments appear to be an extension
|
|
|
|
// to the SPECCTRA DSN specification.
|
|
|
|
if( *wrapee == '#' )
|
|
|
|
return quote_char;
|
|
|
|
|
2012-10-25 15:41:20 +00:00
|
|
|
if( strlen( wrapee ) == 0 )
|
2009-12-11 04:55:24 +00:00
|
|
|
return quote_char;
|
|
|
|
|
|
|
|
bool isFirst = true;
|
|
|
|
|
2012-10-25 15:41:20 +00:00
|
|
|
for( ; *wrapee; ++wrapee, isFirst = false )
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
|
|
|
static const char quoteThese[] = "\t ()"
|
|
|
|
"%" // per Alfons of freerouting.net, he does not like this unquoted as of 1-Feb-2008
|
|
|
|
"{}" // guessing that these are problems too
|
|
|
|
;
|
|
|
|
|
|
|
|
// if the string to be wrapped (wrapee) has a delimiter in it,
|
|
|
|
// return the quote_char so caller wraps the wrapee.
|
|
|
|
if( strchr( quoteThese, *wrapee ) )
|
|
|
|
return quote_char;
|
|
|
|
|
|
|
|
if( !isFirst && '-' == *wrapee )
|
|
|
|
return quote_char;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""; // caller does not need to wrap, can use an unwrapped string.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-25 15:41:20 +00:00
|
|
|
const char* OUTPUTFORMATTER::GetQuoteChar( const char* wrapee )
|
|
|
|
{
|
|
|
|
return GetQuoteChar( wrapee, quoteChar );
|
|
|
|
}
|
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
int OUTPUTFORMATTER::vprint( const char* fmt, va_list ap )
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
2015-06-24 13:53:03 +00:00
|
|
|
// This function can call vsnprintf twice.
|
|
|
|
// But internally, vsnprintf retrieves arguments from the va_list identified by arg as if
|
|
|
|
// va_arg was used on it, and thus the state of the va_list is likely to be altered by the call.
|
|
|
|
// see: www.cplusplus.com/reference/cstdio/vsnprintf
|
|
|
|
// we make a copy of va_list ap for the second call, if happens
|
|
|
|
va_list tmp;
|
|
|
|
va_copy( tmp, ap );
|
2017-11-12 17:29:56 +00:00
|
|
|
int ret = vsnprintf( &m_buffer[0], m_buffer.size(), fmt, ap );
|
2015-06-24 13:53:03 +00:00
|
|
|
|
2017-11-12 17:29:56 +00:00
|
|
|
if( ret >= (int) m_buffer.size() )
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
2017-11-12 17:29:56 +00:00
|
|
|
m_buffer.resize( ret + 1000 );
|
|
|
|
ret = vsnprintf( &m_buffer[0], m_buffer.size(), fmt, tmp );
|
2009-12-11 04:55:24 +00:00
|
|
|
}
|
|
|
|
|
2015-06-24 13:53:03 +00:00
|
|
|
va_end( tmp ); // Release the temporary va_list, initialised from ap
|
|
|
|
|
2009-12-11 04:55:24 +00:00
|
|
|
if( ret > 0 )
|
2017-11-12 17:29:56 +00:00
|
|
|
write( &m_buffer[0], ret );
|
2009-12-11 04:55:24 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
int OUTPUTFORMATTER::sprint( const char* fmt, ... )
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start( args, fmt );
|
|
|
|
int ret = vprint( fmt, args);
|
|
|
|
va_end( args );
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
int OUTPUTFORMATTER::Print( int nestLevel, const char* fmt, ... )
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
|
|
|
#define NESTWIDTH 2 ///< how many spaces per nestLevel
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start( args, fmt );
|
|
|
|
|
|
|
|
int result = 0;
|
|
|
|
int total = 0;
|
|
|
|
|
|
|
|
for( int i=0; i<nestLevel; ++i )
|
|
|
|
{
|
2010-08-08 00:31:07 +00:00
|
|
|
// no error checking needed, an exception indicates an error.
|
2009-12-11 04:55:24 +00:00
|
|
|
result = sprint( "%*c", NESTWIDTH, ' ' );
|
|
|
|
|
|
|
|
total += result;
|
|
|
|
}
|
|
|
|
|
2010-08-08 00:31:07 +00:00
|
|
|
// no error checking needed, an exception indicates an error.
|
|
|
|
result = vprint( fmt, args );
|
2009-12-11 04:55:24 +00:00
|
|
|
|
|
|
|
va_end( args );
|
|
|
|
|
|
|
|
total += result;
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
std::string OUTPUTFORMATTER::Quotes( const std::string& aWrapee )
|
2010-08-11 19:52:44 +00:00
|
|
|
{
|
2019-03-07 21:41:30 +00:00
|
|
|
std::string ret;
|
2010-12-28 16:55:28 +00:00
|
|
|
|
2019-03-07 21:41:30 +00:00
|
|
|
ret.reserve( aWrapee.size()*2 + 2 );
|
2010-08-11 19:52:44 +00:00
|
|
|
|
2019-03-07 21:41:30 +00:00
|
|
|
ret += '"';
|
2011-01-30 19:26:03 +00:00
|
|
|
|
2019-03-07 21:41:30 +00:00
|
|
|
for( std::string::const_iterator it = aWrapee.begin(); it!=aWrapee.end(); ++it )
|
|
|
|
{
|
|
|
|
switch( *it )
|
2010-08-11 19:52:44 +00:00
|
|
|
{
|
2019-03-07 21:41:30 +00:00
|
|
|
case '\n':
|
|
|
|
ret += '\\';
|
|
|
|
ret += 'n';
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
ret += '\\';
|
|
|
|
ret += 'r';
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
ret += '\\';
|
|
|
|
ret += '\\';
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
ret += '\\';
|
|
|
|
ret += '"';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret += *it;
|
2010-08-11 19:52:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 21:41:30 +00:00
|
|
|
ret += '"';
|
|
|
|
|
|
|
|
return ret;
|
2010-08-11 19:52:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
std::string OUTPUTFORMATTER::Quotew( const wxString& aWrapee )
|
2011-02-02 19:41:35 +00:00
|
|
|
{
|
2012-10-25 15:41:20 +00:00
|
|
|
// wxStrings are always encoded as UTF-8 as we convert to a byte sequence.
|
2011-02-02 19:41:35 +00:00
|
|
|
// The non-virutal function calls the virtual workhorse function, and if
|
|
|
|
// a different quoting or escaping strategy is desired from the standard,
|
|
|
|
// a derived class can overload Quotes() above, but
|
|
|
|
// should never be a reason to overload this Quotew() here.
|
|
|
|
return Quotes( (const char*) aWrapee.utf8_str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-09 02:03:16 +00:00
|
|
|
//-----<STRING_FORMATTER>----------------------------------------------------
|
2010-08-08 00:31:07 +00:00
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
void STRING_FORMATTER::write( const char* aOutBuf, int aCount )
|
2010-08-08 00:31:07 +00:00
|
|
|
{
|
2017-11-12 17:29:56 +00:00
|
|
|
m_mystring.append( aOutBuf, aCount );
|
2010-08-08 00:31:07 +00:00
|
|
|
}
|
|
|
|
|
2010-08-09 02:03:16 +00:00
|
|
|
void STRING_FORMATTER::StripUseless()
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
2017-11-12 17:29:56 +00:00
|
|
|
std::string copy = m_mystring;
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2017-11-12 17:29:56 +00:00
|
|
|
m_mystring.clear();
|
2009-12-11 04:55:24 +00:00
|
|
|
|
|
|
|
for( std::string::iterator i=copy.begin(); i!=copy.end(); ++i )
|
|
|
|
{
|
|
|
|
if( !isspace( *i ) && *i!=')' && *i!='(' && *i!='"' )
|
|
|
|
{
|
2017-11-12 17:29:56 +00:00
|
|
|
m_mystring += *i;
|
2009-12-11 04:55:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-25 07:10:59 +00:00
|
|
|
//-----<FILE_OUTPUTFORMATTER>----------------------------------------
|
|
|
|
|
2018-08-13 22:26:38 +00:00
|
|
|
FILE_OUTPUTFORMATTER::FILE_OUTPUTFORMATTER( const wxString& aFileName, const wxChar* aMode,
|
|
|
|
char aQuoteChar ):
|
2012-10-25 15:41:20 +00:00
|
|
|
OUTPUTFORMATTER( OUTPUTFMTBUFZ, aQuoteChar ),
|
2012-10-25 07:10:59 +00:00
|
|
|
m_filename( aFileName )
|
|
|
|
{
|
|
|
|
m_fp = wxFopen( aFileName, aMode );
|
|
|
|
|
|
|
|
if( !m_fp )
|
2018-08-14 13:57:42 +00:00
|
|
|
THROW_IO_ERROR( strerror( errno ) );
|
2012-10-25 07:10:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FILE_OUTPUTFORMATTER::~FILE_OUTPUTFORMATTER()
|
|
|
|
{
|
|
|
|
if( m_fp )
|
|
|
|
fclose( m_fp );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
void FILE_OUTPUTFORMATTER::write( const char* aOutBuf, int aCount )
|
2012-10-25 07:10:59 +00:00
|
|
|
{
|
2018-08-13 22:26:38 +00:00
|
|
|
if( fwrite( aOutBuf, (unsigned) aCount, 1, m_fp ) != 1 )
|
2018-08-14 13:57:42 +00:00
|
|
|
THROW_IO_ERROR( strerror( errno ) );
|
2012-10-25 07:10:59 +00:00
|
|
|
}
|
|
|
|
|
2010-08-08 00:31:07 +00:00
|
|
|
|
|
|
|
//-----<STREAM_OUTPUTFORMATTER>--------------------------------------
|
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
void STREAM_OUTPUTFORMATTER::write( const char* aOutBuf, int aCount )
|
2010-08-08 00:31:07 +00:00
|
|
|
{
|
|
|
|
int lastWrite;
|
|
|
|
|
|
|
|
// This might delay awhile if you were writing to say a socket, but for
|
|
|
|
// a file it should only go through the loop once.
|
|
|
|
for( int total = 0; total<aCount; total += lastWrite )
|
|
|
|
{
|
2017-11-12 17:29:56 +00:00
|
|
|
lastWrite = m_os.Write( aOutBuf, aCount ).LastWrite();
|
2010-08-08 00:31:07 +00:00
|
|
|
|
2017-11-12 17:29:56 +00:00
|
|
|
if( !m_os.IsOk() )
|
2010-08-08 00:31:07 +00:00
|
|
|
{
|
2010-12-30 22:15:53 +00:00
|
|
|
THROW_IO_ERROR( _( "OUTPUTSTREAM_OUTPUTFORMATTER write error" ) );
|
2010-08-08 00:31:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|