Debian Codegraph @

utils/qmidb/Fields.py

raw
1 # -*- Mode: python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 #
3 # This program is free software; you can redistribute it and/or modify it under
4 # the terms of the GNU General Public License as published by the Free Software
5 # Foundation, version 2.
6 #
7 # This program is distributed in the hope that it will be useful, but WITHOUT
8 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10 # details.
11 #
12 # You should have received a copy of the GNU General Public License along with
13 # this program; if not, write to the Free Software Foundation, Inc., 51
14 # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15 #
16 # Copyright (C) 2011 - 2012 Red Hat, Inc.
17 #
18
19 import utils
20
21
22 # eDB2FieldType
23 FIELD_TYPE_STD = 0 # Field is a standard type (see below)
24 FIELD_TYPE_ENUM_UNSIGNED = 1 # Field is an unsigned enumerated type
25 FIELD_TYPE_ENUM_SIGNED = 2 # Field is a signed enumerated type
26
27 # eDB2StdFieldType
28 FIELD_STD_BOOL = 0 # boolean (0/1, false/true)
29 FIELD_STD_INT8 = 1 # 8-bit signed integer
30 FIELD_STD_UINT8 = 2 # 8-bit unsigned integer
31 FIELD_STD_INT16 = 3 # 16-bit signed integer
32 FIELD_STD_UINT16 = 4 # 16-bit unsigned integer
33 FIELD_STD_INT32 = 5 # 32-bit signed integer
34 FIELD_STD_UINT32 = 6 # 32-bit unsigned integer
35 FIELD_STD_INT64 = 7 # 64-bit signed integer
36 FIELD_STD_UINT64 = 8 # 64-bit unsigned integer
37 FIELD_STD_STRING_A = 9 # ANSI (ASCII?) fixed length string; size in bits
38 FIELD_STD_STRING_U = 10 # UCS-2 fixed length string
39 FIELD_STD_STRING_ANT = 11 # ANSI (ASCII?) NULL terminated string
40 FIELD_STD_STRING_UNT = 12 # UCS-2 NULL terminated string
41 FIELD_STD_FLOAT32 = 13 # 32-bit floating point value
42 FIELD_STD_FLOAT64 = 14 # 64-bit floating point value
43 FIELD_STD_STRING_U8 = 15 # UTF-8 encoded fixed length string
44 FIELD_STD_STRING_U8NT = 16 # UTF-8 encoded NULL terminated string
45
46 stdtypes = {
47 # Maps field type to [ <C type>, <is array>, <element size in bits> ]
48 FIELD_STD_BOOL: [ 'bool', False, 8 ],
49 FIELD_STD_INT8: [ 'int8', False, 8 ],
50 FIELD_STD_UINT8: [ 'uint8', False, 8 ],
51 FIELD_STD_INT16: [ 'int16', False, 16 ],
52 FIELD_STD_UINT16: [ 'uint16', False, 16 ],
53 FIELD_STD_INT32: [ 'int32', False, 32 ],
54 FIELD_STD_UINT32: [ 'uint32', False, 32 ],
55 FIELD_STD_INT64: [ 'int64', False, 64 ],
56 FIELD_STD_UINT64: [ 'uint64', False, 64 ],
57 FIELD_STD_STRING_A: [ 'char', True, 8 ],
58 FIELD_STD_STRING_U: [ 'uint16', True, 16 ],
59 FIELD_STD_STRING_ANT: [ 'char *', False, 8 ],
60 FIELD_STD_STRING_UNT: [ 'char *', False, 8 ],
61 FIELD_STD_FLOAT32: [ 'float32', False, 32 ],
62 FIELD_STD_FLOAT64: [ 'float64', False, 64 ],
63 }
64
65 def is_array_type(t):
66 return stdtypes[t][1]
67
68
69 class Field:
70 def __init__(self, line):
71 parts = line.split('^')
72 if len(parts) < 6:
73 raise Exception("Invalid field line '%s'" % line)
74 self.id = int(parts[0])
75 self.name = parts[1].replace('"', '')
76 self.size = int(parts[2]) # in *bits*
77 self.type = int(parts[3]) # eDB2FieldType
78 self.typeval = int(parts[4]) # eDB2StdFieldType if 'type' == 0
79 self.hex = int(parts[5]) != 0
80 self.descid = None
81 self.internal = False
82 if len(parts) > 6:
83 self.descid = int(parts[6])
84 if len(parts) > 7:
85 self.internal = int(parts[7])
86
87 # Field.txt:50118^"IP V4 Address"^8^0^2^0
88
89 #Field.txt:50118^"IP V4 Address"^8^0^2^0
90
91 def get_charsize(self):
92 if self.type == FIELD_TYPE_STD:
93 if self.typeval == FIELD_STD_STRING_A or self.typeval == FIELD_STD_STRING_ANT or \
94 self.typeval == FIELD_STD_STRING_U or self.typeval == FIELD_STD_STRING_UNT:
95 return stdtypes[self.typeval][2]
96 raise Exception("Called for non-string type")
97
98 def emit(self, do_print, indent, enums, num_elements, comment, isarray):
99 ctype = ''
100 arraypart = ''
101
102 sizebits = 0
103 if self.type == FIELD_TYPE_STD: # eDB2_FIELD_STD
104 tinfo = stdtypes[self.typeval]
105 ctype = tinfo[0]
106
107 if is_array_type(self.typeval) or num_elements > 0:
108 if num_elements > 0:
109 arraypart = "[%d]" % num_elements
110 sizebits = num_elements * tinfo[2]
111 elif isarray:
112 # array with size given by previous fragment
113 arraypart = "[0]"
114 sizebits = 0
115 else:
116 arraypart = "[%d]" % (self.size / tinfo[2])
117 sizebits = self.size * tinfo[2]
118 elif self.type == FIELD_TYPE_ENUM_UNSIGNED or self.type == FIELD_TYPE_ENUM_SIGNED:
119 # It's a enum; find the enum
120 e = enums.get_child(self.typeval)
121 ctype = "gobi_%s" % utils.nicename(e.name)
122 if isarray:
123 if num_elements != 0:
124 raise Exception("Unhandled ENUM field type with size %d" % num_elements)
125 arraypart = "[0]";
126 sizebits = 0
127 else:
128 if self.size > 0:
129 # enum size is # of bits
130 arraypart = ":%d" % self.size
131 sizebits = self.size
132 else:
133 sizebits = 32
134 else:
135 raise ValueError("Unknown Field type")
136
137 if comment:
138 comment = " (%s)" % comment
139 if do_print:
140 print "%s%s %s%s; /* %s%s */" % ("\t" * indent, ctype, utils.nicename(self.name), arraypart, self.name, comment)
141 return sizebits
142
143 class Fields:
144 def __init__(self, path):
145 self.byid = {}
146
147 f = file(path + "Field.txt")
148 for line in f:
149 field = Field(line.strip())
150 self.byid[field.id] = field
151
152 def has_child(self, fid):
153 return fid in self.byid
154
155 def get_child(self, fid):
156 return self.byid[fid]
157