File: sprintf.c

package info (click to toggle)
tcng 10b-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,632 kB
  • ctags: 2,515
  • sloc: ansic: 19,038; pascal: 4,640; yacc: 2,619; sh: 1,908; perl: 1,546; lex: 772; makefile: 755
file content (122 lines) | stat: -rw-r--r-- 2,008 bytes parent folder | download | duplicates (5)
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
120
121
122
/*
 * sprintf.c - sprintf for DATA
 *
 * Written 2001,2002 by Werner Almesberger
 * Copyright 2001 EPFL-ICA
 * Copyright 2001,2002 Bivio Networks
 */


#include <stdarg.h>
#include <assert.h>

#include "error.h"
#include "data.h"
#include "sprintf_generic.h"
#include "sprintf.h"


struct dsc {
    const DATA_LIST *curr;
    const DATA_LIST *list;
};


static void first_arg(void *dsc)
{
    struct dsc *d = dsc;

    d->curr = d->list;
}


static void next_arg(void *dsc)
{
    struct dsc *d = dsc;

    assert(d->curr);
    d->curr = d->curr->next;
}


static int got_arg(void *dsc)
{
    struct dsc *d = dsc;

    return !!d->curr;
}


static unsigned int get_unum(void *dsc)
{
    struct dsc *d = dsc;

    if (!d->curr) yyerror("not enough arguments for format string");
    return data_convert(*d->curr->ref,dt_unum).u.unum;
}


static int get_unum_int(void *dsc)
{
    return (int) get_unum(dsc);
}


static double get_float(void *dsc)
{
    struct dsc *d = dsc;

    if (!d->curr) yyerror("not enough arguments for format string");
    return data_convert(*d->curr->ref,dt_fnum).u.fnum;
}


static const char *get_string(void *dsc)
{
    struct dsc *d = dsc;

    if (!d->curr) yyerror("not enough arguments for format string");
    return data_convert(*d->curr->ref,dt_string).u.string;
}


static void my_errorf(void *dsc,const char *msg,...)
{
    va_list ap;

    va_start(ap,msg);
    vyyerrorf(msg,ap);
    va_end(ap);
}


static void my_warnf(void *dsc,const char *msg,...)
{
    va_list ap;

    va_start(ap,msg);
    vyywarnf(msg,ap);
    va_end(ap);
}


static struct sprintf_ops ops = {
    .first_arg = first_arg,
    .next_arg = next_arg,
    .got_arg = got_arg,
    .get_sint = get_unum_int,
    .get_uint = get_unum,
    .get_float = get_float,
    .get_string = get_string,
    .errorf = my_errorf,
    .warnf = my_warnf
};


char *sprintf_data(const char *fmt,const DATA_LIST *data_list)
{
    struct dsc dsc;

    dsc.list = data_list;
    return sprintf_generic(fmt,&ops,&dsc);
}