2 libcore: library for basic data structures and algorithms.
3 Copyright (C) 2005 IPD Goos, Universit"at Karlsruhe, Germany
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.
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.
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
22 * A customizable printf clone.
23 * @author Sebastian Hack
35 #include "lc_common_t.h"
37 #include "lc_printf.h"
38 #include "lc_defines.h"
42 /* printf implementation */
44 typedef struct _lc_arg_t {
45 struct _lc_arg_t *next;
49 const lc_arg_handler_t *handler;
52 struct _lc_arg_env_t {
53 set *args; /**< Map for named arguments. */
54 lc_arg_t *lower[26]; /**< Map for lower conversion specifiers. */
55 lc_arg_t *upper[26]; /**< Map for upper conversion specifiers. */
58 /** The default argument environment. */
59 static lc_arg_env_t *default_env = NULL;
61 static INLINE lc_arg_env_t *_lc_arg_get_default_env(void)
64 default_env = lc_arg_add_std(lc_arg_new_env());
69 lc_arg_env_t *lc_arg_get_default_env(void)
71 return _lc_arg_get_default_env();
74 static int lc_arg_cmp(const void *p1, const void *p2, UNUSED(size_t size))
76 const lc_arg_t *a1 = p1;
77 const lc_arg_t *a2 = p2;
78 return strcmp(a1->name, a2->name);
82 lc_arg_env_t *lc_arg_new_env(void)
84 lc_arg_env_t *env = XMALLOCZ(lc_arg_env_t);
85 env->args = new_set(lc_arg_cmp, 16);
89 void lc_arg_free_env(lc_arg_env_t *env)
95 int lc_arg_register(lc_arg_env_t *env, const char *name, char letter, const lc_arg_handler_t *handler)
100 lc_arg_t **map = NULL;
104 arg.handler = handler;
106 if(isupper(letter)) {
110 else if(islower(letter)) {
115 ent = set_insert(env->args, &arg, sizeof(arg), HASH_STR(name, strlen(name)));
118 map[letter - base] = ent;
123 void lc_arg_unregister(UNUSED(lc_arg_env_t *env), UNUSED(const char *name))
127 int lc_arg_append(lc_appendable_t *app, const lc_arg_occ_t *occ, const char *str, size_t len)
131 /* Set the padding to zero, if the zero is given and we are not left
132 * justified. (A minus overrides the zero). See printf(3). */
133 if(!occ->flag_minus && occ->flag_zero)
136 return lc_appendable_snwadd(app, str, len, LC_MAX(0, occ->width), occ->flag_minus, pad);
140 static int dispatch_snprintf(char *buf, size_t len, const char *fmt,
141 int lc_arg_type, const lc_arg_value_t *val)
145 switch(lc_arg_type) {
146 #define LC_ARG_TYPE(type,name) \
147 case lc_arg_type_ ## name: res = snprintf(buf, len, fmt, val->v_ ## name); break;
148 #include "lc_printf_arg_types.def"
155 static char *make_fmt(char *buf, size_t len, const lc_arg_occ_t *occ)
164 if(occ->precision > 0)
165 snprintf(prec, sizeof(prec), ".%d", occ->precision);
168 snprintf(width, sizeof(width), "%d", occ->width);
170 assert(occ->modifier && "modifier must not be NULL");
171 strncpy(mod, occ->modifier, sizeof(mod) - 1);
172 mod[LC_MIN(sizeof(mod) - 1, occ->modifier_length)] = '\0';
174 snprintf(buf, len, "%%%s%s%s%s%s%s%s%s%c",
175 occ->flag_space ? "#" : "",
176 occ->flag_hash ? "#" : "",
177 occ->flag_plus ? "+" : "",
178 occ->flag_minus ? "-" : "",
179 occ->flag_zero ? "0" : "",
181 mod, occ->conversion);
187 * Standard argument handler.
189 static int std_get_lc_arg_type(const lc_arg_occ_t *occ)
191 int modlen = occ->modifier_length;
193 /* check, if the type can be derived from the modifier */
195 const char *mod = occ->modifier;
198 return modlen > 1 && mod[1] == 'l' ? lc_arg_type_long_long : lc_arg_type_long;
199 #define TYPE_CASE(letter,type) case letter: return lc_arg_type_ ## type;
201 TYPE_CASE('j', intmax_t);
202 TYPE_CASE('z', size_t);
203 TYPE_CASE('t', ptrdiff_t);
205 TYPE_CASE('L', long_double);
210 /* The type is given by the conversion specifier and cannot be
211 * determined from the modifier. */
212 switch (occ->conversion) {
219 return lc_arg_type_double;
223 return lc_arg_type_ptr;
225 return lc_arg_type_int;
229 static int std_emit(lc_appendable_t *app, const lc_arg_occ_t *occ, const lc_arg_value_t *val)
234 make_fmt(fmt, sizeof(fmt), occ);
236 switch(occ->conversion) {
238 /* Store the number of written characters in the given
239 * int pointer location */
242 int *num = val->v_ptr;
247 /* strings are dumped directly, since they can get really big. A
248 * buffer of 128 letters for all other types should be enough. */
251 const char *str = val->v_ptr;
252 res = lc_arg_append(app, occ, str, strlen(str));
258 int len = LC_MAX(128, occ->width + 1);
259 char *buf = XMALLOCN(char, len);
260 res = dispatch_snprintf(buf, len, fmt, occ->lc_arg_type, val);
261 res = lc_appendable_snadd(app, buf, res);
269 static const lc_arg_handler_t std_handler = {
274 lc_arg_env_t *lc_arg_add_std(lc_arg_env_t *env)
276 lc_arg_register(env, "std:c", 'c', &std_handler);
277 lc_arg_register(env, "std:i", 'i', &std_handler);
278 lc_arg_register(env, "std:d", 'd', &std_handler);
279 lc_arg_register(env, "std:o", 'o', &std_handler);
280 lc_arg_register(env, "std:u", 'u', &std_handler);
281 lc_arg_register(env, "std:x", 'x', &std_handler);
282 lc_arg_register(env, "std:X", 'X', &std_handler);
284 lc_arg_register(env, "std:e", 'e', &std_handler);
285 lc_arg_register(env, "std:E", 'E', &std_handler);
286 lc_arg_register(env, "std:f", 'f', &std_handler);
287 lc_arg_register(env, "std:F", 'F', &std_handler);
288 lc_arg_register(env, "std:g", 'g', &std_handler);
289 lc_arg_register(env, "std:G", 'G', &std_handler);
291 lc_arg_register(env, "std:s", 's', &std_handler);
292 lc_arg_register(env, "std:p", 'p', &std_handler);
293 lc_arg_register(env, "std:n", 'n', &std_handler);
298 static char *read_int(const char *s, int *value)
301 int res = (int) strtol(s, &endptr, 10);
302 *value = endptr == s ? -1 : res;
306 /* Generic printf() function. */
308 int lc_evpprintf(const lc_arg_env_t *env, lc_appendable_t *app, const char *fmt, va_list args)
312 const char *last = fmt + strlen(fmt);
314 /* Find the first % */
315 s = strchr(fmt, '%');
317 /* Emit the text before the first % was found */
318 lc_appendable_snadd(app, fmt, (s ? s : last) - fmt);
323 const lc_arg_t *arg = NULL;
327 /* We must be at a '%' */
330 /* Reset the occurrence structure */
331 memset(&occ, 0, sizeof(occ));
333 /* Eat all flags and set the corresponding flags in the occ struct */
334 for(++s; strchr("#0-+", *s); ++s) {
354 /* Read the width if given */
355 s = read_int(s, &occ.width);
359 /* read the precision if given */
362 s = read_int(s + 1, &val);
364 /* Negative or lacking precision after a '.' is treated as
366 occ.precision = LC_MAX(0, val);
370 * Now, we can either have:
371 * - a named argument like {node}
372 * - some modifiers followed by a conversion specifier
373 * - or some other character, which ends this format invalidly
379 res += lc_appendable_chadd(app, '%');
383 const char *named = ++s;
385 /* Read until the closing brace or end of the string. */
386 for(ch = *s; ch != '}' && ch != '\0'; ch = *++s);
393 name = malloc(sizeof(char) * (n + 1));
394 memcpy(name, named, sizeof(char) * n);
398 arg = set_find(env->args, &tmp, sizeof(tmp), HASH_STR(named, n));
400 occ.modifier_length = 0;
402 /* Set the conversion specifier of the occurrence to the
403 * letter specified in the argument description. */
405 occ.conversion = arg->letter;
409 /* If we ended with a closing brace, move the current
410 * pointer after it, since it is not to be dumped. */
421 /* Read, as long there are letters */
422 while(isalpha(ch) && !arg) {
424 lc_arg_t * const *map = env->lower;
426 /* If uppercase, select the uppercase map from the environment */
432 if(map[ch - base] != NULL) {
434 occ.modifier_length = s - mod;
436 arg = map[ch - base];
444 /* Call the handler if an argument was determined */
445 if(arg != NULL && arg->handler != NULL) {
446 const lc_arg_handler_t *handler = arg->handler;
448 /* Let the handler determine the type of the argument based on the
449 * information gathered. */
450 occ.lc_arg_type = handler->get_lc_arg_type(&occ);
452 /* Store the value according to argument information */
453 switch(occ.lc_arg_type) {
454 #define LC_ARG_TYPE(type,name) case lc_arg_type_ ## name: val.v_ ## name = va_arg(args, type); break;
455 #include "lc_printf_arg_types.def"
459 /* Finally, call the handler. */
460 res += handler->emit(app, &occ, &val);
465 res += lc_appendable_snadd(app, old, (s ? s : last) - old);
471 /* Convenience implementations */
473 int lc_epprintf(const lc_arg_env_t *env, lc_appendable_t *app, const char *fmt, ...)
478 res = lc_evpprintf(env, app, fmt, args);
483 int lc_pprintf(lc_appendable_t *app, const char *fmt, ...)
488 res = lc_vpprintf(app, fmt, args);
493 int lc_vpprintf(lc_appendable_t *app, const char *fmt, va_list args)
495 return lc_evpprintf(_lc_arg_get_default_env(), app, fmt, args);
498 int lc_eprintf(const lc_arg_env_t *env, const char *fmt, ...)
503 res = lc_efprintf(env, stdout, fmt, args);
508 int lc_esnprintf(const lc_arg_env_t *env, char *buf, size_t len, const char *fmt, ...)
513 res = lc_evsnprintf(env, buf, len, fmt, args);
518 int lc_efprintf(const lc_arg_env_t *env, FILE *file, const char *fmt, ...)
523 res = lc_evfprintf(env, file, fmt, args);
528 int lc_eoprintf(const lc_arg_env_t *env, struct obstack *obst, const char *fmt, ...)
533 res = lc_evoprintf(env, obst, fmt, args);
538 int lc_evprintf(const lc_arg_env_t *env, const char *fmt, va_list args)
540 return lc_evfprintf(env, stdout, fmt, args);
543 int lc_evsnprintf(const lc_arg_env_t *env, char *buf, size_t len, const char *fmt, va_list args)
548 lc_appendable_init(&app, lc_appendable_string, buf, len);
549 res = lc_evpprintf(env, &app, fmt, args);
550 lc_appendable_finish(&app);
554 int lc_evfprintf(const lc_arg_env_t *env, FILE *f, const char *fmt, va_list args)
559 lc_appendable_init(&app, lc_appendable_file, f, 0);
560 res = lc_evpprintf(env, &app, fmt, args);
561 lc_appendable_finish(&app);
565 int lc_evoprintf(const lc_arg_env_t *env, struct obstack *obst, const char *fmt, va_list args)
570 lc_appendable_init(&app, lc_appendable_obstack, obst, 0);
571 res = lc_evpprintf(env, &app, fmt, args);
572 lc_appendable_finish(&app);
577 int lc_printf(const char *fmt, ...)
582 res = lc_vprintf(fmt, args);
587 int lc_snprintf(char *buf, size_t len, const char *fmt, ...)
592 res = lc_vsnprintf(buf, len, fmt, args);
597 int lc_fprintf(FILE *f, const char *fmt, ...)
602 res = lc_vfprintf(f, fmt, args);
607 int lc_oprintf(struct obstack *obst, const char *fmt, ...)
612 res = lc_voprintf(obst, fmt, args);
618 int lc_vprintf(const char *fmt, va_list args)
620 return lc_evprintf(_lc_arg_get_default_env(), fmt, args);
623 int lc_vsnprintf(char *buf, size_t len, const char *fmt, va_list args)
625 return lc_evsnprintf(_lc_arg_get_default_env(), buf, len, fmt, args);
628 int lc_vfprintf(FILE *f, const char *fmt, va_list args)
630 return lc_evfprintf(_lc_arg_get_default_env(), f, fmt, args);
633 int lc_voprintf(struct obstack *obst, const char *fmt, va_list args)
635 return lc_evoprintf(_lc_arg_get_default_env(), obst, fmt, args);