Sha256: d8d57ecf6a5504ab847b10a6554a76697f4f98548baf76ca8b4720d1fc8194a2

Contents?: true

Size: 962 Bytes

Versions: 2

Compression:

Stored size: 962 Bytes

Contents

id_list, sym_list = Hash.new, Hash.new
[['id', id_list], ['sym', sym_list]].each do |is, list|
	File.foreach("#{__dir__}/#{is}.txt").with_index do |line, i|
		ary = line.chomp.split(' ')
		case ary.size
		when 0
			nil
		when 1
			list.store(ary[0], ary[0])
		when 2
			list.store(ary[0], ary[1])
		else
			raise "Line #{i+1} in #{is}.txt cannot be recognized"
		end
	end
end

File.open('id_sym.c', 'w') do |f|
	[['id', id_list, 'ID', ''], ['sym', sym_list, 'VALUE', 'rb_id2sym']].each do |is, list, iv, func|
		list.each_key do |key|
			f.puts "#{iv} #{is}_#{key};"
		end
		f.puts ''
		f.puts "void\nkm_init_#{is}(void)\n{"
		list.each do |key, value|
			f.puts "\t#{is}_#{key} = #{func}(rb_intern(#{value.inspect}));"
		end
		f.puts '}'
		f.puts ''
	end
end

File.open('id_sym.h', 'w') do |f|
	[['id', id_list, 'ID'], ['sym', sym_list, 'VALUE']].each do |is, list, iv|
		list.each_key do |key|
			f.puts "extern #{iv} #{is}_#{key};"
		end
		f.puts ''
	end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
kmat-0.1.0 ext/kmat/id_sym.rb
kmat-0.0.3 ext/kmat/id_sym.rb