bump product version to 6.3.0.0.beta1
[LibreOffice.git] / solenv / gdb / libreoffice / tl.py
blob44f3c78210cb1834249b0864f4749d001a8aaccc
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 BigIntPrinter(object):
15 '''Prints big integer'''
17 def __init__(self, typename, val):
18 self.val = val
20 def to_string(self):
21 if self.val['bIsSet']:
22 if self.val['bIsBig']:
23 return self._value()
24 else:
25 return self.val['nVal']
26 else:
27 return "unset %s" % self.typename
29 def _value(self):
30 len = self.val['nLen']
31 digits = self.val['nNum']
32 dsize = digits.dereference().type.sizeof * 8
33 num = 0
34 # The least significant byte is on index 0
35 for i in reversed(range(0, len)):
36 num <<= dsize
37 num += digits[i]
38 return num
40 class ColorPrinter(object):
41 '''Prints color as rgb(r, g, b) or rgba(r, g, b, a)'''
43 def __init__(self, typename, val):
44 self.val = val
46 def to_string(self):
47 r = self.val['R']
48 g = self.val['G']
49 b = self.val['B']
50 a = self.val['A']
51 if a:
52 return "rgba(%d, %d, %d, %d)" % (r, g, b, a)
53 else:
54 return "rgb(%d, %d, %d)" % (r, g, b)
56 class FractionPrinter(object):
57 '''Prints fraction'''
59 def __init__(self, typename, val):
60 self.typename = typename
61 self.val = val
63 def to_string(self):
64 # Workaround gdb bug <https://sourceware.org/bugzilla/show_bug.cgi?id=22968> "ptype does not
65 # find inner C++ class type without -readnow"
66 gdb.lookup_type('Fraction')
67 # This would be simpler and more reliable if we could call the operator* on mpImpl to get the internal Impl.
68 # Different libc have different structures. Some have one _M_t, some have two nested.
69 tmp = self.val['mpImpl']['_M_t']
70 if tmp.type.fields()[0].name == '_M_t': tmp = tmp['_M_t']
71 impl = tmp['_M_head_impl'].dereference().cast(gdb.lookup_type('Fraction::Impl'))
72 numerator = impl['value']['num']
73 denominator = impl['value']['den']
74 if impl['valid']:
75 return "%d/%d" % (numerator, denominator)
76 else:
77 return "invalid %s %d/%d" % (self.typename, numerator, denominator)
79 class DateTimeImpl(object):
81 def __init__(self, date, time):
82 self.date = date
83 self.time = time
85 def __str__(self):
86 result = ''
87 if self.date:
88 result += str(self.date)
89 if self.time:
90 result += ' '
91 if self.time:
92 result += str(self.time)
93 return result
95 @staticmethod
96 def parse(val):
97 return DateTimeImpl(DateImpl.parse(val), TimeImpl.parse(val))
99 class DateTimePrinter(object):
100 '''Prints date and time'''
102 def __init__(self, typename, val):
103 self.val = val
105 def to_string(self):
106 return str(DateTimeImpl.parse(self.val))
108 class DateImpl(DateTimeImpl):
110 def __init__(self, year, month, day):
111 super(DateImpl, self).__init__(self, None)
112 self.year = year
113 self.month = month
114 self.day = day
116 def __str__(self):
117 return "%d-%d-%d" % (self.year, self.month, self.day)
119 @staticmethod
120 def parse(val):
121 date = val['mnDate']
122 y = date / 10000
123 if date < 0:
124 date = -date
125 m = (date / 100) % 100
126 d = date % 100
127 return DateImpl(y, m, d)
129 class DatePrinter(object):
130 '''Prints date'''
132 def __init__(self, typename, val):
133 self.val = val
135 def to_string(self):
136 return str(DateImpl.parse(self.val))
138 class TimeImpl(DateTimeImpl):
140 def __init__(self, hour, minute, second, nanosecond = 0):
141 super(TimeImpl, self).__init__(None, self)
142 self.hour = hour
143 self.minute = minute
144 self.second = second
145 self.nanosecond = nanosecond
147 def __str__(self):
148 decimal = ''
149 if self.nanosecond != 0:
150 decimal = '.%09d' % self.nanosecond
151 return "%02d:%02d:%02d%s" % (self.hour, self.minute, self.second, decimal)
153 @staticmethod
154 def parse(val):
155 time = val['nTime']
156 h = time / 10000000000000
157 m = (time / 100000000000) % 100
158 s = (time / 1000000000) % 100
159 ns = time % 1000000000
160 return TimeImpl(h, m, s, ns)
162 class TimePrinter(object):
163 '''Prints time'''
165 def __init__(self, typename, val):
166 self.val = val
168 def to_string(self):
169 return str(TimeImpl.parse(self.val))
171 class PointPrinter(object):
172 '''Prints a Point.'''
174 def __init__(self, typename, value):
175 self.typename = typename
176 self.value = value
178 def to_string(self):
179 return "%s" % (self.typename)
181 def children(self):
182 x = self.value['nA']
183 y = self.value['nB']
184 children = [('x', x), ('y', y)]
185 return children.__iter__()
187 class SizePrinter(object):
188 '''Prints a Size.'''
190 def __init__(self, typename, value):
191 self.typename = typename
192 self.value = value
194 def to_string(self):
195 return "%s" % (self.typename)
197 def children(self):
198 width = self.value['nA']
199 height = self.value['nB']
200 children = [('width', width), ('height', height)]
201 return children.__iter__()
203 class RectanglePrinter(object):
204 '''Prints a Rectangle.'''
206 def __init__(self, typename, value):
207 self.typename = typename
208 self.value = value
210 def to_string(self):
211 return "%s" % (self.typename)
213 def children(self):
214 left = self.value['nLeft']
215 top = self.value['nTop']
216 right = self.value['nRight']
217 bottom = self.value['nBottom']
218 children = [('left', left), ('top', top), ('right', right), ('bottom', bottom)]
219 return children.__iter__()
221 printer = None
223 def build_pretty_printers():
224 global printer
226 printer = printing.Printer('libreoffice/tl')
228 # various types
229 printer.add('BigInt', BigIntPrinter)
230 printer.add('Color', ColorPrinter)
231 printer.add('Fraction', FractionPrinter)
232 printer.add('DateTime', DateTimePrinter)
233 printer.add('Date', DatePrinter)
234 printer.add('Time', TimePrinter)
235 printer.add('Point', PointPrinter)
236 printer.add('Size', SizePrinter)
237 printer.add('Rectangle', RectanglePrinter)
239 def register_pretty_printers(obj):
240 printing.register_pretty_printer(printer, obj)
242 build_pretty_printers()
244 # vim:set shiftwidth=4 softtabstop=4 expandtab: