Blame tools/gobj2dot.rb

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