Blame tools/gobj2dot.rb

Packit bc1512
#!/usr/bin/env ruby
Packit bc1512
Packit bc1512
# This program is free software; you can redistribute it and/or modify
Packit bc1512
# it under the terms of the GNU General Public License as published by
Packit bc1512
# the Free Software Foundation; either version 3 of the License, or
Packit bc1512
# (at your option) any later version.
Packit bc1512
#
Packit bc1512
# This program is distributed in the hope that it will be useful,
Packit bc1512
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit bc1512
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit bc1512
# GNU General Public License for more details.
Packit bc1512
#
Packit bc1512
# You should have received a copy of the GNU General Public License
Packit bc1512
# along with this program; if not, see <http://www.gnu.org/licenses/>.
Packit bc1512
#
Packit bc1512
# Copyright (C) 2009 Henrik Akesson
Packit bc1512
Packit bc1512
require 'find'
Packit bc1512
Packit bc1512
if ARGV[0] == nil or ARGV.length != 1 or ARGV[0] == "-h"
Packit bc1512
  puts "Utility for generating inheritance diagrams from c-code using "
Packit bc1512
  puts "the gobject library"
Packit bc1512
  puts "Usage: gobjviz.rb path"
Packit bc1512
  puts "       or"
Packit bc1512
  puts "       gobjviz.rb path | dot -Tpng > output.png"
Packit bc1512
  exit;
Packit bc1512
end
Packit bc1512
Packit bc1512
dir = ARGV[0]
Packit bc1512
Packit bc1512
header = <
Packit bc1512
digraph G {
Packit bc1512
        rankdir = RL
Packit bc1512
Packit bc1512
        fontname = "Bitstream Vera Sans"
Packit bc1512
        fontsize = 8
Packit bc1512
Packit bc1512
        node [ shape = "record" ]
Packit bc1512
Packit bc1512
        edge [ arrowhead = empty ]
Packit bc1512
HDR
Packit bc1512
Packit bc1512
footer = <
Packit bc1512
}
Packit bc1512
FTR
Packit bc1512
Packit bc1512
def tocamelcase( underscored )
Packit bc1512
  underscored.gsub!( /^[a-z]|_[a-z]/ ) { |a| a.upcase }
Packit bc1512
  underscored.gsub!( /_/, '')
Packit bc1512
end
Packit bc1512
Packit bc1512
def gegltocamelcase( gegltype )
Packit bc1512
  gegltype =~ /([a-zA-Z0-9_]+)_TYPE_([a-zA-Z0-9_]+)/
Packit bc1512
  gegltype = tocamelcase( $1.downcase + "_" + $2.downcase )
Packit bc1512
end
Packit bc1512
Packit bc1512
def inheritance( from, to )
Packit bc1512
  puts "\t" + from + " -> " + to if ( to != "GObject")
Packit bc1512
end
Packit bc1512
Packit bc1512
def implementation( klass, interf )
Packit bc1512
  puts "\t" + klass + " -> " + interf + " [ style = dashed ]"
Packit bc1512
end
Packit bc1512
Packit bc1512
def interfacedecl( interf )
Packit bc1512
  puts "\t" + interf + " [ label = \"{\\<\\<interface\\>\\>\\l" + interf + "}\" ]"
Packit bc1512
end
Packit bc1512
Packit bc1512
puts header
Packit bc1512
Find.find( dir ) do |path| 
Packit bc1512
  Find.prune if File.basename(path) == '.git' 
Packit bc1512
  if ( ( File.extname(path) == '.c' or File.extname(path) == '.h' ) and File.file? path )
Packit bc1512
    begin
Packit bc1512
      open( path ) do |file| 
Packit bc1512
        content = file.read
Packit bc1512
        if( content =~ /G_DEFINE_TYPE\s*\(\s*\w+,\s*(\w+),\s*(\w+)\s*\)/m )
Packit bc1512
          inheritance( tocamelcase( $1 ), gegltocamelcase( $2 ) )
Packit bc1512
        elsif( content =~ /G_DEFINE_TYPE_WITH_CODE\s*\(\s*(\w+).*G_IMPLEMENT_INTERFACE\s*\s*\(\s*(\w+)/m )
Packit bc1512
          implementation( $1, gegltocamelcase( $2 ) )
Packit bc1512
        elsif( content =~ /G_TYPE_INTERFACE\s*,\s*\"([^\"]+)\"/m )
Packit bc1512
          interfacedecl( $1 )
Packit bc1512
        end
Packit bc1512
      end
Packit bc1512
    rescue
Packit bc1512
      warn "Failed to parse #{path}, probably invalid utf8"
Packit bc1512
    end
Packit bc1512
  end
Packit bc1512
end
Packit bc1512
puts footer