|
0
|
1 /**
|
|
|
2 * "Portable" printf implementation, with some extras.
|
|
|
3 *
|
|
|
4 * Copyright (c) Paper 2025
|
|
|
5 *
|
|
|
6 * Permission to use, copy, modify, and/or distribute this software for any
|
|
|
7 * purpose with or without fee is hereby granted.
|
|
|
8 *
|
|
|
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
|
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
11 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
|
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
|
14 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
15 * PERFORMANCE OF THIS SOFTWARE.
|
|
|
16 *
|
|
|
17 * NOTE: Floating point support is very scuffed, and doesn't
|
|
|
18 * work for many numbers. It also depends on math.h. If you don't
|
|
|
19 * care or don't want floating point support, toggle the #define
|
|
|
20 * to disable it.
|
|
|
21 *
|
|
|
22 * EXTRA NOTE: You don't need malloc() if you turn off wide character
|
|
|
23 * support :)
|
|
|
24 **/
|
|
|
25
|
|
|
26 #ifndef MY_PRINTF_H_
|
|
|
27 #define MY_PRINTF_H_
|
|
|
28
|
|
|
29 #include <stdarg.h>
|
|
|
30 #include <stddef.h>
|
|
|
31 #include <stdio.h>
|
|
|
32 #include <limits.h>
|
|
|
33
|
|
|
34 /* XXX maybe we shouldn't use int as a return type. */
|
|
|
35
|
|
|
36 enum {
|
|
|
37 MY_ENOMEM = -1, /* Out of memory */
|
|
|
38 MY_EINVAL = -2, /* Invalid format string */
|
|
|
39 MY_EENCOD = -3, /* Invalid wide character */
|
|
|
40 MY_EOVERFLOW = -4, /* Return value overflows */
|
|
|
41 };
|
|
|
42
|
|
|
43 /* Get string representation of error */
|
|
|
44 const char *my_strerror(int err);
|
|
|
45
|
|
|
46 /* Free dynamically allocated memory */
|
|
|
47 void my_free(void *x);
|
|
|
48
|
|
|
49 /* ------ Actual printf stuff */
|
|
|
50
|
|
|
51 /* put function spec */
|
|
|
52 typedef void (*put_spec)(void *opaque, unsigned char c);
|
|
|
53
|
|
|
54 /* iprintf: my invention. it's basically the function that actually
|
|
|
55 * implements everything :) */
|
|
|
56 int my_viprintf(put_spec put, void *opaque, const char *format, va_list ap);
|
|
|
57 int my_iprintf(put_spec put, void *opaque, const char *format, ...);
|
|
|
58
|
|
|
59 /* Equivalent to C99 [v]snprintf */
|
|
|
60 int my_vsnprintf(char *s, size_t n, const char *format, va_list ap);
|
|
|
61 int my_snprintf(char *s, size_t n, const char *format, ...);
|
|
|
62
|
|
|
63 /* Puts an allocated printf'd string into '*ps'. Use my_free to deallocate. */
|
|
|
64 int my_vasprintf(char **ps, const char *format, va_list ap);
|
|
|
65 int my_asprintf(char **ps, const char *format, ...);
|
|
|
66
|
|
|
67 int my_vfprintf(FILE *f, const char *fmt, va_list ap);
|
|
|
68 int my_fprintf(FILE *f, const char *fmt, ...);
|
|
|
69
|
|
|
70 int my_vprintf(const char *fmt, va_list ap);
|
|
|
71 int my_printf(const char *fmt, ...);
|
|
|
72
|
|
|
73 #endif |