From b99af1704659cad3cccaabc6522c5e3035e44798 Mon Sep 17 00:00:00 2001 From: Jesse van den Kieboom Date: Tue, 15 Nov 2005 12:02:55 +0000 Subject: [PATCH] Improved --- gnoemoe/genclass.rb | 113 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 91 insertions(+), 22 deletions(-) diff --git a/gnoemoe/genclass.rb b/gnoemoe/genclass.rb index b139aca..b506dc0 100644 --- a/gnoemoe/genclass.rb +++ b/gnoemoe/genclass.rb @@ -1,6 +1,7 @@ #!/usr/bin/ruby +require 'getoptlong' -def replaceAll(line, subst) +def replace_all(line, subst) subst.each do |sub,rep| line = line.gsub('{' + sub + '}', rep) end @@ -8,40 +9,108 @@ def replaceAll(line, subst) return line end -def degenerateCaps(text) - text = text.gsub(/[A-Z]+[a-z]+/) do |s| - s.downcase + '_' +def split_parts(text) + parts = [] + text = text.gsub(/[A-Z]+[a-z]*/) do |s| + parts << s.downcase end - return text[0..-2] + return parts end +def gen_underscore_lcase(parts) + ret = "" + + parts.each do |part| + ret += part.downcase + '_' + end + + return ret[0..-2] +end + +def gen_from_template(template, nameparts, parentparts, subst, out, ext) + begin + tc = IO.readlines(template) + rescue StandardError => boom + print "Template file not found #{template}!\n" + return + end + + if out then + f = File.open(out + ext, 'w') + else + f = $stdout + end + + tc.each do |line| + f.write(replace_all(line, subst)) + end + + if f != $stdout then + f.close + end +end + +opts = GetoptLong.new( + ["--body", "-b", GetoptLong::NO_ARGUMENT], + ["--header", "-h", GetoptLong::NO_ARGUMENT], + ["--parent", "-p", GetoptLong::REQUIRED_ARGUMENT], + ["--body-template", "-c", GetoptLong::REQUIRED_ARGUMENT], + ["--header-template", "-t", GetoptLong::REQUIRED_ARGUMENT], + ["--out", "-o", GetoptLong::REQUIRED_ARGUMENT] +) + +options = {} +opts.each do |arg,value| + options[arg] = value +end + +parent = (options["--parent"] or "GObject") +body_template = (options["--body-template"] or "template.c") +header_template = (options["--header-template"] or "template.h") +out = (options["--out"] or nil) + subst = {} name = ARGV[0] if (name == nil) then - print "Class name (capitalized): " - name = gets[0..-2] + print "Class name (camel case, including namespace): " + name = gets.chomp end -subst['template_'] = degenerateCaps(name) -subst['template-'] = subst['template_'].sub('_', '-') +nameparts = split_parts(name) + +subst['namespace'] = nameparts[0].downcase +subst['NAMESPACE'] = nameparts[0].upcase +subst['Namespace'] = nameparts[0].capitalize + +subst['template_'] = gen_underscore_lcase(nameparts[1..-1]) +subst['template-'] = subst['template_'].gsub('_', '-') subst['TEMPLATE'] = subst['template_'].upcase -subst['Template'] = name +subst['Template'] = name[nameparts[0].length..-1] -tc = IO.readlines("template.c") -th = IO.readlines("template.h") +parentparts = split_parts(parent) -f = File.open('gm-' + subst['template-'] + '.c', 'w') -tc.each do |line| - f.write(replaceAll(line, subst)) +subst['parent_namespace'] = parentparts[0].downcase +subst['PARENT_NAMESPACE'] = parentparts[0].upcase +subst['Parent_Namespace'] = parentparts[0].capitalize + +subst['parent_'] = gen_underscore_lcase(parentparts[1..-1]) +subst['parent-'] = subst['parent_'].gsub('_', '-') +subst['PARENT'] = subst['parent_'].upcase +subst['Parent'] = parent[parentparts[0].length..-1] + +if !options["--body"] and !options["--header"] then + gen_from_template(body_template, nameparts, parentparts, subst, out, ".c") + gen_from_template(header_template, nameparts, parentparts, subst, out, '.h') +else + if options['--body'] then + gen_from_template(body_template, nameparts, parentparts, subst, out, '.c') + end + + if options['--header'] then + gen_from_template(header_template, nameparts, parentparts, subst, out, '.h') + end end -f.close - -f = File.open('gm-' + subst['template-'] + '.h', 'w') -th.each do |line| - f.write(replaceAll(line, subst)) -end -f.close print "Done ...\n"