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