# 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 = @EnumName.sub(%r^[A-Z]/){|v| v.downcase}.gsub(%r[A-Z]+/){|v| "_" + v.downcase}.sub(%r(^_|_$)/, "") @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 96 def self.parse(data, g_type_prefix, options={}) options ||= {} enums = [] data.force_encoding("utf-8") if data.respond_to?(:force_encoding) data.scan(%r^\s*typedef\s+enum\s* \{?\s*(.*?) \}\s*(\w+);/x){|constants, name| enum_options = {} force_flags_patterns = [(options[:force_flags] || [])].flatten if force_flags_patterns.any? {|pattern| pattern === name} enum_options[:force_flags] = true end enum = new(name, constants, g_type_prefix, enum_options) enums << enum } enums end
# File lib/glib-mkenums.rb, line 66 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 89 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 51 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) ret = "" 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