OpenCores
URL https://opencores.org/ocsvn/eco32/eco32/trunk

Subversion Repositories eco32

[/] [eco32/] [trunk/] [lcc/] [src/] [output.c] - Blame information for rev 190

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 hellwig
#include "c.h"
2
 
3
static char rcsid[] = "$Id: output.c,v 1.1 2002/08/28 23:12:45 drh Exp $";
4
 
5
static char *outs(const char *str, FILE *f, char *bp) {
6
        if (f)
7
                fputs(str, f);
8
        else
9
                while (*bp = *str++)
10
                        bp++;
11
        return bp;
12
}
13
 
14
static char *outd(long n, FILE *f, char *bp) {
15
        unsigned long m;
16
        char buf[25], *s = buf + sizeof buf;
17
 
18
        *--s = '\0';
19
        if (n < 0)
20
                m = -n;
21
        else
22
                m = n;
23
        do
24
                *--s = m%10 + '0';
25
        while ((m /= 10) != 0);
26
        if (n < 0)
27
                *--s = '-';
28
        return outs(s, f, bp);
29
}
30
 
31
static char *outu(unsigned long n, int base, FILE *f, char *bp) {
32
        char buf[25], *s = buf + sizeof buf;
33
 
34
        *--s = '\0';
35
        do
36
                *--s = "0123456789abcdef"[n%base];
37
        while ((n /= base) != 0);
38
        return outs(s, f, bp);
39
}
40
void print(const char *fmt, ...) {
41
        va_list ap;
42
 
43
        va_start(ap, fmt);
44
        vfprint(stdout, NULL, fmt, ap);
45
        va_end(ap);
46
}
47
/* fprint - formatted output to  f */
48
void fprint(FILE *f, const char *fmt, ...) {
49
        va_list ap;
50
 
51
        va_start(ap, fmt);
52
        vfprint(f, NULL, fmt, ap);
53
        va_end(ap);
54
}
55
 
56
/* stringf - formatted output to a saved string */
57
char *stringf(const char *fmt, ...) {
58
        char buf[1024];
59
        va_list ap;
60
 
61
        va_start(ap, fmt);
62
        vfprint(NULL, buf, fmt, ap);
63
        va_end(ap);
64
        return string(buf);
65
}
66
 
67
/* vfprint - formatted output to f or string bp */
68
void vfprint(FILE *f, char *bp, const char *fmt, va_list ap) {
69
        for (; *fmt; fmt++)
70
                if (*fmt == '%')
71
                        switch (*++fmt) {
72
                        case 'd': bp = outd(va_arg(ap, int), f, bp); break;
73
                        case 'D': bp = outd(va_arg(ap, long), f, bp); break;
74
                        case 'U': bp = outu(va_arg(ap, unsigned long), 10, f, bp); break;
75
                        case 'u': bp = outu(va_arg(ap, unsigned), 10, f, bp); break;
76
                        case 'o': bp = outu(va_arg(ap, unsigned), 8, f, bp); break;
77
                        case 'X': bp = outu(va_arg(ap, unsigned long), 16, f, bp); break;
78
                        case 'x': bp = outu(va_arg(ap, unsigned), 16, f, bp); break;
79
                        case 'f': case 'e':
80
                        case 'g': {
81
                                        static char format[] = "%f";
82
                                        char buf[128];
83
                                        format[1] = *fmt;
84
                                        sprintf(buf, format, va_arg(ap, double));
85
                                        bp = outs(buf, f, bp);
86
                                  }
87
; break;
88
                        case 's': bp = outs(va_arg(ap, char *), f, bp); break;
89
                        case 'p': {
90
                                void *p = va_arg(ap, void *);
91
                                if (p)
92
                                        bp = outs("0x", f, bp);
93
                                bp = outu((unsigned long)p, 16, f, bp);
94
                                break;
95
                                  }
96
                        case 'c': if (f) fputc(va_arg(ap, int), f); else *bp++ = va_arg(ap, int); break;
97
                        case 'S': { char *s = va_arg(ap, char *);
98
                                    int n = va_arg(ap, int);
99
                                    if (s)
100
                                        for ( ; n-- > 0; s++)
101
                                                if (f) (void)putc(*s, f); else *bp++ = *s;
102
 } break;
103
                        case 'k': { int t = va_arg(ap, int);
104
                                    static char *tokens[] = {
105
#define xx(a,b,c,d,e,f,g) g,
106
#define yy(a,b,c,d,e,f,g) g,
107
#include "token.h"
108
                                    };
109
                                    assert(tokens[t&0177]);
110
                                    bp = outs(tokens[t&0177], f, bp);
111
 } break;
112
                        case 't': { Type ty = va_arg(ap, Type);
113
                                    assert(f);
114
                                    outtype(ty ? ty : voidtype, f);
115
 } break;
116
                        case 'w': { Coordinate *p = va_arg(ap, Coordinate *);
117
                                    if (p->file && *p->file) {
118
                                        bp = outs(p->file, f, bp);
119
                                        bp = outs(":", f, bp);
120
                                    }
121
                                    bp = outd(p->y, f, bp);
122
 } break;
123
                        case 'I': { int n = va_arg(ap, int);
124
                                    while (--n >= 0)
125
                                        if (f) (void)putc(' ', f); else *bp++ = ' ';
126
 } break;
127
                        default:  if (f) (void)putc(*fmt, f); else *bp++ = *fmt; break;
128
                        }
129
                else if (f)
130
                        (void)putc(*fmt, f);
131
                else
132
                        *bp++ = *fmt;
133
        if (!f)
134
                *bp = '\0';
135
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.