varargs to printf all arguments - c++

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

Related

How do I modify strcat so that it wouldn't edit str1

Why does strcat gives me its version of str1? As far as I know there has to be & thing before paramatater in function prototype and implementation if you want to get it editted, but I don't see it here.
char *strcat( char *str1, const char *str2 );
How do I edit this function so that it would only return new string but leave out the ones I give it?
My try
char *strApp(char *dest, const char *src)
{
size_t i,j;
size_t k = 0;
for (i = 0; dest[i] != '\0'; i++);
char rdest[100];
do {
rdest[k] = dest[k];
} while(++k<=i);
for (j = 0; src[j] != '\0'; j++)
rdest[i+j] = src[j];
rdest[i+j] = '\0';
return rdest;
}
It damages second string. Could anyone give me safe and correct version? Thanks in advance.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *strApp(const char *s1, const char *s2)
{
char *pointer = malloc(strlen(s1) + strlen(s2) + 1);
if (pointer == NULL)
{
perror("failed to allocate memory");
exit(EXIT_FAILURE);
}
return strcat(strcpy(pointer, s1), s2);
}
int main()
{
char *s1 = "original";
char *s2 = " modified";
char *s3 = strApp(s1, s2);
printf("%s\n", s1);
printf("%s\n", s2);
printf("%s\n", s3);
free(s3);
return 0;
}
Just trying to point out you don't need to completely rewrite strcat() to get what you want.
strcat is, by definition, altering the target. If you don't want to, you should make a copy yourself in a target memory location you allocate yourself.
You've tagged your question with both C and C++. I'm providing a C solution. Adjustments may be needed for C++.
#include <stdlib.h>
#include <string.h>
char* strdupcat(const char* s1, const char* s2) {
size_t s1_len = strlen(s1);
size_t s2_len = strlen(s2);
char* s = malloc(s1_len + s2_len + 1);
if (s == NULL)
return NULL;
{
char* s_end = s;
s_end = mempcpy(s_end, s1, s1_len);
s_end = mempcpy(s_end, s2, s2_len);
*s_end = '\0';
}
return s;
}
Example usage:
#include <stdio.h>
int main() {
char* s = strdupcat("abc", "def");
if (s == NULL) {
perror("Can't concatenate");
return EXIT_FAILURE;
}
puts(s);
free(s);
return EXIT_SUCCESS;
}
This function is used similarly to strdup.
DESCRIPTION
The strdupcat() function returns a pointer to a new string which is a duplicate of the string s1 with a duplicate of string s2 appended. Memory for the new string is obtained with malloc(3), and can be freed with free(3).
RETURN VALUE
The strdupcat() function returns a pointer to the duplicated string, or NULL if insufficient memory was available.
ERRORS
ENOMEM Insufficient memory available to allocate the new string.
You can use strerror or perror to obtain an error message when strdupcat() returns NULL.
Here's a version that accepts an arbitrary number of arguments:
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
char* strdupcatmany(int dummy, ...) {
#define strdupcatmany(...) strdupcatmany(0, __VA_ARGS__, NULL)
size_t len = 0;
char* s;
char* s_dst;
const char* s_src;
va_list ap;
va_start(ap, dummy);
while (1) {
s_src = va_arg(ap, const char*);
if (s_src == NULL)
break;
len += strlen(s_src);
}
va_end(ap);
s = malloc(len + 1);
if (s == NULL)
return NULL;
s_dst = s;
va_start(ap, dummy);
while (1) {
s_src = va_arg(ap, const char*);
if (s_src == NULL)
break;
s_dst = stpcpy(s_dst, s_src);
}
va_end(ap);
*s_dst = '\0';
return s;
}
For example,
#include <stdio.h>
int main() {
char* s = strdupcatmany("abc", "def", "ghi");
if (s == NULL) {
perror("Can't concatenate");
return EXIT_FAILURE;
}
puts(s);
free(s);
return EXIT_SUCCESS;
}
Note: I don't know how portable __VA_ARGS__ args is.

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;
}

I've tried to wrap vsnwprintf in another function with strange results, it seems it give off its address instead of actual value

code :
#include <stdarg.h>
#include <string>
#include <iostream>
std::wstring string_format(const std::wstring &fmt, ...) {
int size = 100;
std::wstring str;
va_list ap;
while (1) {
str.resize(size);
va_start(ap, fmt);
int n = vsnwprintf((wchar_t *)str.c_str(), size, fmt.c_str(), ap);
va_end(ap);
if (n > -1 && n < size) {
str.resize(n);
return str;
}
if (n > -1) size = n + 1;
else size *= 2;
}
return str;
}
std::wstring printf_wrapper(std::wstring text, ...) {
using namespace std;
va_list args;
va_start(args, text);
wstring formatted_text = string_format(text, args);
va_end (args);
return formatted_text;
}
int main(int argc, char *argv[])
{
using namespace std;
wcout << printf_wrapper(L"example%d",1) << endl;
return 0;
}
and it returns : example2293384
the function is from here: https://stackoverflow.com/a/8098080/393087
origin of this code is from the documentation of vsnprintf: http://www.tin.org/bin/man.cgi?section=3&topic=vsnprintf
You cannot "forward" varargs like that. So string_format needs to take a va_list as its 2nd argument, not ....

What is wrong with this recursive va_arg code?

I'm trying to make a generic function taking a variable argument list. A part of the design is that some of these functions call each other. Unfortunately it doesn't seem to work. As you can see if you run the simple code below, the call to command() always fails, but the direct call to marshal_size() succeeds in decoding the two strings "FIRST_STR_ARG" and "SECOND_STR_ARG" according to the format string "FORMAT_STRING".
What is wrong in my reasoning?
The sample code compiles equally well with "g++ main.cpp" or "gcc main.c".
Thanks,
jules
#include <stdarg.h>
#include <stdio.h>
#include <inttypes.h>
#include <string.h>
#include <stdlib.h>
#define MARSHAL_FORMAT "%s%s"
#define FIRST_STR_ARG "THIS_IS_ARG_ONE"
#define SECOND_STR_ARG "THIS_IS_ARG_TWO"
#define d(msg__, ...) do { printf("%s#%d: "msg__"\n", __FILE__, __LINE__, ## __VA_ARGS__); } while (0)
static uint32_t
marshal_size(const char *format, ...)
{
uint32_t retv = 0;
uint8_t ub;
uint16_t uw;
uint32_t ul;
char *s;
va_list ap;
if (!format || !strlen(format))
return 0;
d("format = %s \n", format);
va_start(ap, format);
for (; '\0' != *format; format++) {
d("*format = %c \n", *format);
if ('%' == *format) {
format++;
if ('u' == *format)
format++;
} else {
d("FORMAT ERROR\n");
continue;
}
d("*format = %c \n", *format);
switch (*format) {
case 's':
s = va_arg(ap, char*);
d("va_arg = %s\n", (s ? s : "NULL"));
if (s)
retv += strlen(s) + 1;
break;
case 'l':
ul = va_arg(ap, uint32_t);
retv += sizeof(uint32_t);
break;
case 'w':
uw = (uint16_t)va_arg(ap, int);
retv += sizeof(uint16_t);
break;
case 'b':
ub = (uint8_t)va_arg(ap, int);
retv += sizeof(uint8_t);
break;
default:
goto exit;
}
continue;
exit:
break;
}
va_end(ap);
return retv;
}
static uint32_t
command(const char * const format,
...)
{
uint32_t retv;
va_list ap;
va_start(ap, format);
retv = marshal_size(format, ap);
va_end(ap);
return retv;
}
int
main(int argc, char *argv)
{
uint32_t size;
size = command(MARSHAL_FORMAT, FIRST_STR_ARG, SECOND_STR_ARG);
d("size = %d", size);
size = marshal_size(MARSHAL_FORMAT, FIRST_STR_ARG, SECOND_STR_ARG);
d("size = %d", size);
return EXIT_SUCCESS;
}
You have to make marshal_size take a va_list rather than .... See Question 15.12 in the FAQ at c-faq.com.
I don't think you can pass a va_list to a function that takes variable arguments. I believe you need another version of marshal_size that takes a va_list as a hard parameter.
I'm a little rusty on the details of how this all works underneath. So I won't attempt an explanation. But as support, I'd suggest this is why we have the vprintf, vfprintf, and vsprintf versions of such functions.
Most likely you could make marshal_size(const char *format, ...) call the marshal_size(const char *format, va_list arg_ptr) to actually perform its functionality, thus not having to duplicate any code. Then command could call the va_list version as well, and everything should work.
The return value of va_start (which is actually a macro) is a pointer to an element on the stack, so the address of something on the stack. If you pass this as argument to another function, you don't pass the stack value, but the pointer to the value on the stack.
To get this working, the command function should use va_arg to get the actual stack value and pass this to the marshal_size function, however this cannot be done if you don't know the type of the value on the stack. In that case, pass ap as va_list argument and change the argument of marshall_size to get a va_list argument and continue the stack value processing there.
As the other answers point out, if you want to "pass through" variable arguments from one function to another, you need to write your function to take an explicit va_list argument. This is quite easy: you just need to rewrite your marshal_size() function as vmarshal_size() that takes a va_list, then turn marshal_size() itself into a wrapper:
static uint32_t
marshal_size(const char *format, ...)
{
va_list ap;
uint32_t retval;
va_start(ap, format);
retval = vmarshal_size(format, ap);
va_end(ap);
return retval;
}
static uint32_t
vmarshal_size(const char *format, va_list ap)
{
uint32_t retv = 0;
uint8_t ub;
uint16_t uw;
uint32_t ul;
char *s;
if (!format || !strlen(format))
return 0;
d("format = %s \n", format);
/* No va_start() */
for (; '\0' != *format; format++) {
d("*format = %c \n", *format);
if ('%' == *format) {
format++;
if ('u' == *format)
format++;
} else {
d("FORMAT ERROR\n");
continue;
}
d("*format = %c \n", *format);
switch (*format) {
case 's':
s = va_arg(ap, char*);
d("va_arg = %s\n", (s ? s : "NULL"));
if (s)
retv += strlen(s) + 1;
break;
case 'l':
ul = va_arg(ap, uint32_t);
retv += sizeof(uint32_t);
break;
case 'w':
uw = (uint16_t)va_arg(ap, int);
retv += sizeof(uint16_t);
break;
case 'b':
ub = (uint8_t)va_arg(ap, int);
retv += sizeof(uint8_t);
break;
default:
goto exit;
}
continue;
exit:
break;
}
/* No va_end() */
return retv;
}
Your command() function can then call vmarshal_size() in the same way (although I note that your command() function is exactly the same as the new marshal_size(), presumably you actually want to do something different there).