Added init function to appender. This fixes ir_snprintf, since the
[libfirm] / ir / ir / irprintf.h
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/irprintf.h
4  * Purpose:     A little printf understanding some firm types.
5  * Author:      Sebastian Hack
6  * Created:     29.11.2004
7  * CVS-ID:      $Id$
8  * Copyright:   (c) 1998-2004 Universität Karlsruhe
9  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
10  */
11
12 /**
13  * @file irprintf.h
14  *
15  * A little printf understanding some firm types.
16  * @author Sebastian Hack
17  * @date 29.11.2004
18  */
19
20 #ifndef _IRPRINTF_H
21 #define _IRPRINTF_H
22
23 #include <stddef.h>
24 #include <stdio.h>
25
26 /**
27  * Something that can append strings and chars to something.
28  */
29 typedef struct _appender_t {
30         void (*init)(void *object, size_t n);
31         void (*append_char)(void *object, size_t n, char ch);
32         void (*append_str)(void *object, size_t n, const char *str);
33 } appender_t;
34
35 /**
36  * A callback function type to add something to an appender.
37  *
38  * @param app    The appender.
39  * @param object The object for the appender.
40  * @param limit  The limit for the appender.
41  * @param arg    The thing to append.
42  */
43 typedef void (ir_printf_cb_t)(const appender_t *app, void *object, size_t limit, const void *arg);
44
45 /**
46  * A string formatting routine for ir objects.
47  * This function rudimentarily implements a kind of printf(3) for ir
48  * nodes. Following conversion specifiers. No length, special or field
49  * width specifiers are accepted.
50  * - @%p A pointer.
51  * - @%s A string.
52  * - @%I An ident.
53  * - @%e An entity name.
54  * - @%E An entity ld_name.
55  * - @%n A full description of a node.
56  * - @%O The opcode name of an ir node.
57  * - @%m The mode name of an ir mode.
58  * - @%N The node number of an ir node.
59  * - @%B The block node number of the nodes block.
60  * - @%b A bitset.
61  * - @%t A tarval.
62  *
63  * Each of these can be prepended by a '+' which means, that the given
64  * pointer is a collection of items specified by the format. In this
65  * case you also have to pass an iterator interface to ir_printf()
66  * suitable for the instance of the collection. So, imagine you have a
67  * @c pset of ir_nodes and want to dump it, you write:
68  * @code
69  *   pset *nodes;
70  *   ...
71  *   ir_printf("Some nodes: %+n\n", it_pset, nodes);
72  * @endcode
73  * The @c it_pset is an iterator interface (of type
74  * @c iterator_t that allows the dumper to traverse the set.
75  *
76  * As special case when working with collections, you can also give a
77  * callback function which will be invoked on each element in the
78  * collection. It gets the appender (the thing where the textual
79  * representation of the element is written to) and its parameters
80  * passed by the dumping function. Suppose you have your own datatype
81  * @c xyz_t and want to dump a pset of it, you have:
82  * @code
83  *   void xyz_dump(const appender_t *app, void *object, size_t limit,
84  *       const void *arg)
85  *   {
86  *     const xyz_t *xyz = arg;
87  *     app->append_str(object, limit, xyz->name);
88  *   }
89  *   ...
90  *   pset *xyzs;
91  *
92  *   ir_printf("A set of xyz\'s: %+C\n", it_pset, xyzs, xyz_dump);
93  * @endcode
94  *
95  * @param fmt The format string.
96  */
97 void ir_printf(const char *fmt, ...);
98
99 /**
100  * @see irn_printf.
101  */
102 void ir_fprintf(FILE *f, const char *fmt, ...);
103
104 /**
105  * @see irn_printf.
106  */
107 void ir_snprintf(char *buf, size_t n, const char *fmt, ...);
108
109
110 #endif