basic implemtation general purpose clock and tinyprintf
[cortex-from-scratch] / include / lib / tinyprintf.h
1 /*
2 File: tinyprintf.h
3
4 Copyright (C) 2004  Kustaa Nyholm
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20 */
21
22 #ifndef __TFP_PRINTF__
23 #define __TFP_PRINTF__
24
25 #include <stdarg.h>
26
27 /* Global configuration */
28
29 /* Set this to 0 if you do not want to provide tfp_printf */
30 #ifndef TINYPRINTF_DEFINE_TFP_PRINTF
31 # define TINYPRINTF_DEFINE_TFP_PRINTF 1
32 #endif
33
34 /* Set this to 0 if you do not want to provide
35    tfp_sprintf/snprintf/vsprintf/vsnprintf */
36 #ifndef TINYPRINTF_DEFINE_TFP_SPRINTF
37 # define TINYPRINTF_DEFINE_TFP_SPRINTF 1
38 #endif
39
40 /* Set this to 0 if you do not want tfp_printf and
41    tfp_{vsn,sn,vs,s}printf to be also available as
42    printf/{vsn,sn,vs,s}printf */
43 #ifndef TINYPRINTF_OVERRIDE_LIBC
44 # define TINYPRINTF_OVERRIDE_LIBC 1
45 #endif
46
47 /* Optional external types dependencies */
48
49 #if TINYPRINTF_DEFINE_TFP_SPRINTF
50 # include <sys/types.h>  /* size_t */
51 #endif
52
53 /* Declarations */
54
55 #ifdef __GNUC__
56 # define _TFP_SPECIFY_PRINTF_FMT(fmt_idx,arg1_idx) \
57     __attribute__((format (printf, fmt_idx, arg1_idx)))
58 #else
59 # define _TFP_SPECIFY_PRINTF_FMT(fmt_idx,arg1_idx)
60 #endif
61
62 #ifdef  __cplusplus
63 extern "C" {
64 #endif
65
66 typedef void (*putcf) (void *, char);
67
68 /*
69    'tfp_format' really is the central function for all tinyprintf. For
70    each output character after formatting, the 'putf' callback is
71    called with 2 args:
72      - an arbitrary void* 'putp' param defined by the user and
73        passed unmodified from 'tfp_format',
74      - the character.
75    The 'tfp_printf' and 'tfp_sprintf' functions simply define their own
76    callback and pass to it the right 'putp' it is expecting.
77 */
78 void tfp_format(void *putp, putcf putf, const char *fmt, va_list va);
79
80 #if TINYPRINTF_DEFINE_TFP_SPRINTF
81 int tfp_vsnprintf(char *str, size_t size, const char *fmt, va_list ap);
82 int tfp_snprintf(char *str, size_t size, const char *fmt, ...) \
83      _TFP_SPECIFY_PRINTF_FMT(3, 4);
84 int tfp_vsprintf(char *str, const char *fmt, va_list ap);
85 int tfp_sprintf(char *str, const char *fmt, ...) \
86     _TFP_SPECIFY_PRINTF_FMT(2, 3);
87 # if TINYPRINTF_OVERRIDE_LIBC
88 #  define vsnprintf tfp_vsnprintf
89 #  define snprintf tfp_snprintf
90 #  define vsprintf tfp_vsprintf
91 #  define sprintf tfp_sprintf
92 # endif
93 #endif
94
95 #if TINYPRINTF_DEFINE_TFP_PRINTF
96 void init_printf(void *putp, putcf putf);
97 void tfp_printf(char *fmt, ...) _TFP_SPECIFY_PRINTF_FMT(1, 2);
98 # if TINYPRINTF_OVERRIDE_LIBC
99 #  define printf tfp_printf
100 # endif
101 #endif
102
103 #ifdef  __cplusplus
104 }
105 #endif
106
107 #endif