author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Thu, 16 Apr 2009 12:49:13 +0200 | |
changeset 62 | b7061c0e239f |
parent 60 | 47c13ca8c4d0 |
child 63 | 5a82f89d607e |
permissions | -rw-r--r-- |
58
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
1 |
/** |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
2 |
* test/demos/myprintf.c |
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... */ |
60 | 42 |
return EOF; |
43 |
} |
|
44 |
s_str = ptr; |
|
45 |
} |
|
58
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
46 |
for (i = 0; i < strlen (s); i++) { |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
47 |
s_str[pos++] = s[i]; |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
48 |
} |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
49 |
break; |
60 | 50 |
|
58
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
51 |
case 'd': |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
52 |
value = va_arg (list, int); |
60 | 53 |
sprintf (v_str, "%d", value); |
54 |
for (i = 0; i < strlen (v_str); i++) { |
|
55 |
s_str[pos++] = v_str[i]; |
|
58
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
56 |
} |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
57 |
break; |
60 | 58 |
|
58
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
59 |
default: |
60 | 60 |
s_str[pos++] = *fmt; |
58
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
61 |
} |
60 | 62 |
|
63 |
c = *fmt++; |
|
58
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
64 |
default: |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
65 |
s_str[pos++] = *fmt++; |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
66 |
} |
60 | 67 |
|
68 |
if (pos > (size * BUF_SIZE / 2)) { |
|
69 |
if ((ptr = realloc (s_str, ++size * BUF_SIZE)) == NULL) { |
|
70 |
return EOF; |
|
71 |
} |
|
72 |
s_str = ptr; |
|
73 |
} |
|
58
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
74 |
} |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
75 |
|
62
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
76 |
va_end (list); |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
77 |
|
58
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
78 |
s_str[pos] = '\0'; |
62
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
79 |
fputs (s_str, fp); |
60 | 80 |
free (s_str); |
58
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
81 |
|
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
82 |
return pos; |
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 |
|
62
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
85 |
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
|
86 |
{ |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
87 |
va_list list; |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
88 |
int ret; |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
89 |
|
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
90 |
va_start (list, fmt); |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
91 |
ret = myvfprintf (fp, fmt, list); |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
92 |
va_end (list); |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
93 |
|
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
94 |
return ret; |
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 |
|
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
97 |
int myprintf (const char *fmt, ...) |
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 |
va_list list; |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
100 |
int ret; |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
101 |
|
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
102 |
va_start (list, fmt); |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
103 |
ret = myvfprintf (stdout, fmt, list); |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
104 |
va_end (list); |
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
105 |
|
b7061c0e239f
unicode.c added and myprintf improved
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
60
diff
changeset
|
106 |
return ret; |
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 |
|
58
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
109 |
int main (int argc, char **argv) |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
110 |
{ |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
111 |
int len; |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
112 |
|
60 | 113 |
len = myprintf ("Printf replacement %% [Version %d - %s]...\n", 5, "LSF Rostock"); |
114 |
||
58
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
115 |
return EXIT_SUCCESS; |
9609c54a54d3
myprintf.c added - replaces varargs.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
116 |
} |