# This file defines handy gdb macros
# To use it, add this line to your ~/.gdbinit :
# source /path/to/kde/sources/kdesdk/scripts/kde-devel-gdb

# Disable printing of static members. Qt has too many, it clutters the output
set print static-members off

# Prints the contents of a QString
define printqstring
       set $i=0
       set $out="\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
       set $cur=0
       set $s = $arg0
       while $i < $s.d->len
         set $out[$cur++] = (char)($s.d->unicode[$i++].ucs & 0xff)
         if $cur >= 40
           p $out
           set $cur = 0
           set $out="\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
         end
       end
       if $cur < 40
         p $out
       end
end

# Prints the contents of a QString encoded in utf8. Nice if you run your debug session in
# a utf8 enabled terminal.
define printqstring_utf8
       set $i=0
       set $out="\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
       set $cur=0
       set $s = $arg0
       while $i < $s.d->len
         set $uc = (unsigned short) $s.d->unicode[$i++].ucs
	 if ( $uc < 0x80 )
           set $out[$cur++] = (unsigned char)($uc & 0x7f)
	 else
	   if ( $uc < 0x0800 )
             set $out[$cur++] = (unsigned char)(0xc0 | ($uc >> 6))
	   else
             set $out[$cur++] = (unsigned char)(0xe0 | ($uc >> 12)
             set $out[$cur++] = (unsigned char)(0x80 | (($uc > 6) &0x3f)
	   end
	   set $out[$cur++] = (unsigned char)(0x80 | ((uchar) $uc & 0x3f))
	 end
         if $cur >= 40
           p $out
           set $cur = 0
           set $out="\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
         end
       end
       if $cur < 40
         p $out
       end
end

# Prints the contents of a QString, this one works even without a running process
# but prints one char per line
define printqstring_noprocess
       set $i=0
       set $s = $arg0
       while $i < $s.d->len
         p (char)($s.d->unicode[$i++].ucs & 0xff)
       end
end

# Prints the contents of a QCString
define printqcstring
      p $arg0.shd.data
      p $arg0.shd.len
end

# Prints the contents of a QFont
define printqfont
       print *($arg0).d
       printqstring ($arg0).d->req.family
end

# Prints the contents of a QColor
define printqcolor
  printf "(%d,%d,%d)\n", ($arg0).red(), ($arg0).green(), ($arg0).blue()
end
document printqcolor
  Prints a QColor as (R,G,B).
  Usage: 'qcolor <QColor col>
end

