| <html lang="en"> |
| <head> |
| <title>Printf Extension Example - The GNU C Library</title> |
| <meta http-equiv="Content-Type" content="text/html"> |
| <meta name="description" content="The GNU C Library"> |
| <meta name="generator" content="makeinfo 4.13"> |
| <link title="Top" rel="start" href="index.html#Top"> |
| <link rel="up" href="Customizing-Printf.html#Customizing-Printf" title="Customizing Printf"> |
| <link rel="prev" href="Defining-the-Output-Handler.html#Defining-the-Output-Handler" title="Defining the Output Handler"> |
| <link rel="next" href="Predefined-Printf-Handlers.html#Predefined-Printf-Handlers" title="Predefined Printf Handlers"> |
| <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> |
| <!-- |
| This file documents the GNU C library. |
| |
| This is Edition 0.12, last updated 2007-10-27, |
| of `The GNU C Library Reference Manual', for version |
| 2.8 (Sourcery G++ Lite 2011.03-41). |
| |
| Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002, |
| 2003, 2007, 2008, 2010 Free Software Foundation, Inc. |
| |
| Permission is granted to copy, distribute and/or modify this document |
| under the terms of the GNU Free Documentation License, Version 1.3 or |
| any later version published by the Free Software Foundation; with the |
| Invariant Sections being ``Free Software Needs Free Documentation'' |
| and ``GNU Lesser General Public License'', the Front-Cover texts being |
| ``A GNU Manual'', and with the Back-Cover Texts as in (a) below. A |
| copy of the license is included in the section entitled "GNU Free |
| Documentation License". |
| |
| (a) The FSF's Back-Cover Text is: ``You have the freedom to |
| copy and modify this GNU manual. Buying copies from the FSF |
| supports it in developing GNU and promoting software freedom.''--> |
| <meta http-equiv="Content-Style-Type" content="text/css"> |
| <style type="text/css"><!-- |
| pre.display { font-family:inherit } |
| pre.format { font-family:inherit } |
| pre.smalldisplay { font-family:inherit; font-size:smaller } |
| pre.smallformat { font-family:inherit; font-size:smaller } |
| pre.smallexample { font-size:smaller } |
| pre.smalllisp { font-size:smaller } |
| span.sc { font-variant:small-caps } |
| span.roman { font-family:serif; font-weight:normal; } |
| span.sansserif { font-family:sans-serif; font-weight:normal; } |
| --></style> |
| <link rel="stylesheet" type="text/css" href="../cs.css"> |
| </head> |
| <body> |
| <div class="node"> |
| <a name="Printf-Extension-Example"></a> |
| <p> |
| Next: <a rel="next" accesskey="n" href="Predefined-Printf-Handlers.html#Predefined-Printf-Handlers">Predefined Printf Handlers</a>, |
| Previous: <a rel="previous" accesskey="p" href="Defining-the-Output-Handler.html#Defining-the-Output-Handler">Defining the Output Handler</a>, |
| Up: <a rel="up" accesskey="u" href="Customizing-Printf.html#Customizing-Printf">Customizing Printf</a> |
| <hr> |
| </div> |
| |
| <h4 class="subsection">12.13.4 <code>printf</code> Extension Example</h4> |
| |
| <p>Here is an example showing how to define a <code>printf</code> handler function. |
| This program defines a data structure called a <code>Widget</code> and |
| defines the ‘<samp><span class="samp">%W</span></samp>’ conversion to print information about <code>Widget *</code><!-- /@w --> |
| arguments, including the pointer value and the name stored in the data |
| structure. The ‘<samp><span class="samp">%W</span></samp>’ conversion supports the minimum field width and |
| left-justification options, but ignores everything else. |
| |
| <pre class="smallexample"> #include <stdio.h> |
| #include <stdlib.h> |
| #include <printf.h> |
| |
| typedef struct |
| { |
| char *name; |
| } |
| Widget; |
| |
| int |
| print_widget (FILE *stream, |
| const struct printf_info *info, |
| const void *const *args) |
| { |
| const Widget *w; |
| char *buffer; |
| int len; |
| |
| /* <span class="roman">Format the output into a string.</span> */ |
| w = *((const Widget **) (args[0])); |
| len = asprintf (&buffer, "<Widget %p: %s>", w, w->name); |
| if (len == -1) |
| return -1; |
| |
| /* <span class="roman">Pad to the minimum field width and print to the stream.</span> */ |
| len = fprintf (stream, "%*s", |
| (info->left ? -info->width : info->width), |
| buffer); |
| |
| /* <span class="roman">Clean up and return.</span> */ |
| free (buffer); |
| return len; |
| } |
| |
| |
| int |
| print_widget_arginfo (const struct printf_info *info, size_t n, |
| int *argtypes) |
| { |
| /* <span class="roman">We always take exactly one argument and this is a pointer to the |
| structure..</span> */ |
| if (n > 0) |
| argtypes[0] = PA_POINTER; |
| return 1; |
| } |
| |
| |
| int |
| main (void) |
| { |
| /* <span class="roman">Make a widget to print.</span> */ |
| Widget mywidget; |
| mywidget.name = "mywidget"; |
| |
| /* <span class="roman">Register the print function for widgets.</span> */ |
| register_printf_function ('W', print_widget, print_widget_arginfo); |
| |
| /* <span class="roman">Now print the widget.</span> */ |
| printf ("|%W|\n", &mywidget); |
| printf ("|%35W|\n", &mywidget); |
| printf ("|%-35W|\n", &mywidget); |
| |
| return 0; |
| } |
| </pre> |
| <p>The output produced by this program looks like: |
| |
| <pre class="smallexample"> |<Widget 0xffeffb7c: mywidget>| |
| | <Widget 0xffeffb7c: mywidget>| |
| |<Widget 0xffeffb7c: mywidget> | |
| </pre> |
| </body></html> |
| |