Blame manual/examples/rprintf.c

Packit 6c4009
/* Printf Extension Example
Packit 6c4009
   Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
Packit 6c4009
   This program is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU General Public License
Packit 6c4009
   as published by the Free Software Foundation; either version 2
Packit 6c4009
   of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   This program is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 6c4009
   GNU General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU General Public License
Packit 6c4009
   along with this program; if not, if not, see <http://www.gnu.org/licenses/>.
Packit 6c4009
*/
Packit 6c4009
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <printf.h>
Packit 6c4009
Packit 6c4009
/*@group*/
Packit 6c4009
typedef struct
Packit 6c4009
{
Packit 6c4009
  char *name;
Packit 6c4009
}
Packit 6c4009
Widget;
Packit 6c4009
/*@end group*/
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
print_widget (FILE *stream,
Packit 6c4009
	      const struct printf_info *info,
Packit 6c4009
	      const void *const *args)
Packit 6c4009
{
Packit 6c4009
  const Widget *w;
Packit 6c4009
  char *buffer;
Packit 6c4009
  int len;
Packit 6c4009
Packit 6c4009
  /* Format the output into a string. */
Packit 6c4009
  w = *((const Widget **) (args[0]));
Packit 6c4009
  len = asprintf (&buffer, "<Widget %p: %s>", w, w->name);
Packit 6c4009
  if (len == -1)
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  /* Pad to the minimum field width and print to the stream. */
Packit 6c4009
  len = fprintf (stream, "%*s",
Packit 6c4009
		 (info->left ? -info->width : info->width),
Packit 6c4009
		 buffer);
Packit 6c4009
Packit 6c4009
  /* Clean up and return. */
Packit 6c4009
  free (buffer);
Packit 6c4009
  return len;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
print_widget_arginfo (const struct printf_info *info, size_t n,
Packit 6c4009
                      int *argtypes)
Packit 6c4009
{
Packit 6c4009
  /* We always take exactly one argument and this is a pointer to the
Packit 6c4009
     structure..  */
Packit 6c4009
  if (n > 0)
Packit 6c4009
    argtypes[0] = PA_POINTER;
Packit 6c4009
  return 1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (void)
Packit 6c4009
{
Packit 6c4009
  /* Make a widget to print. */
Packit 6c4009
  Widget mywidget;
Packit 6c4009
  mywidget.name = "mywidget";
Packit 6c4009
Packit 6c4009
  /* Register the print function for widgets. */
Packit 6c4009
  register_printf_function ('W', print_widget, print_widget_arginfo);
Packit 6c4009
Packit 6c4009
  /* Now print the widget. */
Packit 6c4009
  printf ("|%W|\n", &mywidget);
Packit 6c4009
  printf ("|%35W|\n", &mywidget);
Packit 6c4009
  printf ("|%-35W|\n", &mywidget);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}