KiCad PCB EDA Suite
STRING_FORMATTER Class Reference

Implement an OUTPUTFORMATTER to a memory buffer. More...

#include <richio.h>

Inheritance diagram for STRING_FORMATTER:
OUTPUTFORMATTER

Public Member Functions

 STRING_FORMATTER (int aReserve=OUTPUTFMTBUFZ, char aQuoteChar='"' )
 Reserve space in the buffer. More...
 
void Clear ()
 Clear the buffer and empties the internal string. More...
 
void StripUseless ()
 Removes whitespace, '(', and ')' from the string. More...
 
const std::string & GetString ()
 
virtual const char * GetQuoteChar (const char *wrapee) const
 Perform quote character need determination. More...
 
int PRINTF_FUNC Print (int nestLevel, const char *fmt,...)
 Format and write text to the output stream. More...
 
virtual std::string Quotes (const std::string &aWrapee) const
 Check aWrapee input string for a need to be quoted (e.g. More...
 
std::string Quotew (const wxString &aWrapee) const
 

Protected Member Functions

void write (const char *aOutBuf, int aCount) override
 Should be coded in the interface implementation (derived) classes. More...
 

Static Protected Member Functions

static const char * GetQuoteChar (const char *wrapee, const char *quote_char)
 Perform quote character need determination according to the Specctra DSN specification. More...
 

Private Member Functions

int sprint (const char *fmt,...)
 
int vprint (const char *fmt, va_list ap)
 

Private Attributes

std::string m_mystring
 
std::vector< char > m_buffer
 
char quoteChar [2]
 

Detailed Description

Implement an OUTPUTFORMATTER to a memory buffer.

After Print()ing the string is available through GetString()

Definition at line 414 of file richio.h.

Constructor & Destructor Documentation

◆ STRING_FORMATTER()

STRING_FORMATTER::STRING_FORMATTER ( int  aReserve = OUTPUTFMTBUFZ,
char  aQuoteChar = '"' 
)
inline

Reserve space in the buffer.

Definition at line 420 of file richio.h.

420 :
421 OUTPUTFORMATTER( aReserve, aQuoteChar )
422 {
423 }
OUTPUTFORMATTER(int aReserve=OUTPUTFMTBUFZ, char aQuoteChar='"' )
Definition: richio.h:312

Member Function Documentation

◆ Clear()

void STRING_FORMATTER::Clear ( )
inline

Clear the buffer and empties the internal string.

Definition at line 428 of file richio.h.

429 {
430 m_mystring.clear();
431 }
std::string m_mystring
Definition: richio.h:447

References m_mystring.

Referenced by PCB_PLUGIN::GetStringOutput(), and DSN::ELEM::makeHash().

◆ GetQuoteChar() [1/2]

const char * OUTPUTFORMATTER::GetQuoteChar ( const char *  wrapee) const
virtualinherited

Perform quote character need determination.

It returns the quote character as a single character string for a given input wrapee string. If the wrappee does not need to be quoted, the return value is "" (the null string), such as when there are no delimiters in the input wrapee string. If you want the quote_char to be assuredly not "", then pass in "(" as the wrappee.

Implementations are free to override the default behavior, which is to call the static function of the same name.

Parameters
wrapeeA string that might need wrapping on each end.
Returns
the quote_char as a single character string, or "" if the wrapee does not need to be wrapped.

Definition at line 389 of file richio.cpp.

390{
391 return GetQuoteChar( wrapee, quoteChar );
392}
char quoteChar[2]
Definition: richio.h:401
static const char * GetQuoteChar(const char *wrapee, const char *quote_char)
Perform quote character need determination according to the Specctra DSN specification.
Definition: richio.cpp:355

References OUTPUTFORMATTER::GetQuoteChar(), and OUTPUTFORMATTER::quoteChar.

◆ GetQuoteChar() [2/2]

const char * OUTPUTFORMATTER::GetQuoteChar ( const char *  wrapee,
const char *  quote_char 
)
staticprotectedinherited

Perform quote character need determination according to the Specctra DSN specification.

Parameters
wrapeeA string that might need wrapping on each end.
quote_charA single character C string which provides the current quote character, should it be needed by the wrapee.
Returns
the quote_char as a single character string, or "" if the wrapee does not need to be wrapped.

Definition at line 355 of file richio.cpp.

356{
357 // Include '#' so a symbol is not confused with a comment. We intend
358 // to wrap any symbol starting with a '#'.
359 // Our LEXER class handles comments, and comments appear to be an extension
360 // to the SPECCTRA DSN specification.
361 if( *wrapee == '#' )
362 return quote_char;
363
364 if( strlen( wrapee ) == 0 )
365 return quote_char;
366
367 bool isFirst = true;
368
369 for( ; *wrapee; ++wrapee, isFirst = false )
370 {
371 static const char quoteThese[] = "\t ()"
372 "%" // per Alfons of freerouting.net, he does not like this unquoted as of 1-Feb-2008
373 "{}" // guessing that these are problems too
374 ;
375
376 // if the string to be wrapped (wrapee) has a delimiter in it,
377 // return the quote_char so caller wraps the wrapee.
378 if( strchr( quoteThese, *wrapee ) )
379 return quote_char;
380
381 if( !isFirst && '-' == *wrapee )
382 return quote_char;
383 }
384
385 return ""; // caller does not need to wrap, can use an unwrapped string.
386}

Referenced by DSN::PROPERTY::Format(), DSN::RECTANGLE::Format(), DSN::LAYER_RULE::Format(), DSN::PATH::Format(), DSN::CIRCLE::Format(), DSN::QARC::Format(), DSN::KEEPOUT::Format(), DSN::VIA::Format(), DSN::LAYER::Format(), DSN::SPECCTRA_LAYER_PAIR::Format(), DSN::STRINGPROP::Format(), DSN::PLACE::Format(), DSN::COMPONENT::Format(), DSN::PIN::Format(), DSN::IMAGE::Format(), DSN::PADSTACK::Format(), DSN::FROMTO::Format(), DSN::COMP_ORDER::Format(), DSN::NET::Format(), DSN::CLASS::Format(), DSN::WIRE::Format(), DSN::WIRE_VIA::Format(), DSN::PCB::Format(), DSN::ANCESTOR::Format(), DSN::SUPPLY_PIN::Format(), DSN::NET_OUT::Format(), DSN::SESSION::Format(), DSN::PARSER::FormatContents(), DSN::CLASSES::FormatContents(), DSN::REGION::FormatContents(), DSN::PADSTACK::FormatContents(), DSN::HISTORY::FormatContents(), DSN::PIN_REF::FormatIt(), and OUTPUTFORMATTER::GetQuoteChar().

◆ GetString()

◆ Print()

int OUTPUTFORMATTER::Print ( int  nestLevel,
const char *  fmt,
  ... 
)
inherited

Format and write text to the output stream.

Parameters
nestLevelThe multiple of spaces to precede the output with.
fmtA printf() style format string.
...a variable list of parameters that will get blended into the output under control of the format string.
Returns
int - the number of characters output.
Exceptions
IO_ERROR,ifthere is a problem outputting, such as a full disk.

Definition at line 433 of file richio.cpp.

434{
435#define NESTWIDTH 2
436
437 va_list args;
438
439 va_start( args, fmt );
440
441 int result = 0;
442 int total = 0;
443
444 for( int i = 0; i < nestLevel; ++i )
445 {
446 // no error checking needed, an exception indicates an error.
447 result = sprint( "%*c", NESTWIDTH, ' ' );
448
449 total += result;
450 }
451
452 // no error checking needed, an exception indicates an error.
453 result = vprint( fmt, args );
454
455 va_end( args );
456
457 total += result;
458 return total;
459}
int sprint(const char *fmt,...)
Definition: richio.cpp:421
int vprint(const char *fmt, va_list ap)
Definition: richio.cpp:395
#define NESTWIDTH

References NESTWIDTH, OUTPUTFORMATTER::sprint(), and OUTPUTFORMATTER::vprint().

Referenced by PANEL_SYM_LIB_TABLE::convertLibrary(), PCB_PLUGIN::format(), NETLIST::Format(), DS_DATA_MODEL_IO::format(), DS_DATA_MODEL_IO::Format(), PCB_CALCULATOR_DATAFILE::Format(), PCB_PLOT_PARAMS::Format(), EDA_TEXT::Format(), PAGE_INFO::Format(), TITLE_BLOCK::Format(), COMPONENT_NET::Format(), COMPONENT::Format(), SYMBOL_LIB_TABLE::Format(), FP_LIB_TABLE::Format(), STROKE_PARAMS::Format(), XNODE::Format(), DSN::ELEM::Format(), LIB_TABLE_ROW::Format(), TEMPLATE_FIELDNAME::Format(), DSN::POINT::Format(), DSN::PROPERTY::Format(), DSN::UNIT_RES::Format(), DSN::RECTANGLE::Format(), DSN::RULE::Format(), DSN::LAYER_RULE::Format(), DSN::PATH::Format(), DSN::BOUNDARY::Format(), DSN::CIRCLE::Format(), DSN::QARC::Format(), DSN::WINDOW::Format(), DSN::KEEPOUT::Format(), DSN::VIA::Format(), DSN::CONTROL::Format(), DSN::LAYER::Format(), DSN::SPECCTRA_LAYER_PAIR::Format(), DSN::LAYER_NOISE_WEIGHT::Format(), DSN::TOKPROP::Format(), DSN::STRINGPROP::Format(), DSN::GRID::Format(), DSN::PLACE::Format(), DSN::COMPONENT::Format(), DSN::SHAPE::Format(), DSN::PIN::Format(), DSN::IMAGE::Format(), DSN::PADSTACK::Format(), DSN::FROMTO::Format(), DSN::COMP_ORDER::Format(), DSN::NET::Format(), DSN::CLASS::Format(), DSN::WIRE::Format(), DSN::WIRE_VIA::Format(), DSN::PCB::Format(), DSN::ANCESTOR::Format(), DSN::SUPPLY_PIN::Format(), DSN::NET_OUT::Format(), DSN::SESSION::Format(), TEMPLATES::Format(), SCH_SEXPR_PLUGIN::Format(), SCH_LEGACY_PLUGIN::Format(), formatArc(), formatBezier(), PCB_PLUGIN::formatBoardLayers(), BOARD_STACKUP::FormatBoardStackup(), formatCircle(), XNODE::FormatContents(), DSN::PARSER::FormatContents(), DSN::CLASSES::FormatContents(), DSN::REGION::FormatContents(), DSN::PLACEMENT::FormatContents(), DSN::IMAGE::FormatContents(), DSN::PADSTACK::FormatContents(), DSN::HISTORY::FormatContents(), DSN::ROUTE::FormatContents(), DSN::WAS_IS::FormatContents(), DS_DATA_MODEL_IO::formatCoordinate(), formatFill(), PCB_PLUGIN::formatGeneral(), DSN::PIN_REF::FormatIt(), PCB_PLUGIN::formatLayer(), PCB_PLUGIN::formatLayers(), PCB_PLUGIN::formatNetInformation(), formatNode(), DS_DATA_MODEL_IO::formatOptions(), formatPoly(), PCB_PLUGIN::formatPolyPts(), PCB_PLUGIN::formatProperties(), formatRect(), PCB_PLUGIN::formatRenderCache(), DS_DATA_MODEL_IO::formatRepeatParameters(), PCB_PLUGIN::formatSetup(), GENDRILL_WRITER_BASE::GenDrillReportFile(), GENDRILL_WRITER_BASE::printToolSummary(), CLIPBOARD_IO::Save(), PCB_PLUGIN::Save(), SCH_LEGACY_PLUGIN_CACHE::saveArc(), SCH_LEGACY_PLUGIN_CACHE::saveBezier(), SCH_LEGACY_PLUGIN::saveBitmap(), SCH_SEXPR_PLUGIN::saveBitmap(), SCH_LEGACY_PLUGIN::saveBusAlias(), SCH_SEXPR_PLUGIN::saveBusAlias(), SCH_LEGACY_PLUGIN::saveBusEntry(), SCH_SEXPR_PLUGIN::saveBusEntry(), SCH_LEGACY_PLUGIN_CACHE::saveCircle(), SCH_LEGACY_PLUGIN_CACHE::saveDocFile(), SCH_LEGACY_PLUGIN_CACHE::saveField(), SCH_SEXPR_PLUGIN_CACHE::saveField(), SCH_LEGACY_PLUGIN::saveField(), SCH_SEXPR_PLUGIN::saveField(), SCH_SEXPR_PLUGIN::saveInstances(), SCH_LEGACY_PLUGIN::saveJunction(), SCH_SEXPR_PLUGIN::saveJunction(), SCH_LEGACY_PLUGIN::saveLine(), SCH_SEXPR_PLUGIN::saveLine(), SCH_LEGACY_PLUGIN::saveNoConnect(), SCH_SEXPR_PLUGIN::saveNoConnect(), SCH_LEGACY_PLUGIN_CACHE::savePin(), SCH_SEXPR_PLUGIN_CACHE::savePin(), SCH_LEGACY_PLUGIN_CACHE::savePolyLine(), SCH_LEGACY_PLUGIN_CACHE::saveRectangle(), CLIPBOARD_IO::SaveSelection(), SCH_LEGACY_PLUGIN::saveSheet(), SCH_SEXPR_PLUGIN::saveSheet(), SCH_SEXPR_PLUGIN_CACHE::SaveSymbol(), SCH_LEGACY_PLUGIN_CACHE::SaveSymbol(), SCH_LEGACY_PLUGIN::saveSymbol(), SCH_SEXPR_PLUGIN::saveSymbol(), SCH_LEGACY_PLUGIN_CACHE::saveText(), SCH_SEXPR_PLUGIN_CACHE::saveText(), SCH_LEGACY_PLUGIN::saveText(), SCH_SEXPR_PLUGIN::saveText(), SCH_SEXPR_PLUGIN_CACHE::saveTextBox(), SCH_SEXPR_PLUGIN::saveTextBox(), PANEL_REGULATOR::WriteDataFile(), NGSPICE_CIRCUIT_MODEL::WriteDirectives(), NETLIST_EXPORTER_SPICE::WriteDirectives(), NETLIST_EXPORTER_SPICE::WriteHead(), NETLIST_EXPORTER_SPICE_MODEL::WriteHead(), PCB_CALCULATOR_DATAFILE::WriteHeader(), NETLIST_EXPORTER_SPICE::writeInclude(), NETLIST_EXPORTER_SPICE::writeItems(), NETLIST_EXPORTER_SPICE::writeModels(), NETLIST_EXPORTER_SPICE::WriteTail(), and NETLIST_EXPORTER_SPICE_MODEL::WriteTail().

◆ Quotes()

std::string OUTPUTFORMATTER::Quotes ( const std::string &  aWrapee) const
virtualinherited

Check 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 escaped such that the DSNLEXER will correctly parse the string from a file later.

Parameters
aWrapeeis a string that might need wrapping in double quotes, and it might need to have its internal content escaped, or not.
Returns
a std::string- whose c_str() function can be called for passing to printf() style functions that output UTF8 encoded s-expression streams.
Exceptions
IO_ERROR,ifthere is any kind of problem with the input string.

Definition at line 462 of file richio.cpp.

463{
464 std::string ret;
465
466 ret.reserve( aWrapee.size() * 2 + 2 );
467
468 ret += '"';
469
470 for( std::string::const_iterator it = aWrapee.begin(); it != aWrapee.end(); ++it )
471 {
472 switch( *it )
473 {
474 case '\n':
475 ret += '\\';
476 ret += 'n';
477 break;
478 case '\r':
479 ret += '\\';
480 ret += 'r';
481 break;
482 case '\\':
483 ret += '\\';
484 ret += '\\';
485 break;
486 case '"':
487 ret += '\\';
488 ret += '"';
489 break;
490 default:
491 ret += *it;
492 }
493 }
494
495 ret += '"';
496
497 return ret;
498}

Referenced by PCB_PLUGIN::format(), formatNode(), OUTPUTFORMATTER::Quotew(), and SCH_SEXPR_PLUGIN_CACHE::SaveSymbol().

◆ Quotew()

std::string OUTPUTFORMATTER::Quotew ( const wxString &  aWrapee) const
inherited

Definition at line 501 of file richio.cpp.

502{
503 // wxStrings are always encoded as UTF-8 as we convert to a byte sequence.
504 // The non-virtual function calls the virtual workhorse function, and if
505 // a different quoting or escaping strategy is desired from the standard,
506 // a derived class can overload Quotes() above, but
507 // should never be a reason to overload this Quotew() here.
508 return Quotes( (const char*) aWrapee.utf8_str() );
509}
virtual std::string Quotes(const std::string &aWrapee) const
Check aWrapee input string for a need to be quoted (e.g.
Definition: richio.cpp:462

References OUTPUTFORMATTER::Quotes().

Referenced by PCB_PLUGIN::format(), DS_DATA_MODEL_IO::format(), PCB_CALCULATOR_DATAFILE::Format(), EDA_TEXT::Format(), PAGE_INFO::Format(), TITLE_BLOCK::Format(), COMPONENT_NET::Format(), COMPONENT::Format(), LIB_TABLE_ROW::Format(), TEMPLATE_FIELDNAME::Format(), PCB_PLUGIN::formatBoardLayers(), BOARD_STACKUP::FormatBoardStackup(), XNODE::FormatContents(), PCB_PLUGIN::formatLayer(), PCB_PLUGIN::formatLayers(), PCB_PLUGIN::formatNetInformation(), PCB_PLUGIN::formatProperties(), PCB_PLUGIN::formatRenderCache(), SCH_SEXPR_PLUGIN::saveBusAlias(), SCH_SEXPR_PLUGIN_CACHE::saveField(), SCH_SEXPR_PLUGIN::saveField(), SCH_SEXPR_PLUGIN::saveInstances(), SCH_SEXPR_PLUGIN_CACHE::savePin(), SCH_SEXPR_PLUGIN::saveSheet(), SCH_SEXPR_PLUGIN_CACHE::SaveSymbol(), SCH_SEXPR_PLUGIN::saveSymbol(), SCH_SEXPR_PLUGIN_CACHE::saveText(), SCH_SEXPR_PLUGIN::saveText(), SCH_SEXPR_PLUGIN_CACHE::saveTextBox(), SCH_SEXPR_PLUGIN::saveTextBox(), and PCB_CALCULATOR_DATAFILE::WriteHeader().

◆ sprint()

int OUTPUTFORMATTER::sprint ( const char *  fmt,
  ... 
)
privateinherited

Definition at line 421 of file richio.cpp.

422{
423 va_list args;
424
425 va_start( args, fmt );
426 int ret = vprint( fmt, args);
427 va_end( args );
428
429 return ret;
430}

References OUTPUTFORMATTER::vprint().

Referenced by OUTPUTFORMATTER::Print().

◆ StripUseless()

void STRING_FORMATTER::StripUseless ( )

Removes whitespace, '(', and ')' from the string.

Definition at line 519 of file richio.cpp.

520{
521 std::string copy = m_mystring;
522
523 m_mystring.clear();
524
525 for( std::string::iterator i = copy.begin(); i != copy.end(); ++i )
526 {
527 if( !isspace( *i ) && *i != ')' && *i != '(' && *i != '"' )
528 {
529 m_mystring += *i;
530 }
531 }
532}

References copy, and m_mystring.

Referenced by DSN::ELEM::makeHash().

◆ vprint()

int OUTPUTFORMATTER::vprint ( const char *  fmt,
va_list  ap 
)
privateinherited

Definition at line 395 of file richio.cpp.

396{
397 // This function can call vsnprintf twice.
398 // But internally, vsnprintf retrieves arguments from the va_list identified by arg as if
399 // va_arg was used on it, and thus the state of the va_list is likely to be altered by the call.
400 // see: www.cplusplus.com/reference/cstdio/vsnprintf
401 // we make a copy of va_list ap for the second call, if happens
402 va_list tmp;
403 va_copy( tmp, ap );
404 int ret = vsnprintf( &m_buffer[0], m_buffer.size(), fmt, ap );
405
406 if( ret >= (int) m_buffer.size() )
407 {
408 m_buffer.resize( ret + 1000 );
409 ret = vsnprintf( &m_buffer[0], m_buffer.size(), fmt, tmp );
410 }
411
412 va_end( tmp ); // Release the temporary va_list, initialised from ap
413
414 if( ret > 0 )
415 write( &m_buffer[0], ret );
416
417 return ret;
418}
virtual void write(const char *aOutBuf, int aCount)=0
Should be coded in the interface implementation (derived) classes.
std::vector< char > m_buffer
Definition: richio.h:400

References OUTPUTFORMATTER::m_buffer, and OUTPUTFORMATTER::write().

Referenced by OUTPUTFORMATTER::Print(), and OUTPUTFORMATTER::sprint().

◆ write()

void STRING_FORMATTER::write ( const char *  aOutBuf,
int  aCount 
)
overrideprotectedvirtual

Should be coded in the interface implementation (derived) classes.

Parameters
aOutBufis the start of a byte buffer to write.
aCounttells how many bytes to write.
Exceptions
IO_ERROR,ifthere is a problem outputting, such as a full disk.

Implements OUTPUTFORMATTER.

Definition at line 514 of file richio.cpp.

515{
516 m_mystring.append( aOutBuf, aCount );
517}

References m_mystring.

Member Data Documentation

◆ m_buffer

std::vector<char> OUTPUTFORMATTER::m_buffer
privateinherited

Definition at line 400 of file richio.h.

Referenced by OUTPUTFORMATTER::vprint().

◆ m_mystring

std::string STRING_FORMATTER::m_mystring
private

Definition at line 447 of file richio.h.

Referenced by Clear(), GetString(), StripUseless(), and write().

◆ quoteChar

char OUTPUTFORMATTER::quoteChar[2]
privateinherited

Definition at line 401 of file richio.h.

Referenced by OUTPUTFORMATTER::GetQuoteChar(), and OUTPUTFORMATTER::OUTPUTFORMATTER().


The documentation for this class was generated from the following files: