author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Sun, 19 Jul 2009 19:44:48 +0200 | |
changeset 101 | 176448af80fa |
parent 77 | 49e0babccb23 |
permissions | -rw-r--r-- |
58
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
1 |
/** |
77 | 2 |
* myprintf.c |
58
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
3 |
* Copyright (C) 2008 Markus Broeker |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
4 |
*/ |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
5 |
|
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
6 |
#include <stdio.h> |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
7 |
#include <stdlib.h> |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
8 |
#include <stdarg.h> |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
9 |
#include <string.h> |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
10 |
|
60 | 11 |
#define BUF_SIZE 1024 |
12 |
||
62
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
13 |
int myvfprintf (FILE * fp, const char *fmt, va_list list) |
58
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
14 |
{ |
60 | 15 |
int i, value, pos = 0, size = 1; |
16 |
char c; |
|
17 |
||
18 |
char *ptr; |
|
19 |
char *s; |
|
20 |
char *s_str; |
|
21 |
char v_str[28]; |
|
22 |
||
23 |
if ((s_str = malloc (BUF_SIZE)) == NULL) { |
|
24 |
return EOF; |
|
25 |
} |
|
58
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
26 |
|
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
27 |
while (*fmt) { |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
28 |
switch (*fmt) { |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
29 |
case '%': |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
30 |
switch (*++fmt) { |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
31 |
case 'c': |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
32 |
c = va_arg (list, int); |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
33 |
s_str[pos++] = c; |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
34 |
break; |
60 | 35 |
|
58
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
36 |
case 's': |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
37 |
s = va_arg (list, char *); |
60 | 38 |
while ((pos + strlen (s)) > (size * BUF_SIZE)) { |
39 |
if ((ptr = realloc (s_str, ++size * BUF_SIZE)) == NULL) { |
|
40 |
s_str[pos] = '\0'; |
|
62
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
41 |
fputs (s_str, fp); /* print it anyway... */ |
63
5a82f89d607e
uint vs size_t and two bugfixes in fak and unicode
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
62
diff
changeset
|
42 |
free (s_str); |
60 | 43 |
return EOF; |
44 |
} |
|
45 |
s_str = ptr; |
|
46 |
} |
|
58
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
47 |
for (i = 0; i < strlen (s); i++) { |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
48 |
s_str[pos++] = s[i]; |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
49 |
} |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
50 |
break; |
60 | 51 |
|
58
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
52 |
case 'd': |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
53 |
value = va_arg (list, int); |
60 | 54 |
sprintf (v_str, "%d", value); |
55 |
for (i = 0; i < strlen (v_str); i++) { |
|
56 |
s_str[pos++] = v_str[i]; |
|
58
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
57 |
} |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
58 |
break; |
60 | 59 |
|
58
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
60 |
default: |
60 | 61 |
s_str[pos++] = *fmt; |
58
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
62 |
} |
60 | 63 |
|
64 |
c = *fmt++; |
|
58
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
65 |
default: |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
66 |
s_str[pos++] = *fmt++; |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
67 |
} |
60 | 68 |
|
69 |
if (pos > (size * BUF_SIZE / 2)) { |
|
70 |
if ((ptr = realloc (s_str, ++size * BUF_SIZE)) == NULL) { |
|
63
5a82f89d607e
uint vs size_t and two bugfixes in fak and unicode
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
62
diff
changeset
|
71 |
free (s_str); |
60 | 72 |
return EOF; |
73 |
} |
|
74 |
s_str = ptr; |
|
75 |
} |
|
58
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
76 |
} |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
77 |
|
62
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
78 |
va_end (list); |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
79 |
|
58
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
80 |
s_str[pos] = '\0'; |
62
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
81 |
fputs (s_str, fp); |
60 | 82 |
free (s_str); |
58
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
83 |
|
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
84 |
return pos; |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
85 |
} |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
86 |
|
62
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
87 |
int myfprintf (FILE * fp, const char *fmt, ...) |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
88 |
{ |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
89 |
va_list list; |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
90 |
int ret; |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
91 |
|
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
92 |
va_start (list, fmt); |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
93 |
ret = myvfprintf (fp, fmt, list); |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
94 |
va_end (list); |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
95 |
|
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
96 |
return ret; |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
97 |
} |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
98 |
|
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
99 |
int myprintf (const char *fmt, ...) |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
100 |
{ |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
101 |
va_list list; |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
102 |
int ret; |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
103 |
|
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
104 |
va_start (list, fmt); |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
105 |
ret = myvfprintf (stdout, fmt, list); |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
106 |
va_end (list); |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
107 |
|
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
108 |
return ret; |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
109 |
} |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
110 |
|
58
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
111 |
int main (int argc, char **argv) |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
112 |
{ |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
113 |
int len; |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
114 |
|
60 | 115 |
len = myprintf ("Printf replacement %% [Version %d - %s]...\n", 5, "LSF Rostock"); |
116 |
||
58
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
117 |
return EXIT_SUCCESS; |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
118 |
} |