# # glib-mkenums.rb # # C language enum description generation library like as glib-mkenums tool. # # Copyright(C) 2006-2008 Ruby-GNOME2 Project. # # This program is licenced under the same license of Ruby-GNOME2. # module GLib class EnumDefinition attr_accessor :EnumName, :enum_name, :ENUM_NAME, :ENUM_SHORT attr_accessor :type, :Type attr_accessor :g_type_prefix, :prefix attr_reader :constants def initialize(name, const_lines, g_type_prefix, options={}) @options = options || {} @EnumName = name @g_type_prefix = g_type_prefix @constants = [] @enum_name = @EnumName.sub(/^[A-Z]/){|v| v.downcase}.gsub(/[A-Z]+/){|v| "_" + v.downcase}.sub(/(^_|_$)/, "") @ENUM_NAME = @enum_name.upcase @ENUM_SHORT = @ENUM_NAME.sub(/^#{@g_type_prefix.sub(/_TYPE.*$/, "")}/, "").sub(/^_/, "") parse_const_lines(const_lines) end def parse_const_lines(const_lines) ret = "" if @options[:force_flags] or /< lines into target_filename.c def self.create(target_filename, files, g_type_prefix, include_files, options) puts "creating #{target_filename}.c" mkenums = MkEnums.new(target_filename, files, g_type_prefix, include_files, options) open("#{target_filename}.c", "w") do |out| out.write(mkenums.create_c) end puts "creating #{target_filename}.h" open("#{target_filename}.h", "w") do |out| out.write(mkenums.create_h) end end # Initialize GLib::MkEnums # # * target_filename: the target file name. This creates #{target_filename.c} and #{target_filename.h}. # * files: header files to parse # * g_type_prefix: the gtype prefix such as GTK_TYPE_ # * include_files: define #include lines into target_filename.c def initialize(target_filename, files, g_type_prefix, include_files, options) @target_filename = target_filename @include_files = include_files @targets = [] files.each do |path| data = "" File.open(path) do |i| data = i.read end @targets << [path, EnumDefinition.parse(data, g_type_prefix, options)] end end def create_enums(meth) # :nodoc: ret = "" @targets.each do |target| if target[1].size > 0 ret << %Q[\n\n/* enumerations from "#{target[0]}" */] target[1].each{|enum| ret << enum.__send__(meth) } end end ret end # Create a C source as a String. def create_c ret = "\n/* Generated by glib-mkenums.rb ($Id$) */ \n\n" ret << %Q[#include "#{@target_filename}.h"\n] @include_files.each do |file| ret << "#include <#{file}>\n" end ret << "\n" ret << create_enums(:create_c) ret << "\n\n/* Generated data ends here */\n\n" ret end # Create a C header as a String. def create_h header = "#{@target_filename}.h" const = "__#{File.basename(header).upcase.gsub(/-|\./, '_')}__" ret = "\n/* Generated by glib-mkenums.rb ($Id$) */ \n\n" ret << "#ifndef #{const}\n" ret << "#define #{const}\n\n" ret << "#include \n\n" ret << "G_BEGIN_DECLS" ret << create_enums(:create_h) ret << "\n\nG_END_DECLS\n\n" ret << "#endif /* #{const} */\n" ret << "\n/* Generated data ends here */\n\n" ret end end end