Class: FFIGen::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/ffi_gen.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWriter

Returns a new instance of Writer.



230
231
232
233
# File 'lib/ffi_gen.rb', line 230

def initialize
  @indentation = ""
  @output = ""
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



228
229
230
# File 'lib/ffi_gen.rb', line 228

def output
  @output
end

Instance Method Details

#comment(&block) ⇒ Object



242
243
244
# File 'lib/ffi_gen.rb', line 242

def comment(&block)
  indent "# ", &block
end

#indent(prefix = " ") ⇒ Object



235
236
237
238
239
240
# File 'lib/ffi_gen.rb', line 235

def indent(prefix = "  ")
  previous_indentation = @indentation
  @indentation += prefix
  yield
  @indentation = previous_indentation
end

#prepare_comment_line(line) ⇒ Object



259
260
261
262
263
264
265
266
267
# File 'lib/ffi_gen.rb', line 259

def prepare_comment_line(line)
  line = line.dup
  line.sub! /\ ?\*+\/\s*$/, ''
  line.sub! /^\s*\/?\*+ ?/, ''
  line.gsub! /\\(brief|determine) /, ''
  line.gsub! '[', '('
  line.gsub! ']', ')'
  line
end

#puts(*lines) ⇒ Object



246
247
248
249
250
# File 'lib/ffi_gen.rb', line 246

def puts(*lines)
  lines.each do |line|
    @output << "#{@indentation}#{line}\n"
  end
end

#write_array(array, separator = "", first_line_prefix = "", other_lines_prefix = "") ⇒ Object



252
253
254
255
256
257
# File 'lib/ffi_gen.rb', line 252

def write_array(array, separator = "", first_line_prefix = "", other_lines_prefix = "")
  array.each_with_index do |entry, index|
    entry = yield entry if block_given?
    puts "#{index == 0 ? first_line_prefix : other_lines_prefix}#{entry}#{index < array.size - 1 ? separator : ''}"
  end
end

#write_description(description, not_documented_message = true, first_line_prefix = "", other_lines_prefix = "") ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
# File 'lib/ffi_gen.rb', line 269

def write_description(description, not_documented_message = true, first_line_prefix = "", other_lines_prefix = "")
  if description.is_a? String
    description = description.split("\n").map { |line| prepare_comment_line(line) }
  end
  
  description.shift while not description.empty? and description.first.strip.empty?
  description.pop while not description.empty? and description.last.strip.empty?
  description << (not_documented_message ? "(Not documented)" : "") if description.empty?
  
  write_array description, "", first_line_prefix, other_lines_prefix
end