use "" include path inside the project
[libfirm] / ir / libcore / lc_printf.h
1 /*
2   libcore: library for basic data structures and algorithms.
3   Copyright (C) 2005  IPD Goos, Universit"at Karlsruhe, Germany
4
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, write to the Free Software
17   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20
21
22 /**
23  * Flexible printf().
24  * @author Sebastian Hack
25  * @date 3.1.2005
26  */
27
28 #ifndef _LIBCORE_XPRINTF_H
29 #define _LIBCORE_XPRINTF_H
30
31 #include <stddef.h>
32 #include <stdarg.h>
33 #include <stdio.h>
34
35 #include <obstack.h>
36
37 #include "lc_config.h"
38 #include "lc_appendable.h"
39
40 typedef struct _lc_arg_occ_t {
41         int width;                                                              /**< The width, or 0 if not given. */
42         int precision;                                          /**< The precision, or 0 if not given */
43
44         const char *modifier;                   /**< A string of of modifiers preceding the
45                                                                                                                         conversion specifier. Attention: This string is not
46                                                                                                                         zero terminated. Use @c modifier_length to get the
47                                                                                                                         number of valid chars in it. */
48         size_t modifier_length;         /**< The number of valid chars in @c modifier. */
49         char conversion;                                        /**< The conversion specifier. */
50         int lc_arg_type;                                                        /**< The type of the argument as determined by the
51                                                                                                                         @c get_lc_arg_type member function of the handler. */
52
53         unsigned flag_hash : 1;         /**< @c # flag was seen. */
54         unsigned flag_zero : 1;         /**< @c 0 flag was seen. */
55         unsigned flag_minus : 1;        /**< @c - flag was seen. */
56         unsigned flag_plus : 1;         /**< @c + flag was seen. */
57         unsigned flag_space : 1;        /**< A space flag was seen. */
58 } lc_arg_occ_t;
59
60 /**
61  * A value from the ... arguments of the printf function.
62  * Look at the file 'xprintf_lc_arg_types.def'. The second argument of the
63  * @c ARG_TYPE macro is the name of the union member preceded by $c v_
64  */
65 typedef union {
66 #define LC_ARG_TYPE(type,name) type v_ ## name;
67 #include "lc_printf_arg_types.def"
68 #undef LC_ARG_TYPE
69 } lc_arg_value_t;
70
71 enum {
72 #define LC_ARG_TYPE(type,name) lc_arg_type_ ## name,
73 #include "lc_printf_arg_types.def"
74 #undef LC_ARG_TYPE
75   lc_arg_type_last
76 };
77
78 typedef struct _lc_arg_handler {
79         int (*get_lc_arg_type)(const lc_arg_occ_t *occ);
80         int (*emit)(lc_appendable_t *app, const lc_arg_occ_t *occ, const lc_arg_value_t *arg);
81 } lc_arg_handler_t;
82
83 typedef struct _lc_arg_env_t lc_arg_env_t;
84
85 lc_arg_env_t *lc_arg_new_env(void);
86 void lc_arg_free_env(lc_arg_env_t *env);
87 lc_arg_env_t *lc_arg_get_default_env(void);
88
89 int lc_arg_register(lc_arg_env_t *env, const char *name, char letter, const lc_arg_handler_t *handler);
90 void lc_arg_unregister(lc_arg_env_t *env, const char *name);
91
92 lc_arg_env_t *lc_arg_add_std(lc_arg_env_t *env);
93
94 int lc_arg_append(lc_appendable_t *app, const lc_arg_occ_t *occ, const char *str, size_t len);
95
96 int lc_epprintf(const lc_arg_env_t *env, lc_appendable_t *app, const char *fmt, ...);
97 int lc_evpprintf(const lc_arg_env_t *env, lc_appendable_t *app, const char *fmt, va_list args);
98 int lc_pprintf(lc_appendable_t *app, const char *fmt, ...);
99 int lc_vpprintf(lc_appendable_t *app, const char *fmt, va_list args);
100
101 int lc_eprintf(const lc_arg_env_t *env, const char *fmt, ...);
102 int lc_esnprintf(const lc_arg_env_t *env, char *buf, size_t len, const char *fmt, ...);
103 int lc_efprintf(const lc_arg_env_t *env, FILE *file, const char *fmt, ...);
104 int lc_eoprintf(const lc_arg_env_t *env, struct obstack *obst, const char *fmt, ...);
105
106 int lc_evprintf(const lc_arg_env_t *env, const char *fmt, va_list args);
107 int lc_evsnprintf(const lc_arg_env_t *env, char *buf, size_t len, const char *fmt, va_list args);
108 int lc_evfprintf(const lc_arg_env_t *env, FILE *f, const char *fmt, va_list args);
109 int lc_evoprintf(const lc_arg_env_t *env, struct obstack *obst, const char *fmt, va_list args);
110
111 int lc_printf(const char *fmt, ...);
112 int lc_snprintf(char *buf, size_t len, const char *fmt, ...);
113 int lc_fprintf(FILE *f, const char *fmt, ...);
114 int lc_oprintf(struct obstack *obst, const char *fmt, ...);
115
116 int lc_vprintf(const char *fmt, va_list args);
117 int lc_vsnprintf(char *buf, size_t len, const char *fmt, va_list args);
118 int lc_vfprintf(FILE *f, const char *fmt, va_list args);
119 int lc_voprintf(struct obstack *obst, const char *fmt, va_list args);
120
121 #endif