Dmitry wrote:
void uprintf(void (*func)(char c), const char *fmt,...);
...
How about changing func defenition to 'int', like:
int uprintf(int (*func)(char c), const char *fmt,...);
So, if func returns zero, printing continues, otherwise abort with error code
stated by 'func'.
What do you think?
or maybe the oter way round?
printf usualy returns the number of bytes printed, thats also a nice
feature if you need to do some basic text formating.
putchar should also return an int:
---
RETURNS
If successful, `putchar' returns its argument CH. If an error
intervenes, the result is `EOF'....
---
i think it would make sense to keep up with these definitions.
for the sake of backwards compatibility, you should not change uprintf.
it would brake old programs that pass a "void" function...
on the other hand its quite nice with IAR. you have a "printf" function.
it uses "putchar" for the output. the putchar function has to be
provided by the user. (the printf implementation can be chosen too, a
small version that only supports %d, %s but the bigger versions have
support for float and field width etc. 3 different implementations in total)
the ANSI C printf also copes with failures:
---
RETURNS
`sprintf' and `asprintf' return the number of bytes in the output
string, save that the concluding `NULL' is not counted. `printf' and
`fprintf' return the number of characters transmitted. If an error
occurs, `printf' and `fprintf' return `EOF' and `asprintf' returns -1.
No error returns occur for `sprintf'.
---
i would prefer somthing like that:
libc provides a medium sized printf (i.e. strings and ints, with field
width, such that "0x%04x" is supported) that works like to above
documented. it would use "putchar" for the output.
and important: it's implemented as weakref. that way we can provide
alternative printf implementations in a separate lib. such as
"libfloatprintf", "libsmallprintf" or such that you can easily change
printf implementation with a linker option ("-l floatprintf" in that
example)
that would be more portable to other platforms, calls to printf are
shorter than to uprintf, its more flexible and your "EOF" problem would
be solved automaticaly... what do you think?
chris