KiCad PCB EDA Suite
OUTPUTFORMATTER Class Referenceabstract

Class OUTPUTFORMATTER is an important interface (abstract class) 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,...)
 Function Print formats and writes text to the output stream. More...
 
virtual const char * GetQuoteChar (const char *wrapee)
 Function GetQuoteChar performs quote character need determination. More...
 
virtual std::string Quotes (const std::string &aWrapee)
 Function Quotes checks aWrapee input string for a need to be quoted (e.g. More...
 
std::string Quotew (const wxString &aWrapee)
 

Protected Member Functions

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

Detailed Description

Class OUTPUTFORMATTER is an important interface (abstract class) 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 327 of file richio.h.

Constructor & Destructor Documentation

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

Definition at line 337 of file richio.h.

337  :
338  buffer( aReserve, '\0' )
339  {
340  quoteChar[0] = aQuoteChar;
341  quoteChar[1] = '\0';
342  }
std::vector< char > buffer
Definition: richio.h:329
char quoteChar[2]
Definition: richio.h:330
virtual OUTPUTFORMATTER::~OUTPUTFORMATTER ( )
inlineprotectedvirtual

Definition at line 344 of file richio.h.

344 {}

Member Function Documentation

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

Function GetQuoteChar performs 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
const char* - the quote_char as a single character string, or "" if the wrapee does not need to be wrapped.

Definition at line 331 of file richio.cpp.

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

332 {
333  // Include '#' so a symbol is not confused with a comment. We intend
334  // to wrap any symbol starting with a '#'.
335  // Our LEXER class handles comments, and comments appear to be an extension
336  // to the SPECCTRA DSN specification.
337  if( *wrapee == '#' )
338  return quote_char;
339 
340  if( strlen( wrapee ) == 0 )
341  return quote_char;
342 
343  bool isFirst = true;
344 
345  for( ; *wrapee; ++wrapee, isFirst = false )
346  {
347  static const char quoteThese[] = "\t ()"
348  "%" // per Alfons of freerouting.net, he does not like this unquoted as of 1-Feb-2008
349  "{}" // guessing that these are problems too
350  ;
351 
352  // if the string to be wrapped (wrapee) has a delimiter in it,
353  // return the quote_char so caller wraps the wrapee.
354  if( strchr( quoteThese, *wrapee ) )
355  return quote_char;
356 
357  if( !isFirst && '-' == *wrapee )
358  return quote_char;
359  }
360 
361  return ""; // caller does not need to wrap, can use an unwrapped string.
362 }
const char * OUTPUTFORMATTER::GetQuoteChar ( const char *  wrapee)
virtual

Function GetQuoteChar performs 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
const char* - the quote_char as a single character string, or "" if the wrapee does not need to be wrapped.

Definition at line 365 of file richio.cpp.

References GetQuoteChar(), and quoteChar.

366 {
367  return GetQuoteChar( wrapee, quoteChar );
368 }
static const char * GetQuoteChar(const char *wrapee, const char *quote_char)
Function GetQuoteChar performs quote character need determination according to the Specctra DSN speci...
Definition: richio.cpp:331
char quoteChar[2]
Definition: richio.h:330
int OUTPUTFORMATTER::Print ( int  nestLevel,
const char *  fmt,
  ... 
)

Function Print formats and writes 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 408 of file richio.cpp.

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

Referenced by COMPONENT_NET::Format(), XNODE::Format(), TEMPLATE_FIELDNAME::Format(), SYMBOL_LIB_TABLE::Format(), SCH_LEGACY_PLUGIN::Format(), FP_LIB_TABLE::Format(), NETLIST_EXPORTER_PSPICE::Format(), TEMPLATES::Format(), TITLE_BLOCK::Format(), DSN::POINT::Format(), PCB_PLOT_PARAMS::Format(), DSN::PROPERTY::Format(), LIB_TABLE_ROW::Format(), PAGE_INFO::Format(), PCB_IO::format(), COMPONENT::Format(), NETCLASS::Format(), DSN::ELEM::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(), 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(), DSN::PIN_REF::FormatIt(), PCB_IO::formatLayer(), PCB_IO::formatLayers(), formatNode(), GENDRILL_WRITER_BASE::GenDrillReportFile(), GENDRILL_WRITER_BASE::printToolSummary(), LIB_CIRCLE::Save(), LIB_POLYLINE::Save(), LIB_BEZIER::Save(), LIB_RECTANGLE::Save(), LIB_TEXT::Save(), LIB_ARC::Save(), PCB_IO::Save(), LIB_PIN::Save(), LIB_FIELD::Save(), LIB_PART::Save(), SCH_LEGACY_PLUGIN::saveBitmap(), SCH_LEGACY_PLUGIN::saveBusEntry(), SCH_LEGACY_PLUGIN::saveComponent(), LIB_PART::SaveDateAndTime(), LIB_ALIAS::SaveDoc(), SCH_LEGACY_PLUGIN_CACHE::saveDocFile(), SCH_LEGACY_PLUGIN::saveField(), SCH_LEGACY_PLUGIN::saveJunction(), SCH_LEGACY_PLUGIN::saveLine(), SCH_LEGACY_PLUGIN::saveNoConnect(), LIB_EDIT_FRAME::SaveOneSymbol(), SCH_LEGACY_PLUGIN::saveSheet(), SCH_LEGACY_PLUGIN::saveText(), NETLIST_EXPORTER_PSPICE_SIM::writeDirectives(), NETLIST_EXPORTER_PSPICE::writeDirectives(), and DIALOG_BOM::~DIALOG_BOM().

409 {
410 #define NESTWIDTH 2
411 
412  va_list args;
413 
414  va_start( args, fmt );
415 
416  int result = 0;
417  int total = 0;
418 
419  for( int i=0; i<nestLevel; ++i )
420  {
421  // no error checking needed, an exception indicates an error.
422  result = sprint( "%*c", NESTWIDTH, ' ' );
423 
424  total += result;
425  }
426 
427  // no error checking needed, an exception indicates an error.
428  result = vprint( fmt, args );
429 
430  va_end( args );
431 
432  total += result;
433  return total;
434 }
int sprint(const char *fmt,...)
Definition: richio.cpp:396
#define NESTWIDTH
int vprint(const char *fmt, va_list ap)
Definition: richio.cpp:370
std::string OUTPUTFORMATTER::Quotes ( const std::string &  aWrapee)
virtual

Function Quotes checks 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 wraping in double quotes, and it might need to have its internal content escaped, or not.
Returns
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 437 of file richio.cpp.

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

438 {
439  static const char quoteThese[] = "\t ()\n\r";
440 
441  if( !aWrapee.size() || // quote null string as ""
442  aWrapee[0]=='#' || // quote a potential s-expression comment, so it is not a comment
443  aWrapee[0]=='"' || // NextTok() will travel through DSN_STRING path anyway, then must apply escapes
444  aWrapee.find_first_of( quoteThese ) != std::string::npos )
445  {
446  std::string ret;
447 
448  ret.reserve( aWrapee.size()*2 + 2 );
449 
450  ret += '"';
451 
452  for( std::string::const_iterator it = aWrapee.begin(); it!=aWrapee.end(); ++it )
453  {
454  switch( *it )
455  {
456  case '\n':
457  ret += '\\';
458  ret += 'n';
459  break;
460  case '\r':
461  ret += '\\';
462  ret += 'r';
463  break;
464  case '\\':
465  ret += '\\';
466  ret += '\\';
467  break;
468  case '"':
469  ret += '\\';
470  ret += '"';
471  break;
472  default:
473  ret += *it;
474  }
475  }
476 
477  ret += '"';
478 
479  return ret;
480  }
481 
482  return aWrapee;
483 }
std::string OUTPUTFORMATTER::Quotew ( const wxString &  aWrapee)

Definition at line 486 of file richio.cpp.

References Quotes().

Referenced by COMPONENT_NET::Format(), XNODE::Format(), TEMPLATE_FIELDNAME::Format(), TITLE_BLOCK::Format(), PCB_PLOT_PARAMS::Format(), LIB_TABLE_ROW::Format(), PAGE_INFO::Format(), PCB_IO::format(), COMPONENT::Format(), NETCLASS::Format(), XNODE::FormatContents(), PCB_IO::formatLayer(), PCB_IO::formatLayers(), PCB_IO::Save(), and DIALOG_BOM::~DIALOG_BOM().

487 {
488  // wxStrings are always encoded as UTF-8 as we convert to a byte sequence.
489  // The non-virutal function calls the virtual workhorse function, and if
490  // a different quoting or escaping strategy is desired from the standard,
491  // a derived class can overload Quotes() above, but
492  // should never be a reason to overload this Quotew() here.
493  return Quotes( (const char*) aWrapee.utf8_str() );
494 }
virtual std::string Quotes(const std::string &aWrapee)
Function Quotes checks aWrapee input string for a need to be quoted (e.g.
Definition: richio.cpp:437
int OUTPUTFORMATTER::sprint ( const char *  fmt,
  ... 
)
private

Definition at line 396 of file richio.cpp.

References vprint().

Referenced by Print().

397 {
398  va_list args;
399 
400  va_start( args, fmt );
401  int ret = vprint( fmt, args);
402  va_end( args );
403 
404  return ret;
405 }
int vprint(const char *fmt, va_list ap)
Definition: richio.cpp:370
int OUTPUTFORMATTER::vprint ( const char *  fmt,
va_list  ap 
)
private

Definition at line 370 of file richio.cpp.

References buffer, and write().

Referenced by Print(), and sprint().

371 {
372  // This function can call vsnprintf twice.
373  // But internally, vsnprintf retrieves arguments from the va_list identified by arg as if
374  // va_arg was used on it, and thus the state of the va_list is likely to be altered by the call.
375  // see: www.cplusplus.com/reference/cstdio/vsnprintf
376  // we make a copy of va_list ap for the second call, if happens
377  va_list tmp;
378  va_copy( tmp, ap );
379  int ret = vsnprintf( &buffer[0], buffer.size(), fmt, ap );
380 
381  if( ret >= (int) buffer.size() )
382  {
383  buffer.resize( ret + 1000 );
384  ret = vsnprintf( &buffer[0], buffer.size(), fmt, tmp );
385  }
386 
387  va_end( tmp ); // Release the temporary va_list, initialised from ap
388 
389  if( ret > 0 )
390  write( &buffer[0], ret );
391 
392  return ret;
393 }
virtual void write(const char *aOutBuf, int aCount)=0
Function write should be coded in the interface implementation (derived) classes. ...
std::vector< char > buffer
Definition: richio.h:329
virtual void OUTPUTFORMATTER::write ( const char *  aOutBuf,
int  aCount 
)
protectedpure virtual

Function write 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

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

Definition at line 329 of file richio.h.

Referenced by vprint().

char OUTPUTFORMATTER::quoteChar[2]
private

Definition at line 330 of file richio.h.

Referenced by GetQuoteChar().


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