summaryrefslogtreecommitdiff
path: root/jsongen.py
blob: d684b6fba029484f9c69889da0b7ba83fb9204bf (plain)
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
#!/usr/bin/env python
#
# Never hand-hack what you can generate...
#
# This code senerate template declarations for AIS-JSON parsing from a
# declarative specification of a JSON structure - and generate those
# declarative specification from the Python format strings for
# dumping the structures
#
import sys, getopt

# Make shared field names to data types 
typemap = {
    # AIS fields
    "raim":"boolean",
    "accuracy":"boolean",
    "timestamp":"string",
    }

# Map C and Python-type format letters to JSON parser datatypes 
fmtmap = {
    "d": "integer",
    "u": "uinteger",
    "f": "real",
    "a": "string",
    }


ais_specs = (
    ("json_ais4", "\tAIS_HEADER,", "ais->type4",
     "\"timestamp\":\"%4u:%02u:%02uT%02u:%02u:%02uZ\","\
     "\"accuracy\":%s,\"lon\":%d,\"lat\":%d,"\
     "\"epfd\":%u,\"raim\":%s,\"radio\":%d}\r\n"
     ),
    )
    
def generate(initname, header, structname, specifier):
    specifier = specifier.strip()
    if specifier[-1] == "}":
        specifier = specifier[:-1]
    else:
        print "Missing terminating }"
        sys.exit(1)

    print "    const struct json_attr_t %s[] = {" % initname
    if header:
        print header
    for item in specifier.split(","):
        if "timestamp" in item:
            (attr, itype) = ("timestamp", "string")
        else:
            itype = None
            (attr, fmt) = item.split(":")
            if attr[0] == '"':
                attr = attr[1:]
            if attr[-1] == '"':
                attr = attr[:-1]
            if attr in typemap:
                itype = typemap[attr]
            if fmt[-1] in fmtmap:
                itype = fmtmap[fmt[-1]]
        if itype == "string":
            deref = ""
        else:
            deref = "&"
        print '\t{"%s",%s%s,%s.addr.%s = %s%s.%s,' % \
               (attr, " "*(12-len(attr)),itype, " "*(12-len(itype)), itype, deref, structname, attr)
        leader = " " * 37
        if itype == "string":
            print leader + ".maxlen = sizeof(%s.%s)}," % (structname, attr)
        elif itype == "boolean":
            print leader + ".dflt.boolean = false},"
        elif itype == "real":
            print leader + ".dflt.real = 0.0},"
        else:
            print leader + ".dflt.%s = 0}," % itype

    print """\
	{NULL},
    };

"""

if __name__ == '__main__':
    for description in ais_specs:
        generate(initname=description[0],
                 header=description[1],
                 structname=description[2],
                 specifier=description[3])