# File lib/glib-mkenums.rb, line 19 def initialize(name, const_lines, g_type_prefix, options={}) @options = options || {} @EnumName = name @g_type_prefix = g_type_prefix @constants = [] @enum_name = to_snail_case(@EnumName) @ENUM_NAME = @enum_name.upcase @ENUM_SHORT = @ENUM_NAME.sub(%r^#{@g_type_prefix.sub(/_TYPE.*$/, "")}/, "").sub(%r^_/, "") parse_const_lines(const_lines) end
# File lib/glib-mkenums.rb, line 64 def create_c constants = "\n" + @constants.collect{|name, nick| %Q[ { #{name}, "#{name}", "#{nick}" },\n] }.join + %Q[ { 0, NULL, NULL }] ret = " GType #{@enum_name}_get_type (void) { static GType etype = 0; if (etype == 0) { static const G#{@Type}Value values[] = {#{constants} }; etype = g_#{@type}_register_static ("#{@EnumName}", values); } return etype; } " ret end
# File lib/glib-mkenums.rb, line 87 def create_h %Q[ GType #{@enum_name}_get_type (void); #define #{@g_type_prefix}#{@ENUM_SHORT} (#{@enum_name}_get_type())] end
# File lib/glib-mkenums.rb, line 49 def extract_prefix(ary) return [] if ary == nil a = ary[0].split(%r/) if ary.size == 1 @ENUM_NAME + "_" else ary[1..-1].each do |b| b = b.split(%r/) l = [a.length, b.length].min a = a[0, (0...l).find{|i| a[i] != b[i] } || l] end a.join('') end end
# File lib/glib-mkenums.rb, line 31 def parse_const_lines(const_lines) if @options[:force_flags] or %r<</ =~ const_lines @type = "flags" @Type = "Flags" else @type = "enum" @Type = "Enum" end constants = [] const_lines.scan(%r^\s*([^\s,]*).*\n/) do |name| constants << name[0] unless name[0] =~ %r(^[\/\*]|^#|^$)/ end @prefix = extract_prefix(constants) constants.each do |name| @constants << [name, name.sub(%r#{@prefix}/, "").gsub(%r_/, "-").downcase] end end