# Pretty-printers for osp.

# Copyright (C) 2008-2013 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import gdb
import itertools
import re

# Try to use the new-style pretty-printing if available.
_use_gdb_pp = True
try:
    import gdb.printing
except ImportError:
    _use_gdb_pp = False

# Try to install type-printers.
_use_type_printing = False
try:
    import gdb.types
    if hasattr(gdb.types, 'TypePrinter'):
        _use_type_printing = True
except ImportError:
    pass

class OspStringPrinter(object):
    "Print a Tizen String"
    def __init__(self, val):
        self.val = val
    def to_string(self):
        return "\"" + self.val['__pValue'].string() + "\""

def string_lookup_function(val):
    lookup_tag = val.type.tag
    if lookup_tag == None:
        return None
    regex = re.compile("^Tizen::Base::String$")
    if regex.match(lookup_tag):
        return OspStringPrinter(val)
    return None

def register_osp_printers (obj):
    "Register osp pretty-printers with objfile Obj."

    global _use_gdb_pp

    if _use_gdb_pp:
        gdb.printing.register_pretty_printer(obj, string_lookup_function)
    else:
        if obj is None:
            obj = gdb
        obj.pretty_printers.append(string_lookup_function)


