This repository has been archived on 2020-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
gnoemoe/gnoemoe/mcp/mcpclasses

77 lines
1.8 KiB
Ruby
Executable File

#!/usr/bin/ruby
require 'getoptlong'
def print_body(defs, prefix)
includes = ['<glib-object.h>']
content = prefix + "GList *\ngm_mcp_classes_initialize() {\n" + prefix + "\tGList *result = NULL;\n\n"
defs.each do |line|
ucase = line.gsub(/[A-Z]+[a-z]+/) do |s|
s.upcase + '_'
end
ucase.chop!
mcase = ucase.downcase.gsub('_', '-')
includes << '"gm-mcp-' + mcase + '.h"'
content += prefix + "\tresult = g_list_append(result, \n" + prefix + "\t\t\tg_type_class_ref(GM_TYPE_MCP_" + ucase + "));\n"
end
includes.each {|inc| $stdout.write(prefix + '#include ' + inc + "\n")}
$stdout.write("\n" + content + "\n" + prefix + "\treturn result;\n" + prefix + "}\n")
end
def print_header(defs, prefix)
$stdout.write(prefix + "GList *gm_mcp_classes_initialize();\n")
end
def print_files(defs, prefix)
out = ''
defs.each do |line|
lcase = line.gsub(/[A-Z]+[a-z]*/) do |s|
s.downcase + '-'
end
lcase.chop!
out += prefix + "gm-mcp-" + lcase + ".c gm-mcp-" + lcase + ".h \\\n"
end
$stdout.write(out[0..-3])
end
opts = GetoptLong.new(
["--body", "-b", GetoptLong::NO_ARGUMENT],
["--header", "-h", GetoptLong::NO_ARGUMENT],
["--files", "-f", GetoptLong::NO_ARGUMENT],
["--prefix", "-p", GetoptLong::REQUIRED_ARGUMENT],
["--defs", "-d", GetoptLong::REQUIRED_ARGUMENT]
)
options = {}
opts.each do |arg,value|
options[arg] = value
end
if !options['--defs']
print "Specify a definition file with --defs!\n"
return
end
prefix = (options['--prefix'] or '')
begin
defs = File.readlines(options['--defs'])
defs.collect! {|elem| elem.chomp}
if (options['--header'])
print_header(defs, prefix)
elsif (options['--body'])
print_body(defs, prefix)
elsif (options['--files'])
print_files(defs, prefix)
end
rescue StandardError => boom
p boom
exit(1)
end