KiCad PCB EDA Suite
richio.cpp File Reference
#include <cstdarg>
#include <config.h>
#include <ignore.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 39 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 79 of file richio.cpp.

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

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

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

References ignore_unused(), and vprint().

◆ vprint()

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

Definition at line 43 of file richio.cpp.

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

Referenced by StrPrintf().