KiCad PCB EDA Suite
richio.cpp File Reference
#include <cstdarg>
#include <config.h>
#include <richio.h>
#include <errno.h>
#include <wx/file.h>
#include <wx/translation.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,...)
 This 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,...)
 This is like sprintf() but the output is returned in a std::string instead of to a character array. More...
 

Macro Definition Documentation

◆ getc_unlocked

#define getc_unlocked   getc

Definition at line 38 of file richio.cpp.

◆ NESTWIDTH

#define NESTWIDTH   2

Function Documentation

◆ StrPrintf() [1/2]

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

This 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
the count of bytes appended to the result string, no terminating nul is included.

Definition at line 78 of file richio.cpp.

79 {
80  va_list args;
81 
82  va_start( args, format );
83  int ret = vprint( result, format, args );
84  va_end( args );
85 
86  return ret;
87 }
static int vprint(std::string *result, const char *format, va_list ap)
Definition: richio.cpp:42

References vprint().

Referenced by fmt_mask(), PCB_IO::format(), FormatProbeItem(), GenCADLayerName(), GenCADLayerNameFlipped(), KICAD_MANAGER_FRAME::ImportNonKiCadProject(), PCB_PARSER::init(), GENDRILL_WRITER_BASE::layerName(), PCB_EDIT_FRAME::OpenProjectFiles(), ReplaceIllegalFileNameChars(), SCH_EDIT_FRAME::SendCrossProbeNetName(), PCB_EDIT_FRAME::SendCrossProbeNetName(), and SCH_EDIT_FRAME::SetCrossProbeConnection().

◆ StrPrintf() [2/2]

std::string StrPrintf ( const char *  format,
  ... 
)

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

91 {
92  std::string ret;
93  va_list args;
94 
95  va_start( args, format );
96  int ignore = vprint( &ret, format, args );
97  (void) ignore;
98  va_end( args );
99 
100  return ret;
101 }
static int vprint(std::string *result, const char *format, va_list ap)
Definition: richio.cpp:42

References vprint().

◆ vprint()

static int vprint ( std::string *  result,
const char *  format,
va_list  ap 
)
static

Definition at line 42 of file richio.cpp.

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

Referenced by StrPrintf().