[Xcb-commit] xcb-proto

Jamey Sharp jamey at kemper.freedesktop.org
Tue Jun 13 01:59:44 PDT 2006


 xcb-proto/src/size.py |   65 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 65 insertions(+)

New commits:
diff-tree 5dfad53a67c8c49c9c1561bce9cbac95b6274d0b (from 06e188a9872078a07bddd353c2475e0716f6fdb3)
Author: Jamey Sharp <jamey at minilop.net>
Date:   Tue Jun 13 01:58:53 2006 -0700

    New tool to annotate XML-XCB <field>s with their size in bytes.

diff --git a/xcb-proto/src/size.py b/xcb-proto/src/size.py
new file mode 100755
index 0000000..907f2b5
--- /dev/null
+++ b/xcb-proto/src/size.py
@@ -0,0 +1,65 @@
+#!/usr/bin/python
+
+from xml.sax.saxutils import XMLFilterBase, XMLGenerator
+from xml.sax.xmlreader import AttributesImpl
+from xml.sax import make_parser
+import sys
+
+class AttributesUnion(AttributesImpl):
+	def __init__(self, base, **values):
+		baseitems = dict(base)
+		baseitems.update(values)
+		AttributesImpl.__init__(self, baseitems)
+
+class AnnotateSize(XMLFilterBase):
+	types = {
+		'BYTE': 1,
+		'BOOL': 1,
+		'CARD8': 1,
+		'CARD16': 2,
+		'CARD32': 4,
+		'INT8': 1,
+		'INT16': 2,
+		'INT32': 4,
+		'float': 4,
+		'double': 8,
+	}
+	header = None
+	def setTypeSize(self, name, size):
+		self.types[name] = size
+		self.types[self.header + ':' + name] = size
+
+	struct = None
+	union = None
+	def startElement(self, name, attrs):
+		if name == 'xcb':
+			self.header = attrs['header']
+		elif name == 'field':
+			size = self.types.get(attrs['type'], 0)
+			if self.struct is not None:
+				self.totalsize += size
+			elif self.union is not None:
+				self.totalsize = max(self.totalsize, size)
+			attrs = AttributesUnion(attrs, bytes=str(size))
+		elif name == 'xidtype':
+			self.setTypeSize(attrs['name'], 4)
+		elif name == 'typedef':
+			self.setTypeSize(attrs['newname'], self.types[attrs['oldname']])
+		elif name == 'struct' or name == 'union':
+			assert self.struct is None and self.union is None
+			setattr(self, name, attrs['name'])
+			self.totalsize = 0
+		XMLFilterBase.startElement(self, name, attrs)
+
+	def endElement(self, name):
+		if name == 'struct' or name == 'union':
+			assert getattr(self, name) is not None
+			self.setTypeSize(getattr(self, name), self.totalsize)
+			setattr(self, name, None)
+			del self.totalsize
+		XMLFilterBase.endElement(self, name)
+
+annotator = AnnotateSize(make_parser())
+annotator.setContentHandler(XMLGenerator())
+for f in sys.argv[1:]:
+	annotator.parse(f)


More information about the xcb-commit mailing list