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/.
14 from libreoffice
.util
import printing
15 from libreoffice
.util
.string
import StringPrinterHelper
17 class RtlStringPrinter(StringPrinterHelper
):
18 '''Prints rtl_String or rtl_uString'''
20 def __init__(self
, typename
, val
, encoding
= None):
21 super(RtlStringPrinter
, self
).__init
__(typename
, val
, encoding
)
24 return self
.val
['buffer'].address
27 return self
.val
['length']
29 class StringPrinter(StringPrinterHelper
):
30 '''Prints rtl:: strings and string buffers'''
32 def __init__(self
, typename
, val
, encoding
= None):
33 super(StringPrinter
, self
).__init
__(typename
, val
, encoding
)
36 return self
.val
['pData']
39 assert self
.val
['pData']
40 return self
.val
['pData'].dereference()['buffer'].address
43 assert self
.val
['pData']
44 return self
.val
['pData'].dereference()['length']
46 class SalUnicodePrinter(StringPrinterHelper
):
47 '''Prints a sal_Unicode*'''
49 def __init__(self
, typename
, val
):
50 super(SalUnicodePrinter
, self
).__init
__(typename
, val
, 'utf-16')
57 type = type.unqualified()
58 if type.code
!= gdb
.TYPE_CODE_PTR
:
60 return str(type.target()) == 'sal_Unicode'
62 class RtlReferencePrinter(object):
63 '''Prints rtl::Reference'''
65 def __init__(self
, typename
, val
):
66 self
.typename
= typename
70 pointee
= self
.val
['m_pBody']
72 return '%s to %s' % (self
.typename
, str(pointee
))
74 return "empty %s" % self
.typename
76 class OslFileStatusPrinter(object):
77 '''Prints oslFileStatus'''
79 def __init__(self
, typename
, val
):
83 osl_file_type
= gdb
.lookup_type('oslFileType').strip_typedefs()
84 fields_to_enum_val
= gdb
.types
.make_enum_dict(osl_file_type
)
86 etype
= self
.field_val_if_valid('eType')
88 pretty_etype
= '<unknown type>' # in case it's not one of the fields
90 for field_name
, field_val
in six
.iteritems(fields_to_enum_val
):
91 if etype
== field_val
:
92 pretty_etype
= self
.pretty_file_type(field_name
)
94 pretty_etype
= '<invalid type>'
96 file_url
= self
.field_val_if_valid('ustrFileURL')
97 if file_url
is not None:
98 pretty_file_url
= str(file_url
.dereference())
100 pretty_file_url
= '<invalid file url>'
102 pretty_file_status
= pretty_etype
+ ': ' + pretty_file_url
104 # for links append the link target if valid
105 if etype
== fields_to_enum_val
['osl_File_Type_Link']:
106 link_target
= self
.field_val_if_valid('ustrLinkTargetURL')
107 if link_target
is None:
108 pretty_link_target
= '<invalid link target>'
110 pretty_link_target
= str(link_target
.dereference())
112 pretty_file_status
+= ' -> ' + pretty_link_target
114 return pretty_file_status
116 def pretty_file_type(self
, file_type_name
):
117 if file_type_name
!= 'osl_File_Type_Regular':
118 return file_type_name
.replace('osl_File_Type_', '').lower()
120 return 'file' # regular is not very descriptive, file is better
122 def field_val_if_valid(self
, field
):
123 mask_for_field
= {'eType': 0x00000001,
124 'uAttributes': 0x00000002,
125 'aCreationTime': 0x00000010,
126 'aAccessTime': 0x00000020,
127 'aModifyTime': 0x00000040,
128 'uFileSize': 0x00000080,
129 'ustrFileName': 0x00000100,
130 'ustrFileURL': 0x00000200,
131 'ustrLinkTargetURL': 0x00000400}
133 valid_fields
= self
.val
['uValidFields']
134 if valid_fields
& mask_for_field
[field
]:
135 return self
.val
[field
]
141 def build_pretty_printers():
144 printer
= printing
.Printer("libreoffice/sal")
146 # strings and string buffers
147 printer
.add('_rtl_String', RtlStringPrinter
)
148 printer
.add('_rtl_uString', lambda name
, val
: RtlStringPrinter(name
,
150 printer
.add('rtl::OString', StringPrinter
)
151 printer
.add('rtl::OUString', lambda name
, val
: StringPrinter(name
, val
, 'utf-16'))
152 printer
.add('rtl::OStringBuffer', StringPrinter
)
153 printer
.add('rtl::OUStringBuffer', lambda name
, val
: StringPrinter(name
, val
, 'utf-16'))
154 printer
.add('sal_Unicode', SalUnicodePrinter
, SalUnicodePrinter
.query
)
157 printer
.add('rtl::Reference', RtlReferencePrinter
)
158 printer
.add('_oslFileStatus', OslFileStatusPrinter
)
162 def register_pretty_printers(obj
):
163 printing
.register_pretty_printer(printer
, obj
)
165 build_pretty_printers()
167 # vim:set shiftwidth=4 softtabstop=4 expandtab: