KiCad PCB EDA Suite
FILE_OUTPUTFORMATTER Class Reference

Used for text file output. More...

#include <richio.h>

Inheritance diagram for FILE_OUTPUTFORMATTER:
OUTPUTFORMATTER

Public Member Functions

 FILE_OUTPUTFORMATTER (const wxString &aFileName, const wxChar *aMode=wxT("wt"), char aQuoteChar='"' )
 
 ~FILE_OUTPUTFORMATTER ()
 
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...
 

Protected Attributes

FILE * m_fp
 takes ownership More...
 
wxString m_filename
 

Detailed Description

Used for text file output.

It is about 8 times faster than STREAM_OUTPUTFORMATTER for file streams.

Definition at line 453 of file richio.h.

Constructor & Destructor Documentation

◆ FILE_OUTPUTFORMATTER()

FILE_OUTPUTFORMATTER::FILE_OUTPUTFORMATTER ( const wxString &  aFileName,
const wxChar *  aMode = wxT( "wt" ),
char  aQuoteChar = '"' 
)
Parameters
aFileNameis the full filename to open and save to as a text file.
aModeis what you would pass to wxFopen()'s mode, defaults to wxT( "wt" ) for text files that are to be created here and now.
aQuoteCharis a char used for quoting problematic strings (with whitespace or special characters in them).
Exceptions
IO_ERRORif the file cannot be opened.

Definition at line 511 of file richio.cpp.

512  :
513  OUTPUTFORMATTER( OUTPUTFMTBUFZ, aQuoteChar ),
514  m_filename( aFileName )
515 {
516  m_fp = wxFopen( aFileName, aMode );
517 
518  if( !m_fp )
519  THROW_IO_ERROR( strerror( errno ) );
520 }
wxString m_filename
Definition: richio.h:474
OUTPUTFORMATTER(int aReserve=OUTPUTFMTBUFZ, char aQuoteChar='"' )
Definition: richio.h:309
#define OUTPUTFMTBUFZ
default buffer size for any OUTPUT_FORMATTER
Definition: richio.h:289
FILE * m_fp
takes ownership
Definition: richio.h:473
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38

References m_fp, and THROW_IO_ERROR.

◆ ~FILE_OUTPUTFORMATTER()

FILE_OUTPUTFORMATTER::~FILE_OUTPUTFORMATTER ( )

Definition at line 523 of file richio.cpp.

524 {
525  if( m_fp )
526  fclose( m_fp );
527 }
FILE * m_fp
takes ownership
Definition: richio.h:473

References m_fp.

Member Function Documentation

◆ GetQuoteChar() [1/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 331 of file richio.cpp.

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 }

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

◆ GetQuoteChar() [2/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 365 of file richio.cpp.

366 {
367  return GetQuoteChar( wrapee, quoteChar );
368 }
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:331
char quoteChar[2]
Definition: richio.h:398

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

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

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

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

Referenced by PANEL_SYM_LIB_TABLE::convertLibrary(), PCB_CALCULATOR_DATAFILE::Format(), COMPONENT_NET::Format(), TEMPLATE_FIELDNAME::Format(), XNODE::Format(), FP_LIB_TABLE::Format(), SCH_LEGACY_PLUGIN::Format(), SYMBOL_LIB_TABLE::Format(), TEMPLATES::Format(), TITLE_BLOCK::Format(), NETLIST_EXPORTER_PSPICE::Format(), DSN::POINT::Format(), DSN::PROPERTY::Format(), PAGE_INFO::Format(), LIB_TABLE_ROW::Format(), COMPONENT::Format(), PCB_PLOT_PARAMS::Format(), PCB_IO::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(), 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_LEGACY_PLUGIN::saveBitmap(), SCH_LEGACY_PLUGIN::saveBusAlias(), SCH_LEGACY_PLUGIN::saveBusEntry(), SCH_SEXPR_PLUGIN_CACHE::saveCircle(), SCH_LEGACY_PLUGIN_CACHE::saveCircle(), SCH_LEGACY_PLUGIN::saveComponent(), SCH_LEGACY_PLUGIN_CACHE::saveDocFile(), SCH_LEGACY_PLUGIN::saveField(), SCH_SEXPR_PLUGIN_CACHE::saveField(), SCH_LEGACY_PLUGIN_CACHE::saveField(), SCH_LEGACY_PLUGIN::saveJunction(), SCH_LEGACY_PLUGIN::saveLine(), 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_LEGACY_PLUGIN::saveSheet(), SCH_SEXPR_PLUGIN_CACHE::SaveSymbol(), SCH_LEGACY_PLUGIN_CACHE::SaveSymbol(), 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
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 437 of file richio.cpp.

438 {
439  std::string ret;
440 
441  ret.reserve( aWrapee.size() * 2 + 2 );
442 
443  ret += '"';
444 
445  for( std::string::const_iterator it = aWrapee.begin(); it != aWrapee.end(); ++it )
446  {
447  switch( *it )
448  {
449  case '\n':
450  ret += '\\';
451  ret += 'n';
452  break;
453  case '\r':
454  ret += '\\';
455  ret += 'r';
456  break;
457  case '\\':
458  ret += '\\';
459  ret += '\\';
460  break;
461  case '"':
462  ret += '\\';
463  ret += '"';
464  break;
465  default:
466  ret += *it;
467  }
468  }
469 
470  ret += '"';
471 
472  return ret;
473 }

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

◆ Quotew()

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

Definition at line 476 of file richio.cpp.

477 {
478  // wxStrings are always encoded as UTF-8 as we convert to a byte sequence.
479  // The non-virutal function calls the virtual workhorse function, and if
480  // a different quoting or escaping strategy is desired from the standard,
481  // a derived class can overload Quotes() above, but
482  // should never be a reason to overload this Quotew() here.
483  return Quotes( (const char*) aWrapee.utf8_str() );
484 }
virtual std::string Quotes(const std::string &aWrapee) const
Check aWrapee input string for a need to be quoted (e.g.
Definition: richio.cpp:437

References OUTPUTFORMATTER::Quotes().

Referenced by PCB_CALCULATOR_DATAFILE::Format(), COMPONENT_NET::Format(), TEMPLATE_FIELDNAME::Format(), TITLE_BLOCK::Format(), PAGE_INFO::Format(), LIB_TABLE_ROW::Format(), COMPONENT::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_CACHE::saveField(), SCH_SEXPR_PLUGIN_CACHE::savePin(), SCH_SEXPR_PLUGIN_CACHE::SaveSymbol(), SCH_SEXPR_PLUGIN_CACHE::saveText(), and PCB_CALCULATOR_DATAFILE::WriteHeader().

◆ write()

void FILE_OUTPUTFORMATTER::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 530 of file richio.cpp.

531 {
532  if( fwrite( aOutBuf, (unsigned) aCount, 1, m_fp ) != 1 )
533  THROW_IO_ERROR( strerror( errno ) );
534 }
FILE * m_fp
takes ownership
Definition: richio.h:473
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38

References m_fp, and THROW_IO_ERROR.

Member Data Documentation

◆ m_filename

wxString FILE_OUTPUTFORMATTER::m_filename
protected

Definition at line 474 of file richio.h.

◆ m_fp

FILE* FILE_OUTPUTFORMATTER::m_fp
protected

takes ownership

Definition at line 473 of file richio.h.

Referenced by FILE_OUTPUTFORMATTER(), write(), and ~FILE_OUTPUTFORMATTER().


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