update credits
[LibreOffice.git] / solenv / gdb / libreoffice / svl.py
blob83fe60919491ce87933b7a05b4f412a8f2af23c9
1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 import gdb
12 from libreoffice.util import printing
14 class SvArrayPrinter(object):
15 '''Prints macro-declared arrays from svl module'''
17 def __init__(self, typename, value):
18 self.typename = typename
19 self.value = value
21 def to_string(self):
22 if int(self.value['nA']):
23 return "%s of length %d" % (self.typename, self.value['nA'])
24 else:
25 return "empty " + self.typename
27 def children(self):
28 return self._iterator(self.value['pData'], self.value['nA'])
30 def display_hint(self):
31 return 'array'
33 class _iterator(object):
35 def __init__(self, data, count):
36 self.data = data
37 self.count = count
38 self.pos = 0
39 self._check_invariant()
41 def __iter__(self):
42 return self
44 def next(self):
45 if self.pos == self.count:
46 raise StopIteration()
48 pos = self.pos
49 elem = self.data[pos]
50 self.pos = self.pos + 1
52 self._check_invariant()
53 return (str(pos), elem)
55 def _check_invariant(self):
56 assert self.count >= 0
57 if self.count > 0:
58 assert self.data
59 assert self.pos >= 0
60 assert self.pos <= self.count
62 @staticmethod
63 def query(type):
64 if type.code == gdb.TYPE_CODE_REF:
65 type = type.target()
66 type = type.unqualified().strip_typedefs()
68 if not type.tag:
69 return False
71 ushort = gdb.lookup_type('sal_uInt16')
72 conforming = True
73 for field in type.fields():
74 if field.name == 'pData':
75 conforming = field.type.code == gdb.TYPE_CODE_PTR
76 elif field.name == 'nFree':
77 conforming = field.type == ushort
78 elif field.name == 'nA':
79 conforming = field.type == ushort
80 else:
81 conforming = False
82 if not conforming:
83 return False
85 try:
86 gdb.lookup_type('FnForEach_' + type.tag)
87 except RuntimeError:
88 return False
90 return True
92 printer = None
94 def build_pretty_printers():
95 global printer
97 printer = printing.Printer("libreoffice/svl")
99 # macro-based arrays from svl module
100 printer.add('SvArray', SvArrayPrinter, SvArrayPrinter.query)
102 def register_pretty_printers(obj):
103 printing.register_pretty_printer(printer, obj)
105 build_pretty_printers()
107 # vim:set shiftwidth=4 softtabstop=4 expandtab: