translate vswscanf from c to c++? - c++

I need to translate, vswscanf function from c to c++
using wide string stream and var arg,
can you help me ?
#include <stdarg.h>
#include <wchar.h>
void GetWideMatches ( const wchar_t * str, const wchar_t * format, ... )
{
va_list args;
va_start (args, format);
vswscanf (str, format, args);
va_end (args);
}
int main ()
{
int val;
wchar_t buf[100];
GetWideMatches ( L"99 bottles of beer on the wall", L" %d %ls ", &val, buf);
wprintf (L"Product: %ls\nQuantity: %d\n", buf, val);
return 0;
}

Related

Different behavior when using vsnprintf on msvc and gcc

I have the following code to append a buffer using vsnprintf().
#include <stdio.h>
#include <stdarg.h>
using namespace std;
void MyPrintFunc(char *buffer, size_t sizeOfBuffer, const char* format, ...)
{
va_list arg;
va_start(arg, format);
vsnprintf(buffer, sizeOfBuffer, format, arg);
va_end(arg);
}
int main()
{
char buffer[1000] = { 0 };
const char* abc = "abc";
const char* def = "def";
MyPrintFunc(buffer, 1000, "%s", abc);
MyPrintFunc(buffer, 1000, "%s%s", buffer, def);
printf("%s\n", buffer);
return 0;
}
It gave different output on both Windows and Linux.
On Windows (using msvc-14.0 compiler), it give desired output of abcdef.
But on Linux (using gcc-5.4), it only print output of def.
How can I get the correct output?
Don't use the same buffer as both an output and input to vsnprintf.
Thanks everyone for the input.
My solution to this is to create a copy of the buffer if user of the MyPrintFunc() used it to append buffer:
void MyPrintFunc(char *buffer, size_t sizeOfBuffer, const char* format, ...)
{
va_list arg;
va_list arg_copy;
va_start(arg, format);
va_copy(arg_copy, arg);
if (buffer == va_arg(arg, char*))
{
char temp[strlen(buffer)];
strcpy(temp, buffer);
MyPrintFunc(buffer, sizeOfBuffer, format, temp, va_arg(arg,char*));
}
else
{
vsnprintf(buffer, sizeOfBuffer, format, arg_copy);
}
va_end(arg);
va_end(arg_copy);
}

How to write percent character into a file using variable argument list, va_list, va_start, va_end

I need to use %% in format string in order to write % character like this:
printf("%%");
I am trying to do this in my variable argument list function. Format specifiers work fine such as %d, %s etc. But when I try to write % character using %%, it doesn't work. Why and is there any solution?
void foo(LPCSTR sFrmtStr, ...)
{
char buffer[4096] = { 0 };
va_list argList;
va_start(argList, sFrmtStr);
vsprintf(buffer, sFrmtStr, argList);
va_end(argList);
printf(buffer);
}
Of course "it doesn't work". You're calling printf with a string that has a single % in it after calling vsprintf():
void foo(LPCSTR sFrmtStr, ...)
{
char buffer[4096] = { 0 };
va_list argList;
va_start(argList, sFrmtStr);
vsprintf(buffer, sFrmtStr, argList);
va_end(argList);
/* Now buffer holds a string with a single '%' in it */
printf(buffer);
}
The fix:
void foo(LPCSTR sFrmtStr, ...)
{
/* doesn't need to be initialized */
char buffer[4096];
va_list argList;
va_start(argList, sFrmtStr);
/* assume vsnprintf is available */
vsnprintf(buffer, sizeof( buffer ), sFrmtStr, argList);
va_end(argList);
/* puts() is probably better... */
/* and fputs( buffer, stdout ) is even better... */
fputs( buffer, stdout );
/* printf("%s", buffer); */
}
Your code works with the following usage:
#include <stdarg.h>
#include <stdio.h>
void foo(char* sFrmtStr, ...)
{
char buffer[4096] = { 0 };
va_list argList;
va_start(argList, sFrmtStr);
vsprintf(buffer, sFrmtStr, argList);
va_end(argList);
printf(buffer);
}
int main()
{
foo("n=%d pct %%\n", 23);
return 0;
}
Output:
n=23 pct %
However, gcc rightfully does not like it with a warning about printf(). Better use puts() instead of printf(), then you do not have to worry about stray % characters that printf might try to interpret.

Debug C++ Runtime Component in a WP8 project

I've tried a lot of solutions, but none of them is working.
printf("Test : %d", 123);
std::cout << "Test" << 123 << std::endl;
...
Actually I've setup my project's debugger like this, I'm able to write in the console using OutputDebugStringA("Test"); but this function doesn't accept more than one parameter.
How can I print something like this : ("Test : %d", 123)?
Bogy's answer is on the right track but I don't like the allocation in GetWC which looks like a memory leak, and it uses unsafe functions, try this:
void Debug::Log(const wchar_t *format, ...)
{
wchar_t buffer[BUFFER_LENGTH];
va_list args;
va_start(args, format);
_vsnwprintf_s(buffer, BUFFER_LENGTH, _TRUNCATE, format, args);
va_end(args);
buffer[BUFFER_LENGTH - 1] = '\0'; //prevent buffer overflow
OutputDebugString(buffer);
}
Usage like:
Debug::Log(L"Hello %S\n", "sailor");
I found this code in a C++ training on Pluralsight that I am using for Windows 8 (you just say TRACE("Test : %d", 123); to use it):
#pragma once
#include <assert.h>
#define ASSERT assert
#ifdef _DEBUG
inline auto Trace(wchar_t const * format, ...) -> void
{
va_list args;
va_start(args, format);
wchar_t buffer[256];
ASSERT(-1 != _vsnwprintf_s(buffer, _countof(buffer) - 1, format, args));
va_end(args);
OutputDebugString(buffer);
}
#endif
struct Tracer
{
char const * m_filename;
unsigned m_line;
Tracer(char const * filename, unsigned const line) :
m_filename{ filename },
m_line{ line }
{
}
template <typename... Args>
auto operator()(wchar_t const * format, Args... args) const -> void
{
wchar_t buffer[256];
auto count = swprintf_s(buffer, L"%S(%d): ", m_filename, m_line);
ASSERT(-1 != count);
ASSERT(-1 != _snwprintf_s(buffer + count, _countof(buffer) - count, _countof(buffer) - count - 1, format, args...));
OutputDebugString(buffer);
}
};
#ifdef _DEBUG
#define TRACE Tracer(__FILE__, __LINE__)
#else
#define TRACE __noop
#endif
I've finally found this solution (here and here) :
Debug.cpp:
#pragma once
#include "pch.h"
#define BUFFER_LENGTH 1024
//char* to wchar_t*
const wchar_t *GetWC(const char *c)
{
const size_t cSize = strlen(c) + 1;
wchar_t* wc = new wchar_t[cSize];
mbstowcs(wc, c, cSize);
return wc;
}
void Debug::Log(const char *format, ...)
{
char buffer[BUFFER_LENGTH];
va_list args;
va_start(args, format);
vsnprintf(buffer, BUFFER_LENGTH, format, args);
va_end(args);
buffer[BUFFER_LENGTH - 1] = '\0'; //prevent buffer overflow
OutputDebugString(GetWC(buffer));
}
Debug.h:
class Debug{
public:
static void Log(const char *format, ...);
};
We can use it like printf function:
Debug::Log("Test : %d", 123);

_vscwprintf on Mac OS X/Linux

I am porting an application on Mac OS X which was written for Windows.
In this application, there are many instances of _vscwprintf and _vscprintf.
This question helped me to implement _vsprintf on Mac OS X. But same technique for _vswprintf is not working.
Can anyone give the alternative of _vscwprintf on Mac OS X? Or there any equivalent method for this?
Microsoft describes the functions as returning the number of characters that would be used if the string were formatted — 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
);
Initial answer
These functions can, therefore, be emulated 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 the leading underscore; I would.
Note that the _vscwprintf() implementation above is flawed; see the code below.
vscprintf() and scprintf()
Apologies: I wrote vsprintf() where I needed to write vsnprintf() (now fixed in the code above); however, vswprintf() already has the safer interface with the buffer length, so there is no vsnwprintf(). There's a reason I prefer to test compile code before (or shortly after) posting it — it's been irksome not having the wherewithal to do so for a couple of days.
Here's an SSCCE for vscprintf() (and scprintf()):
#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 the vsnprintf() function. Specifically, vswprintf() reports an error if the formatted string won't fit in the formatted space, whereas vsnprintf() reports the number of characters that would have been needed in the buffer if it was going to fit. Hence, you have to work by trial and error:
#include <stdio.h>
#include <stdarg.h>
#include <wchar.h>
extern int vscwprintf(const wchar_t *format, va_list argptr);
extern int scwprintf(const wchar_t *format, ...);
int vscwprintf(const wchar_t *format, va_list argptr)
{
// Unlike vsnprintf(), vswprintf() does not tell you how many
// characters would have been written if there was space enough in
// the buffer - it just reports an error when there is not enough
// space. Assume a moderately large 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 GCC 4.7.3 (which was built on Mac OS X 10.7.5, but that shouldn't cause any problems).
I recommend using open_wmemstream instead. Something like that:
#include <stdio.h>
#include <string>
#include <stdarg.h>
using namespace std;
wstring wstring_format(const wchar_t* format, ...)
{
wchar_t* buf;
size_t size;
FILE* stream = open_wmemstream(&buf, &size);
va_list args;
va_start(args, format);
vfwprintf(stream, format, args);
va_end(args);
fclose(stream);
wstring result(buf, buf + size);
free(buf);
return result;
}

varargs to printf all arguments

if I have this function:
printAll(const char *message, ...)
{
va_list argptr = NULL;
va_start(argptr, message);
// todo: how to printf all the arguments in the message?
va_end(argptr);
}
Suppose I call the function like this:
printAll("My info: Value1 = %d, Value 2=%d", 1, 2);
In this line: // todo: how to printf all the arguments in the message?
How can I print them all in order to have:
My info: Value1 = 1, Value 2=2
You're looking for the vprintf() function which was designed to do exactly this:
vprintf(message, argptr);
The v*printf() family of functions work basically in the same way as their normal counterparts, except they take a va_list instead of varargs. They don't call va_end() for you, so the way you have it now is correct.
Here's a sketch of the general idea (though a finished version, has to deal with quite a few more things such as field width, precision, more conversions, etc.
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
int my_vfprintf(FILE *file, char const *fmt, va_list arg) {
int int_temp;
char char_temp;
char *string_temp;
char ch;
int length = 0;
char buffer[512];
while ( ch = *fmt++) {
if ( '%' == ch ) {
switch (ch = *fmt++) {
/* %% - print out a single % */
case '%':
fputc('%', file);
length++;
break;
/* %c: print out a character */
case 'c':
char_temp = va_arg(arg, int);
fputc(char_temp, file);
length++;
break;
/* %s: print out a string */
case 's':
string_temp = va_arg(arg, char *);
fputs(string_temp, file);
length += strlen(string_temp);
break;
/* %d: print out an int */
case 'd':
int_temp = va_arg(arg, int);
itoa(int_temp, buffer, 10);
fputs(buffer, file);
length += strlen(buffer);
break;
/* %x: print out an int in hex */
case 'x':
int_temp = va_arg(arg, int);
itoa(int_temp, buffer, 16);
fputs(buffer, file);
length += strlen(buffer);
break;
}
}
else {
putc(ch, file);
length++;
}
}
return length;
}
int my_printf(char const *fmt, ...) {
va_list arg;
int length;
va_start(arg, fmt);
length = my_vfprintf(stdout, fmt, arg);
va_end(arg);
return length;
}
int my_fprintf(FILE *file, char const *fmt, ...) {
va_list arg;
int length;
va_start(arg, fmt);
length = my_vfprintf(file, fmt, arg);
va_end(arg);
return length;
}
#ifdef TEST
int main() {
my_printf("%s", "Some string");
return 0;
}
#endif
Here is a printAll() that will do exactly as you would like...
#ifdef __cplusplus
#include <cstring>
#include <cstdio>
#include <cstdarg>
using namespace std;
#else
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#endif
void printAll(const char* fmt, ...) {
va_list ap;
va_start(ap, fmt);
const size_t MAX_LEN = 1023;
#ifdef __cplusplus
char msg[MAX_LEN+1] = {0};
#else
char msg[MAX_LEN+1];
for (int i = 0; i < MAX_LEN+1; i++)
{
msg[i] = 0;
}
#endif
vsnprintf(msg, MAX_LEN, fmt, ap);
printf("%s\n", msg);
va_end(ap);
}
int main() {
printAll("My info: Value1 = %d, Value 2=%d", 1, 2);
return 0;
}
My info: Value1 = 1, Value 2=2