KiCad PCB EDA Suite
OUTPUTFORMATTER Class Referenceabstract

An interface used to output 8 bit text in a convenient way. More...

#include <richio.h>

Inheritance diagram for OUTPUTFORMATTER:
FILE_OUTPUTFORMATTER STREAM_OUTPUTFORMATTER STRING_FORMATTER

Public Member Functions

int PRINTF_FUNC Print (int nestLevel, const char *fmt,...)
 Format and write text to the output stream. More...
 
virtual const char * GetQuoteChar (const char *wrapee) const
 Perform quote character need determination. 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

 OUTPUTFORMATTER (int aReserve=OUTPUTFMTBUFZ, char aQuoteChar='"' )
 
virtual ~OUTPUTFORMATTER ()
 
virtual void write (const char *aOutBuf, int aCount)=0
 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::vector< char > m_buffer
 
char quoteChar [2]
 

Detailed Description

An interface used to output 8 bit text in a convenient way.

The primary interface is "printf() - like" but with support for indentation control. The destination of the 8 bit wide text is up to the implementer.

The implementer only has to implement the write() function, but can also optionally re-implement GetQuoteChar().

If you want to output a wxString, then use TO_UTF8() on it before passing it as an argument to Print().

Since this is an abstract interface, only classes derived from this one may actually be used.

Definition at line 309 of file richio.h.

Constructor & Destructor Documentation

◆ OUTPUTFORMATTER()

OUTPUTFORMATTER::OUTPUTFORMATTER ( int  aReserve = OUTPUTFMTBUFZ,
char  aQuoteChar = '"' 
)
inlineprotected

Definition at line 312 of file richio.h.

312  :
313  m_buffer( aReserve, '\0' )
314  {
315  quoteChar[0] = aQuoteChar;
316  quoteChar[1] = '\0';
317  }
std::vector< char > m_buffer
Definition: richio.h:400
char quoteChar[2]
Definition: richio.h:401

References quoteChar.

◆ ~OUTPUTFORMATTER()

virtual OUTPUTFORMATTER::~OUTPUTFORMATTER ( )
inlineprotectedvirtual

Definition at line 319 of file richio.h.

319 {}

Member Function Documentation

◆ GetQuoteChar() [1/2]

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

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 348 of file richio.cpp.

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

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 GetQuoteChar().

◆ GetQuoteChar() [2/2]

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

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 382 of file richio.cpp.

383 {
384  return GetQuoteChar( wrapee, quoteChar );
385 }
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:348
char quoteChar[2]
Definition: richio.h:401

References GetQuoteChar(), and quoteChar.

◆ Print()

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

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 426 of file richio.cpp.

427 {
428 #define NESTWIDTH 2
429 
430  va_list args;
431 
432  va_start( args, fmt );
433 
434  int result = 0;
435  int total = 0;
436 
437  for( int i = 0; i < nestLevel; ++i )
438  {
439  // no error checking needed, an exception indicates an error.
440  result = sprint( "%*c", NESTWIDTH, ' ' );
441 
442  total += result;
443  }
444 
445  // no error checking needed, an exception indicates an error.
446  result = vprint( fmt, args );
447 
448  va_end( args );
449 
450  total += result;
451  return total;
452 }
int sprint(const char *fmt,...)
Definition: richio.cpp:414
#define NESTWIDTH
int vprint(const char *fmt, va_list ap)
Definition: richio.cpp:388

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

Referenced by PANEL_SYM_LIB_TABLE::convertLibrary(), PCB_CALCULATOR_DATAFILE::Format(), PCB_PLOT_PARAMS::Format(), COMPONENT_NET::Format(), TEMPLATE_FIELDNAME::Format(), XNODE::Format(), FP_LIB_TABLE::Format(), SCH_SEXPR_PLUGIN::Format(), SCH_LEGACY_PLUGIN::Format(), SYMBOL_LIB_TABLE::Format(), NETLIST_EXPORTER_PSPICE::Format(), TEMPLATES::Format(), TITLE_BLOCK::Format(), DSN::POINT::Format(), COMPONENT::Format(), PAGE_INFO::Format(), DSN::PROPERTY::Format(), LIB_TABLE_ROW::Format(), DSN::ELEM::Format(), PCB_IO::format(), NETLIST::Format(), EDA_TEXT::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(), PCB_IO::formatBoardLayers(), BOARD_STACKUP::FormatBoardStackup(), 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(), formatFill(), PCB_IO::formatGeneral(), DSN::PIN_REF::FormatIt(), PCB_IO::formatLayer(), PCB_IO::formatLayers(), PCB_IO::formatNetInformation(), formatNode(), PCB_IO::formatProperties(), PCB_IO::formatSetup(), formatStroke(), GENDRILL_WRITER_BASE::GenDrillReportFile(), GENDRILL_WRITER_BASE::printToolSummary(), CLIPBOARD_IO::Save(), PCB_IO::Save(), SCH_SEXPR_PLUGIN_CACHE::saveArc(), SCH_LEGACY_PLUGIN_CACHE::saveArc(), SCH_SEXPR_PLUGIN_CACHE::saveBezier(), SCH_LEGACY_PLUGIN_CACHE::saveBezier(), SCH_SEXPR_PLUGIN::saveBitmap(), SCH_LEGACY_PLUGIN::saveBitmap(), SCH_SEXPR_PLUGIN::saveBusAlias(), SCH_LEGACY_PLUGIN::saveBusAlias(), SCH_SEXPR_PLUGIN::saveBusEntry(), SCH_LEGACY_PLUGIN::saveBusEntry(), SCH_SEXPR_PLUGIN_CACHE::saveCircle(), SCH_LEGACY_PLUGIN_CACHE::saveCircle(), SCH_LEGACY_PLUGIN_CACHE::saveDocFile(), SCH_SEXPR_PLUGIN::saveField(), SCH_LEGACY_PLUGIN::saveField(), SCH_SEXPR_PLUGIN_CACHE::saveField(), SCH_LEGACY_PLUGIN_CACHE::saveField(), SCH_SEXPR_PLUGIN::saveInstances(), SCH_SEXPR_PLUGIN::saveJunction(), SCH_LEGACY_PLUGIN::saveJunction(), SCH_SEXPR_PLUGIN::saveLine(), SCH_LEGACY_PLUGIN::saveLine(), SCH_SEXPR_PLUGIN::saveNoConnect(), SCH_LEGACY_PLUGIN::saveNoConnect(), SCH_SEXPR_PLUGIN_CACHE::savePin(), SCH_LEGACY_PLUGIN_CACHE::savePin(), SCH_SEXPR_PLUGIN_CACHE::savePolyLine(), SCH_LEGACY_PLUGIN_CACHE::savePolyLine(), SCH_SEXPR_PLUGIN_CACHE::saveRectangle(), SCH_LEGACY_PLUGIN_CACHE::saveRectangle(), CLIPBOARD_IO::SaveSelection(), SCH_SEXPR_PLUGIN::saveSheet(), SCH_LEGACY_PLUGIN::saveSheet(), SCH_SEXPR_PLUGIN::saveSymbol(), SCH_LEGACY_PLUGIN::saveSymbol(), SCH_SEXPR_PLUGIN_CACHE::SaveSymbol(), SCH_LEGACY_PLUGIN_CACHE::SaveSymbol(), SCH_SEXPR_PLUGIN::saveText(), SCH_LEGACY_PLUGIN::saveText(), SCH_SEXPR_PLUGIN_CACHE::saveText(), SCH_LEGACY_PLUGIN_CACHE::saveText(), PCB_CALCULATOR_FRAME::WriteDataFile(), NETLIST_EXPORTER_PSPICE_SIM::writeDirectives(), NETLIST_EXPORTER_PSPICE::writeDirectives(), and PCB_CALCULATOR_DATAFILE::WriteHeader().

◆ Quotes()

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

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 455 of file richio.cpp.

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

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

◆ Quotew()

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

Definition at line 494 of file richio.cpp.

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

References Quotes().

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

◆ sprint()

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

Definition at line 414 of file richio.cpp.

415 {
416  va_list args;
417 
418  va_start( args, fmt );
419  int ret = vprint( fmt, args);
420  va_end( args );
421 
422  return ret;
423 }
int vprint(const char *fmt, va_list ap)
Definition: richio.cpp:388

References vprint().

Referenced by Print().

◆ vprint()

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

Definition at line 388 of file richio.cpp.

389 {
390  // This function can call vsnprintf twice.
391  // But internally, vsnprintf retrieves arguments from the va_list identified by arg as if
392  // va_arg was used on it, and thus the state of the va_list is likely to be altered by the call.
393  // see: www.cplusplus.com/reference/cstdio/vsnprintf
394  // we make a copy of va_list ap for the second call, if happens
395  va_list tmp;
396  va_copy( tmp, ap );
397  int ret = vsnprintf( &m_buffer[0], m_buffer.size(), fmt, ap );
398 
399  if( ret >= (int) m_buffer.size() )
400  {
401  m_buffer.resize( ret + 1000 );
402  ret = vsnprintf( &m_buffer[0], m_buffer.size(), fmt, tmp );
403  }
404 
405  va_end( tmp ); // Release the temporary va_list, initialised from ap
406 
407  if( ret > 0 )
408  write( &m_buffer[0], ret );
409 
410  return ret;
411 }
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 m_buffer, and write().

Referenced by Print(), and sprint().

◆ write()

virtual void OUTPUTFORMATTER::write ( const char *  aOutBuf,
int  aCount 
)
protectedpure virtual

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.

Implemented in STREAM_OUTPUTFORMATTER, FILE_OUTPUTFORMATTER, and STRING_FORMATTER.

Referenced by vprint().

Member Data Documentation

◆ m_buffer

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

Definition at line 400 of file richio.h.

Referenced by vprint().

◆ quoteChar

char OUTPUTFORMATTER::quoteChar[2]
private

Definition at line 401 of file richio.h.

Referenced by GetQuoteChar(), and OUTPUTFORMATTER().


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