KiCad PCB EDA Suite
richio.cpp File Reference
#include <cstdarg>
#include <config.h>
#include <richio.h>

Go to the source code of this file.

Macros

#define getc_unlocked   getc
 
#define NESTWIDTH   2
 

Functions

static int vprint (std::string *result, const char *format, va_list ap)
 
int StrPrintf (std::string *result, const char *format,...)
 Function StrPrintf is like sprintf() but the output is appended to a std::string instead of to a character array. More...
 
std::string StrPrintf (const char *format,...)
 Function StrPrintf is like sprintf() but the output is returned in a std::string instead of to a character array. More...
 

Macro Definition Documentation

#define getc_unlocked   getc

Definition at line 34 of file richio.cpp.

Referenced by FILE_LINE_READER::ReadLine().

#define NESTWIDTH   2

Referenced by OUTPUTFORMATTER::Print().

Function Documentation

int StrPrintf ( std::string *  aResult,
const char *  aFormat,
  ... 
)

Function StrPrintf is like sprintf() but the output is appended to a std::string instead of to a character array.

Parameters
aResultis the string to append to, previous text is not clear()ed.
aFormatis a printf() style format string.
Returns
int - the count of bytes appended to the result string, no terminating nul is included.

Definition at line 74 of file richio.cpp.

References vprint().

Referenced by checkConnectedTo(), fmt_mask(), GITHUB_PLUGIN::FootprintDelete(), GITHUB_PLUGIN::FootprintSave(), PCB_IO::format(), FormatProbeItem(), GenCADLayerName(), GenCADLayerNameFlipped(), PCB_PARSER::init(), GENDRILL_WRITER_BASE::layerName(), SCH_EDIT_FRAME::OpenProjectFiles(), PCB_EDIT_FRAME::OpenProjectFiles(), GITHUB_GETLIBLIST::remoteGetJSON(), GITHUB_PLUGIN::remoteGetZip(), ReplaceIllegalFileNameChars(), and CVPCB_MAINFRAME::SendMessageToEESCHEMA().

75 {
76  va_list args;
77 
78  va_start( args, format );
79  int ret = vprint( result, format, args );
80  va_end( args );
81 
82  return ret;
83 }
static int vprint(std::string *result, const char *format, va_list ap)
Definition: richio.cpp:38
std::string StrPrintf ( const char *  format,
  ... 
)

Function StrPrintf is like sprintf() but the output is returned in a std::string instead of to a character array.

Parameters
formatis a printf() style format string.
Returns
std::string - the result of the sprintf().

Definition at line 86 of file richio.cpp.

References vprint().

87 {
88  std::string ret;
89  va_list args;
90 
91  va_start( args, format );
92  int ignore = vprint( &ret, format, args );
93  (void) ignore;
94  va_end( args );
95 
96  return ret;
97 }
static int vprint(std::string *result, const char *format, va_list ap)
Definition: richio.cpp:38
static int vprint ( std::string *  result,
const char *  format,
va_list  ap 
)
static

Definition at line 38 of file richio.cpp.

Referenced by StrPrintf().

39 {
40  char msg[512];
41  // This function can call vsnprintf twice.
42  // But internally, vsnprintf retrieves arguments from the va_list identified by arg as if
43  // va_arg was used on it, and thus the state of the va_list is likely to be altered by the call.
44  // see: www.cplusplus.com/reference/cstdio/vsnprintf
45  // we make a copy of va_list ap for the second call, if happens
46  va_list tmp;
47  va_copy( tmp, ap );
48 
49  size_t len = vsnprintf( msg, sizeof(msg), format, ap );
50 
51  if( len < sizeof(msg) ) // the output fit into msg
52  {
53  result->append( msg, msg + len );
54  }
55  else
56  {
57  // output was too big, so now incur the expense of allocating
58  // a buf for holding suffient characters.
59 
60  std::vector<char> buf;
61  buf.reserve( len+1 ); // reserve(), not resize() which writes. +1 for trailing nul.
62 
63  len = vsnprintf( &buf[0], len+1, format, tmp );
64 
65  result->append( &buf[0], &buf[0] + len );
66  }
67 
68  va_end( tmp ); // Release the temporary va_list, initialised from ap
69 
70  return len;
71 }