L
lithiumcat
Hi,
I was wondering whether there is a standard-compliant way to extend
printf with a new %code that is not yet reserved (e.g. %r) with a
given argument type.
My guess would be to write a function that looks like « int
my_printf(const char *fmt, ...); », with a special processing of fmt
to replace the new %code with the correct data, and then hand over the
processed fmt and the remaining arguments to vprintf.
However I don't know much about variable arguments, but the standards
seems pretty light about what can be done with them. So I have no idea
of how to write such a my_printf function.
Is there a protable way to do this within the C? If anybody knows,
although it's a bit off-topic, would POSIX allow it if standard C
doesn't? (btw, what is the topical newsgroup for such POSIX-related
questions?)
On a side note, although I'm interested in the answers to the
questions above for my general knowledge of C, the problem I'm trying
to solve is fprintf-ing and snprintf-ing a non-C string, represented
by a struct containing a char* (that is not zero-terminated) and a
size_t length. So I'm also interested in a solution for that
particular problem. For now I'm using a sequence of fprintf/fwrite/
fprintf/... or snprintf/memcpy/snprintf/... but that looks ugly
(especially with the size checks in the second case).
I was wondering whether there is a standard-compliant way to extend
printf with a new %code that is not yet reserved (e.g. %r) with a
given argument type.
My guess would be to write a function that looks like « int
my_printf(const char *fmt, ...); », with a special processing of fmt
to replace the new %code with the correct data, and then hand over the
processed fmt and the remaining arguments to vprintf.
However I don't know much about variable arguments, but the standards
seems pretty light about what can be done with them. So I have no idea
of how to write such a my_printf function.
Is there a protable way to do this within the C? If anybody knows,
although it's a bit off-topic, would POSIX allow it if standard C
doesn't? (btw, what is the topical newsgroup for such POSIX-related
questions?)
On a side note, although I'm interested in the answers to the
questions above for my general knowledge of C, the problem I'm trying
to solve is fprintf-ing and snprintf-ing a non-C string, represented
by a struct containing a char* (that is not zero-terminated) and a
size_t length. So I'm also interested in a solution for that
particular problem. For now I'm using a sequence of fprintf/fwrite/
fprintf/... or snprintf/memcpy/snprintf/... but that looks ugly
(especially with the size checks in the second case).