T
Tomás Ó hÉilidhe
Firstly... before the guns go off... I realise that the C Standard
doesn't mention anything about the existence of colour, which is why
I'm writing a small little cross-platform library for setting the
console text colour. It'll have a function like as follows:
void SetCon(char const background,char const foreground)
{
/* Platform-specific code in here for setting
the console text and background colour */
}
OK, now onto the good stuff. What I want to do is extend the
functionality of printf so that you can set colours. For instance,
let's say you wanted "Hello World", with the Hello in red and the
World in yellow, well you would do the following:
cprintf("^brHello^byWorld);
The ^br means "black background, red foreground".
The ^by means "black background, yellow foreground".
The trick is though, that I want this to be usable just like printf,
so you might have:
cprintf("^brMy dog is ^by%u ^bryears old", some_unsigned_variable);
I aim to achieve this by using variable-length argument lists in
conjunction with vprintf.
My first crack at it is as follows, but it segfaults on my Linux
machine here:
#define SPECIAL_CHAR ((char)'^')
#include <stdarg.h>
#include <stdio.h>
void SetCon(char const background,char const foreground)
{
/* Platform-specific code in here for setting
the console text and background colour */
}
void cfprintf(FILE *out,char *str,...)
{
va_list args;
char *p;
va_start(args,str);
for (p = str; *p; ++p)
{
if (*p == SPECIAL_CHAR)
{
*p = 0;
vfprintf(out,str,args);
SetCon(p[1],p[2]); /* My cross-platform function */
p += 2;
str = p+1;
}
}
if (p != str) /* No point in sending a null string to vfprintf */
{
vfprintf(out,str,args);
}
va_end(args);
}
int main(void)
{
cfprintf(stdout,"blah^brblah");
return 0;
}
OK so I have a "va_list" object and I pass it to vprintf. As you can
see from my code, I was under the assumption that vprintf would
increment through the arguments, and that these increments would be
evident in my own function (i.e. I wouldn't have to call va_arg
redundantly to step through the arguments that have already been dealt
with).
Is my assumption wrong, are the increments performed by vprintf not
observed in my own function? (I want to ask before I go through the
pain in the ass procedure of doing all the "va_arg" calls for every
kind of type).
doesn't mention anything about the existence of colour, which is why
I'm writing a small little cross-platform library for setting the
console text colour. It'll have a function like as follows:
void SetCon(char const background,char const foreground)
{
/* Platform-specific code in here for setting
the console text and background colour */
}
OK, now onto the good stuff. What I want to do is extend the
functionality of printf so that you can set colours. For instance,
let's say you wanted "Hello World", with the Hello in red and the
World in yellow, well you would do the following:
cprintf("^brHello^byWorld);
The ^br means "black background, red foreground".
The ^by means "black background, yellow foreground".
The trick is though, that I want this to be usable just like printf,
so you might have:
cprintf("^brMy dog is ^by%u ^bryears old", some_unsigned_variable);
I aim to achieve this by using variable-length argument lists in
conjunction with vprintf.
My first crack at it is as follows, but it segfaults on my Linux
machine here:
#define SPECIAL_CHAR ((char)'^')
#include <stdarg.h>
#include <stdio.h>
void SetCon(char const background,char const foreground)
{
/* Platform-specific code in here for setting
the console text and background colour */
}
void cfprintf(FILE *out,char *str,...)
{
va_list args;
char *p;
va_start(args,str);
for (p = str; *p; ++p)
{
if (*p == SPECIAL_CHAR)
{
*p = 0;
vfprintf(out,str,args);
SetCon(p[1],p[2]); /* My cross-platform function */
p += 2;
str = p+1;
}
}
if (p != str) /* No point in sending a null string to vfprintf */
{
vfprintf(out,str,args);
}
va_end(args);
}
int main(void)
{
cfprintf(stdout,"blah^brblah");
return 0;
}
OK so I have a "va_list" object and I pass it to vprintf. As you can
see from my code, I was under the assumption that vprintf would
increment through the arguments, and that these increments would be
evident in my own function (i.e. I wouldn't have to call va_arg
redundantly to step through the arguments that have already been dealt
with).
Is my assumption wrong, are the increments performed by vprintf not
observed in my own function? (I want to ask before I go through the
pain in the ass procedure of doing all the "va_arg" calls for every
kind of type).