]>
Commit | Line | Data |
---|---|---|
b7ed259b CP |
1 | #include <stdio.h> |
2 | #include <stdlib.h> | |
3 | #include <stdarg.h> | |
4 | #include <string.h> | |
79313d98 | 5 | #include <time.h> |
b7ed259b | 6 | #include "../lib/sstring.h" |
ba8a65c4 | 7 | #include "../lib/stringbuf.h" |
fdfed71a | 8 | #include "../core/error.h" |
b7ed259b CP |
9 | |
10 | #define MAXARGS 10 | |
fdfed71a | 11 | #define CONVBUF 512 |
b7ed259b | 12 | |
b7ed259b | 13 | void q9vsnprintf(char *buf, size_t size, const char *format, const char *args, va_list ap) { |
ba8a65c4 | 14 | StringBuf b; |
b7ed259b CP |
15 | const char *p; |
16 | char *c; | |
fdfed71a | 17 | char convbuf[MAXARGS][CONVBUF]; |
b7ed259b CP |
18 | |
19 | if(size == 0) | |
20 | return; | |
21 | ||
22 | { | |
fdfed71a CP |
23 | int argno = 0, i; |
24 | ||
25 | int d; | |
b7ed259b | 26 | char *s; |
fdfed71a CP |
27 | double g; |
28 | unsigned int u; | |
79313d98 | 29 | time_t t; |
b7ed259b CP |
30 | |
31 | for(i=0;i<MAXARGS;i++) | |
32 | convbuf[i][0] = '\0'; | |
33 | ||
fdfed71a CP |
34 | for(;*args;args++) { |
35 | char *cb = convbuf[argno++]; | |
36 | ||
37 | switch(*args) { | |
b7ed259b CP |
38 | case 's': |
39 | s = va_arg(ap, char *); | |
fdfed71a | 40 | snprintf(cb, CONVBUF, "%s", s); |
b7ed259b CP |
41 | break; |
42 | case 'd': | |
43 | d = va_arg(ap, int); | |
fdfed71a CP |
44 | snprintf(cb, CONVBUF, "%d", d); |
45 | break; | |
46 | case 'u': | |
47 | u = va_arg(ap, unsigned int); | |
48 | snprintf(cb, CONVBUF, "%u", u); | |
b7ed259b CP |
49 | break; |
50 | case 'g': | |
fdfed71a CP |
51 | g = va_arg(ap, double); |
52 | snprintf(cb, CONVBUF, "%.1f", g); | |
b7ed259b | 53 | break; |
79313d98 CP |
54 | case 'T': |
55 | t = va_arg(ap, time_t); | |
56 | strftime(cb, 15, "%d/%m/%y %H:%M", gmtime(&t)); | |
57 | break; | |
fdfed71a CP |
58 | default: |
59 | /* calls exit(0) */ | |
60 | Error("chanserv", ERR_STOP, "Bad format specifier '%c' supplied in q9vsnprintf, format: '%s'", *args, format); | |
b7ed259b CP |
61 | } |
62 | } | |
63 | } | |
64 | ||
65 | b.buf = buf; | |
66 | b.capacity = size; | |
67 | b.len = 0; | |
68 | ||
69 | for(p=format;*p;p++) { | |
70 | if(*p != '$') { | |
ba8a65c4 | 71 | if(!sbaddchar(&b, *p)) |
b7ed259b CP |
72 | break; |
73 | continue; | |
74 | } | |
75 | p++; | |
76 | if(*p == '\0') | |
77 | break; | |
78 | if(*p == '$') { | |
ba8a65c4 | 79 | if(!sbaddchar(&b, *p)) |
b7ed259b CP |
80 | break; |
81 | continue; | |
82 | } | |
83 | ||
84 | c = NULL; | |
85 | switch(*p) { | |
86 | /* | |
87 | case 'C': | |
88 | c = botname; break; | |
89 | case 'N': | |
90 | c = network; break; | |
91 | */ | |
92 | case '0': | |
93 | case '1': | |
94 | case '2': | |
95 | case '3': | |
96 | case '4': | |
97 | case '5': | |
98 | case '6': | |
99 | case '7': | |
100 | case '8': | |
101 | case '9': | |
102 | c = convbuf[*p - '0']; break; | |
103 | default: | |
104 | c = "(bad format specifier)"; | |
105 | } | |
106 | if(c) | |
ba8a65c4 | 107 | if(!sbaddstr(&b, c)) |
b7ed259b CP |
108 | break; |
109 | } | |
110 | ||
0be0b2d0 | 111 | sbterminate(&b); |
b7ed259b CP |
112 | |
113 | /* not required */ | |
114 | /* | |
115 | buf[size-1] = '\0'; | |
116 | */ | |
117 | } | |
118 | ||
119 | void q9snprintf(char *buf, size_t size, const char *format, const char *args, ...) { | |
120 | va_list ap; | |
121 | ||
122 | va_start(ap, args); | |
123 | q9vsnprintf(buf, size, format, args, ap); | |
124 | va_end(ap); | |
125 | } |