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>
|
2011-09-30 18:15:37 +00:00
|
|
|
* Copyright (C) 2007 KiCad Developers, see change_log.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>
|
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <richio.h>
|
2009-12-11 04:55:24 +00:00
|
|
|
|
|
|
|
// This file defines 3 classes useful for working with DSN text files and is named
|
|
|
|
// "richio" after its author, Richard Hollenbeck, aka Dick Hollenbeck.
|
|
|
|
|
|
|
|
|
|
|
|
//-----<LINE_READER>------------------------------------------------------
|
|
|
|
|
2010-03-03 06:13:46 +00:00
|
|
|
LINE_READER::LINE_READER( unsigned aMaxLineLength )
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
|
|
|
lineNum = 0;
|
2010-10-20 22:47:34 +00:00
|
|
|
|
|
|
|
if( aMaxLineLength == 0 ) // caller is goofed up.
|
|
|
|
aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX;
|
|
|
|
|
2009-12-11 04:55:24 +00:00
|
|
|
maxLineLength = aMaxLineLength;
|
|
|
|
|
2010-10-20 22:47:34 +00:00
|
|
|
// start at the INITIAL size, expand as needed up to the MAX size in maxLineLength
|
|
|
|
capacity = LINE_READER_LINE_INITIAL_SIZE;
|
|
|
|
|
|
|
|
// but never go above user's aMaxLineLength, and leave space for trailing nul
|
2010-10-21 13:21:32 +00:00
|
|
|
if( capacity > aMaxLineLength+1 )
|
|
|
|
capacity = aMaxLineLength+1;
|
2009-12-11 04:55:24 +00:00
|
|
|
|
|
|
|
line = new char[capacity];
|
|
|
|
|
|
|
|
line[0] = '\0';
|
|
|
|
length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-20 22:47:34 +00:00
|
|
|
void LINE_READER::expandCapacity( unsigned newsize )
|
|
|
|
{
|
|
|
|
// length can equal maxLineLength and nothing breaks, there's room for
|
|
|
|
// the terminating nul. cannot go over this.
|
|
|
|
if( newsize > maxLineLength+1 )
|
|
|
|
newsize = maxLineLength+1;
|
|
|
|
|
|
|
|
if( newsize > capacity )
|
|
|
|
{
|
|
|
|
capacity = newsize;
|
|
|
|
|
|
|
|
// resize the buffer, and copy the original data
|
|
|
|
char* bigger = new char[capacity];
|
|
|
|
|
2010-10-21 13:21:32 +00:00
|
|
|
wxASSERT( capacity >= length+1 );
|
2010-10-20 23:11:00 +00:00
|
|
|
|
2010-10-20 22:47:34 +00:00
|
|
|
memcpy( bigger, line, length );
|
2010-10-21 13:21:32 +00:00
|
|
|
bigger[length] = 0;
|
2010-10-20 22:47:34 +00:00
|
|
|
|
2010-10-20 23:11:00 +00:00
|
|
|
delete[] line;
|
2010-10-20 22:47:34 +00:00
|
|
|
line = bigger;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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 ) :
|
2010-10-05 07:15:29 +00:00
|
|
|
LINE_READER( aMaxLineLength ),
|
2010-12-28 02:44:30 +00:00
|
|
|
iOwn( doOwn ),
|
2010-10-05 07:15:29 +00:00
|
|
|
fp( aFile )
|
2010-03-03 06:13:46 +00:00
|
|
|
{
|
2010-12-28 02:44:30 +00:00
|
|
|
source = aFileName;
|
|
|
|
lineNum = aStartingLineNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FILE_LINE_READER::~FILE_LINE_READER()
|
|
|
|
{
|
|
|
|
if( iOwn && fp )
|
|
|
|
fclose( fp );
|
2010-03-03 06:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-09 15:45:11 +00:00
|
|
|
unsigned FILE_LINE_READER::ReadLine() throw( IO_ERROR )
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
2010-10-20 22:47:34 +00:00
|
|
|
length = 0;
|
|
|
|
line[0] = 0;
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2011-01-12 04:51:29 +00:00
|
|
|
// fgets always puts a terminating nul at end of its read.
|
2010-10-20 22:47:34 +00:00
|
|
|
while( fgets( line + length, capacity - length, fp ) )
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
2010-10-20 22:47:34 +00:00
|
|
|
length += strlen( line + length );
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2011-01-12 04:51:29 +00:00
|
|
|
if( length >= maxLineLength )
|
2010-12-30 22:15:53 +00:00
|
|
|
THROW_IO_ERROR( _("Line length exceeded") );
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2010-10-21 13:21:32 +00:00
|
|
|
// a normal line breaks here, once through while loop
|
|
|
|
if( length+1 < capacity || line[length-1] == '\n' )
|
2010-10-20 22:47:34 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
expandCapacity( capacity * 2 );
|
2009-12-11 04:55:24 +00:00
|
|
|
}
|
|
|
|
|
2011-01-12 04:51:29 +00:00
|
|
|
// lineNum is incremented even if there was no line read, because this
|
|
|
|
// leads to better error reporting when we hit an end of file.
|
|
|
|
++lineNum;
|
2010-10-20 22:47:34 +00:00
|
|
|
|
2009-12-11 04:55:24 +00:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-28 02:44:30 +00:00
|
|
|
STRING_LINE_READER::STRING_LINE_READER( const std::string& aString, const wxString& aSource ) :
|
|
|
|
LINE_READER( LINE_READER_LINE_DEFAULT_MAX ),
|
|
|
|
lines( aString ),
|
|
|
|
ndx( 0 )
|
|
|
|
{
|
|
|
|
// Clipboard text should be nice and _use multiple lines_ so that
|
|
|
|
// we can report _line number_ oriented error messages when parsing.
|
|
|
|
source = aSource;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
STRING_LINE_READER::STRING_LINE_READER( const STRING_LINE_READER& aStartingPoint ) :
|
|
|
|
LINE_READER( LINE_READER_LINE_DEFAULT_MAX ),
|
|
|
|
lines( aStartingPoint.lines ),
|
|
|
|
ndx( aStartingPoint.ndx )
|
|
|
|
{
|
|
|
|
// since we are keeping the same "source" name, for error reporting purposes
|
|
|
|
// we need to have the same notion of line number and offset.
|
|
|
|
|
|
|
|
source = aStartingPoint.source;
|
|
|
|
lineNum = aStartingPoint.lineNum;
|
|
|
|
}
|
|
|
|
|
2010-11-09 15:45:11 +00:00
|
|
|
unsigned STRING_LINE_READER::ReadLine() throw( IO_ERROR )
|
2010-03-03 06:13:46 +00:00
|
|
|
{
|
2010-10-21 15:49:36 +00:00
|
|
|
size_t nlOffset = lines.find( '\n', ndx );
|
2010-03-03 06:13:46 +00:00
|
|
|
|
|
|
|
if( nlOffset == std::string::npos )
|
2010-10-20 22:47:34 +00:00
|
|
|
length = lines.length() - ndx;
|
2010-03-03 06:13:46 +00:00
|
|
|
else
|
2010-10-20 22:47:34 +00:00
|
|
|
length = nlOffset - ndx + 1; // include the newline, so +1
|
2010-03-03 06:13:46 +00:00
|
|
|
|
2010-10-20 22:47:34 +00:00
|
|
|
if( length )
|
2010-03-03 06:13:46 +00:00
|
|
|
{
|
2010-10-20 22:47:34 +00:00
|
|
|
if( length >= maxLineLength )
|
2010-12-30 22:15:53 +00:00
|
|
|
THROW_IO_ERROR( _("Line length exceeded") );
|
2010-03-03 06:13:46 +00:00
|
|
|
|
2010-10-21 15:49:36 +00:00
|
|
|
if( length+1 > capacity ) // +1 for terminating nul
|
2010-10-21 13:21:32 +00:00
|
|
|
expandCapacity( length+1 );
|
2010-03-03 06:13:46 +00:00
|
|
|
|
2010-10-20 22:47:34 +00:00
|
|
|
wxASSERT( ndx + length <= lines.length() );
|
|
|
|
|
2010-10-21 15:49:36 +00:00
|
|
|
memcpy( line, &lines[ndx], length );
|
2010-03-03 06:13:46 +00:00
|
|
|
|
2010-10-20 22:47:34 +00:00
|
|
|
ndx += length;
|
2010-03-03 06:13:46 +00:00
|
|
|
}
|
|
|
|
|
2011-01-05 21:13:47 +00:00
|
|
|
++lineNum; // this gets incremented even if no bytes were read
|
|
|
|
|
2010-10-20 22:47:34 +00:00
|
|
|
line[length] = 0;
|
2010-03-03 06:26:48 +00:00
|
|
|
|
2010-10-20 22:47:34 +00:00
|
|
|
return length;
|
2010-03-03 06:13:46 +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;
|
|
|
|
|
|
|
|
if( strlen(wrapee)==0 )
|
|
|
|
return quote_char;
|
|
|
|
|
|
|
|
bool isFirst = true;
|
|
|
|
|
|
|
|
for( ; *wrapee; ++wrapee, isFirst=false )
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-09 15:45:11 +00:00
|
|
|
int OUTPUTFORMATTER::vprint( const char* fmt, va_list ap ) throw( IO_ERROR )
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
|
|
|
int ret = vsnprintf( &buffer[0], buffer.size(), fmt, ap );
|
|
|
|
if( ret >= (int) buffer.size() )
|
|
|
|
{
|
2011-01-09 13:35:13 +00:00
|
|
|
buffer.resize( ret+2000 );
|
2009-12-11 04:55:24 +00:00
|
|
|
ret = vsnprintf( &buffer[0], buffer.size(), fmt, ap );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ret > 0 )
|
2010-08-08 00:31:07 +00:00
|
|
|
write( &buffer[0], ret );
|
2009-12-11 04:55:24 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-09 15:45:11 +00:00
|
|
|
int OUTPUTFORMATTER::sprint( const char* fmt, ... ) throw( IO_ERROR )
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-09 15:45:11 +00:00
|
|
|
int OUTPUTFORMATTER::Print( int nestLevel, const char* fmt, ... ) throw( IO_ERROR )
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-02 19:41:35 +00:00
|
|
|
std::string OUTPUTFORMATTER::Quotes( const std::string& aWrapee ) throw( IO_ERROR )
|
2010-08-11 19:52:44 +00:00
|
|
|
{
|
2011-01-30 19:26:03 +00:00
|
|
|
static const char quoteThese[] = "\t ()\n\r";
|
2010-08-11 19:52:44 +00:00
|
|
|
|
2011-01-30 19:26:03 +00:00
|
|
|
if( !aWrapee.size() || // quote null string as ""
|
|
|
|
aWrapee[0]=='#' || // quote a potential s-expression comment, so it is not a comment
|
|
|
|
aWrapee[0]=='"' || // NextTok() will travel through DSN_STRING path anyway, then must apply escapes
|
|
|
|
aWrapee.find_first_of( quoteThese ) != std::string::npos )
|
|
|
|
{
|
|
|
|
std::string ret;
|
2010-12-28 16:55:28 +00:00
|
|
|
|
2011-01-30 19:26:03 +00:00
|
|
|
ret.reserve( aWrapee.size()*2 + 2 );
|
2010-08-11 19:52:44 +00:00
|
|
|
|
2011-01-30 19:26:03 +00:00
|
|
|
ret += '"';
|
|
|
|
|
|
|
|
for( std::string::const_iterator it = aWrapee.begin(); it!=aWrapee.end(); ++it )
|
2010-08-11 19:52:44 +00:00
|
|
|
{
|
2011-01-30 19:26:03 +00:00
|
|
|
switch( *it )
|
|
|
|
{
|
|
|
|
case '\n':
|
|
|
|
ret += '\\';
|
|
|
|
ret += 'n';
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
ret += '\\';
|
2011-01-30 20:06:05 +00:00
|
|
|
ret += 'r';
|
2011-01-30 19:26:03 +00:00
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
ret += '\\';
|
|
|
|
ret += '\\';
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
ret += '\\';
|
|
|
|
ret += '"';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret += *it;
|
|
|
|
}
|
2010-08-11 19:52:44 +00:00
|
|
|
}
|
|
|
|
|
2011-01-30 19:26:03 +00:00
|
|
|
ret += '"';
|
|
|
|
|
|
|
|
return ret;
|
2010-08-11 19:52:44 +00:00
|
|
|
}
|
|
|
|
|
2011-01-30 19:26:03 +00:00
|
|
|
return aWrapee;
|
2010-08-11 19:52:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-02 19:41:35 +00:00
|
|
|
std::string OUTPUTFORMATTER::Quotew( const wxString& aWrapee ) throw( IO_ERROR )
|
|
|
|
{
|
|
|
|
// s-expressions atoms are always encoded as UTF-8.
|
|
|
|
// 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
|
|
|
|
2010-11-09 15:45:11 +00:00
|
|
|
void STRING_FORMATTER::write( const char* aOutBuf, int aCount ) throw( IO_ERROR )
|
2010-08-08 00:31:07 +00:00
|
|
|
{
|
|
|
|
mystring.append( aOutBuf, aCount );
|
|
|
|
}
|
|
|
|
|
2010-08-09 02:03:16 +00:00
|
|
|
void STRING_FORMATTER::StripUseless()
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
|
|
|
std::string copy = mystring;
|
|
|
|
|
|
|
|
mystring.clear();
|
|
|
|
|
|
|
|
for( std::string::iterator i=copy.begin(); i!=copy.end(); ++i )
|
|
|
|
{
|
|
|
|
if( !isspace( *i ) && *i!=')' && *i!='(' && *i!='"' )
|
|
|
|
{
|
|
|
|
mystring += *i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-08 00:31:07 +00:00
|
|
|
|
|
|
|
//-----<STREAM_OUTPUTFORMATTER>--------------------------------------
|
|
|
|
|
|
|
|
const char* STREAM_OUTPUTFORMATTER::GetQuoteChar( const char* wrapee )
|
|
|
|
{
|
|
|
|
return OUTPUTFORMATTER::GetQuoteChar( wrapee, quoteChar );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-09 15:45:11 +00:00
|
|
|
void STREAM_OUTPUTFORMATTER::write( const char* aOutBuf, int aCount ) throw( IO_ERROR )
|
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 )
|
|
|
|
{
|
|
|
|
lastWrite = os.Write( aOutBuf, aCount ).LastWrite();
|
|
|
|
|
|
|
|
if( !os.IsOk() )
|
|
|
|
{
|
2010-12-30 22:15:53 +00:00
|
|
|
THROW_IO_ERROR( _( "OUTPUTSTREAM_OUTPUTFORMATTER write error" ) );
|
2010-08-08 00:31:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|