5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2007 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
20 /****h* silcutil/Snprintf
24 * Platform independent version of snprintf and other similar string
25 * formatting routines.
29 #ifndef SILCSNPRINTF_H
30 #define SILCSNPRINTF_H
32 /****f* silcutil/SilcSnprintf/SilcSnprintfRender
36 * typedef char *(*SilcSnprintfRender)(void *data);
40 * Snprintf rendering function. This function can be used with '%@'
41 * formatting character. The `data' is rendered into a string and
42 * allocated string is returned. If NULL is returned the rendering
43 * is skipped and ignored. If the returned string does not fit to
44 * the destination buffer it may be truncated.
48 * char *id_render(void *data)
54 * // Call id_render function to render the 'client_id'.
55 * silc_snprintf(buf, sizeof(buf), "Client ID %@", id_render, client_id);
58 typedef char *(*SilcSnprintfRender)(void *data);
60 /****f* silcutil/SilcSnprintf/silc_snprintf
64 * int silc_snprintf(char *str, size_t count, const char *fmt, ...);
68 * Outputs string into `str' of maximum of size `count' including the
69 * trailing '\0' according to the `fmt'. The `fmt' is equivalent to
70 * snprintf(3) and printf(3) formatting. Returns the number of character
71 * in `str' or negative value on error.
73 * This also supports '%@' formatting to render data and structures
74 * using SilcSnprintfRender.
77 int silc_snprintf(char *str, size_t count, const char *fmt, ...);
79 /****f* silcutil/SilcSnprintf/silc_vsnprintf
83 * int silc_vsnprintf(char *str, size_t count, const char *fmt,
88 * Same as silc_snprintf but takes the argument for the formatting from
89 * the `args' variable argument list.
92 int silc_vsnprintf(char *str, size_t count, const char *fmt, va_list args);
94 /****f* silcutil/SilcSnprintf/silc_asprintf
98 * int silc_asprintf(char **ptr, const char *format, ...)
102 * Same as silc_snprintf but allocates a string large enough to hold the
103 * output including the trailing '\0'. The caller must free the `ptr'.
106 int silc_asprintf(char **ptr, const char *format, ...);
108 /****f* silcutil/SilcSnprintf/silc_vasprintf
112 * int silc_vasprintf(char **ptr, const char *format, va_list ap)
116 * Same as silc_asprintf but takes the argument from the `ap' variable
120 int silc_vasprintf(char **ptr, const char *format, va_list ap);
122 #endif /* SILCSNPRINTF_H */