Class: GLib::MkEnums

Inherits:
Object
  • Object
show all
Defined in:
lib/glib-mkenums.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_filename, files, g_type_prefix, include_files, options) ⇒ MkEnums

Initialize GLib::MkEnums

  • target_filename: the target file name. This creates #GLib::MkEnums.target_filenametarget_filename.c and #GLib::MkEnums.target_filenametarget_filename.h.

  • files: header files to parse

  • g_type_prefix: the gtype prefix such as GTK_TYPE_

  • include_files: define #include <file> lines into target_filename.c



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/glib-mkenums.rb', line 160

def initialize(target_filename, files, g_type_prefix, include_files, options)
  @target_filename = target_filename
  @include_files = include_files
  @targets = []
  files.sort.each do |path|
    data = ""
    File.open(path) do |i|
      data = i.read
    end
    @targets << [path, EnumDefinition.parse(data, g_type_prefix, options)]
  end
  @preamble = options[:preamble]
end

Class Method Details

.create(target_filename, files, g_type_prefix, include_files, options) ⇒ Object

Create target_filename.c and target_filename.h from files with g_type_prefix and include_files.

  • target_filename: the target file name. This creates #target_filenametarget_filename.c and #target_filenametarget_filename.h.

  • files: header files to parse

  • g_type_prefix: the gtype prefix such as GTK_TYPE_

  • include_files: define #include <file> lines into target_filename.c



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/glib-mkenums.rb', line 139

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

Instance Method Details

#create_cObject

Create a C source as a String.



188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/glib-mkenums.rb', line 188

def create_c
  ret = "\n/* Generated by glib-mkenums.rb ($Id$) */ \n\n"
  ret << "#{@preamble}\n" if @preamble
  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_enums(meth) ⇒ Object

:nodoc:



174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/glib-mkenums.rb', line 174

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_hObject

Create a C header as a String.



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/glib-mkenums.rb', line 202

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 <glib-object.h>\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