| 1 |
/* |
| 2 |
* libmowgli: A collection of useful routines for programming. |
| 3 |
* formatter.c: Reusable formatting. |
| 4 |
* |
| 5 |
* Copyright (c) 2007 William Pitcock <nenolod -at- sacredspiral.co.uk> |
| 6 |
* |
| 7 |
* Permission to use, copy, modify, and/or distribute this software for any |
| 8 |
* purpose with or without fee is hereby granted, provided that the above |
| 9 |
* copyright notice and this permission notice is present in all copies. |
| 10 |
* |
| 11 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 12 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 13 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 14 |
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
| 15 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 16 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 17 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 18 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 19 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 20 |
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 21 |
* POSSIBILITY OF SUCH DAMAGE. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "mowgli.h" |
| 25 |
|
| 26 |
void |
| 27 |
mowgli_formatter_format_from_argstack(char *buf, size_t bufstr, const char *fmtstr, const char *descstr, mowgli_argstack_t *stack) |
| 28 |
{ |
| 29 |
size_t pos = 0; |
| 30 |
char *i = buf; |
| 31 |
const char *fiter = fmtstr; |
| 32 |
|
| 33 |
return_if_fail(buf != NULL); |
| 34 |
return_if_fail(fmtstr != NULL); |
| 35 |
return_if_fail(descstr != NULL); |
| 36 |
|
| 37 |
*i = '\0'; |
| 38 |
|
| 39 |
while (*fiter && pos <= bufstr) |
| 40 |
{ |
| 41 |
int arg; |
| 42 |
mowgli_argstack_element_t *e; |
| 43 |
|
| 44 |
pos = strlen(buf); |
| 45 |
|
| 46 |
switch (*fiter) |
| 47 |
{ |
| 48 |
case '%': |
| 49 |
fiter++; |
| 50 |
arg = atoi(fiter); |
| 51 |
e = mowgli_node_nth_data(&stack->stack, arg - 1); |
| 52 |
|
| 53 |
while (isdigit((unsigned char)*fiter)) |
| 54 |
{ |
| 55 |
fiter++; |
| 56 |
} |
| 57 |
|
| 58 |
if (e == NULL) |
| 59 |
{ |
| 60 |
arg = snprintf(i, bufstr - (i - buf), "(unknown)"); |
| 61 |
i += arg; |
| 62 |
continue; |
| 63 |
} |
| 64 |
|
| 65 |
switch (e->type) |
| 66 |
{ |
| 67 |
case MOWGLI_ARG_STRING: |
| 68 |
arg = snprintf(i, bufstr - (i - buf), "%s", e->data.string); |
| 69 |
i += arg; |
| 70 |
break; |
| 71 |
case MOWGLI_ARG_NUMERIC: |
| 72 |
arg = snprintf(i, bufstr - (i - buf), "%d", e->data.numeric); |
| 73 |
i += arg; |
| 74 |
break; |
| 75 |
case MOWGLI_ARG_POINTER: |
| 76 |
arg = snprintf(i, bufstr - (i - buf), "%p", e->data.pointer); |
| 77 |
i += arg; |
| 78 |
break; |
| 79 |
case MOWGLI_ARG_BOOLEAN: |
| 80 |
arg = snprintf(i, bufstr - (i - buf), "%s", e->data.boolean ? "TRUE" : "FALSE"); |
| 81 |
i += arg; |
| 82 |
break; |
| 83 |
default: |
| 84 |
mowgli_log("unhandled type"); |
| 85 |
break; |
| 86 |
} |
| 87 |
|
| 88 |
continue; |
| 89 |
|
| 90 |
default: |
| 91 |
*i = *fiter; |
| 92 |
} |
| 93 |
|
| 94 |
i++; |
| 95 |
fiter++; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
void |
| 100 |
mowgli_formatter_format(char *buf, size_t bufstr, const char *fmtstr, const char *descstr, ...) |
| 101 |
{ |
| 102 |
va_list va; |
| 103 |
mowgli_argstack_t *stack; |
| 104 |
|
| 105 |
va_start(va, descstr); |
| 106 |
stack = mowgli_argstack_create_from_va_list(descstr, va); |
| 107 |
va_end(va); |
| 108 |
|
| 109 |
mowgli_formatter_format_from_argstack(buf, bufstr, fmtstr, descstr, stack); |
| 110 |
} |
| 111 |
|
| 112 |
void |
| 113 |
mowgli_formatter_print(const char *fmtstr, const char *descstr, ...) |
| 114 |
{ |
| 115 |
va_list va; |
| 116 |
char buf[65535]; |
| 117 |
mowgli_argstack_t *stack; |
| 118 |
|
| 119 |
va_start(va, descstr); |
| 120 |
stack = mowgli_argstack_create_from_va_list(descstr, va); |
| 121 |
va_end(va); |
| 122 |
|
| 123 |
mowgli_formatter_format_from_argstack(buf, 65535, fmtstr, descstr, stack); |
| 124 |
printf("%s", buf); |
| 125 |
} |