blob: 4b8a3506b99d1beb9f36a27ab4552581a4143488 [file] [log] [blame]
Dan Talaycof75360a2010-02-05 22:22:54 -08001#!/usr/bin/env python2.5
2"""This script reads struct from C/C++ header file and output query
3
4Author ykk
5Date June 2009
6"""
7import sys
8import getopt
9import cheader
10import c2py
11
12
13def usage():
14 """Display usage
15 """
16 print "Usage "+sys.argv[0]+" <options> header_files... struct_name\n"+\
17 "Options:\n"+\
18 "-h/--help\n\tPrint this usage guide\n"+\
19 "-c/--cstruct\n\tPrint C struct\n"+\
20 "-n/--name\n\tPrint names of struct\n"+\
21 "-s/--size\n\tPrint size of struct\n"+\
22 ""
23
24#Parse options and arguments
25try:
26 opts, args = getopt.getopt(sys.argv[1:], "hcsn",
27 ["help","cstruct","size","names"])
28except getopt.GetoptError:
29 usage()
30 sys.exit(2)
31
32#Check there is at least 1 input file and struct name
33if (len(args) < 2):
34 usage()
35 sys.exit(2)
36
37#Parse options
38##Print C struct
39printc = False
40##Print names
41printname = False
42##Print size
43printsize = False
44for opt,arg in opts:
45 if (opt in ("-h","--help")):
46 usage()
47 sys.exit(0)
48 elif (opt in ("-s","--size")):
49 printsize = True
50 elif (opt in ("-c","--cstruct")):
51 printc = True
52 elif (opt in ("-n","--names")):
53 printname = True
54 else:
55 print "Unhandled option :"+opt
56 sys.exit(1)
57
58headerfile = cheader.cheaderfile(args[:-1])
59cstruct = headerfile.structs[args[-1].strip()]
60cs2p = c2py.cstruct2py()
61pattern = cs2p.get_pattern(cstruct)
62
63#Print C struct
64if (printc):
65 print cstruct
66
67#Print pattern
68print "Python pattern = "+pattern
69
70#Print name
71if (printname):
72 print cstruct.get_names()
73
74#Print size
75if (printsize):
76 print "Size = "+str(cs2p.get_size(pattern))