Back to index

glibc  2.9
rprintf.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <printf.h>
00004 
00005 /*@group*/
00006 typedef struct
00007 {
00008   char *name;
00009 }
00010 Widget;
00011 /*@end group*/
00012 
00013 int
00014 print_widget (FILE *stream,
00015              const struct printf_info *info,
00016              const void *const *args)
00017 {
00018   const Widget *w;
00019   char *buffer;
00020   int len;
00021 
00022   /* Format the output into a string. */
00023   w = *((const Widget **) (args[0]));
00024   len = asprintf (&buffer, "<Widget %p: %s>", w, w->name);
00025   if (len == -1)
00026     return -1;
00027 
00028   /* Pad to the minimum field width and print to the stream. */
00029   len = fprintf (stream, "%*s",
00030                (info->left ? -info->width : info->width),
00031                buffer);
00032 
00033   /* Clean up and return. */
00034   free (buffer);
00035   return len;
00036 }
00037 
00038 
00039 int
00040 print_widget_arginfo (const struct printf_info *info, size_t n,
00041                       int *argtypes)
00042 {
00043   /* We always take exactly one argument and this is a pointer to the
00044      structure..  */
00045   if (n > 0)
00046     argtypes[0] = PA_POINTER;
00047   return 1;
00048 }
00049 
00050 
00051 int
00052 main (void)
00053 {
00054   /* Make a widget to print. */
00055   Widget mywidget;
00056   mywidget.name = "mywidget";
00057 
00058   /* Register the print function for widgets. */
00059   register_printf_function ('W', print_widget, print_widget_arginfo);
00060 
00061   /* Now print the widget. */
00062   printf ("|%W|\n", &mywidget);
00063   printf ("|%35W|\n", &mywidget);
00064   printf ("|%-35W|\n", &mywidget);
00065 
00066   return 0;
00067 }