Reference Link:
http://msdn.microsoft.com/en-us/library/28d5ce15(v=vs.71).aspx
http://stackoverflow.com/questions/16351523/vscwprintf-on-mac-os-x-linux
Microsoft describes the functions asreturning the number of characters that would be used if the string wereformatted — note that they are documented as not including the null terminator.
int _vscprintf(
const char *format,
va_list argptr
);
int _vscwprintf(
const wchar_t *format,
va_list argptr
);
These functions can, therefore, beemulated with vsprintf() and vswprintf():
int _vscprintf(const char *format,va_list argptr)
{
return(vsnprintf(0, 0, format, argptr));
}
int _vscwprintf(const wchar_t *format,va_list argptr)
{
return(vswprintf(0, 0, format, argptr));
}
It is up to you whether you remove theleading underscore; I would.
vscprintf() and scprintf()
Apologies: I wrote vsprintf() where Ineeded to write vsnprintf() (now fixed in the code above); however, vswprintf()already has the safer interface with the buffer length, so there is novsnwprintf(). There's a reason I prefer to test compile code before (or shortlyafter) posting it — it's been irksome not having the wherewithal to do so for acouple of days.
Here's an SSCCE for vscprintf() (andscprintf()):
#include <stdio.h>
#include <stdarg.h>
extern int vscprintf(const char*format, va_list argptr);
extern int scprintf(const char*format, ...);
int vscprintf(const char *format,va_list argptr)
{
return(vsnprintf(0, 0, format, argptr));
}
int scprintf(const char *format, ...)
{
va_list args;
va_start(args, format);
int rc = vscprintf(format, args);
va_end(args);
return rc;
}
int main(void)
{
int l = scprintf("%-8s %8d\n", "abc", 123);
if (l > 0)
{
char buffer[l+1];
int n = snprintf(buffer, sizeof(buffer), "%-8s %8d\n","abc", 123);
printf("%d = %d: %s", l, n, buffer);
}
return 0;
}
Output:
18 = 18: abc 123
vscwprintf() and scwprintf()
It turns out to be harder to simulate_vscwprintf() because the vswprintf() function is not as helpful as thevsnprintf() function. Specifically, vswprintf() reports an error if theformatted string won't fit in the formatted space, whereas vsnprintf() reportsthe number of characters that would have been needed in the buffer if it wasgoing to fit. Hence, you have to work by trial and error
// the buffer - it just reports anerror when there is not enough
// space. Assume a moderatelylarge machine so kilobytes of wchar_t
// on the stack is not a problem.
int buf_size = 1024;
while (buf_size < 1024 * 1024)
{
va_list args;
va_copy(args, argptr);
wchar_t buffer[buf_size];
int fmt_size = vswprintf(buffer, sizeof(buffer)/sizeof(buffer[0]),format, args);
if (fmt_size >= 0)
return fmt_size;
buf_size *= 2;
}
return -1;
}
int scwprintf(const wchar_t *format,...)
{
va_list args;
va_start(args, format);
int rc = vscwprintf(format, args);
va_end(args);
return rc;
}
int main(void)
{
int l = scwprintf(L"%-8ls %8d\n", L"abc", 123);
if (l > 0)
{
wchar_t buffer[l+1];
int n = swprintf(buffer, sizeof(buffer)/sizeof(buffer[0]), L"%-8ls%8d\n", L"abc", 123);
wprintf(L"%d = %d: %ls", l, n, buffer);
}
return 0;
}
When run, this produces the output
18 = 18: abc 123
(the same as before).
Tested on Mac OS X 10.8.3 using GCC4.7.3 (which was built on Mac OS X 10.7.5, but that shouldn't cause anyproblems).