implement OUTPUTFORMATTER::Quoted()

This commit is contained in:
Dick Hollenbeck 2010-08-11 14:52:44 -05:00
parent 0980306f2a
commit 0ceb19879b
5 changed files with 51 additions and 12 deletions

View File

@ -204,6 +204,46 @@ int OUTPUTFORMATTER::Print( int nestLevel, const char* fmt, ... ) throw( IOError
}
const char* OUTPUTFORMATTER::Quoted( std::string* aWrapee ) throw( IOError )
{
// derived class's notion of what a quote character is
char quote = *GetQuoteChar( "(" );
// Will the string be wrapped based on its interior content?
const char* squote = GetQuoteChar( aWrapee->c_str() );
// Search the interior of the string for 'quote' chars
// and replace them as found with duplicated quotes.
// Note that necessarily any string which has internal quotes will
// also be wrapped in quotes later in this function.
for( unsigned i=0; i<aWrapee->size(); ++i )
{
if( (*aWrapee)[i] == quote )
{
aWrapee->insert( aWrapee->begin()+i, quote );
++i;
}
else if( (*aWrapee)[0]=='\r' || (*aWrapee)[0]=='\n' )
{
// In a desire to maintain accurate line number reporting within DSNLEXER
// a decision was made to make all S-expression strings be on a single
// line. You can embedd \n (human readable) in the text but not
// '\n' which is 0x0a.
throw IOError( _( "S-expression string has newline" ) );
}
}
if( *squote )
{
// wrap the beginning and end of the string in a quote.
aWrapee->insert( aWrapee->begin(), quote );
aWrapee->insert( aWrapee->end(), quote );
}
return aWrapee->c_str();
}
//-----<STRING_FORMATTER>----------------------------------------------------
void STRING_FORMATTER::write( const char* aOutBuf, int aCount ) throw( IOError )

View File

@ -51,18 +51,16 @@ void XNODE::Format( OUTPUTFORMATTER* out, int nestLevel ) throw( IOError )
void XNODE::FormatContents( OUTPUTFORMATTER* out, int nestLevel ) throw( IOError )
{
std::string utf8;
const char* quote;
// output attributes first if they exist
for( XATTR* attr = (XATTR*) GetAttributes(); attr; attr = (XATTR*) attr->GetNext() )
{
utf8 = CONV_TO_UTF8( attr->GetValue() ); // capture the content
quote = out->GetQuoteChar( utf8.c_str() );
out->Print( 0, " (%s %s%s%s)",
out->Print( 0, " (%s %s)",
// attr names should never need quoting, no spaces, we designed the file.
CONV_TO_UTF8( attr->GetName() ),
quote, utf8.c_str(), quote );
out->Quoted( &utf8 ) );
}
// we only expect to have used one of two types here:
@ -88,8 +86,7 @@ void XNODE::FormatContents( OUTPUTFORMATTER* out, int nestLevel ) throw( IOError
case wxXML_TEXT_NODE:
utf8 = CONV_TO_UTF8( GetContent() );
quote = out->GetQuoteChar( utf8.c_str() );
out->Print( 0, " %s%s%s", quote, utf8.c_str(), quote );
out->Print( 0, " %s", out->Quoted( &utf8 ) );
break;
default:

View File

@ -1010,7 +1010,7 @@ XNODE* EXPORT_HELP::makeGenericComponents()
bool EXPORT_HELP::WriteGENERICNetList( WinEDA_SchematicFrame* frame, const wxString& aOutFileName )
{
#if 0
#if 1
// this code seems to work now, for S-expression support.

View File

@ -11,7 +11,7 @@
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
<xsl:template match="export">
<xsl:template match="/export">
<xsl:text>*PADS-PCB*&nl;*PART*&nl;</xsl:text>
<xsl:apply-templates select="components/comp"/>
<xsl:text>&nl;*NET*&nl;</xsl:text>

View File

@ -301,7 +301,7 @@ public:
/**
* Function Quoted
* checks \a aWrappee input string for a need to be quoted
* checks \a aWrapee input string for a need to be quoted
* (e.g. contains a ')' character or a space), and for \" double quotes
* within the string that need to be doubled up such that the DSNLEXER
* will correctly parse the string from a file later.
@ -312,9 +312,11 @@ public:
*
* @return const char* - useful for passing to printf() style functions that
* must output utf8 streams.
virtual const char* Quoted( std::string* aWrapee );
thinking about using wxCharBuffer* instead.
* @throw IOError, if aWrapee has any \r or \n bytes in it which is
* illegal according to the DSNLEXER who does not ever want them
* within a string.
*/
virtual const char* Quoted( std::string* aWrapee ) throw( IOError );
//-----</interface functions>-----------------------------------------
};