KiCad PCB EDA Suite
FILE_OUTPUTFORMATTER Class Reference

Class FILE_OUTPUTFORMATTER may be 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= '"' )
 Constructor. More...
 
 ~FILE_OUTPUTFORMATTER ()
 
virtual const char * GetQuoteChar (const char *wrapee)
 Function GetQuoteChar performs quote character need determination. More...
 
int PRINTF_FUNC Print (int nestLevel, const char *fmt,...)
 Function Print formats and writes text to the output stream. 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

void write (const char *aOutBuf, int aCount) override
 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...
 

Protected Attributes

FILE * m_fp
 takes ownership More...
 
wxString m_filename
 

Detailed Description

Class FILE_OUTPUTFORMATTER may be used for text file output.

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

Definition at line 492 of file richio.h.

Constructor & Destructor Documentation

FILE_OUTPUTFORMATTER::FILE_OUTPUTFORMATTER ( const wxString &  aFileName,
const wxChar *  aMode = wxT( "wt" ),
char  aQuoteChar = '"' 
)

Constructor.

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

References Format(), m_filename, m_fp, and THROW_IO_ERROR.

518  :
519  OUTPUTFORMATTER( OUTPUTFMTBUFZ, aQuoteChar ),
520  m_filename( aFileName )
521 {
522  m_fp = wxFopen( aFileName, aMode );
523 
524  if( !m_fp )
525  {
526  wxString msg = wxString::Format(
527  _( "cannot open or save file '%s'" ),
528  m_filename.GetData() );
529  THROW_IO_ERROR( msg );
530  }
531 }
OUTPUTFORMATTER(int aReserve=OUTPUTFMTBUFZ, char aQuoteChar= '"' )
Definition: richio.h:337
wxString m_filename
Definition: richio.h:517
#define OUTPUTFMTBUFZ
default buffer size for any OUTPUT_FORMATTER
Definition: richio.h:309
FILE * m_fp
takes ownership
Definition: richio.h:516
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
FILE_OUTPUTFORMATTER::~FILE_OUTPUTFORMATTER ( )

Definition at line 534 of file richio.cpp.

References m_fp.

535 {
536  if( m_fp )
537  fclose( m_fp );
538 }
FILE * m_fp
takes ownership
Definition: richio.h:516

Member Function Documentation

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

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

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

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

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

362 {
363  return GetQuoteChar( wrapee, quoteChar );
364 }
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:327
char quoteChar[2]
Definition: richio.h:330
int OUTPUTFORMATTER::Print ( int  nestLevel,
const char *  fmt,
  ... 
)
inherited

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

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

Referenced by COMPONENT_NET::Format(), XNODE::Format(), TEMPLATE_FIELDNAME::Format(), SCH_LEGACY_PLUGIN::Format(), FP_LIB_TABLE::Format(), SYMBOL_LIB_TABLE::Format(), NETLIST_EXPORTER_PSPICE::Format(), TEMPLATES::Format(), TITLE_BLOCK::Format(), DSN::POINT::Format(), LIB_TABLE_ROW::Format(), DSN::PROPERTY::Format(), PCB_PLOT_PARAMS::Format(), PAGE_INFO::Format(), COMPONENT::Format(), PCB_IO::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(), PCB_IO::formatBoardLayers(), 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(), PCB_IO::formatGeneral(), DSN::PIN_REF::FormatIt(), PCB_IO::formatLayer(), PCB_IO::formatLayers(), PCB_IO::formatNetInformation(), formatNode(), PCB_IO::formatSetup(), GENDRILL_WRITER_BASE::GenDrillReportFile(), GENDRILL_WRITER_BASE::printToolSummary(), CLIPBOARD_IO::Save(), PCB_IO::Save(), SCH_LEGACY_PLUGIN::saveBitmap(), SCH_LEGACY_PLUGIN::saveBusEntry(), SCH_LEGACY_PLUGIN::saveComponent(), LIB_PART::SaveDateAndTime(), SCH_LEGACY_PLUGIN_CACHE::saveDocFile(), SCH_LEGACY_PLUGIN::saveField(), SCH_LEGACY_PLUGIN::saveJunction(), SCH_LEGACY_PLUGIN::saveLine(), SCH_LEGACY_PLUGIN::saveNoConnect(), CLIPBOARD_IO::SaveSelection(), SCH_LEGACY_PLUGIN::saveSheet(), SCH_LEGACY_PLUGIN::saveText(), NETLIST_EXPORTER_PSPICE_SIM::writeDirectives(), NETLIST_EXPORTER_PSPICE::writeDirectives(), and DIALOG_BOM::~DIALOG_BOM().

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

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

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

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

Definition at line 482 of file richio.cpp.

References OUTPUTFORMATTER::Quotes().

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

483 {
484  // wxStrings are always encoded as UTF-8 as we convert to a byte sequence.
485  // The non-virutal function calls the virtual workhorse function, and if
486  // a different quoting or escaping strategy is desired from the standard,
487  // a derived class can overload Quotes() above, but
488  // should never be a reason to overload this Quotew() here.
489  return Quotes( (const char*) aWrapee.utf8_str() );
490 }
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:433
void FILE_OUTPUTFORMATTER::write ( const char *  aOutBuf,
int  aCount 
)
overrideprotectedvirtual

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.

Implements OUTPUTFORMATTER.

Definition at line 541 of file richio.cpp.

References Format(), m_filename, m_fp, and THROW_IO_ERROR.

542 {
543  if( 1 != fwrite( aOutBuf, aCount, 1, m_fp ) )
544  {
545  wxString msg = wxString::Format(
546  _( "error writing to file '%s'" ),
547  m_filename.GetData() );
548  THROW_IO_ERROR( msg );
549  }
550 }
wxString m_filename
Definition: richio.h:517
FILE * m_fp
takes ownership
Definition: richio.h:516
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38

Member Data Documentation

wxString FILE_OUTPUTFORMATTER::m_filename
protected

Definition at line 517 of file richio.h.

Referenced by FILE_OUTPUTFORMATTER(), and write().

FILE* FILE_OUTPUTFORMATTER::m_fp
protected

takes ownership

Definition at line 516 of file richio.h.

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


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