Dan Talayco | f75360a | 2010-02-05 22:22:54 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python2.5 |
| 2 | """This script reads struct from C/C++ header file and output query |
| 3 | |
| 4 | Author ykk |
| 5 | Date June 2009 |
| 6 | """ |
| 7 | import sys |
| 8 | import getopt |
| 9 | import cheader |
| 10 | import c2py |
| 11 | |
| 12 | |
| 13 | def 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 |
| 25 | try: |
| 26 | opts, args = getopt.getopt(sys.argv[1:], "hcsn", |
| 27 | ["help","cstruct","size","names"]) |
| 28 | except getopt.GetoptError: |
| 29 | usage() |
| 30 | sys.exit(2) |
| 31 | |
| 32 | #Check there is at least 1 input file and struct name |
| 33 | if (len(args) < 2): |
| 34 | usage() |
| 35 | sys.exit(2) |
| 36 | |
| 37 | #Parse options |
| 38 | ##Print C struct |
| 39 | printc = False |
| 40 | ##Print names |
| 41 | printname = False |
| 42 | ##Print size |
| 43 | printsize = False |
| 44 | for 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 | |
| 58 | headerfile = cheader.cheaderfile(args[:-1]) |
| 59 | cstruct = headerfile.structs[args[-1].strip()] |
| 60 | cs2p = c2py.cstruct2py() |
| 61 | pattern = cs2p.get_pattern(cstruct) |
| 62 | |
| 63 | #Print C struct |
| 64 | if (printc): |
| 65 | print cstruct |
| 66 | |
| 67 | #Print pattern |
| 68 | print "Python pattern = "+pattern |
| 69 | |
| 70 | #Print name |
| 71 | if (printname): |
| 72 | print cstruct.get_names() |
| 73 | |
| 74 | #Print size |
| 75 | if (printsize): |
| 76 | print "Size = "+str(cs2p.get_size(pattern)) |